Path: blob/master/test/functional/cmdLineTests/shareClassTests/URLHelperTests/PartitioningTests/URLPartitioningStoreFindTest.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 PartitioningTests;2223import java.io.BufferedReader;24import java.io.FileInputStream;25import java.io.InputStreamReader;26import java.net.URL;27import java.util.Properties;2829import CustomCLs.CustomPartitioningURLLoader;30import Utilities.StringManipulator;31import Utilities.URLClassPathCreator;3233/**34* @author Matthew Kilner35*/36public class URLPartitioningStoreFindTest {3738StringManipulator manipulator = new StringManipulator();3940public static void main(String[] args) {4142if(args.length != 2){43System.out.println("\n Incorrect usage");44System.out.println("\n Please specifiy -testfile <filename>");45}4647URLPartitioningStoreFindTest test = new URLPartitioningStoreFindTest();4849String testFile = args[1];5051test.run(testFile);52}5354private void run(String testFile){5556Properties props = new Properties();57try{58FileInputStream PropertiesFile = new FileInputStream(testFile);59props.load(PropertiesFile);6061PropertiesFile.close();62} catch (Exception e){63e.printStackTrace();64}6566String numberOfUrlsString = props.getProperty("NumberOfUrls");67Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);68int numberOfUrls = tempNumberOfUrls.intValue();6970int maxClassesToLoad = 0;71int maxClassesToFind = 0;72String[] urls = new String[numberOfUrls];73String [] partitionStrings = new String[numberOfUrls];74for(int index = 0; index < numberOfUrls; index++){75urls[index] = props.getProperty("Url"+index);76partitionStrings[index] = props.getProperty("Partition"+index);77String ctl = props.getProperty("NumberOfClassesToLoad"+index);78Integer intctl = Integer.valueOf(ctl);79maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);80String ctf = props.getProperty("NumberOfClassesToFind"+index);81Integer intctf = Integer.valueOf(ctl);82maxClassesToFind = ((intctl.intValue() > maxClassesToFind) ? intctl.intValue() : maxClassesToFind);83}8485String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];86String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];87String[][] results = new String[numberOfUrls][maxClassesToFind];8889for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){90String loadClasses = props.getProperty("LoadClasses"+urlIndex);91String findClasses = props.getProperty("FindClasses"+urlIndex);92String result = props.getProperty("Results"+urlIndex);93String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);94Integer intctl = Integer.valueOf(ctl);95int numberOfClassesToLoad = intctl.intValue();96String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);97Integer intctf = Integer.valueOf(ctl);98int numberOfClassesToFind = intctf.intValue();99for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){100classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);101}102for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){103classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);104results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);105}106}107108String batchFile = props.getProperty("BatchFileToRun");109110boolean passed = runTest(urls, partitionStrings, classesToLoad, classesToFind, results, batchFile);111112if(passed){113System.out.println("\nTEST PASSED");114} else {115System.out.println("\nTEST FAILED");116}117}118119private boolean runTest(String[] urls, String[] partitionStrings, String[][] classesToLoad, String[][] classesToFind, String[][] results, String batchFile){120121String urlsString = urls[0];122for(int index = 1; index < urls.length; index++){123urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();124}125System.out.println("\n** urlsString: "+urlsString);126URLClassPathCreator creator = new URLClassPathCreator(urlsString);127URL[] urlPath;128urlPath = creator.createURLClassPath();129CustomPartitioningURLLoader cl = new CustomPartitioningURLLoader(urlPath, this.getClass().getClassLoader());130131for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){132for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){133String classToLoad = classesToLoad[urlIndex][classIndex];134if(classToLoad != null){135cl.setPartition(partitionStrings[urlIndex]);136cl.loadClassFrom(classToLoad, urlIndex);137}138}139}140141if(0 != batchFile.length()){142runBatchFile(batchFile);143}144145boolean result = true;146147for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){148cl.setPartition(partitionStrings[urlIndex]);149for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){150String classToFind = classesToFind[urlIndex][classIndex];151String expectedResult = results[urlIndex][classIndex];152if(classToFind != null){153String testResult = String.valueOf(cl.isClassInSharedCache(urlIndex, classToFind));154if(!(expectedResult.equals(testResult))){155System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);156result = false;157}158}159}160}161return result;162}163164private void runBatchFile(String batch){165System.out.println("\n** makeStale: "+batch);166String s = null;167try{168Process p = Runtime.getRuntime().exec(batch);169170BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));171BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));172173System.out.println("Here is the standard output of the command:\n");174while ((s = stdInput.readLine()) != null) {175System.out.println(s);176}177178System.out.println("Here is the standard error of the command (if any):\n");179while ((s = stdError.readLine()) != null) {180System.out.println(s);181}182183} catch (Exception e){184e.printStackTrace();185}186}187}188189190