You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openoffice.apache.org by li...@apache.org on 2012/09/11 07:29:37 UTC

svn commit: r1383247 - in /incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common: FileProvider.java FileUtil.java Testspace.java

Author: liuzhe
Date: Tue Sep 11 05:29:36 2012
New Revision: 1383247

URL: http://svn.apache.org/viewvc?rev=1383247&view=rev
Log:
Add an JUnit Runner: FileProvider

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

Added: 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=1383247&view=auto
==============================================================================
--- incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java (added)
+++ incubator/ooo/trunk/test/testcommon/source/org/openoffice/test/common/FileProvider.java Tue Sep 11 05:29:36 2012
@@ -0,0 +1,216 @@
+package org.openoffice.test.common;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.runner.Runner;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.Suite;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+
+public class FileProvider extends Suite {
+
+	private static final List<Runner> NO_RUNNERS = Collections.<Runner> emptyList();
+
+	private final ArrayList<Runner> runners = new ArrayList<Runner>();
+
+	private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
+
+		private final Object[] parameters;
+
+		TestClassRunnerForParameters(Class<?> type, Object[] parameters) throws InitializationError {
+			super(type);
+			this.parameters = parameters;
+		}
+
+		@Override
+		public Object createTest() throws Exception {
+			return getTestClass().getOnlyConstructor().newInstance(parameters);
+		}
+
+		@Override
+		protected String getName() {
+			return getTestClass().getJavaClass().getSimpleName() +  Arrays.toString(parameters);
+		}
+
+		@Override
+		protected String testName(FrameworkMethod method) {
+			return method.getName() + Arrays.toString(parameters);
+		}
+
+		@Override
+		protected void validateConstructor(List<Throwable> errors) {
+			validateOnlyOneConstructor(errors);
+		}
+
+		@Override
+		protected Statement classBlock(RunNotifier notifier) {
+			return childrenInvoker(notifier);
+		}
+
+		@Override
+		protected Annotation[] getRunnerAnnotations() {
+			return new Annotation[0];
+		}
+	}
+
+	@Retention(RetentionPolicy.RUNTIME)
+	@Target(ElementType.FIELD)
+	public static @interface FileRepos {
+	}
+
+	@Retention(RetentionPolicy.RUNTIME)
+	@Target(ElementType.FIELD)
+	public static @interface FileFilter {
+	}
+
+	/**
+	 * 
+	 * Only called reflectively. Do not use programmatically.
+	 */
+	public FileProvider(Class<?> klass) throws Throwable {
+		super(klass, NO_RUNNERS);
+
+		String repos = null;
+		String filter = null;
+		Field fs[] = klass.getFields();
+		for (Field f : fs) {
+			Annotation a = f.getAnnotation(FileRepos.class);
+			if (a != null) {
+				Object v = f.get(null);
+				if (!(v instanceof String))
+					throw new InitializationError(String.format("Field annotated FileRepos '%s' must be String.", f.getName()));
+
+				repos = (String) v;
+			}
+
+			a = f.getAnnotation(FileFilter.class);
+			if (a != null) {
+				Object v = f.get(null);
+				if (v != null && !(v instanceof String))
+					throw new InitializationError(String.format("Field annotated FileFilter '%s' must be String.", f.getName()));
+				filter = (String) v;
+			}
+		}
+
+		File reposFile = new File(repos);
+		if (!reposFile.exists())
+			throw new InitializationError(String.format("repos '%s' does not exists ", repos));
+
+		ArrayList<ArrayList<String>> filterItems = new ArrayList<ArrayList<String>>();
+		if (filter != null) {
+			String[] args = SystemUtil.parseCommandLine(filter);
+			ArrayList<String> filterItem = new ArrayList<String>();
+			for (int i = 0; i < args.length; i++) {
+				String a = args[i];
+				if (a.equals("-f")) {
+					if (filterItem.size() > 0) 
+						filterItems.add(filterItem);
+					filterItem = new ArrayList<String>();
+				} else {
+					filterItem.add(a);
+				}
+			}
+			
+			if (filterItem.size() > 0) 
+				filterItems.add(filterItem);
+		}
+		
+		
+		ArrayList<Object[]> list = new ArrayList<Object[]>();
+		if (reposFile.isDirectory()) {
+			collectFromDir(reposFile, list, filterItems);
+		} else {
+			collectFromFile(reposFile, list, filterItems);
+		}
+		
+		for (Object[] t : list) {
+			TestClassRunnerForParameters runner = new TestClassRunnerForParameters(getTestClass().getJavaClass(), t);
+			runners.add(runner);
+		}
+
+	}
+
+	/**
+	 * @param dir
+	 * @param list
+	 */
+	public static void collectFromDir(File dir, ArrayList<Object[]> list, ArrayList<ArrayList<String>> filterItems) {
+		File[] files = dir.listFiles();
+		if (files == null)
+			return;
+		Arrays.sort(files);
+		for (File file : files) {
+			if (file.isDirectory()) {
+				collectFromDir(file, list, filterItems);
+				continue;
+			}
+
+			filter(file.getAbsolutePath(), list, filterItems);
+		}
+	}
+
+	public static void collectFromFile(File file, ArrayList<Object[]> list, ArrayList<ArrayList<String>> filterItems) {
+		BufferedReader reader = null;
+		try{	
+			reader = new BufferedReader(new FileReader(file));
+			String line = null;
+			while((line = reader.readLine()) != null){
+				filter(line, list, filterItems);
+			}
+		}catch(Exception e){
+			//ignore
+		}finally{
+			try{
+				if(reader != null){
+					reader.close();
+					reader = null;
+				}
+			
+			}catch(Exception io){
+				//ignore;
+			}
+		}
+	}
+	
+	private static void filter(String filePath, ArrayList<Object[]> list, ArrayList<ArrayList<String>> filterItems) {
+		if (filterItems.size() == 0) {
+			Object[] data = { filePath};
+			list.add(data);
+			return;
+		}
+
+		for (int i = 0; i < filterItems.size(); i++) {
+			ArrayList<String> filterItem = filterItems.get(i);
+			String pattern = filterItem.get(0);
+			if (pattern != null && filePath.matches(pattern)) {
+				Object[] data = new Object[filterItem.size()];
+				data[0] = filePath;
+				for (int j = 1; j < filterItem.size(); j++)
+					data[j] = filterItem.get(j);
+				
+				list.add(data);
+			}
+		}
+	}
+	
+	
+	@Override
+	protected List<Runner> getChildren() {
+		return runners;
+	}
+}

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=1383247&r1=1383246&r2=1383247&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 05:29:36 2012
@@ -33,12 +33,14 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Properties;
+import java.util.logging.Level;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
@@ -60,6 +62,8 @@ public class FileUtil {
 	 
 	private final static DateFormat FILENAME_FORMAT = new SimpleDateFormat("yyMMddHHmm");
 	
+	private final static Logger log = Logger.getLogger(FileUtil.class);
+	
 	private FileUtil(){
 		
 	}
@@ -644,11 +648,6 @@ public class FileUtil {
 		return totalSize/1000;
 	}
 	
-	/**
-     * unzip file to the unzipToLoc
-     * @param folder's path
-     * @return Kb
-     */
 	public static void unzipFile(String unzipfile, String unzipDest){
 		    try {
 		      File dest = new File(unzipDest); 
@@ -704,7 +703,6 @@ public class FileUtil {
 	public static File download(String urlString, File output) {
 		InputStream in = null;
 		OutputStream out = null;
-		System.out.println("[Vclauto] Download '" + urlString + "'");
 		try {
 			URL url = new URL(urlString);
 			URLConnection urlConnection = url.openConnection();
@@ -728,16 +726,14 @@ public class FileUtil {
 					int nowProgress = totalCount * 10 / totalSize;
 					if (nowProgress > progress) {
 						progress = nowProgress;
-						System.out.print(".");
 					}
 				}
 				
 			}
-			System.out.println("Done!");
+			log.info("Download [" + urlString + "] -> [" + output + "] OK!");
 			return output;
 		} catch (Exception e) {
-			e.printStackTrace();
-			System.out.println("Error!");
+			log.log(Level.SEVERE, "Download [" + urlString + "] -> [" + output + "] Fail!", e);
 			return null;
 		} finally {
 			if (in != null)
@@ -755,6 +751,7 @@ public class FileUtil {
 	}
 
 	
+	@SuppressWarnings("deprecation")
 	public static String getUrl(File file) {
 		try {
 			String url = file.getCanonicalFile().toURL().toString();
@@ -771,4 +768,14 @@ public class FileUtil {
 	public static String getUrl(String path) {
 		return getUrl(new File(path));
 	}
+	
+	public static boolean isUrl(String address) {
+		try {
+			new URL(address);
+			return true;
+		} catch (MalformedURLException e) {
+			return false;
+		}
+		
+	}
 }

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=1383247&r1=1383246&r2=1383247&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 05:29:36 2012
@@ -74,13 +74,34 @@ public class Testspace {
 	}
 	
 	public static String prepareData(String dataFilePath) {
-		return prepareData(dataFilePath, "temp/" + dataFilePath);
+		File dataFile = prepareDataFile(dataFilePath);
+		return dataFile.getAbsolutePath();
 	}
 	
 	public static String prepareData(String dataFilePath, String to) {
-		File dataFile = new File(dataFilePath);
+		File dataFile = prepareDataFile(dataFilePath,to);
+		return dataFile.getAbsolutePath();
+	}
+	
+	public static File prepareDataFile(String dataFilePath) {
+		String name = new File(dataFilePath).getName();
+		return prepareDataFile(dataFilePath, "temp/" + name);
+	}
+	
+	public static File prepareDataFile(String dataFilePath, String to) {
 		File workingFile = getFile(to);
 		
+		if (FileUtil.isUrl(dataFilePath)) {
+			if (FileUtil.download(dataFilePath, workingFile) == null) {
+				throw new RuntimeException("Can not prepare data: " + dataFilePath);
+			}
+			return workingFile;
+		}
+		
+		
+		File dataFile = new File(dataFilePath);
+		
+		
 		if (!dataFile.isAbsolute()) 
 			dataFile = new File(testdata, dataFilePath);
 		if (!dataFile.exists()) {
@@ -94,7 +115,7 @@ public class Testspace {
 				throw new RuntimeException("Can not prepare data: " + dataFilePath);
 		}
 		
-		return workingFile.getAbsolutePath();
+		return workingFile;
 	}
 	
 	public static boolean deleteFile(String path) {