Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLHelperURLClassPathHelperCompatibilityTest.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.File;22import java.io.FileInputStream;23import java.net.URL;24import java.util.Properties;2526import CustomCLs.CustomURLClassLoader;27import CustomCLs.CustomURLLoader;28import Utilities.StringManipulator;29import Utilities.TestClass;30import Utilities.URLClassPathCreator;3132/**33* @author Matthew Kilner34*/35public class URLHelperURLClassPathHelperCompatibilityTest {3637StringManipulator manipulator = new StringManipulator();3839public static void main(String[] args) {4041if(args.length != 2){42System.out.println("\n Incorrect usage");43System.out.println("\n Please specifiy -testfile <filename>");44}4546URLHelperURLClassPathHelperCompatibilityTest test = new URLHelperURLClassPathHelperCompatibilityTest();4748String testFile = args[1];4950test.run(testFile);5152}5354public void run(String testFile){5556Properties props = new Properties();57try{58FileInputStream PropertiesFile = new FileInputStream(testFile);59props.load(PropertiesFile);6061PropertiesFile.close();62} catch (Exception e){63e.printStackTrace();64}6566String numberOfUrlsString = props.getProperty("NumberOfUrls");67Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);68int numberOfUrls = tempNumberOfUrls.intValue();6970int maxClassesToLoad = 0;71String[] urls = new String[numberOfUrls];72for(int index = 0; index < numberOfUrls; index++){73urls[index] = props.getProperty("Url"+index);74String ctl = props.getProperty("NumberOfClassesToLoad"+index);75Integer intctl = Integer.valueOf(ctl);76maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);77}7879String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];8081for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){82String loadClasses = props.getProperty("LoadClasses"+urlIndex);83String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);84Integer intctl = Integer.valueOf(ctl);85int numberOfClassesToLoad = intctl.intValue();86for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){87classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);88}89}9091String classPath = props.getProperty("Classpath");9293String ctf = props.getProperty("NumberOfClassesToFind");94Integer intctf = Integer.valueOf(ctf);95int numberOfClassesToFind = intctf.intValue();9697String classesString = props.getProperty("FindClasses");98String [] classesToFind = new String[numberOfClassesToFind];99String resultsString = props.getProperty("Results");100String[] results = new String[numberOfClassesToFind];101String foundAtString = props.getProperty("FoundAt");102String[] foundAt = new String[numberOfClassesToFind];103for(int index = 0; index < numberOfClassesToFind; index++){104classesToFind[index] = manipulator.getStringElement(index, classesString);105results[index] = manipulator.getStringElement(index, resultsString);106foundAt[index] = manipulator.getStringElement(index, foundAtString);107}108109boolean passed = executeTest(urls, classesToLoad, classPath, classesToFind, results, foundAt);110111if(passed){112System.out.println("\nTEST PASSED");113} else {114System.out.println("\nTEST FAILED");115}116}117118private boolean executeTest(String[] urls, String[][] classesToLoad, String classPath, String[] classesToFind, String[] results, String[] foundAt) {119120String urlsString = urls[0];121for(int index = 1; index < urls.length; index++){122urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).append(File.pathSeparatorChar).toString();123}124125URLClassPathCreator creator = new URLClassPathCreator(urlsString);126URL[] urlPath;127urlPath = creator.createURLClassPath();128CustomURLLoader[] loaderArray = new CustomURLLoader[urls.length];129130for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){131for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){132loaderArray[urlIndex] = new CustomURLLoader(urlPath, this.getClass().getClassLoader());133String classToLoad = classesToLoad[urlIndex][classIndex];134if(classToLoad != null){135loaderArray[urlIndex].loadClassFrom(classToLoad, urlIndex);136}137}138}139140boolean result = true;141142URLClassPathCreator creator2 = new URLClassPathCreator(classPath);143URL[] urlPath2;144urlPath2 = creator2.createURLClassPath();145146CustomURLClassLoader cl = new CustomURLClassLoader(urlPath2, this.getClass().getClassLoader());147for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){148String classToFind = classesToFind[classIndex];149String expectedResult = results[classIndex];150if (classToFind != null){151String testResult = String.valueOf(cl.isClassInSharedCache(classToFind));152if(!(expectedResult.equals(testResult))){153System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);154result = false;155} else {156if(testResult.equals("true")){157result = validateReturnedClass(classToFind, foundAt[classIndex], cl);158}159}160}161}162return result;163}164165boolean validateReturnedClass(String className, String foundAt, CustomURLClassLoader loader){166boolean result = false;167Class classData = null;168classData = loader.getClassFromCache(className);169if(null != classData){170Object o = null;171try{172o = classData.newInstance();173} catch(Exception e){174e.printStackTrace();175}176TestClass tc = (TestClass)o;177String classLocation = tc.getLocation();178if(classLocation.equals(foundAt)){179result = true;180} else {181System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);182}183}184return result;185}186}187188189