Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLClassPathHelperURLHelperStaleEntryCompatibilityTest.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.FileInputStream;23import java.io.InputStreamReader;24import java.net.URL;25import java.util.Properties;2627import CustomCLs.CustomURLClassLoader;28import CustomCLs.CustomURLLoader;29import Utilities.StringManipulator;30import Utilities.URLClassPathCreator;3132/**33* @author Matthew Kilner34*/35public class URLClassPathHelperURLHelperStaleEntryCompatibilityTest {3637StringManipulator manipulator = new StringManipulator();3839public static void main(String[] args) {4041if(args.length != 4){42System.out.println("\n Incorrect usage");43System.out.println("\n Please specifiy -testfile <filename> -javacdir <path to javac>");44}4546URLClassPathHelperURLHelperStaleEntryCompatibilityTest test = new URLClassPathHelperURLHelperStaleEntryCompatibilityTest();4748String testFile = args[1];49String javacdir = args[3];5051test.run(testFile, javacdir);5253}5455public void run(String testFile, String javacpath){5657Properties props = new Properties();58try{59FileInputStream PropertiesFile = new FileInputStream(testFile);60props.load(PropertiesFile);6162PropertiesFile.close();63} catch (Exception e){64e.printStackTrace();65}6667String classPath = props.getProperty("Classpath");6869String ctl = props.getProperty("NumberOfClassesToLoad");70Integer intctl = Integer.valueOf(ctl);71int numberOfClassesToLoad = intctl.intValue();7273String classesString = props.getProperty("LoadClasses");74String [] classesToLoad = new String[numberOfClassesToLoad];75for(int index = 0; index < numberOfClassesToLoad; index++){76classesToLoad[index] = manipulator.getStringElement(index, classesString);77}7879String numberOfUrlsString = props.getProperty("NumberOfUrls");80Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);81int numberOfUrls = tempNumberOfUrls.intValue();8283int maxClassesToFind = 0;84String[] urls = new String[numberOfUrls];85for(int index = 0; index < numberOfUrls; index++){86urls[index] = props.getProperty("Url"+index);87String ctf = props.getProperty("NumberOfClassesToFind"+index);88Integer intctf = Integer.valueOf(ctf);89maxClassesToFind = ((intctf.intValue() > maxClassesToFind) ? intctf.intValue() : maxClassesToFind);90}9192String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];93String[][] results = new String[numberOfUrls][maxClassesToFind];9495for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){96String findClasses = props.getProperty("FindClasses"+urlIndex);97String result = props.getProperty("Results"+urlIndex);98String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);99Integer intctf = Integer.valueOf(ctf);100int numberOfClassesToFind = intctf.intValue();101for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){102classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);103results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);104}105}106107String batchFile = props.getProperty("BatchFileToRun");108109boolean passed = executeTest(classPath, classesToLoad, urls, classesToFind, results, batchFile, javacpath);110111if(passed){112System.out.println("\nTEST PASSED");113} else {114System.out.println("\nTEST FAILED");115}116}117118private boolean executeTest(String classPath, String[] classesToLoad, String[] urls, String[][] classesToFind, String[][] results, String batchFile, String javacpath) {119120URLClassPathCreator creator = new URLClassPathCreator(classPath);121URL[] urlPath;122urlPath = creator.createURLClassPath();123124CustomURLClassLoader cl = new CustomURLClassLoader(urlPath, this.getClass().getClassLoader());125for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){126String classToLoad = classesToLoad[classIndex];127if (classToLoad != null){128try{129cl.loadClass(classToLoad);130} catch (Exception e){131e.printStackTrace();132}133}134}135136runBatchFile(batchFile, javacpath);137138String urlsString = urls[0];139for(int index = 1; index < urls.length; index++){140urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();141}142System.out.println("\n** urlsString: "+urlsString);143URLClassPathCreator creator2 = new URLClassPathCreator(urlsString);144URL[] urlPath2;145urlPath2 = creator2.createURLClassPath();146CustomURLLoader urlcl = new CustomURLLoader(urlPath2, this.getClass().getClassLoader());147148boolean result = true;149150for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){151for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){152String classToFind = classesToFind[urlIndex][classIndex];153String expectedResult = results[urlIndex][classIndex];154if(classToFind != null){155String testResult = String.valueOf(urlcl.isClassInSharedCache(urlIndex, classToFind));156if(!(expectedResult.equals(testResult))){157System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);158result = false;159}160}161}162}163return result;164}165166private void runBatchFile(String batch, String javacpath){167String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();168System.out.println("\n** Running: "+command);169String s = null;170try{171Process p = Runtime.getRuntime().exec(command);172173BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));174BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));175176System.out.println("Here is the standard output of the command:\n");177while ((s = stdInput.readLine()) != null) {178System.out.println(s);179}180181System.out.println("Here is the standard error of the command (if any):\n");182while ((s = stdError.readLine()) != null) {183System.out.println(s);184}185186} catch (Exception e){187e.printStackTrace();188}189}190}191192193