incubator-ooo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liu...@apache.org
Subject svn commit: r1383263 - in /incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common: FileProvider.java FileUtil.java Testspace.java
Date Tue, 11 Sep 2012 07:11:45 GMT
Author: liuzhe
Date: Tue Sep 11 07:11:45 2012
New Revision: 1383263

URL: http://svn.apache.org/viewvc?rev=1383263&view=rev
Log:
Add missed license header and some java doc

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

Modified: incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java?rev=1383263&r1=1383262&r2=1383263&view=diff
==============================================================================
--- incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java
(original)
+++ incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java
Tue Sep 11 07:11:45 2012
@@ -1,3 +1,24 @@
+/**************************************************************
+ * 
+ * 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.BufferedReader;
@@ -32,9 +53,12 @@ public class FileProvider extends Suite 
 
 		private final Object[] parameters;
 
-		TestClassRunnerForParameters(Class<?> type, Object[] parameters) throws InitializationError
{
+		private int index;
+		
+		TestClassRunnerForParameters(Class<?> type, Object[] parameters, int index) throws
InitializationError {
 			super(type);
 			this.parameters = parameters;
+			this.index = index;
 		}
 
 		@Override
@@ -44,7 +68,7 @@ public class FileProvider extends Suite 
 
 		@Override
 		protected String getName() {
-			return getTestClass().getJavaClass().getSimpleName() +  Arrays.toString(parameters);
+			return getTestClass().getJavaClass().getSimpleName() + "[" + index + "]" +  Arrays.toString(parameters);
 		}
 
 		@Override
@@ -77,11 +101,7 @@ public class FileProvider extends Suite 
 	@Target(ElementType.FIELD)
 	public static @interface FileFilter {
 	}
-
-	/**
-	 * 
-	 * Only called reflectively. Do not use programmatically.
-	 */
+	
 	public FileProvider(Class<?> klass) throws Throwable {
 		super(klass, NO_RUNNERS);
 
@@ -132,27 +152,39 @@ public class FileProvider extends Suite 
 		
 		
 		ArrayList<Object[]> list = new ArrayList<Object[]>();
-		if (reposFile.isDirectory()) {
-			collectFromDir(reposFile, list, filterItems);
-		} else {
-			collectFromFile(reposFile, list, filterItems);
-		}
+		if (!collectFromFile(reposFile, list, filterItems))
+			if (!collectFromFiles(reposFile, list, filterItems))
+				collectFromDir(reposFile, list, filterItems);
 		
-		for (Object[] t : list) {
-			TestClassRunnerForParameters runner = new TestClassRunnerForParameters(getTestClass().getJavaClass(),
t);
+		for (int i = 0; i < list.size(); i++) {
+			Object[] t = list.get(i);
+			TestClassRunnerForParameters runner = new TestClassRunnerForParameters(getTestClass().getJavaClass(),
t, i);
 			runners.add(runner);
 		}
 
 	}
-
-	/**
-	 * @param dir
-	 * @param list
-	 */
-	public static void collectFromDir(File dir, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {
+	
+	private static boolean collectFromFiles(File dir, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {
+		if (!dir.isDirectory())
+			return false;
+		
+		boolean hasListFile = false;
+		File[] files = dir.listFiles();
+		for (File f : files) {
+			if (f.isFile() && f.getName().endsWith(".files")) {
+				hasListFile = true;
+				collectFromFile(f, list, filterItems);
+			}
+		}
+		
+		return hasListFile;
+	}
+	
+	private static boolean collectFromDir(File dir, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {
+		if (!dir.isDirectory())
+			return false;
+		
 		File[] files = dir.listFiles();
-		if (files == null)
-			return;
 		Arrays.sort(files);
 		for (File file : files) {
 			if (file.isDirectory()) {
@@ -162,9 +194,14 @@ public class FileProvider extends Suite 
 
 			filter(file.getAbsolutePath(), list, filterItems);
 		}
+		
+		return true;
 	}
 
-	public static void collectFromFile(File file, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {
+	private static boolean collectFromFile(File file, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {
+		if (!file.isFile())
+			return false;
+		
 		BufferedReader reader = null;
 		try{	
 			reader = new BufferedReader(new FileReader(file));
@@ -185,6 +222,8 @@ public class FileProvider extends Suite 
 				//ignore;
 			}
 		}
+		
+		return true;
 	}
 	
 	private static void filter(String filePath, ArrayList<Object[]> list, ArrayList<ArrayList<String>>
filterItems) {

Modified: incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileUtil.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileUtil.java?rev=1383263&r1=1383262&r2=1383263&view=diff
==============================================================================
--- incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileUtil.java (original)
+++ incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileUtil.java Tue
Sep 11 07:11:45 2012
@@ -68,6 +68,11 @@ public class FileUtil {
 		
 	}
 	
+	/**
+	 * Parse XML file to Document model
+	 * @param path
+	 * @return
+	 */
 	public static Document parseXML(String path) {
 		try {
 			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
@@ -79,6 +84,12 @@ public class FileUtil {
 		}
 	}
 	
+	/**
+	 * Get a string by XPATH from a xml file
+	 * @param xml
+	 * @param xpathStr
+	 * @return
+	 */
 	public static String getStringByXPath(String xml, String xpathStr) {
 		Document doc = parseXML(xml);
 		if (doc == null)
@@ -340,6 +351,11 @@ public class FileUtil {
 	}
 	
 
+	/**
+	 * Write string into a file
+	 * @param filePath
+	 * @param contents
+	 */
 	public static void writeStringToFile(String filePath, String contents) {
 		FileWriter writer = null;
 		try {
@@ -473,6 +489,12 @@ public class FileUtil {
     }
     
 
+    /**
+     * Pump data from an inputstream into a file
+     * @param from
+     * @param toFile
+     * @return
+     */
 	public static boolean writeToFile(InputStream from, File toFile) {
 		FileOutputStream to = null;
 		try {
@@ -504,6 +526,12 @@ public class FileUtil {
 		}
 	}
     
+	/**
+	 * Pump data from an inputstream into an output stream
+	 * @param from
+	 * @param to
+	 * @return
+	 */
 	public static boolean pump(InputStream from, OutputStream to) {
 		try {
 			byte[] buffer = new byte[4096];
@@ -564,7 +592,7 @@ public class FileUtil {
     }
     
     /**
-     * Delete a file
+     * Delete a file or directory
      * @param file
      * @return
      */
@@ -586,10 +614,20 @@ public class FileUtil {
 		return path.delete();
 	}
     
+    /**
+     * Delete a file or directory.
+     * @param path
+     * @return
+     */
     public static boolean deleteFile(String path) {
 		return deleteFile(new File(path));
 	}
     
+    /**
+     * Check if a file exists
+     * @param file
+     * @return
+     */
     public static boolean fileExists(String file) {
     	return new File(file).exists();
     }
@@ -607,82 +645,87 @@ public class FileUtil {
 			return null;
 		return file.substring(i+1);
 	}
-	 /**
-     * Get the file's size
-     * @param file
-     * @return KB
-     */
+
+	
+	/**
+	 * Get file size. If it's a directory, it calculates the total size of files
+	 * @param filePath
+	 * @return
+	 */
 	public static long getFileSize(String filePath){
-		long totalSize = 0;
-		FileInputStream f = null;
-		File file = new File(filePath);
-		
-		try {
-			f = new FileInputStream(file);
-			totalSize = f.available();
-			f.close();
-		} catch (Exception e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-		
-		return totalSize/1000;
+		return getFileSize(new File(filePath));
 	}
 
 	/**
-     * Get the folder's size
-     * @param folder's path
-     * @return Kb
-     */
-	public static long getFolderSize(String dir){
-		long totalSize = 0;
-		File[] files = new File(dir).listFiles();
+	 * Get file size. If it's a directory, it calculates the total size of files
+	 * @param file
+	 * @return
+	 */
+	public static long getFileSize(File file){
+		if (file.isFile())
+			return file.length();
+		
+		long size = 0;
+		File[] files = file.listFiles();
+		if (files == null)
+			return size;
 		
-		for(int i=0; i<files.length; i++){
-			if(files[i].isDirectory())
-				totalSize = totalSize + getFolderSize(files[i].getAbsolutePath())*1000;
-			else
-				totalSize = totalSize + files[i].length();
-		}
-		
-		return totalSize/1000;
-	}
-	
-	public static void unzipFile(String unzipfile, String unzipDest){
-		    try {
-		      File dest = new File(unzipDest); 
-		      ZipInputStream zin = new ZipInputStream(new FileInputStream(unzipfile));
-		      ZipEntry entry;
-		      //Create folder
-		      while ( (entry = zin.getNextEntry()) != null){
-		        if (entry.isDirectory()) {
-		          File directory = new File(dest, entry.getName());
-		          if (!directory.exists())
-		            if (!directory.mkdirs())
-		              System.exit(0);
-		          zin.closeEntry();
-		        }
-		        if (!entry.isDirectory()) {
-		          File myFile = new File(entry.getName());
-		          FileOutputStream fout = new FileOutputStream(unzipDest + "/" + myFile.getPath());
-		          DataOutputStream dout = new DataOutputStream(fout);
-		          byte[] b = new byte[1024];
-		          int len = 0;
-		          while ( (len = zin.read(b)) != -1) {
-		            dout.write(b, 0, len);
-		          }
-		          dout.close();
-		          fout.close();
-		          zin.closeEntry();
-		        }
-		      }
-		    }
-		    catch (IOException e) {
-		      e.printStackTrace();
-		      System.out.println(e);
-		    }
+		for (File f : files) {
+			size += getFileSize(f);
+		}
+		return size;
 	}
 	
+	/**
+	 * Unzip a zip file into the destination directory
+	 * @param zipFile
+	 * @param dest
+	 */
+	public static void unzip(String zipFile, String dest) {
+		ZipInputStream zin = null;
+		FileOutputStream fos = null;
+		try {
+			File destFile = new File(dest);
+			zin = new ZipInputStream(new FileInputStream(zipFile));
+			ZipEntry entry;
+			while ((entry = zin.getNextEntry()) != null) {
+				File entryFile = new File(destFile, entry.getName());
+				if (entry.isDirectory()) {
+					entryFile.mkdirs();
+				} else {
+					fos = new FileOutputStream(entryFile);
+					byte[] b = new byte[1024];
+					int len = 0;
+					while ((len = zin.read(b)) != -1) {
+						fos.write(b, 0, len);
+					}
+					fos.close();
+					zin.closeEntry();
+				}
+			}
+		} catch (IOException e) {
+			log.log(Level.SEVERE, "unzip [" + zipFile + "] -> [" + dest + "] Fail!", e);
+		} finally {
+			if (zin != null)
+				try {
+					zin.close();
+				} catch (IOException e) {
+				}
+			if (fos != null)
+				try {
+					fos.close();
+				} catch (IOException e) {
+				}
+		}
+	}
+	
+	/**
+	 * Get an unique name under the specified directory
+	 * @param dir
+	 * @param prefix
+	 * @param suffix
+	 * @return
+	 */
 	public static File getUniqueFile(File dir, String prefix, String suffix) {
 		String name = prefix + "." + FILENAME_FORMAT.format(new Date()) + ".";
 		for (int i = 0; i < Integer.MAX_VALUE; i++) {
@@ -695,11 +738,24 @@ public class FileUtil {
 		return null;
 	}
 	
+	/**
+	 * Get an unique name under the specified directory
+	 * @param dir
+	 * @param prefix
+	 * @param suffix
+	 * @return
+	 */
 	public static File getUniqueFile(String dir, String prefix, String suffix) {
 		return getUniqueFile(new File(dir), prefix, suffix);
 	}
 	
 	
+	/**
+	 * Download a file from a url to the local file system
+	 * @param urlString
+	 * @param output
+	 * @return
+	 */
 	public static File download(String urlString, File output) {
 		InputStream in = null;
 		OutputStream out = null;
@@ -708,12 +764,10 @@ public class FileUtil {
 			URLConnection urlConnection = url.openConnection();
 			int totalSize = urlConnection.getContentLength();
 			in = urlConnection.getInputStream();
-			if (output.isDirectory()) {
+			if (output.isDirectory()) 
 				output = new File(output, url.getPath());
-				output.getParentFile().mkdirs();
-			}
+			output.getParentFile().mkdirs();
 			out = new FileOutputStream(output);
-
 			byte[] buffer = new byte[1024 * 100]; // 100k
 			int count = 0;
 			int totalCount = 0;
@@ -750,7 +804,12 @@ public class FileUtil {
 		}
 	}
 
-	
+
+	/**
+	 * Convert a file path to URL like "file:///dir/some.file"
+	 * @param file
+	 * @return
+	 */
 	@SuppressWarnings("deprecation")
 	public static String getUrl(File file) {
 		try {
@@ -764,11 +823,20 @@ public class FileUtil {
 	}
 	
 	
-	
+	/**
+	 * Convert a file path to URL like "file:///dir/some.file"
+	 * @param file
+	 * @return
+	 */
 	public static String getUrl(String path) {
 		return getUrl(new File(path));
 	}
 	
+	/**
+	 * Check if the given address is valid URL
+	 * @param address
+	 * @return
+	 */
 	public static boolean isUrl(String address) {
 		try {
 			new URL(address);

Modified: incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/Testspace.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/Testspace.java?rev=1383263&r1=1383262&r2=1383263&view=diff
==============================================================================
--- incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/Testspace.java (original)
+++ incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/Testspace.java Tue
Sep 11 07:11:45 2012
@@ -25,6 +25,7 @@ package org.openoffice.test.common;
 
 import java.io.File;
 import java.io.InputStream;
+import java.net.URL;
 
 /**
  * A testspace is a directory on your hard drive where stores the files generated during
testing, e.g. test result, logs, temp files.
@@ -118,6 +119,21 @@ public class Testspace {
 		return workingFile;
 	}
 	
+	public static File getDataFile(String dataFilePath) {
+		File dataFile = new File(dataFilePath);
+		if (!dataFile.isAbsolute())
+			dataFile = new File(testdata, dataFilePath);
+		if (!dataFile.exists()) {
+			URL url = Testspace.class.getClassLoader().getResource(dataFilePath);
+			if (url == null)
+				return null;
+			return new File(url.getFile());
+		} else {
+			return dataFile;
+		}
+	}
+	
+	
 	public static boolean deleteFile(String path) {
 		return FileUtil.deleteFile(getPath(path));
 	}



Mime
View raw message