Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/PartitioningURLClassPathHelperURLHelperStaleEntryCompatibilityTest.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.CustomPartitioningURLCL;28import CustomCLs.CustomPartitioningURLLoader;29import Utilities.StringManipulator;30import Utilities.URLClassPathCreator;3132/**33* @author Matthew Kilner34*/35public class PartitioningURLClassPathHelperURLHelperStaleEntryCompatibilityTest {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}4546PartitioningURLClassPathHelperURLHelperStaleEntryCompatibilityTest test = new PartitioningURLClassPathHelperURLHelperStaleEntryCompatibilityTest();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");68String partition = props.getProperty("Partition");6970String ctl = props.getProperty("NumberOfClassesToLoad");71Integer intctl = Integer.valueOf(ctl);72int numberOfClassesToLoad = intctl.intValue();7374String classesString = props.getProperty("LoadClasses");75String [] classesToLoad = new String[numberOfClassesToLoad];76for(int index = 0; index < numberOfClassesToLoad; index++){77classesToLoad[index] = manipulator.getStringElement(index, classesString);78}7980String numberOfUrlsString = props.getProperty("NumberOfUrls");81Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);82int numberOfUrls = tempNumberOfUrls.intValue();8384int maxClassesToFind = 0;85String[] urls = new String[numberOfUrls];86String [] partitionStrings = new String[numberOfUrls];87for(int index = 0; index < numberOfUrls; index++){88urls[index] = props.getProperty("Url"+index);89partitionStrings[index] = props.getProperty("urlPartition"+index);90String ctf = props.getProperty("NumberOfClassesToFind"+index);91Integer intctf = Integer.valueOf(ctl);92maxClassesToFind = ((intctl.intValue() > maxClassesToFind) ? intctl.intValue() : maxClassesToFind);93}9495String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];96String[][] results = new String[numberOfUrls][maxClassesToFind];9798for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){99String findClasses = props.getProperty("FindClasses"+urlIndex);100String result = props.getProperty("Results"+urlIndex);101String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);102Integer intctf = Integer.valueOf(ctf);103int numberOfClassesToFind = intctf.intValue();104for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){105classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);106results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);107}108}109110String batchFile = props.getProperty("BatchFileToRun");111112boolean passed = executeTest(classPath, partition, classesToLoad, urls, partitionStrings, classesToFind, results, batchFile, javacpath);113114if(passed){115System.out.println("\nTEST PASSED");116} else {117System.out.println("\nTEST FAILED");118}119}120121private boolean executeTest(String classPath, String partition, String[] classesToLoad, String[] urls, String[] partitionStrings, String[][] classesToFind, String[][] results, String batchFile, String javacpath) {122123URLClassPathCreator creator = new URLClassPathCreator(classPath);124URL[] urlPath;125urlPath = creator.createURLClassPath();126127CustomPartitioningURLCL cl = new CustomPartitioningURLCL(urlPath, this.getClass().getClassLoader());128cl.setPartition(partition);129for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){130String classToLoad = classesToLoad[classIndex];131if (classToLoad != null){132try{133cl.loadClass(classToLoad);134} catch (Exception e){135e.printStackTrace();136}137}138}139140if(0 != batchFile.length()){141runBatchFile(batchFile, javacpath);142}143144String urlsString = urls[0];145for(int index = 1; index < urls.length; index++){146urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();147}148149URLClassPathCreator creator2 = new URLClassPathCreator(urlsString);150URL[] urlPath2;151urlPath2 = creator2.createURLClassPath();152CustomPartitioningURLLoader urlcl = new CustomPartitioningURLLoader(urlPath2, this.getClass().getClassLoader());153154boolean result = true;155156for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){157urlcl.setPartition(partitionStrings[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(urlcl.isClassInSharedCache(urlIndex, classToFind));163if(!(expectedResult.equals(testResult))){164System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);165result = false;166}167}168}169}170return result;171}172173private void runBatchFile(String batch, String javacpath){174String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();175System.out.println("\n** Running: "+command);176String s = null;177try{178Process p = Runtime.getRuntime().exec(command);179180BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));181BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));182183System.out.println("Here is the standard output of the command:\n");184while ((s = stdInput.readLine()) != null) {185System.out.println(s);186}187188System.out.println("Here is the standard error of the command (if any):\n");189while ((s = stdError.readLine()) != null) {190System.out.println(s);191}192193} catch (Exception e){194e.printStackTrace();195}196}197}198199200