Path: blob/master/test/functional/cmdLineTests/shareClassTests/TokenHelperTests/StaleMarkingTests/StaleMarkingTest.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*******************************************************************************/2122package StaleMarkingTests;2324import java.io.FileInputStream;25import java.net.URL;26import java.util.Properties;2728import CustomCLs.CustomTokenClassLoader;29import Utilities.StringManipulator;30import Utilities.TestClass;31import Utilities.URLClassPathCreator;3233public class StaleMarkingTest {3435StringManipulator manipulator = new StringManipulator();3637public static void main(String[] args) {3839if(args.length != 2){40System.out.println("\n Incorrect usage");41System.out.println("\n Please specifiy -testfile <filename>");42}4344StaleMarkingTest test = new StaleMarkingTest();4546String testFile = args[1];4748test.run(testFile);49}5051public void run(String testFile){5253Properties props = new Properties();54try{55FileInputStream PropertiesFile = new FileInputStream(testFile);56props.load(PropertiesFile);5758PropertiesFile.close();59} catch (Exception e){60e.printStackTrace();61}6263String clc = props.getProperty("NumberOfClassloaders");64Integer c = Integer.valueOf(clc);65int classloaderCount = c.intValue();66String [] classLoaderPaths = new String[classloaderCount];67int maxClassesToLoad =0;68for(int index = 0; index < classloaderCount; index++){69classLoaderPaths[index] = props.getProperty("ClassPath"+index);70String ctl = props.getProperty("NumberOfClassesToLoad"+index);71Integer intctl = Integer.valueOf(ctl);72maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);73}7475String [][] classesToLoad = new String[classloaderCount][maxClassesToLoad];76for(int loaderIndex = 0; loaderIndex < classloaderCount; loaderIndex++){77String nctls = props.getProperty("NumberOfClassesToLoad"+loaderIndex);78Integer i = Integer.valueOf(nctls);79int classesToLoadCount = i.intValue();80String classesString = props.getProperty("ClassesToLoad"+loaderIndex);81for (int classesIndex = 0; classesIndex < classesToLoadCount; classesIndex++){82classesToLoad[loaderIndex][classesIndex] = manipulator.getStringElement(classesIndex, classesString);83}84}8586String findPath = props.getProperty("FindPath");87String ctf = props.getProperty("NumberOfClassesToFind");88Integer intctf = Integer.valueOf(ctf);89int numberOfClassesToFind = intctf.intValue();9091String classesString = props.getProperty("FindClasses");92String [] classesToFind = new String[numberOfClassesToFind];93String resultsString = props.getProperty("Results");94String[] results = new String[numberOfClassesToFind];95String foundAtString = props.getProperty("FoundAt");96String[] foundAt = new String[numberOfClassesToFind];97for(int index = 0; index < numberOfClassesToFind; index++){98classesToFind[index] = manipulator.getStringElement(index, classesString);99results[index] = manipulator.getStringElement(index, resultsString);100foundAt[index] = manipulator.getStringElement(index, foundAtString);101}102103boolean passed = executeTest(classLoaderPaths, classesToLoad, findPath, classesToFind, results, foundAt);104105if(passed){106System.out.println("\nTEST PASSED");107} else {108System.out.println("\nTEST FAILED");109}110}111112public boolean executeTest(String[] loaderPaths, String[][] classesToLoad, String findPath, String[] classesToFind, String[] results, String[] foundAt){113114for(int index = 0; index < loaderPaths.length; index++){115URLClassPathCreator creator = new URLClassPathCreator(loaderPaths[index]);116URL[] urlPath;117urlPath = creator.createURLClassPath();118119CustomTokenClassLoader cl = new CustomTokenClassLoader(urlPath, this.getClass().getClassLoader());120cl.setToken("StaleMarking");121for(int classIndex = 0; classIndex < classesToLoad[index].length; classIndex++){122String classToLoad = classesToLoad[index][classIndex];123if (classToLoad != null){124try{125cl.loadClassNoCache(classToLoad);126} catch (Exception e){127e.printStackTrace();128}129}130}131}132133boolean result = true;134135URLClassPathCreator creator = new URLClassPathCreator(findPath);136URL[] findPathURLS;137findPathURLS = creator.createURLClassPath();138139CustomTokenClassLoader vf = new CustomTokenClassLoader(findPathURLS, this.getClass().getClassLoader());140vf.setToken("StaleMarking");141for(int classIndex = 0; classIndex < classesToFind.length; classIndex++){142String classToFind = classesToFind[classIndex];143String expectedResult = results[classIndex];144if (classToFind != null){145String testResult = String.valueOf(vf.isClassInSharedCache("StaleMarking", classToFind));146if(!(expectedResult.equals(testResult))){147System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);148result = false;149} else {150if(testResult.equals("true")){151result = validateReturnedClass(classToFind, foundAt[classIndex], vf);152}153}154}155}156return result;157}158159boolean validateReturnedClass(String className, String foundAt, CustomTokenClassLoader loader){160boolean result = false;161Class classData = null;162classData = loader.getClassFromCache(className);163if(null != classData){164Object o = null;165try{166o = classData.newInstance();167} catch(Exception e){168e.printStackTrace();169}170TestClass tc = (TestClass)o;171String classLocation = tc.getLocation();172if(classLocation.equals(foundAt)){173result = true;174} else {175System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);176}177}178return result;179}180}181182183