Path: blob/master/test/functional/cmdLineTests/shareClassTests/DataHelperTests/src/apitesting/datahelper/DataCachingTestbase.java
6007 views
/*******************************************************************************1* Copyright (c) 2001, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package apitesting.datahelper;2223import java.io.BufferedInputStream;24import java.io.File;25import java.io.IOException;26import java.io.InputStream;27import java.net.MalformedURLException;28import java.net.URL;2930import apitesting.Testbase;3132import CustomClassloaders.DataCachingClassLoader;3334/**35* Structure of the test data available to these test programs. Where the same file is mentioned as available from36* two places, the contents of the file indicate where it came from (so, below, contents of fileone.txt indicate37* whether it came from dataone or datatwo)38*39* datacaching/dataone.jar40* - contains two text files (fileone.txt, filetwo.txt)41*42* datacaching/datatwo.jar43* - contains two text files (fileone.txt, filethree.txt)44*45* Constants are defined in this base class for paths to these files and the expected contents46*/47public class DataCachingTestbase extends Testbase {4849static final String FILEONE = "fileone.txt";50static final String FILETWO = "filetwo.txt";51static final String FILETHREE = "filethree.txt";5253static final String CLASSPATH_JARONE = "datacaching/dataone.jar";54protected static final String CONTENTS_JARONE_FILEONE = "jar one, file one";55protected static final String CONTENTS_JARONE_FILETWO = "jar one, file two";5657static final String CLASSPATH_JARTWO = "datacaching/datatwo.jar";58protected static final String CONTENTS_JARTWO_FILEONE = "jar two, file one";59protected static final String CONTENTS_JARTWO_FILETHREE = "jar two, file three";6061/**62* Retrieves a DataCachingClassLoader instance - this is a classloader that can be63* configured to utilise the sharedcache for any data that it loads.64*/65protected DataCachingClassLoader getDataCachingLoader(String[] cp) {66try {67URL[] urlcp = new URL[cp.length];68for (int i = 0; i < cp.length; i++) {69urlcp[i] = new File(cp[i]).toURL();70}71return new DataCachingClassLoader(urlcp,this.getClass().getClassLoader());72} catch (MalformedURLException e) {73e.printStackTrace();74fail("unexpected exception "+e.toString());75}76return null;77}7879protected DataCachingClassLoader getDataCachingLoader(String cp) { return getDataCachingLoader(new String[]{cp});}8081/**82* Read a string from the supplied data stream and compare it with the expected text, produce83* an error if they don't match.84*/85protected void readAndCheck(InputStream dataStream, String expectedContainedText) {86try {87if (dataStream==null) fail("should have found the resource but no data retrieved...");88BufferedInputStream bis = new BufferedInputStream(dataStream);89byte[] data = new byte[256];90StringBuffer theData = new StringBuffer();91int readBytes = -1;92while ( (readBytes=bis.read(data))!=-1) {93theData.append(new String(data,0,readBytes));94}95if (theData.toString().indexOf(expectedContainedText)==-1) {96fail("Data from stream '"+theData+"' does not match contain expected data '"+expectedContainedText+"'");97}98} catch (IOException e) {99e.printStackTrace();100fail("problem reading data stream: "+e.toString());101}102103}104105}106107108