Path: blob/master/test/functional/cmdLineTests/shareClassTests/URLHelperTests/StaleClassPathEntryTests/URLStaleClassPathEntryTest.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*******************************************************************************/21package StaleClassPathEntryTests;2223import java.io.BufferedReader;24import java.io.FileInputStream;25import java.io.InputStreamReader;26import java.net.URL;27import java.util.Properties;2829import CustomCLs.CustomURLLoader;30import Utilities.StringManipulator;31import Utilities.URLClassPathCreator;3233/**34* @author Matthew Kilner35*/36public class URLStaleClassPathEntryTest {3738StringManipulator manipulator = new StringManipulator();3940public static void main(String[] args) {4142if(args.length != 4){43System.out.println("\n Incorrect usage");44System.out.println("\n Please specifiy -testfile <filename> -javacdir <path to javac>");45}4647URLStaleClassPathEntryTest test = new URLStaleClassPathEntryTest();4849String testFile = args[1];50String javacdir = args[3];5152test.run(testFile, javacdir);53}5455public void run(String testFileName, String javacpath){5657Properties props = new Properties();58try{59FileInputStream PropertiesFile = new FileInputStream(testFileName);60props.load(PropertiesFile);6162PropertiesFile.close();63} catch (Exception e){64e.printStackTrace();65}6667String numberOfUrlsString = props.getProperty("NumberOfUrls");68Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);69int numberOfUrls = tempNumberOfUrls.intValue();7071int maxClassesToLoad = 0;72int maxClassesToFind = 0;73String[] urls = new String[numberOfUrls];74for(int index = 0; index < numberOfUrls; index++){75urls[index] = props.getProperty("Url"+index);76String ctl = props.getProperty("NumberOfClassesToLoad"+index);77Integer intctl = Integer.valueOf(ctl);78maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);79String ctf = props.getProperty("NumberOfClassesToFind"+index);80Integer intctf = Integer.valueOf(ctl);81maxClassesToFind = ((intctl.intValue() > maxClassesToFind) ? intctl.intValue() : maxClassesToFind);82}8384String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];85String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];86String[][] results = new String[numberOfUrls][maxClassesToFind];8788for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){89String loadClasses = props.getProperty("LoadClasses"+urlIndex);90String findClasses = props.getProperty("FindClasses"+urlIndex);91String result = props.getProperty("Results"+urlIndex);92String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);93Integer intctl = Integer.valueOf(ctl);94int numberOfClassesToLoad = intctl.intValue();95String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);96Integer intctf = Integer.valueOf(ctl);97int numberOfClassesToFind = intctf.intValue();98for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){99classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);100}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(urls, classesToLoad, classesToFind, results, batchFile, javacpath);110111if(passed){112System.out.println("\nTEST PASSED");113} else {114System.out.println("\nTEST FAILED");115}116}117118public boolean executeTest(String[] urls, String[][] classesToLoad, String[][] classesToFind, String[][] results, String batchFile, String javacpath){119120String urlsString = urls[0];121for(int index = 1; index < urls.length; index++){122urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();123}124System.out.println("\n** urlsString: "+urlsString);125URLClassPathCreator creator = new URLClassPathCreator(urlsString);126URL[] urlPath;127urlPath = creator.createURLClassPath();128CustomURLLoader cl = new CustomURLLoader(urlPath, this.getClass().getClassLoader());129130for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){131for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){132String classToLoad = classesToLoad[urlIndex][classIndex];133if(classToLoad != null){134System.out.println("classToLoad = "+classToLoad);135}136}137}138139for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){140for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){141String classToLoad = classesToLoad[urlIndex][classIndex];142if(classToLoad != null){143cl.loadClassFrom(classToLoad, urlIndex);144}145}146}147148/*Sleep for 2 s ... otherwise time stamps may not match ...*/149try {150Thread.sleep(2000);151} catch (Exception e) {152e.printStackTrace();153}154runBatchFile(batchFile, javacpath);155boolean result = true;156157for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){158for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){159String classToFind = classesToFind[urlIndex][classIndex];160String expectedResult = results[urlIndex][classIndex];161if(classToFind != null){162String testResult = String.valueOf(cl.isClassInSharedCache(urlIndex, classToFind));163if(!(expectedResult.equals(testResult))){164System.out.println("testResult = \""+testResult+"\"");165System.out.println("urlIndex = \""+urlIndex+"\"");166System.out.println("classToFind = \""+classToFind+"\"");167System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);168result = false;169}170}171}172}173return result;174}175176private void runBatchFile(String batch, String javacpath){177String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();178System.out.println("\n** Running: "+command);179String s = null;180try{181Process p = Runtime.getRuntime().exec(command);182183BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));184BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));185186System.out.println("Here is the standard output of the command:\n");187while ((s = stdInput.readLine()) != null) {188System.out.println(s);189}190191System.out.println("Here is the standard error of the command (if any):\n");192while ((s = stdError.readLine()) != null) {193System.out.println(s);194}195196} catch (Exception e){197e.printStackTrace();198}199}200}201202203