Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCHelperCompatTests/URLHelperURLClassPathHelperStaleEntryCompatibilityTest.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.BufferedReader;
23
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.InputStreamReader;
26
import java.net.URL;
27
import java.util.Properties;
28
29
import CustomCLs.CustomURLClassLoader;
30
import CustomCLs.CustomURLLoader;
31
import Utilities.StringManipulator;
32
import Utilities.TestClass;
33
import Utilities.URLClassPathCreator;
34
35
/**
36
* @author Matthew Kilner
37
*/
38
public class URLHelperURLClassPathHelperStaleEntryCompatibilityTest {
39
40
StringManipulator manipulator = new StringManipulator();
41
42
public static void main(String[] args) {
43
44
if(args.length != 2){
45
System.out.println("\n Incorrect usage");
46
System.out.println("\n Please specifiy -testfile <filename> -javacdir <path to javac>");
47
}
48
49
URLHelperURLClassPathHelperStaleEntryCompatibilityTest test = new URLHelperURLClassPathHelperStaleEntryCompatibilityTest();
50
51
String testFile = args[1];
52
String javacdir = args[3];
53
54
test.run(testFile, javacdir);
55
56
}
57
58
public void run(String testFile, String javacpath){
59
60
Properties props = new Properties();
61
try{
62
FileInputStream PropertiesFile = new FileInputStream(testFile);
63
props.load(PropertiesFile);
64
65
PropertiesFile.close();
66
} catch (Exception e){
67
e.printStackTrace();
68
}
69
70
String numberOfUrlsString = props.getProperty("NumberOfUrls");
71
Integer tempNumberOfUrls = Integer.valueOf(numberOfUrlsString);
72
int numberOfUrls = tempNumberOfUrls.intValue();
73
74
int maxClassesToLoad = 0;
75
String[] urls = new String[numberOfUrls];
76
for(int index = 0; index < numberOfUrls; index++){
77
urls[index] = props.getProperty("Url"+index);
78
String ctl = props.getProperty("NumberOfClassesToLoad"+index);
79
Integer intctl = Integer.valueOf(ctl);
80
maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);
81
}
82
83
String[][] classesToLoad = new String[numberOfUrls][maxClassesToLoad];
84
85
for(int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++){
86
String loadClasses = props.getProperty("LoadClasses"+urlIndex);
87
String ctl = props.getProperty("NumberOfClassesToLoad"+urlIndex);
88
Integer intctl = Integer.valueOf(ctl);
89
int numberOfClassesToLoad = intctl.intValue();
90
for(int classToLoadIndex = 0; classToLoadIndex < numberOfClassesToLoad; classToLoadIndex++){
91
classesToLoad[urlIndex][classToLoadIndex] = manipulator.getStringElement(classToLoadIndex, loadClasses);
92
}
93
}
94
95
String classPath = props.getProperty("Classpath");
96
97
String ctf = props.getProperty("NumberOfClassesToFind");
98
Integer intctf = Integer.valueOf(ctf);
99
int numberOfClassesToFind = intctf.intValue();
100
101
String classesString = props.getProperty("FindClasses");
102
String [] classesToFind = new String[numberOfClassesToFind];
103
String resultsString = props.getProperty("Results");
104
String[] results = new String[numberOfClassesToFind];
105
String foundAtString = props.getProperty("FoundAt");
106
String[] foundAt = new String[numberOfClassesToFind];
107
for(int index = 0; index < numberOfClassesToFind; index++){
108
classesToFind[index] = manipulator.getStringElement(index, classesString);
109
results[index] = manipulator.getStringElement(index, resultsString);
110
foundAt[index] = manipulator.getStringElement(index, foundAtString);
111
}
112
113
String batchFile = props.getProperty("BatchFileToRun");
114
115
boolean passed = executeTest(urls, classesToLoad, classPath, classesToFind, results, batchFile, foundAt, javacpath);
116
117
if(passed){
118
System.out.println("\nTEST PASSED");
119
} else {
120
System.out.println("\nTEST FAILED");
121
}
122
}
123
124
private boolean executeTest(String[] urls, String[][] classesToLoad, String classPath, String[] classesToFind, String[] results, String batchFile, String[] foundAt, String javacpath) {
125
126
String urlsString = urls[0];
127
for(int index = 1; index < urls.length; index++){
128
urlsString = new StringBuffer(urls[index].length() + 1).append(urlsString).append(urls[index]).append(File.pathSeparatorChar).toString();
129
}
130
System.out.println("\n** urlsString: "+urlsString);
131
132
URLClassPathCreator creator = new URLClassPathCreator(urlsString);
133
URL[] urlPath;
134
urlPath = creator.createURLClassPath();
135
CustomURLLoader[] loaderArray = new CustomURLLoader[urls.length];
136
137
for(int urlIndex = 0; urlIndex < urls.length; urlIndex++){
138
for(int classIndex = 0; classIndex < classesToLoad[urlIndex].length; classIndex++){
139
loaderArray[urlIndex] = new CustomURLLoader(urlPath, this.getClass().getClassLoader());
140
String classToLoad = classesToLoad[urlIndex][classIndex];
141
if(classToLoad != null){
142
loaderArray[urlIndex].loadClassFrom(classToLoad, urlIndex);
143
}
144
}
145
}
146
147
runBatchFile(batchFile, javacpath);
148
149
boolean result = true;
150
151
URLClassPathCreator creator2 = new URLClassPathCreator(classPath);
152
URL[] urlPath2;
153
urlPath2 = creator2.createURLClassPath();
154
155
CustomURLClassLoader cl = new CustomURLClassLoader(urlPath2, this.getClass().getClassLoader());
156
for(int classIndex = 0; classIndex < classesToLoad.length; classIndex++){
157
String classToFind = classesToFind[classIndex];
158
String expectedResult = results[classIndex];
159
if (classToFind != null){
160
String testResult = String.valueOf(cl.isClassInSharedCache(classToFind));
161
if(!(expectedResult.equals(testResult))){
162
System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);
163
result = false;
164
}else {
165
if(testResult.equals("true")){
166
result = validateReturnedClass(classToFind, foundAt[classIndex], cl);
167
}
168
}
169
}
170
}
171
return result;
172
}
173
174
boolean validateReturnedClass(String className, String foundAt, CustomURLClassLoader loader){
175
boolean result = false;
176
Class classData = null;
177
classData = loader.getClassFromCache(className);
178
if(null != classData){
179
Object o = null;
180
try{
181
o = classData.newInstance();
182
} catch(Exception e){
183
e.printStackTrace();
184
}
185
TestClass tc = (TestClass)o;
186
String classLocation = tc.getLocation();
187
if(classLocation.equals(foundAt)){
188
result = true;
189
} else {
190
System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);
191
}
192
} else {
193
System.out.println("\nCould not get class data from cache");
194
}
195
return result;
196
}
197
198
private void runBatchFile(String batch, String javacpath){
199
String command = new StringBuffer(batch.length()+javacpath.length()+1).append(batch).append(" ").append(javacpath).toString();
200
System.out.println("\n** Running: "+command);
201
String s = null;
202
try{
203
Process p = Runtime.getRuntime().exec(command);
204
205
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
206
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
207
208
System.out.println("Here is the standard output of the command:\n");
209
while ((s = stdInput.readLine()) != null) {
210
System.out.println(s);
211
}
212
213
System.out.println("Here is the standard error of the command (if any):\n");
214
while ((s = stdError.readLine()) != null) {
215
System.out.println(s);
216
}
217
218
} catch (Exception e){
219
e.printStackTrace();
220
}
221
}
222
}
223
224