You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2009/08/19 20:51:54 UTC

svn commit: r805928 [2/2] - in /poi/trunk: ./ src/ooxml/testcases/org/apache/poi/ src/ooxml/testcases/org/apache/poi/ss/ src/ooxml/testcases/org/apache/poi/xssf/ src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/ src/ooxml/testcases/org/apache/poi...

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java?rev=805928&r1=805927&r2=805928&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java Wed Aug 19 18:51:44 2009
@@ -26,139 +26,35 @@
 import java.io.InputStream;
 
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.POIDataSamples;
 
 /**
  * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder.
  *
  * @author Josh Micich
  */
-public final class HSSFTestDataSamples {
+public final class HSSFTestDataSamples extends POIDataSamples {
 
-	private static final String TEST_DATA_DIR_SYS_PROPERTY_NAME = "HSSF.testdata.path";
+	private static final HSSFTestDataSamples _inst = new HSSFTestDataSamples("HSSF.testdata.path", "SampleSS.xls");
 
-	private static boolean _isInitialised;
-	private static File _resolvedDataDir;
-	/** <code>true</code> if standard system propery is not set,
-	 * but the data is available on the test runtime classpath */
-	private static boolean _sampleDataIsAvaliableOnClassPath;
+    private HSSFTestDataSamples(String dir, String classPathTestFile){
+        super(dir, classPathTestFile);
+    }
+
+    public static POIDataSamples getInstance(){
+        return _inst;
+    }
+
+    public static InputStream openSampleFileStream(String sampleFileName) {
+        return _inst.openResourceAsStream(sampleFileName);
+    }
+    public static byte[] getTestDataFileContent(String fileName) {
+        return _inst.readFile(fileName);
+    }
 
-	/**
-	 * Opens a sample file from the standard HSSF test data directory
-	 *
-	 * @return an open <tt>InputStream</tt> for the specified sample file
-	 */
-	public static InputStream openSampleFileStream(String sampleFileName) {
-
-		if(!_isInitialised) {
-			try {
-				initialise();
-			} finally {
-				_isInitialised = true;
-			}
-		}
-		if (_sampleDataIsAvaliableOnClassPath) {
-			InputStream result = openClasspathResource(sampleFileName);
-			if(result == null) {
-				throw new RuntimeException("specified test sample file '" + sampleFileName
-						+ "' not found on the classpath");
-			}
-//			System.out.println("opening cp: " + sampleFileName);
-			// wrap to avoid temp warning method about auto-closing input stream
-			return new NonSeekableInputStream(result);
-		}
-		if (_resolvedDataDir == null) {
-			throw new RuntimeException("Must set system property '"
-					+ TEST_DATA_DIR_SYS_PROPERTY_NAME
-					+ "' properly before running tests");
-		}
-
-		File f = new File(_resolvedDataDir, sampleFileName);
-		if (!f.exists()) {
-			throw new RuntimeException("Sample file '" + sampleFileName
-					+ "' not found in data dir '" + _resolvedDataDir.getAbsolutePath() + "'");
-		}
-        try {
-            if(!sampleFileName.equals(f.getCanonicalFile().getName())){
-                throw new RuntimeException("File name is case-sensitive: requested '" + sampleFileName
-                        + "' but actual file is '" + f.getCanonicalFile().getName() + "'");
-            }
-        } catch (IOException e){
-            throw new RuntimeException(e);
-        }
-        
-        try {
-			return new FileInputStream(f);
-		} catch (FileNotFoundException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	private static void initialise() {
-		String dataDirName = System.getProperty(TEST_DATA_DIR_SYS_PROPERTY_NAME);
-		if (dataDirName == null) {
-			// check to see if we can just get the resources from the classpath
-			InputStream is = openClasspathResource("SampleSS.xls");
-			if (is != null) {
-				try {
-					is.close(); // be nice
-				} catch (IOException e) {
-					throw new RuntimeException(e);
-				}
-				_sampleDataIsAvaliableOnClassPath = true;
-				return;
-			}
-
-			throw new RuntimeException("Must set system property '"
-					+ TEST_DATA_DIR_SYS_PROPERTY_NAME + "' before running tests");
-		}
-		File dataDir = new File(dataDirName);
-		if (!dataDir.exists()) {
-			throw new RuntimeException("Data dir '" + dataDirName
-					+ "' specified by system property '" + TEST_DATA_DIR_SYS_PROPERTY_NAME
-					+ "' does not exist");
-		}
-		// convert to canonical file, to make any subsequent error messages
-		// clearer.
-		try {
-			_resolvedDataDir = dataDir.getCanonicalFile();
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	/**
-	 * Opens a test sample file from the 'data' sub-package of this class's package.
-	 * @return <code>null</code> if the sample file is not deployed on the classpath.
-	 */
-	private static InputStream openClasspathResource(String sampleFileName) {
-		return HSSFTestDataSamples.class.getResourceAsStream("data/" + sampleFileName);
-	}
-
-	private static final class NonSeekableInputStream extends InputStream {
-
-		private final InputStream _is;
-
-		public NonSeekableInputStream(InputStream is) {
-			_is = is;
-		}
-
-		public int read() throws IOException {
-			return _is.read();
-		}
-		public int read(byte[] b, int off, int len) throws IOException {
-			return _is.read(b, off, len);
-		}
-		public boolean markSupported() {
-			return false;
-		}
-		public void close() throws IOException {
-			_is.close();
-		}
-	}
-
-	public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {
+    public static HSSFWorkbook openSampleWorkbook(String sampleFileName) {
 		try {
-			return new HSSFWorkbook(openSampleFileStream(sampleFileName));
+			return new HSSFWorkbook(_inst.openResourceAsStream(sampleFileName));
 		} catch (IOException e) {
 			throw new RuntimeException(e);
 		}
@@ -180,27 +76,4 @@
 		}
 	}
 
-	/**
-	 * @return byte array of sample file content from file found in standard hssf test data dir
-	 */
-	public static byte[] getTestDataFileContent(String fileName) {
-		ByteArrayOutputStream bos = new ByteArrayOutputStream();
-
-		try {
-			InputStream fis = HSSFTestDataSamples.openSampleFileStream(fileName);
-
-			byte[] buf = new byte[512];
-			while (true) {
-				int bytesRead = fis.read(buf);
-				if (bytesRead < 1) {
-					break;
-				}
-				bos.write(buf, 0, bytesRead);
-			}
-			fis.close();
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-		return bos.toByteArray();
-	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org