Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/TokenHelperTests/StaleMarkingTests/StaleMarkingTest.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
23
package StaleMarkingTests;
24
25
import java.io.FileInputStream;
26
import java.net.URL;
27
import java.util.Properties;
28
29
import CustomCLs.CustomTokenClassLoader;
30
import Utilities.StringManipulator;
31
import Utilities.TestClass;
32
import Utilities.URLClassPathCreator;
33
34
public class StaleMarkingTest {
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>");
43
}
44
45
StaleMarkingTest test = new StaleMarkingTest();
46
47
String testFile = args[1];
48
49
test.run(testFile);
50
}
51
52
public void run(String testFile){
53
54
Properties props = new Properties();
55
try{
56
FileInputStream PropertiesFile = new FileInputStream(testFile);
57
props.load(PropertiesFile);
58
59
PropertiesFile.close();
60
} catch (Exception e){
61
e.printStackTrace();
62
}
63
64
String clc = props.getProperty("NumberOfClassloaders");
65
Integer c = Integer.valueOf(clc);
66
int classloaderCount = c.intValue();
67
String [] classLoaderPaths = new String[classloaderCount];
68
int maxClassesToLoad =0;
69
for(int index = 0; index < classloaderCount; index++){
70
classLoaderPaths[index] = props.getProperty("ClassPath"+index);
71
String ctl = props.getProperty("NumberOfClassesToLoad"+index);
72
Integer intctl = Integer.valueOf(ctl);
73
maxClassesToLoad = ((intctl.intValue() > maxClassesToLoad) ? intctl.intValue() : maxClassesToLoad);
74
}
75
76
String [][] classesToLoad = new String[classloaderCount][maxClassesToLoad];
77
for(int loaderIndex = 0; loaderIndex < classloaderCount; loaderIndex++){
78
String nctls = props.getProperty("NumberOfClassesToLoad"+loaderIndex);
79
Integer i = Integer.valueOf(nctls);
80
int classesToLoadCount = i.intValue();
81
String classesString = props.getProperty("ClassesToLoad"+loaderIndex);
82
for (int classesIndex = 0; classesIndex < classesToLoadCount; classesIndex++){
83
classesToLoad[loaderIndex][classesIndex] = manipulator.getStringElement(classesIndex, classesString);
84
}
85
}
86
87
String findPath = props.getProperty("FindPath");
88
String ctf = props.getProperty("NumberOfClassesToFind");
89
Integer intctf = Integer.valueOf(ctf);
90
int numberOfClassesToFind = intctf.intValue();
91
92
String classesString = props.getProperty("FindClasses");
93
String [] classesToFind = new String[numberOfClassesToFind];
94
String resultsString = props.getProperty("Results");
95
String[] results = new String[numberOfClassesToFind];
96
String foundAtString = props.getProperty("FoundAt");
97
String[] foundAt = new String[numberOfClassesToFind];
98
for(int index = 0; index < numberOfClassesToFind; index++){
99
classesToFind[index] = manipulator.getStringElement(index, classesString);
100
results[index] = manipulator.getStringElement(index, resultsString);
101
foundAt[index] = manipulator.getStringElement(index, foundAtString);
102
}
103
104
boolean passed = executeTest(classLoaderPaths, classesToLoad, findPath, classesToFind, results, foundAt);
105
106
if(passed){
107
System.out.println("\nTEST PASSED");
108
} else {
109
System.out.println("\nTEST FAILED");
110
}
111
}
112
113
public boolean executeTest(String[] loaderPaths, String[][] classesToLoad, String findPath, String[] classesToFind, String[] results, String[] foundAt){
114
115
for(int index = 0; index < loaderPaths.length; index++){
116
URLClassPathCreator creator = new URLClassPathCreator(loaderPaths[index]);
117
URL[] urlPath;
118
urlPath = creator.createURLClassPath();
119
120
CustomTokenClassLoader cl = new CustomTokenClassLoader(urlPath, this.getClass().getClassLoader());
121
cl.setToken("StaleMarking");
122
for(int classIndex = 0; classIndex < classesToLoad[index].length; classIndex++){
123
String classToLoad = classesToLoad[index][classIndex];
124
if (classToLoad != null){
125
try{
126
cl.loadClassNoCache(classToLoad);
127
} catch (Exception e){
128
e.printStackTrace();
129
}
130
}
131
}
132
}
133
134
boolean result = true;
135
136
URLClassPathCreator creator = new URLClassPathCreator(findPath);
137
URL[] findPathURLS;
138
findPathURLS = creator.createURLClassPath();
139
140
CustomTokenClassLoader vf = new CustomTokenClassLoader(findPathURLS, this.getClass().getClassLoader());
141
vf.setToken("StaleMarking");
142
for(int classIndex = 0; classIndex < classesToFind.length; classIndex++){
143
String classToFind = classesToFind[classIndex];
144
String expectedResult = results[classIndex];
145
if (classToFind != null){
146
String testResult = String.valueOf(vf.isClassInSharedCache("StaleMarking", classToFind));
147
if(!(expectedResult.equals(testResult))){
148
System.out.println("\nFailure finding class: "+classToFind+" result: "+testResult+" expecting: "+expectedResult);
149
result = false;
150
} else {
151
if(testResult.equals("true")){
152
result = validateReturnedClass(classToFind, foundAt[classIndex], vf);
153
}
154
}
155
}
156
}
157
return result;
158
}
159
160
boolean validateReturnedClass(String className, String foundAt, CustomTokenClassLoader loader){
161
boolean result = false;
162
Class classData = null;
163
classData = loader.getClassFromCache(className);
164
if(null != classData){
165
Object o = null;
166
try{
167
o = classData.newInstance();
168
} catch(Exception e){
169
e.printStackTrace();
170
}
171
TestClass tc = (TestClass)o;
172
String classLocation = tc.getLocation();
173
if(classLocation.equals(foundAt)){
174
result = true;
175
} else {
176
System.out.println("\nClass location: "+classLocation+" expecting: "+foundAt);
177
}
178
}
179
return result;
180
}
181
}
182
183