Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/DataHelperTests/src/apitesting/datahelper/DataCachingTestbase.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 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 apitesting.datahelper;
23
24
import java.io.BufferedInputStream;
25
import java.io.File;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.net.MalformedURLException;
29
import java.net.URL;
30
31
import apitesting.Testbase;
32
33
import CustomClassloaders.DataCachingClassLoader;
34
35
/**
36
* Structure of the test data available to these test programs. Where the same file is mentioned as available from
37
* two places, the contents of the file indicate where it came from (so, below, contents of fileone.txt indicate
38
* whether it came from dataone or datatwo)
39
*
40
* datacaching/dataone.jar
41
* - contains two text files (fileone.txt, filetwo.txt)
42
*
43
* datacaching/datatwo.jar
44
* - contains two text files (fileone.txt, filethree.txt)
45
*
46
* Constants are defined in this base class for paths to these files and the expected contents
47
*/
48
public class DataCachingTestbase extends Testbase {
49
50
static final String FILEONE = "fileone.txt";
51
static final String FILETWO = "filetwo.txt";
52
static final String FILETHREE = "filethree.txt";
53
54
static final String CLASSPATH_JARONE = "datacaching/dataone.jar";
55
protected static final String CONTENTS_JARONE_FILEONE = "jar one, file one";
56
protected static final String CONTENTS_JARONE_FILETWO = "jar one, file two";
57
58
static final String CLASSPATH_JARTWO = "datacaching/datatwo.jar";
59
protected static final String CONTENTS_JARTWO_FILEONE = "jar two, file one";
60
protected static final String CONTENTS_JARTWO_FILETHREE = "jar two, file three";
61
62
/**
63
* Retrieves a DataCachingClassLoader instance - this is a classloader that can be
64
* configured to utilise the sharedcache for any data that it loads.
65
*/
66
protected DataCachingClassLoader getDataCachingLoader(String[] cp) {
67
try {
68
URL[] urlcp = new URL[cp.length];
69
for (int i = 0; i < cp.length; i++) {
70
urlcp[i] = new File(cp[i]).toURL();
71
}
72
return new DataCachingClassLoader(urlcp,this.getClass().getClassLoader());
73
} catch (MalformedURLException e) {
74
e.printStackTrace();
75
fail("unexpected exception "+e.toString());
76
}
77
return null;
78
}
79
80
protected DataCachingClassLoader getDataCachingLoader(String cp) { return getDataCachingLoader(new String[]{cp});}
81
82
/**
83
* Read a string from the supplied data stream and compare it with the expected text, produce
84
* an error if they don't match.
85
*/
86
protected void readAndCheck(InputStream dataStream, String expectedContainedText) {
87
try {
88
if (dataStream==null) fail("should have found the resource but no data retrieved...");
89
BufferedInputStream bis = new BufferedInputStream(dataStream);
90
byte[] data = new byte[256];
91
StringBuffer theData = new StringBuffer();
92
int readBytes = -1;
93
while ( (readBytes=bis.read(data))!=-1) {
94
theData.append(new String(data,0,readBytes));
95
}
96
if (theData.toString().indexOf(expectedContainedText)==-1) {
97
fail("Data from stream '"+theData+"' does not match contain expected data '"+expectedContainedText+"'");
98
}
99
} catch (IOException e) {
100
e.printStackTrace();
101
fail("problem reading data stream: "+e.toString());
102
}
103
104
}
105
106
}
107
108