Return-Path: X-Original-To: apmail-incubator-ooo-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-ooo-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 52C43DC0B for ; Tue, 11 Sep 2012 07:12:34 +0000 (UTC) Received: (qmail 11608 invoked by uid 500); 11 Sep 2012 07:12:34 -0000 Delivered-To: apmail-incubator-ooo-commits-archive@incubator.apache.org Received: (qmail 11568 invoked by uid 500); 11 Sep 2012 07:12:34 -0000 Mailing-List: contact ooo-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ooo-dev@incubator.apache.org Delivered-To: mailing list ooo-commits@incubator.apache.org Received: (qmail 11560 invoked by uid 99); 11 Sep 2012 07:12:34 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Sep 2012 07:12:34 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Sep 2012 07:12:30 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C7E4C238896F; Tue, 11 Sep 2012 07:11:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: ooo-commits@incubator.apache.org From: liuzhe@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120911071145.C7E4C238896F@eris.apache.org> 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 list = new ArrayList(); - 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 list, ArrayList> filterItems) { + + private static boolean collectFromFiles(File dir, ArrayList list, ArrayList> 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 list, ArrayList> 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 list, ArrayList> filterItems) { + private static boolean collectFromFile(File file, ArrayList list, ArrayList> 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 list, ArrayList> 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 [" + 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)); }