Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/PartitioningURLHelperURLClassPathHelperStaleEntryCompatibilityTest.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.CustomPartitioningURLCL;29import CustomCLs.CustomPartitioningURLLoader;30import Utilities.StringManipulator;31import Utilities.TestClass;32import Utilities.URLClassPathCreator;3334/**35* @author Matthew Kilner36*/37public class PartitioningURLHelperURLClassPathHelperStaleEntryCompatibilityTest {3839StringManipulator manipulator = new StringManipulator();4041public static void main(String[] args) {4243if(args.length != 4){44System.out.println("\n Incorrect usage");45System.out.println("\n Please specifiy -testfile <filename> -javacdir <path to javac>");46}4748PartitioningURLHelperURLClassPathHelperStaleEntryCompatibilityTest test = new PartitioningURLHelperURLClassPathHelperStaleEntryCompatibilityTest();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];75String [] partitionStrings = new String[numberOfUrls];76for(int index = 0; index < numberOfUrls; index++){77urls[index] = props.getProperty("Url"+index);78partitionStrings[index] = props.getProperty("urlPartition"+index);79String ctl = props.getProperty("NumberOfClassesToLoad"+index);80Integer intctl = Integer.valueOf(ctl);81maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);82}8384String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];8586for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){87String loadClasses = props.getProperty("LoadClasses"+urlIndex);88String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);89Integer intctl = Integer.valueOf(ctl);90int numberOfClassesToLoad = intctl.intValue();91for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){92classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);93}94}9596String classPath = props.getProperty("Classpath");97String partition = props.getProperty("Partition");9899String ctf = props.getProperty("NumberOfClassesToFind");100Integer intctf = Integer.valueOf(ctf);101int numberOfClassesToFind = intctf.intValue();102103String classesString = props.getProperty("FindClasses");104String [] classesToFind = new String[numberOfClassesToFind];105String resultsString = props.getProperty("Results");106String[] results = new String[numberOfClassesToFind];107String foundAtString = props.getProperty("FoundAt");108String[] foundAt = new String[numberOfClassesToFind];109for(int index = 0; index < numberOfClassesToFind; index++){110classesToFind[index] = manipulator.getStringElement(index, classesString);111results[index] = manipulator.getStringElement(index, resultsString);112foundAt[index] = manipulator.getStringElement(index, foundAtString);113}114115String batchFile = props.getProperty("BatchFileToRun");116117boolean passed = executeTest(urls, partitionStrings, classesToLoad, classPath, partition, classesToFind, results, batchFile, foundAt, javacpath);118119if(passed){120System.out.println("\nTEST PASSED");121} else {122System.out.println("\nTEST FAILED");123}124}125126private boolean executeTest(String[] urls, String[] partitionStrings, String[][] classesToLoad, String classPath, String partition, String[] classesToFind, String[] results, String batchFile, String[] foundAt, String javacpath) {127128String urlsString = urls[0];129for(int index = 1; index < urls.length; index++){130urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).append(File.pathSeparatorChar).toString();131}132System.out.println("\n** urlsString: "+urlsString);133134URLClassPathCreator creator = new URLClassPathCreator(urlsString);135URL[] urlPath;136urlPath = creator.createURLClassPath();137CustomPartitioningURLLoader[] loaderArray = new CustomPartitioningURLLoader[urls.length];138139for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){140loaderArray[urlIndex] = new CustomPartitioningURLLoader(urlPath, this.getClass().getClassLoader());141loaderArray[urlIndex].setPartition(partitionStrings[urlIndex]);142for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){143String classToLoad = classesToLoad[urlIndex][classIndex];144if(classToLoad != null){145loaderArray[urlIndex].loadClassFrom(classToLoad, urlIndex);146}147}148}149150if(0 != batchFile.length()){151runBatchFile(batchFile, javacpath);152}153154boolean result = true;155156URLClassPathCreator creator2 = new URLClassPathCreator(classPath);157URL[] urlPath2;158urlPath2 = creator2.createURLClassPath();159160CustomPartitioningURLCL cl = new CustomPartitioningURLCL(urlPath2, this.getClass().getClassLoader());161cl.setPartition(partition);162for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){163String classToFind = classesToFind[classIndex];164String expectedResult = results[classIndex];165if (classToFind != null){166String testResult = String.valueOf(cl.isClassInSharedCache(classToFind));167if(!(expectedResult.equals(testResult))){168System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);169result = false;170} else {171if(testResult.equals("true")){172result = validateReturnedClass(classToFind, foundAt[classIndex], cl);173}174}175}176}177return result;178}179180private void runBatchFile(String batch, String javacpath){181String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();182System.out.println("\n** Running: "+command);183String s = null;184try{185Process p = Runtime.getRuntime().exec(command);186187BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));188BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));189190System.out.println("Here is the standard output of the command:\n");191while ((s = stdInput.readLine()) != null) {192System.out.println(s);193}194195System.out.println("Here is the standard error of the command (if any):\n");196while ((s = stdError.readLine()) != null) {197System.out.println(s);198}199200} catch (Exception e){201e.printStackTrace();202}203}204205boolean validateReturnedClass(String className, String foundAt, CustomPartitioningURLCL loader){206boolean result = false;207Class classData = null;208classData = loader.getClassFromCache(className);209if(null != classData){210Object o = null;211try{212o = classData.newInstance();213} catch(Exception e){214e.printStackTrace();215}216TestClass tc = (TestClass)o;217String classLocation = tc.getLocation();218if(classLocation.equals(foundAt)){219result = true;220} else {221System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);222}223}224return result;225}226}227228229