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