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