incubator-ooo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liu...@apache.org
Subject svn commit: r1360557 - in /incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common: FileUtil.java Testspace.java
Date Thu, 12 Jul 2012 07:29:57 GMT
Author: liuzhe
Date: Thu Jul 12 07:29:56 2012
New Revision: 1360557

URL: http://svn.apache.org/viewvc?rev=1360557&view=rev
Log:
Add a new class Testspace

Added:
    incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/Testspace.java
Modified:
    incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/FileUtil.java

Modified: incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/FileUtil.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/FileUtil.java?rev=1360557&r1=1360556&r2=1360557&view=diff
==============================================================================
--- incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/FileUtil.java
(original)
+++ incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/FileUtil.java
Thu Jul 12 07:29:56 2012
@@ -355,6 +355,7 @@ public class FileUtil {
 		}
 	}
 	
+	
 	/**
 	 * Appeand a string to the tail of a file
 	 * @param file
@@ -432,11 +433,6 @@ public class FileUtil {
      * @return
      */
     public static boolean copyFile(File fromFile, File toFile) {
-    	 if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
-         	System.err.println(fromFile.getAbsolutePath() + "doesn't exist, or isn't file,
or can't be read");
-         	return false;
-         }
-      
          if (toFile.isDirectory())
            toFile = new File(toFile, fromFile.getName());
 
@@ -472,6 +468,62 @@ public class FileUtil {
          }
     }
     
+
+	public static boolean writeToFile(InputStream from, File toFile) {
+		FileOutputStream to = null;
+		try {
+			File p = toFile.getParentFile();
+			if (p != null && !p.exists())
+				p.mkdirs();
+			to = new FileOutputStream(toFile);
+			byte[] buffer = new byte[4096];
+			int bytesRead;
+			while ((bytesRead = from.read(buffer)) != -1)
+				to.write(buffer, 0, bytesRead);
+
+			return true;
+		} catch (IOException e) {
+			// Can't copy
+			e.printStackTrace();
+			return false;
+		} finally {
+			if (from != null)
+				try {
+					from.close();
+				} catch (IOException e) {
+				}
+			if (to != null)
+				try {
+					to.close();
+				} catch (IOException e) {
+				}
+		}
+	}
+    
+	public static boolean pump(InputStream from, OutputStream to) {
+		try {
+			byte[] buffer = new byte[4096];
+			int bytesRead;
+			while ((bytesRead = from.read(buffer)) != -1)
+				to.write(buffer, 0, bytesRead);
+
+			return true;
+		} catch (IOException e) {
+			return false;
+		} finally {
+			if (from != null)
+				try {
+					from.close();
+				} catch (IOException e) {
+				}
+			if (to != null)
+				try {
+					to.close();
+				} catch (IOException e) {
+				}
+		}
+	}
+
     /**
      * Copy a file
      * @param fromFileName
@@ -702,4 +754,21 @@ public class FileUtil {
 		}
 	}
 
+	
+	public static String getUrl(File file) {
+		try {
+			String url = file.toURL().toString();
+			url = url.replace("file:/", "file:///");
+		} catch(Exception e) {
+			
+		}
+		
+		return null;
+	}
+	
+	
+	
+	public static String getUrl(String path) {
+		return getUrl(new File(path));
+	}
 }

Added: incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/Testspace.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/Testspace.java?rev=1360557&view=auto
==============================================================================
--- incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/Testspace.java
(added)
+++ incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/Testspace.java
Thu Jul 12 07:29:56 2012
@@ -0,0 +1,83 @@
+/**************************************************************
+ * 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * 
+ *************************************************************/
+
+
+
+package org.openoffice.test.common;
+
+import java.io.File;
+import java.io.InputStream;
+
+/**
+ * A testspace is a directory on your hard drive where stores the files generated during
testing, e.g. test result, logs, temp files.
+ *
+ */
+public class Testspace {
+
+	private static final File testspace = new File(System.getProperty("testspace", "../testspace"));
+	
+	private static final File testdata = new File(System.getProperty("testdata", "data")); 
+	
+	public static String getPath() {
+		return testspace.getAbsolutePath();
+	}
+
+	public static String getPath(String filePath) {
+		return getFile(filePath).getAbsolutePath();
+	}
+	
+	public static String getUrl(String filePath) {
+		return FileUtil.getUrl(getFile(filePath));
+	}
+	
+	public static File getFile() {
+		return testspace;
+	}
+	
+	public static File getFile(String filePath) {
+		File file = new File(filePath);
+		if (file.isAbsolute())
+			return file;
+		
+		return new File(testspace, filePath);
+	}
+	
+	public static String prepareData(String dataFilePath) {
+		File dataFile = new File(dataFilePath);
+		File workingFile = getFile("data/" + dataFilePath);
+		
+		if (!dataFile.isAbsolute()) 
+			dataFile = new File(testdata, dataFilePath);
+		if (!dataFile.exists()) {
+			InputStream in = Testspace.class.getClassLoader().getResourceAsStream(dataFilePath);
+			if (in == null)
+				throw new RuntimeException("Can not prepare data: " + dataFilePath);
+			if (!FileUtil.writeToFile(in, workingFile))
+				throw new RuntimeException("Can not prepare data: " + dataFilePath);
+		} else {
+			if (!FileUtil.copyFile(dataFile, workingFile))
+				throw new RuntimeException("Can not prepare data: " + dataFilePath);
+		}
+		
+		return workingFile.getAbsolutePath();
+	}
+	
+}



Mime
View raw message