Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLHelperURLClassPathHelperCompatibilityTest.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2005, 2020 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
import java.io.File;
23
import java.io.FileInputStream;
24
import java.net.URL;
25
import java.util.Properties;
26
27
import CustomCLs.CustomURLClassLoader;
28
import CustomCLs.CustomURLLoader;
29
import Utilities.StringManipulator;
30
import Utilities.TestClass;
31
import Utilities.URLClassPathCreator;
32
33
/**
34
* @author Matthew Kilner
35
*/
36
public class URLHelperURLClassPathHelperCompatibilityTest {
37
38
StringManipulator manipulator = new StringManipulator();
39
40
public static void main(String[] args) {
41
42
if(args.length != 2){
43
System.out.println("\n Incorrect usage");
44
System.out.println("\n Please specifiy -testfile <filename>");
45
}
46
47
URLHelperURLClassPathHelperCompatibilityTest test = new URLHelperURLClassPathHelperCompatibilityTest();
48
49
String testFile = args[1];
50
51
test.run(testFile);
52
53
}
54
55
public void run(String testFile){
56
57
Properties props = new Properties();
58
try{
59
FileInputStream PropertiesFile = new FileInputStream(testFile);
60
props.load(PropertiesFile);
61
62
PropertiesFile.close();
63
} catch (Exception e){
64
e.printStackTrace();
65
}
66
67
String numberOfUrlsString = props.getProperty("NumberOfUrls");
68
Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);
69
int numberOfUrls = tempNumberOfUrls.intValue();
70
71
int maxClassesToLoad = 0;
72
String[] urls = new String[numberOfUrls];
73
for(int index = 0; index < numberOfUrls; index++){
74
urls[index] = props.getProperty("Url"+index);
75
String ctl = props.getProperty("NumberOfClassesToLoad"+index);
76
Integer intctl = Integer.valueOf(ctl);
77
maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);
78
}
79
80
String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];
81
82
for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){
83
String loadClasses = props.getProperty("LoadClasses"+urlIndex);
84
String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);
85
Integer intctl = Integer.valueOf(ctl);
86
int numberOfClassesToLoad = intctl.intValue();
87
for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){
88
classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);
89
}
90
}
91
92
String classPath = props.getProperty("Classpath");
93
94
String ctf = props.getProperty("NumberOfClassesToFind");
95
Integer intctf = Integer.valueOf(ctf);
96
int numberOfClassesToFind = intctf.intValue();
97
98
String classesString = props.getProperty("FindClasses");
99
String [] classesToFind = new String[numberOfClassesToFind];
100
String resultsString = props.getProperty("Results");
101
String[] results = new String[numberOfClassesToFind];
102
String foundAtString = props.getProperty("FoundAt");
103
String[] foundAt = new String[numberOfClassesToFind];
104
for(int index = 0; index < numberOfClassesToFind; index++){
105
classesToFind[index] = manipulator.getStringElement(index, classesString);
106
results[index] = manipulator.getStringElement(index, resultsString);
107
foundAt[index] = manipulator.getStringElement(index, foundAtString);
108
}
109
110
boolean passed = executeTest(urls, classesToLoad, classPath, classesToFind, results, foundAt);
111
112
if(passed){
113
System.out.println("\nTEST PASSED");
114
} else {
115
System.out.println("\nTEST FAILED");
116
}
117
}
118
119
private boolean executeTest(String[] urls, String[][] classesToLoad, String classPath, String[] classesToFind, String[] results, String[] foundAt) {
120
121
String urlsString = urls[0];
122
for(int index = 1; index < urls.length; index++){
123
urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).append(File.pathSeparatorChar).toString();
124
}
125
126
URLClassPathCreator creator = new URLClassPathCreator(urlsString);
127
URL[] urlPath;
128
urlPath = creator.createURLClassPath();
129
CustomURLLoader[] loaderArray = new CustomURLLoader[urls.length];
130
131
for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){
132
for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){
133
loaderArray[urlIndex] = new CustomURLLoader(urlPath, this.getClass().getClassLoader());
134
String classToLoad = classesToLoad[urlIndex][classIndex];
135
if(classToLoad != null){
136
loaderArray[urlIndex].loadClassFrom(classToLoad, urlIndex);
137
}
138
}
139
}
140
141
boolean result = true;
142
143
URLClassPathCreator creator2 = new URLClassPathCreator(classPath);
144
URL[] urlPath2;
145
urlPath2 = creator2.createURLClassPath();
146
147
CustomURLClassLoader cl = new CustomURLClassLoader(urlPath2, this.getClass().getClassLoader());
148
for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){
149
String classToFind = classesToFind[classIndex];
150
String expectedResult = results[classIndex];
151
if (classToFind != null){
152
String testResult = String.valueOf(cl.isClassInSharedCache(classToFind));
153
if(!(expectedResult.equals(testResult))){
154
System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);
155
result = false;
156
} else {
157
if(testResult.equals("true")){
158
result = validateReturnedClass(classToFind, foundAt[classIndex], cl);
159
}
160
}
161
}
162
}
163
return result;
164
}
165
166
boolean validateReturnedClass(String className, String foundAt, CustomURLClassLoader loader){
167
boolean result = false;
168
Class classData = null;
169
classData = loader.getClassFromCache(className);
170
if(null != classData){
171
Object o = null;
172
try{
173
o = classData.newInstance();
174
} catch(Exception e){
175
e.printStackTrace();
176
}
177
TestClass tc = (TestClass)o;
178
String classLocation = tc.getLocation();
179
if(classLocation.equals(foundAt)){
180
result = true;
181
} else {
182
System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);
183
}
184
}
185
return result;
186
}
187
}
188
189