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