Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/RasapiTest/src/com/ibm/dump/tests/OutputFile.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2016, 2021 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 com.ibm.dump.tests;
23
24
import java.io.BufferedReader;
25
import java.io.FileReader;
26
import java.io.IOException;
27
import java.util.ArrayList;
28
29
public class OutputFile {
30
31
BufferedReader in;
32
String line;
33
String firstReference;
34
String secondReference;
35
36
public OutputFile(String fileName) throws IOException {
37
in = new BufferedReader(new FileReader(fileName));
38
firstReference = null;
39
}
40
41
/**
42
* Yes, finalizers may not have a very good reputation but it can't do any harm
43
*/
44
protected void finalize() throws Throwable {
45
try {
46
in.close();
47
} finally {
48
super.finalize();
49
}
50
}
51
52
53
/**
54
* Read through the file looking for the given string. Keep going to the end of the file if necessary.
55
* If the string is found, return true, set the instance variable line, and keep that position in the file.
56
* If the string is not found, return false, leave the file positioned at the end.
57
58
* @param lookFor the string to look for
59
* @return true or false according to whether a line with the string was found
60
* @throws IOException
61
*/
62
public boolean skipUnlimitedToLineContaining(String lookFor) throws IOException {
63
line = in.readLine();
64
while (line != null) {
65
if (line.indexOf(lookFor) != -1) {
66
System.err.printf("Successfully found the following line containing \"%s\"\n", lookFor);
67
System.err.printf(line);
68
System.err.printf("\n");
69
return true;
70
}
71
line = in.readLine();
72
}
73
System.err.printf("***Reached end of file before finding a line containing \"%s\"\n", lookFor);
74
return false;
75
}
76
77
/**
78
* Read through the file looking for the given string. Keep going to the end of the file if necessary.
79
* If the string is found AT THE END OF THE LINE, return true, set the instance variable line, and keep that position in the file.
80
* If the string is not found, return false, leave the file positioned at the end.
81
*
82
* This method is especially for searching for the line ending "OBJ com/ibm/dump/tests/types/packed/NestedPacked" in the
83
* classic heapdump. Just using skipUnlimitedToLineContaining finds the lines for NestedPacked1 and NestedPacked2
84
85
* @param lookFor the string to look for
86
* @return true or false according to whether a line with the string was found
87
* @throws IOException
88
*/
89
public boolean skipUnlimitedToLineEnding(String lookFor) throws IOException {
90
line = in.readLine();
91
while (line != null) {
92
if (line.endsWith(lookFor)) {
93
System.err.printf("Successfully found the following line ending \"%s\"\n", lookFor);
94
System.err.printf(line);
95
System.err.printf("\n");
96
return true;
97
}
98
line = in.readLine();
99
}
100
System.err.printf("***Reached end of file before finding a line ending \"%s\"\n", lookFor);
101
return false;
102
}
103
104
/**
105
* Read no more than maxLinesToSkip down the file looking for a line containing the given string
106
* Read some number of lines down the file looking for the given string. Read no more than maxLinesToSkip lines.
107
* If the string is found, hold the line in instance variable line and keep the position in the file.
108
* If the string is not found, return false and restore the position in the file to where it was on entry:
109
* by doing so one failing test does not damage the tests that follow.
110
* @param maxLinesToSkip skip no more lines than this
111
* @param lookFor the string to look for
112
* @return true or false according to whether a line with the string was found
113
* @throws IOException
114
*/
115
public boolean skipLimitedToLineContaining(int maxLinesToSkip, String lookFor) throws IOException {
116
int linesRead = 0;
117
in.mark(maxLinesToSkip * 200); // generous allocation to allow reset to work. Most lines are < 100 bytes long
118
line = in.readLine();
119
while (line != null && linesRead < maxLinesToSkip) {
120
// System.err.printf("line read was %s\n", line);
121
if (line.indexOf(lookFor) != -1) {
122
System.err.printf("Successfully found a line containing \"%s\"\n", lookFor);
123
return true;
124
}
125
line = in.readLine();
126
linesRead++;
127
}
128
System.err.printf("***Failed to find a line containing \"%s\"\n", lookFor);
129
in.reset();
130
return false;
131
}
132
133
134
/**
135
* Examine the last line read and check that the word at the given position is equal to given string
136
* @param lookFor the string to look for
137
* @param whichWord the index of the word to check
138
* @return
139
*/
140
public boolean linePassesCheckForStringAtWord(String lookFor, int whichWord) {
141
if (line == null) {
142
// previous call to skip[Un]limitedToLineContaining has failed
143
// so appropriate error message has been emitted already
144
// just return false and allow test to contine.
145
return false;
146
}
147
return StringUtils.linePassesCheckForStringAtWord(line, whichWord, lookFor);
148
}
149
150
/**
151
* Return true or false depending on whether the word at position whichWord is in the list
152
* of values in validValues
153
*
154
* @param validValues
155
* @param whichWord
156
* @return
157
*/
158
public boolean linePassesCheckForStringsAtWord(String[] validValues, int whichWord) {
159
if (line == null) {
160
// previous call to skip[Un]limitedToLineContaining has failed
161
// so appropriate error message has been emitted already
162
// just return false and allow test to contine.
163
return false;
164
}
165
return StringUtils.linePassesCheckForStringsAtWord(line, whichWord, validValues);
166
}
167
168
/**
169
*
170
* @return the first reference that was found by the previous call to skipToNextLineAndCheckReferencesCount.
171
* This is just the first token that was on that line.
172
*/
173
public String getFirstReference() {
174
return firstReference;
175
}
176
177
/**
178
*
179
* @return the second reference that was found by the previous call to skipToNextLineAndCheckReferencesCount.
180
* This is just the second token that was on that line.
181
*/
182
public String getSecondReference() {
183
return secondReference;
184
}
185
186
/**
187
* Skip down to the lines containing "references:" then check the number of references on the
188
* succeeding line.
189
* @param maxLinesToSkip
190
* @param expectedNumberOfReferences
191
* @return true or false depending on whether the number of references matches the expected number.
192
* @throws IOException
193
*/
194
public boolean skipToLineContainingListOfReferencesAndCheckCount(int maxLinesToSkip, int expectedNumberOfReferences) throws IOException {
195
196
if (! skipLimitedToLineContaining(maxLinesToSkip, "references:")) {
197
// if we cannot find the references line, don't go further
198
return false;
199
}
200
return skipToNextLineAndCheckReferencesCount(expectedNumberOfReferences);
201
202
}
203
204
/**
205
* Skip down the output from jdmpview's x/j command looking for the line containing "references:"
206
* @param maxLinesToSkip
207
* @return
208
* @throws IOException
209
*/
210
public boolean skipLimitedToReferencesLine(int maxLinesToSkip) throws IOException {
211
if (! skipLimitedToLineContaining(maxLinesToSkip, "references:")) {
212
// if we cannot find the references line, don't go further
213
return false;
214
}
215
return true;
216
}
217
218
/**
219
* Read the next line, tokenise it, and check that the number of tokens equals the given parameter.
220
* As a side effect, set the instance variable firstReference.
221
*
222
* This method can be called either from the classic heapdump checkers, when the file will be already
223
* positioned on the OBJ line and the references always come on the next line,
224
* or called from skipToLineContainingListOfReferencesAndCheckCount() when checking the
225
* output from jdmpview's x/j command. In either case, the list of references is on the very next line.
226
*
227
*
228
*
229
* @param expectedNumberOfReferences
230
* @return true or false depending on whether the line does or does not contain the given number of tokens.
231
* @throws IOException
232
*/
233
public boolean skipToNextLineAndCheckReferencesCount(int expectedNumberOfReferences) throws IOException {
234
// assert
235
// if we are called by one of the classic heapdump checkers we are currently on the OBJ line and
236
// skipping to the next line will put us on the list of references, or
237
// we will have been called from skipToReferencesLineAndCheckCount if we are checking the output
238
// from jdmpview's x/j command during phd checking
239
240
241
String line = in.readLine();
242
if (line == null) {
243
System.err.println("***Unable to check count of references - previous call to find the references lines has failed.");
244
return false;
245
}
246
247
ArrayList<String> references = StringUtils.extractTokensFromLine(line);
248
if (references.size() > 0) {
249
firstReference = references.get(0);
250
} else {
251
firstReference = null;
252
}
253
if (references.size() > 1) {
254
secondReference = references.get(1);
255
} else {
256
secondReference = null;
257
}
258
if (references.size() != expectedNumberOfReferences) {
259
System.err.println("***Failure: there should have been exactly " + expectedNumberOfReferences + " reference(s), not " + references.size());
260
return false;
261
} else {
262
System.err.println("Successfully found exactly " + expectedNumberOfReferences + " reference(s)");
263
return true;
264
}
265
}
266
267
/**
268
* Read the next line, tokenise it, and return the the number of tokens (references).
269
*
270
* This method is called from the classic heapdump checkers, when the file will be already
271
* positioned on the OBJ line and the references always come on the next line
272
*
273
* @return number of references
274
* @throws IOException
275
*/
276
public int skipToNextLineAndCountReferences() throws IOException {
277
// if we are called by one of the classic heapdump checkers we are currently on the OBJ line and
278
// skipping to the next line will put us on the list of references
279
String line = in.readLine();
280
if (line == null) {
281
System.err.println("***Unable to check count of references - previous call to find the references lines has failed.");
282
return 0;
283
}
284
285
ArrayList<String> references = StringUtils.extractTokensFromLine(line);
286
return references.size();
287
}
288
289
public static boolean checkReferenceInClassicHeapdumpIsToObject(String fileName, String reference, String className) throws IOException {
290
OutputFile o = new OutputFile(fileName);
291
boolean passed = true;
292
System.err.println("Checking that the reference " + reference + " is to an object of type " + className);
293
passed &= o.skipUnlimitedToLineBeginning(reference);
294
passed &= o.linePassesCheckForStringAtWord(className,4);
295
return passed;
296
}
297
298
private boolean skipUnlimitedToLineBeginning(String lookFor) throws IOException {
299
line = in.readLine();
300
while (line != null) {
301
if (line.startsWith(lookFor) ) {
302
System.err.printf("Successfully found a line beginning \"%s\"\n", lookFor);
303
System.err.printf(line);
304
System.err.printf("\n");
305
return true;
306
}
307
line = in.readLine();
308
}
309
System.err.printf("***Reached end of file before finding a line beginning \"%s\"\n", lookFor);
310
return false;
311
}
312
313
public static boolean checkReferenceIsToObject(String fileName, String reference, String className) throws IOException {
314
OutputFile o = new OutputFile(fileName);
315
boolean passed = true;
316
System.err.println("Checking that the reference " + reference + " is to an object of type " + className);
317
passed &= o.skipUnlimitedToLineContaining(className + " @ " + reference);
318
return passed;
319
}
320
321
}
322
323
324