Path: blob/master/test/functional/cmdLineTests/shareClassTests/URLHelperTests/ClassPathMatchingTests/URLClassPathMatchingTest.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 ClassPathMatchingTests;2223import java.io.File;24import java.io.FileInputStream;25import java.net.URL;26import java.util.Properties;2728import CustomCLs.CustomURLLoader;29import Utilities.StringManipulator;30import Utilities.URLClassPathCreator;3132/**33* @author Matthew Kilner34*/35public class URLClassPathMatchingTest {3637StringManipulator manipulator = new StringManipulator();3839public static void main(String[] args) {4041if(args.length != 2){42System.out.println("\n Incorrect usage");43System.out.println("\n Please specifiy -testfile <filename>");44}4546URLClassPathMatchingTest test = new URLClassPathMatchingTest();4748String testFile = args[1];4950test.run(testFile);51}5253public void run(String testFileName){5455Properties props = new Properties();56try{57FileInputStream PropertiesFile = new FileInputStream(testFileName);58props.load(PropertiesFile);5960PropertiesFile.close();61} catch (Exception e){62e.printStackTrace();63}6465String numberOfUrlsString = props.getProperty("NumberOfUrls");66Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);67int numberOfUrls = tempNumberOfUrls.intValue();6869int maxClassesToLoad = 0;70int maxClassesToFind = 0;71String[] urls = new String[numberOfUrls];72for(int index = 0; index < numberOfUrls; index++){73urls[index] = props.getProperty("Url"+index);74String ctl = props.getProperty("NumberOfClassesToLoad"+index);75Integer intctl = Integer.valueOf(ctl);76maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);77String ctf = props.getProperty("NumberOfClassesToFind"+index);78Integer intctf = Integer.valueOf(ctf);79maxClassesToFind = ((intctf.intValue() > maxClassesToFind) ? intctf.intValue() : maxClassesToFind);80}8182String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];83String[][] classesToFind = new String[numberOfUrls][maxClassesToFind];84String[][] results = new String[numberOfUrls][maxClassesToFind];8586for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){87String loadClasses = props.getProperty("LoadClasses"+urlIndex);88String findClasses = props.getProperty("FindClasses"+urlIndex);89String result = props.getProperty("Results"+urlIndex);90String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);91Integer intctl = Integer.valueOf(ctl);92int numberOfClassesToLoad = intctl.intValue();93String ctf = props.getProperty("NumberOfClassesToFind"+urlIndex);94Integer intctf = Integer.valueOf(ctf);95int numberOfClassesToFind = intctf.intValue();96for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){97classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);98}99for(int classToFindIndex = 0; classToFindIndex < numberOfClassesToFind; classToFindIndex++){100classesToFind[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, findClasses);101results[urlIndex][classToFindIndex] = manipulator.getStringElement(classToFindIndex, result);102}103}104105boolean passed = executeTest(urls, classesToLoad, classesToFind, results);106107if(passed){108System.out.println("\nTEST PASSED");109} else {110System.out.println("\nTEST FAILED");111}112}113114public boolean executeTest(String[] urls, String[][] classesToLoad, String[][] classesToFind, String[][] results){115116String urlsString = urls[0];117for(int index = 1; index < urls.length; index++){118urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).toString();119}120121URLClassPathCreator creator = new URLClassPathCreator(urlsString);122URL[] urlPath;123urlPath = creator.createURLClassPath();124CustomURLLoader cl = new CustomURLLoader(urlPath, this.getClass().getClassLoader());125126for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){127for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){128String classToLoad = classesToLoad[urlIndex][classIndex];129if(classToLoad != null){130cl.loadClassFrom(classToLoad, urlIndex);131}132}133}134135boolean result = true;136137for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){138for(int classIndex = 0; classIndex < classesToFind[urlIndex].length; classIndex++){139String classToFind = classesToFind[urlIndex][classIndex];140String expectedResult = results[urlIndex][classIndex];141if(classToFind != null){142String testResult = String.valueOf(cl.isClassInSharedCache(urlIndex, classToFind));143if(!(expectedResult.equals(testResult))){144System.out.println("\nFailure finding class: "+classToFind+" on path: "+urls[urlIndex]+" which is index: "+urlIndex+" result: "+testResult+" expecting: "+expectedResult);145result = false;146}147}148}149}150return result;151}152}153154155