(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 27 Mar 2006 07:10:40 +0000 (07:10 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 27 Mar 2006 07:10:40 +0000 (07:10 +0000)
support/test/org/wamblee/concurrency/JvmLockTest.java
support/test/org/wamblee/concurrency/LockAdviceTest.java
support/test/org/wamblee/test/AssertionUtils.java [new file with mode: 0644]
support/test/org/wamblee/test/FileSystemUtils.java [new file with mode: 0644]

index 319c5546648c7c1ba34ee222d5ab853c389c14d4..9b08f04ab247cbd5a6b0187d5d1d72401fae2907 100644 (file)
 
 package org.wamblee.concurrency;
 
-import org.wamblee.test.EventTracker;
 import org.wamblee.test.TimingUtils;
 
-import junit.framework.TestCase;
-
 /**
  * Tests for the JVMLock.
  */
index f88c4a2f2b36c8acb87a0591f3e9ec1bb6478f28..61b9f7d4adaf028d139e7da599556b7b8d40876b 100644 (file)
 
 package org.wamblee.concurrency;
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-
-import org.aopalliance.intercept.MethodInvocation;
-import org.springframework.aop.framework.AdvisedSupport;
 import org.springframework.aop.framework.ProxyFactoryBean;
-import org.springframework.aop.framework.ReflectiveMethodInvocation;
 import org.wamblee.test.TimingUtils;
 
 /**
diff --git a/support/test/org/wamblee/test/AssertionUtils.java b/support/test/org/wamblee/test/AssertionUtils.java
new file mode 100644 (file)
index 0000000..b7300d7
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2006 the original author or authors.
+ * 
+ * Licensed 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.wamblee.test;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+/**
+ * Useful assertions for use in test cases.
+ */
+public final class AssertionUtils {
+
+    /**
+     * Disabled constructor.
+     * 
+     */
+    private AssertionUtils() {
+        // Empty
+    }
+
+    /**
+     * Asserts that two object arrays are equal.
+     * 
+     * @param aExpected
+     *            Expected object array.
+     * @param aActual
+     *            Actual object array.
+     */
+    public static void assertEquals(Object[] aExpected, Object[] aActual) {
+        assertEquals("", aExpected, aActual);
+    }
+
+    /**
+     * Asserts that two object arrays are equal.
+     * 
+     * @param aMsg
+     *            Message.
+     * @param aExpected
+     *            Expected array.
+     * @param aActual
+     *            Actual array.
+     */
+    public static void assertEquals(String aMsg, Object[] aExpected,
+            Object[] aActual) {
+        TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
+                aActual.length);
+
+        for (int i = 0; i < aExpected.length; i++) {
+            TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
+                    aActual[i]);
+        }
+    }
+
+    /**
+     * Asserts that two objects are equal, and in case the object is an Object[]
+     * delegates to {@link #assertEquals(String, Object[], Object[]).
+     * 
+     * @param aMsg
+     *            Message.
+     * @param aExpected
+     *            Expected result.
+     * @param aActual
+     *            Actual result.
+     */
+    public static void assertEquals(String aMsg, Object aExpected,
+            Object aActual) {
+        if (aExpected instanceof Object[]) {
+            AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
+                    (Object[]) aActual);
+
+            return;
+        }
+
+        TestCase.assertEquals(aMsg, aExpected, aActual);
+    }
+
+    /**
+     * Asserts that two maps are equal by comparing all keys and by checking
+     * that the values for the same keys are the same.
+     * 
+     * @param aMsg
+     *            Message.
+     * @param aExpectedMap
+     *            Expected result.
+     * @param aActual
+     *            Actual result.
+     */
+    public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
+        TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
+                .size());
+
+        Set keys = aExpectedMap.keySet();
+
+        for (Iterator i = keys.iterator(); i.hasNext();) {
+            String key = (String) i.next();
+            TestCase.assertTrue("Map does not containg entry for key:" + key,
+                    aActual.containsKey(key));
+            AssertionUtils.assertEquals("Value of key " + key + " of map",
+                    aExpectedMap.get(key), aActual.get(key));
+        }
+    }
+}
diff --git a/support/test/org/wamblee/test/FileSystemUtils.java b/support/test/org/wamblee/test/FileSystemUtils.java
new file mode 100644 (file)
index 0000000..e030957
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2006 the original author or authors.
+ * 
+ * Licensed 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.wamblee.test;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.security.CodeSource;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * File system utilities.
+ */
+public final class FileSystemUtils {
+
+    private static final Log LOG = LogFactory.getLog(FileSystemUtils.class);
+
+    /**
+     * Test output directory relative to the sub project.
+     */
+    private static final String TEST_OUTPUT_DIR = "resources/testoutput";
+
+    /*
+     * Disabled.
+     * 
+     */
+    private FileSystemUtils() {
+        // Empty
+    }
+
+    /**
+     * Deletes a directory recursively. The test case will fail if the directory
+     * does not exist or if deletion fails.
+     * 
+     * @param aDir
+     *            Directory to delete.
+     */
+    public static void deleteDirRecursively(String aDir) {
+        deleteDirRecursively(new File(aDir));
+    }
+
+    /**
+     * Deletes a directory recursively. See {@link #deleteDirRecursively}.
+     * 
+     * @param aDir
+     *            Directory.
+     */
+    public static void deleteDirRecursively(File aDir) {
+        TestCase.assertTrue(aDir.isDirectory());
+
+        for (File file: aDir.listFiles()) {
+            if (file.isDirectory()) {
+                deleteDirRecursively(file);
+            } else {
+                delete(file);
+            }
+        }
+
+        delete(aDir);
+    }
+
+    /**
+     * Deletes a file or directory. The test case will fail if the file or
+     * directory does not exist or if deletion fails. Deletion of a non-empty
+     * directory will always fail.
+     * 
+     * @param aFile
+     *            File or directory to delete.
+     */
+    public static void delete(File aFile) {
+        TestCase.assertTrue(aFile.delete());
+    }
+
+    /**
+     * Gets a path relative to a sub project. This utility should be used to
+     * easily access file paths within a subproject without requiring any
+     * specific Eclipse configuration.
+     * 
+     * @param aRelativePath
+     *            Relative path.
+     * @param aTestClass
+     *            Test class.
+     */
+    public static File getPath(String aRelativePath, Class aTestClass) {
+        CodeSource source = aTestClass.getProtectionDomain().getCodeSource();
+        if (source == null) {
+            LOG.warn("Could not obtain path for '" + aRelativePath
+                    + "' for class " + aTestClass
+                    + ", using relative path as is");
+            return new File(aRelativePath);
+        }
+        URL location = source.getLocation();
+        String protocol = location.getProtocol();
+        if (!protocol.equals("file")) {
+            return new File(aRelativePath);
+        }
+
+        String path = location.getPath();
+        try {
+            path = URLDecoder.decode(location.getPath(), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            // ignore it.. just don't decode
+        }
+
+        return new File(new File(path).getParentFile(), aRelativePath);
+    }
+
+    /**
+     * Ensures that a directory hierarchy exists (recursively if needed). If it
+     * is not possible to create the directory, then the test case will fail.
+     * 
+     * @param aDir
+     *            Directory to create.
+     */
+    public static void createDir(File aDir) {
+        if (aDir.exists() && !aDir.isDirectory()) {
+            TestCase.fail("'" + aDir
+                    + "' already exists and is not a directory");
+        }
+        if (aDir.exists()) {
+            return;
+        }
+        createDir(aDir.getParentFile());
+        TestCase.assertTrue("Could not create '" + aDir + "'", aDir.mkdir());
+    }
+
+    /**
+     * Gets the test output directory for a specific test class.
+     * 
+     * @param aTestClass
+     *            Test class.
+     * @return Test output directory.
+     */
+    public static File getTestOutputDir(Class aTestClass) {
+        File file = getPath(TEST_OUTPUT_DIR, aTestClass);
+        String packageName = aTestClass.getPackage().getName();
+        String packagePath = packageName.replaceAll("\\.", "/");
+        return new File(file, packagePath);
+    }
+
+    /**
+     * Creates a directory hierarchy for the output directory of a test class if
+     * needed.
+     * 
+     * @param aTestClass
+     *            Test class
+     * @return Test directory.
+     */
+    public static File createTestOutputDir(Class aTestClass) {
+        File file = getTestOutputDir(aTestClass);
+        createDir(file);
+        return file;
+    }
+
+    /**
+     * Gets a test output file name. This returns a File object representing the
+     * output file and ensures that the directory where the file will be created
+     * already exists.
+     * 
+     * @param aName
+     *            Name of the file.
+     * @param aTestClass
+     *            Test class.
+     * @return File.
+     */
+    public static File getTestOutputFile(String aName, Class aTestClass) {
+        File file = new File(getTestOutputDir(aTestClass), aName);
+        createDir(file.getParentFile());
+        return file;
+    }
+}