1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @summary Basic test of jimage protocol handler
27 * @run testng Basic
28 */
29
30import java.io.IOException;
31import java.io.InputStream;
32import java.net.URL;
33import java.net.URLConnection;
34
35import org.testng.annotations.DataProvider;
36import org.testng.annotations.Test;
37import static org.testng.Assert.*;
38
39public class Basic {
40
41    @DataProvider(name = "urls")
42    public Object[][] urls() {
43        Object[][] data = {
44            { "jrt:/java.base/java/lang/Object.class",    true },
45            { "jrt:/java.desktop/java/lang/Object.class", false },
46        };
47        return data;
48    }
49
50    @Test(dataProvider = "urls")
51    public void testConnect(String urlString, boolean exists) throws Exception {
52        URL url = new URL(urlString);
53        URLConnection uc = url.openConnection();
54        try {
55            uc.connect();
56            if (!exists) fail("IOException expected");
57        } catch (IOException ioe) {
58            if (exists) fail("IOException not expected");
59        }
60    }
61
62    @Test(dataProvider = "urls")
63    public void testInputStream(String urlString, boolean exists) throws Exception {
64        URL url = new URL(urlString);
65        URLConnection uc = url.openConnection();
66        try {
67            int b = uc.getInputStream().read();
68            assertTrue(b != -1);
69            if (!exists) fail("IOException expected");
70        } catch (IOException ioe) {
71            if (exists) fail("IOException not expected");
72        }
73    }
74
75    @Test(dataProvider = "urls")
76    public void testContentLength(String urlString, boolean exists) throws Exception {
77        URL url = new URL(urlString);
78        int len = url.openConnection().getContentLength();
79        assertTrue((exists && len > 0) || (!exists && len == -1));
80    }
81
82    @Test(dataProvider = "urls")
83    public void testGetContent(String urlString, boolean exists) throws Exception {
84        URL url = new URL(urlString);
85        try {
86            Object obj = url.getContent();
87            assertTrue(obj != null);
88            if (!exists) fail("IOException expected");
89        } catch (IOException ioe) {
90            if (exists) fail("IOException not expected");
91        }
92    }
93}
94