Path: blob/master/test/functional/cmdLineTests/shareClassTests/TokenHelperTests/TimeStampingTests/TimeStampingTest.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 TimeStampingTests;2223import java.io.BufferedReader;24import java.io.FileInputStream;25import java.io.InputStreamReader;26import java.net.URL;27import java.util.Properties;2829import CustomCLs.CustomTokenClassLoader;30import Utilities.StringManipulator;31import Utilities.URLClassPathCreator;3233public class TimeStampingTest {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> -javacdir <path to javac>");42}4344TimeStampingTest test = new TimeStampingTest();4546String testFile = args[1];47String javacdir = args[3];4849test.run(testFile, javacdir);50}5152public void run(String testFile, String javacpath){5354Properties props = new Properties();55try{56FileInputStream PropertiesFile = new FileInputStream(testFile);57props.load(PropertiesFile);5859PropertiesFile.close();60} catch (Exception e){61e.printStackTrace();62}6364String numberOfUrlsString = props.getProperty("NumberOfUrls");65Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);66int numberOfUrls = tempNumberOfUrls.intValue();6768int maxClassesToLoad = 0;69int maxClassesToFind = 0;70String[] urls = new String[numberOfUrls];71for(int index = 0; index < numberOfUrls; index++){72urls[index] = props.getProperty("Url"+index);73String ctl = props.getProperty("NumberOfClassesToLoad"+index);74Integer intctl = Integer.valueOf(ctl);75maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);76String ctf = props.getProperty("NumberOfClassesToFind"+index);77Integer intctf = Integer.valueOf(ctl);78maxClassesToFind = ((intctl.intValue() > maxClassesToFind) ? intctl.intValue() : maxClassesToFind);79}8081String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];82String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];83String[][] results = new String[numberOfUrls][maxClassesToFind];8485for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){86String loadClasses = props.getProperty("LoadClasses"+urlIndex);87String findClasses = props.getProperty("FindClasses"+urlIndex);88String result = props.getProperty("Results"+urlIndex);89String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);90Integer intctl = Integer.valueOf(ctl);91int numberOfClassesToLoad = intctl.intValue();92String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);93Integer intctf = Integer.valueOf(ctl);94int numberOfClassesToFind = intctf.intValue();95for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){96classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);97}98for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){99classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);100results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);101}102}103104String batchFile = props.getProperty("BatchFileToRun");105106boolean passed = executeTest(urls, classesToLoad, classesToFind, results, batchFile, javacpath);107108if(passed){109System.out.println("\nTEST PASSED");110} else {111System.out.println("\nTEST FAILED");112}113}114115public boolean executeTest(String[] urls, String[][] classesToLoad, String[][] classesToFind, String[][] results, String batchFile, String javacpath){116117String urlsString = urls[0];118for(int index = 1; index < urls.length; index++){119urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();120}121System.out.println("\n** urlsString: "+urlsString);122URLClassPathCreator creator = new URLClassPathCreator(urlsString);123URL[] urlPath;124urlPath = creator.createURLClassPath();125CustomTokenClassLoader cl = new CustomTokenClassLoader(urlPath, this.getClass().getClassLoader());126String token = new String("TimeStamping");127cl.setToken(token);128129for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){130for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){131String classToLoad = classesToLoad[urlIndex][classIndex];132if(classToLoad != null){133try{134cl.loadClass(classToLoad);135}catch (ClassNotFoundException e){136e.printStackTrace();137}138}139}140}141142runBatchFile(batchFile, javacpath);143144boolean result = true;145146for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){147for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){148String classToFind = classesToFind[urlIndex][classIndex];149String expectedResult = results[urlIndex][classIndex];150if(classToFind != null){151String testResult = String.valueOf(cl.isClassInSharedCache(token, classToFind));152if(!(expectedResult.equals(testResult))){153System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);154result = false;155}156}157}158}159return result;160}161162private void runBatchFile(String batch, String javacpath){163String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();164System.out.println("\n** Running: "+command);165String s = null;166try{167Process p = Runtime.getRuntime().exec(command);168169BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));170BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));171172System.out.println("Here is the standard output of the command:\n");173while ((s = stdInput.readLine()) != null) {174System.out.println(s);175}176177System.out.println("Here is the standard error of the command (if any):\n");178while ((s = stdError.readLine()) != null) {179System.out.println(s);180}181182} catch (Exception e){183e.printStackTrace();184}185}186}187188189