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