Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLClassPathHelperURLHelperCompatibilityTest.java
6004 views
/*******************************************************************************1* Copyright (c) 2005, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21import java.io.FileInputStream;22import java.net.URL;23import java.util.Properties;2425import CustomCLs.CustomURLClassLoader;26import CustomCLs.CustomURLLoader;27import Utilities.StringManipulator;28import Utilities.URLClassPathCreator;2930/**31* @author Matthew Kilner32*/33public class URLClassPathHelperURLHelperCompatibilityTest {3435StringManipulator manipulator = new StringManipulator();3637public static void main(String[] args) {3839if(args.length != 2){40System.out.println("\n Incorrect usage");41System.out.println("\n Please specifiy -testfile <filename>");42}4344URLClassPathHelperURLHelperCompatibilityTest test = new URLClassPathHelperURLHelperCompatibilityTest();4546String testFile = args[1];4748test.run(testFile);4950}5152public void run(String testFile){5354Properties props = new Properties();55try{56FileInputStream PropertiesFile = new FileInputStream(testFile);57props.load(PropertiesFile);5859PropertiesFile.close();60} catch (Exception e){61e.printStackTrace();62}6364String classPath = props.getProperty("Classpath");6566String ctl = props.getProperty("NumberOfClassesToLoad");67Integer intctl = Integer.valueOf(ctl);68int numberOfClassesToLoad = intctl.intValue();6970String classesString = props.getProperty("LoadClasses");71String [] classesToLoad = new String[numberOfClassesToLoad];72for(int index = 0; index < numberOfClassesToLoad; index++){73classesToLoad[index] = manipulator.getStringElement(index, classesString);74}7576String numberOfUrlsString = props.getProperty("NumberOfUrls");77Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);78int numberOfUrls = tempNumberOfUrls.intValue();7980int maxClassesToFind = 0;81String[] urls = new String[numberOfUrls];82for(int index = 0; index < numberOfUrls; index++){83urls[index] = props.getProperty("Url"+index);84String ctf = props.getProperty("NumberOfClassesToFind"+index);85Integer intctf = Integer.valueOf(ctf);86maxClassesToFind = ((intctf.intValue() > maxClassesToFind) ? intctf.intValue() : maxClassesToFind);87}8889String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];90String[][] results = new String[numberOfUrls][maxClassesToFind];9192for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){93String findClasses = props.getProperty("FindClasses"+urlIndex);94String result = props.getProperty("Results"+urlIndex);95String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);96Integer intctf = Integer.valueOf(ctf);97int numberOfClassesToFind = intctf.intValue();98for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){99classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);100results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);101}102}103104boolean passed = executeTest(classPath, classesToLoad, urls, classesToFind, results);105106if(passed){107System.out.println("\nTEST PASSED");108} else {109System.out.println("\nTEST FAILED");110}111}112113private boolean executeTest(String classPath, String[] classesToLoad, String[] urls, String[][] classesToFind, String[][] results) {114115URLClassPathCreator creator = new URLClassPathCreator(classPath);116URL[] urlPath;117urlPath = creator.createURLClassPath();118119CustomURLClassLoader cl = new CustomURLClassLoader(urlPath, this.getClass().getClassLoader());120for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){121String classToLoad = classesToLoad[classIndex];122if (classToLoad != null){123try{124cl.loadClass(classToLoad);125} catch (Exception e){126e.printStackTrace();127}128}129}130131String urlsString = urls[0];132for(int index = 1; index < urls.length; index++){133urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();134}135System.out.println("\n** urlsString: "+urlsString);136URLClassPathCreator creator2 = new URLClassPathCreator(urlsString);137URL[] urlPath2;138urlPath2 = creator2.createURLClassPath();139CustomURLLoader urlcl = new CustomURLLoader(urlPath2, this.getClass().getClassLoader());140141boolean result = true;142143for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){144for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){145String classToFind = classesToFind[urlIndex][classIndex];146String expectedResult = results[urlIndex][classIndex];147if(classToFind != null){148String testResult = String.valueOf(urlcl.isClassInSharedCache(urlIndex, classToFind));149if(!(expectedResult.equals(testResult))){150System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);151result = false;152}153}154}155}156return result;157}158}159160161