Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLHelperURLClassPathHelperStaleEntryCompatibilityTest.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.BufferedReader;22import java.io.File;23import java.io.FileInputStream;24import java.io.InputStreamReader;25import java.net.URL;26import java.util.Properties;2728import CustomCLs.CustomURLClassLoader;29import CustomCLs.CustomURLLoader;30import Utilities.StringManipulator;31import Utilities.TestClass;32import Utilities.URLClassPathCreator;3334/**35* @author Matthew Kilner36*/37public class URLHelperURLClassPathHelperStaleEntryCompatibilityTest {3839StringManipulator manipulator = new StringManipulator();4041public static void main(String[] args) {4243if(args.length != 2){44System.out.println("\n Incorrect usage");45System.out.println("\n Please specifiy -testfile <filename> -javacdir <path to javac>");46}4748URLHelperURLClassPathHelperStaleEntryCompatibilityTest test = new URLHelperURLClassPathHelperStaleEntryCompatibilityTest();4950String testFile = args[1];51String javacdir = args[3];5253test.run(testFile, javacdir);5455}5657public void run(String testFile, String javacpath){5859Properties props = new Properties();60try{61FileInputStream PropertiesFile = new FileInputStream(testFile);62props.load(PropertiesFile);6364PropertiesFile.close();65} catch (Exception e){66e.printStackTrace();67}6869String numberOfUrlsString = props.getProperty("NumberOfUrls");70Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);71int numberOfUrls = tempNumberOfUrls.intValue();7273int maxClassesToLoad = 0;74String[] urls = new String[numberOfUrls];75for(int index = 0; index < numberOfUrls; index++){76urls[index] = props.getProperty("Url"+index);77String ctl = props.getProperty("NumberOfClassesToLoad"+index);78Integer intctl = Integer.valueOf(ctl);79maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);80}8182String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];8384for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){85String loadClasses = props.getProperty("LoadClasses"+urlIndex);86String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);87Integer intctl = Integer.valueOf(ctl);88int numberOfClassesToLoad = intctl.intValue();89for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){90classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);91}92}9394String classPath = props.getProperty("Classpath");9596String ctf = props.getProperty("NumberOfClassesToFind");97Integer intctf = Integer.valueOf(ctf);98int numberOfClassesToFind = intctf.intValue();99100String classesString = props.getProperty("FindClasses");101String [] classesToFind = new String[numberOfClassesToFind];102String resultsString = props.getProperty("Results");103String[] results = new String[numberOfClassesToFind];104String foundAtString = props.getProperty("FoundAt");105String[] foundAt = new String[numberOfClassesToFind];106for(int index = 0; index < numberOfClassesToFind; index++){107classesToFind[index] = manipulator.getStringElement(index, classesString);108results[index] = manipulator.getStringElement(index, resultsString);109foundAt[index] = manipulator.getStringElement(index, foundAtString);110}111112String batchFile = props.getProperty("BatchFileToRun");113114boolean passed = executeTest(urls, classesToLoad, classPath, classesToFind, results, batchFile, foundAt, javacpath);115116if(passed){117System.out.println("\nTEST PASSED");118} else {119System.out.println("\nTEST FAILED");120}121}122123private boolean executeTest(String[] urls, String[][] classesToLoad, String classPath, String[] classesToFind, String[] results, String batchFile, String[] foundAt, String javacpath) {124125String urlsString = urls[0];126for(int index = 1; index < urls.length; index++){127urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).append(File.pathSeparatorChar).toString();128}129System.out.println("\n** urlsString: "+urlsString);130131URLClassPathCreator creator = new URLClassPathCreator(urlsString);132URL[] urlPath;133urlPath = creator.createURLClassPath();134CustomURLLoader[] loaderArray = new CustomURLLoader[urls.length];135136for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){137for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){138loaderArray[urlIndex] = new CustomURLLoader(urlPath, this.getClass().getClassLoader());139String classToLoad = classesToLoad[urlIndex][classIndex];140if(classToLoad != null){141loaderArray[urlIndex].loadClassFrom(classToLoad, urlIndex);142}143}144}145146runBatchFile(batchFile, javacpath);147148boolean result = true;149150URLClassPathCreator creator2 = new URLClassPathCreator(classPath);151URL[] urlPath2;152urlPath2 = creator2.createURLClassPath();153154CustomURLClassLoader cl = new CustomURLClassLoader(urlPath2, this.getClass().getClassLoader());155for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){156String classToFind = classesToFind[classIndex];157String expectedResult = results[classIndex];158if (classToFind != null){159String testResult = String.valueOf(cl.isClassInSharedCache(classToFind));160if(!(expectedResult.equals(testResult))){161System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);162result = false;163}else {164if(testResult.equals("true")){165result = validateReturnedClass(classToFind, foundAt[classIndex], cl);166}167}168}169}170return result;171}172173boolean validateReturnedClass(String className, String foundAt, CustomURLClassLoader loader){174boolean result = false;175Class classData = null;176classData = loader.getClassFromCache(className);177if(null != classData){178Object o = null;179try{180o = classData.newInstance();181} catch(Exception e){182e.printStackTrace();183}184TestClass tc = (TestClass)o;185String classLocation = tc.getLocation();186if(classLocation.equals(foundAt)){187result = true;188} else {189System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);190}191} else {192System.out.println("\nCould not get class data from cache");193}194return result;195}196197private void runBatchFile(String batch, String javacpath){198String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();199System.out.println("\n** Running: "+command);200String s = null;201try{202Process p = Runtime.getRuntime().exec(command);203204BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));205BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));206207System.out.println("Here is the standard output of the command:\n");208while ((s = stdInput.readLine()) != null) {209System.out.println(s);210}211212System.out.println("Here is the standard error of the command (if any):\n");213while ((s = stdError.readLine()) != null) {214System.out.println(s);215}216217} catch (Exception e){218e.printStackTrace();219}220}221}222223224