You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/09/22 13:59:22 UTC

svn commit: r817620 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/util/

Author: davsclaus
Date: Tue Sep 22 11:59:22 2009
New Revision: 817620

URL: http://svn.apache.org/viewvc?rev=817620&view=rev
Log:
MR-187: More unit tests. Removed not needed class. Fixed CS.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java   (with props)
Removed:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/SystemHelper.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java?rev=817620&r1=817619&r2=817620&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java Tue Sep 22 11:59:22 2009
@@ -17,20 +17,19 @@
 package org.apache.camel.impl;
 
 import java.lang.reflect.Method;
-
 import javax.xml.bind.annotation.XmlTransient;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.Consume;
 import org.apache.camel.Consumer;
+import org.apache.camel.ConsumerTemplate;
 import org.apache.camel.Endpoint;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.Service;
-import org.apache.camel.ConsumerTemplate;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.bean.ProxyHelper;
 import org.apache.camel.util.CamelContextHelper;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=817620&r1=817619&r2=817620&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java Tue Sep 22 11:59:22 2009
@@ -39,82 +39,13 @@
      */
     public static String normalizePath(String path) {
         // special handling for Windows where we need to convert / to \\
-        if (path != null && System.getProperty("os.name").startsWith("Windows") && path.indexOf('/') >= 0) {
+        if (path != null && isWindows() && path.indexOf('/') >= 0) {
             return path.replace('/', '\\');
         }
         return path;
     }
     
-    public static void mkDir(File dir) {
-        if (dir == null) {
-            throw new IllegalArgumentException("dir attribute is required");
-        }
-
-        if (dir.isFile()) {
-            throw new RuntimeException("Unable to create directory as a file "
-                                    + "already exists with that name: " + dir.getAbsolutePath());
-        }
-
-        if (!dir.exists()) {
-            boolean result = doMkDirs(dir);
-            if (!result) {
-                String msg = "Directory " + dir.getAbsolutePath()
-                             + " creation was not successful for an unknown reason";
-                throw new RuntimeException(msg);
-            }
-        }
-    }
-
-    /**
-     * Attempt to fix possible race condition when creating directories on
-     * WinXP, also Windows2000. If the mkdirs does not work, wait a little and
-     * try again.
-     */
-    private static boolean doMkDirs(File f) {
-        if (!f.mkdirs()) {
-            try {
-                Thread.sleep(RETRY_SLEEP_MILLIS);
-                return f.mkdirs();
-            } catch (InterruptedException ex) {
-                return f.mkdirs();
-            }
-        }
-        return true;
-    }
-
-    public static void removeDir(File d) {
-        String[] list = d.list();
-        if (list == null) {
-            list = new String[0];
-        }
-        for (String s : list) {
-            File f = new File(d, s);
-            if (f.isDirectory()) {
-                removeDir(f);
-            } else {
-                delete(f);
-            }
-        }
-        delete(d);
-    }
-
-    public static void delete(File f) {
-        if (!f.delete()) {
-            if (isWindows()) {
-                System.gc();
-            }
-            try {
-                Thread.sleep(RETRY_SLEEP_MILLIS);
-            } catch (InterruptedException ex) {
-                // Ignore Exception
-            }
-            if (!f.delete()) {
-                f.deleteOnExit();
-            }
-        }
-    }
-
-    private static boolean isWindows() {
+    public static boolean isWindows() {
         String osName = System.getProperty("os.name").toLowerCase(Locale.US);
         return osName.indexOf("windows") > -1;
     }
@@ -217,6 +148,10 @@
      * Compacts a path by stacking it and reducing <tt>..</tt>
      */
     public static String compactPath(String path) {
+        if (path == null) {
+            return null;
+        }
+
         // only normalize path if it contains .. as we want to avoid: path/../sub/../sub2 as this can leads to trouble
         if (path.indexOf("..") == -1) {
             return path;
@@ -283,4 +218,36 @@
         return defaultTempDir;
     }
 
+    private static void removeDir(File d) {
+        String[] list = d.list();
+        if (list == null) {
+            list = new String[0];
+        }
+        for (String s : list) {
+            File f = new File(d, s);
+            if (f.isDirectory()) {
+                removeDir(f);
+            } else {
+                delete(f);
+            }
+        }
+        delete(d);
+    }
+
+    private static void delete(File f) {
+        if (!f.delete()) {
+            if (isWindows()) {
+                System.gc();
+            }
+            try {
+                Thread.sleep(RETRY_SLEEP_MILLIS);
+            } catch (InterruptedException ex) {
+                // Ignore Exception
+            }
+            if (!f.delete()) {
+                f.deleteOnExit();
+            }
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java?rev=817620&r1=817619&r2=817620&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/Time.java Tue Sep 22 11:59:22 2009
@@ -37,14 +37,6 @@
         return new Time(value, TimeUnit.MILLISECONDS);
     }
 
-    public static Time micros(long value) {
-        return new Time(value, TimeUnit.MICROSECONDS);
-    }
-
-    public static Time nanos(long value) {
-        return new Time(value, TimeUnit.NANOSECONDS);
-    }
-
     public static Time seconds(long value) {
         return new Time(value, TimeUnit.SECONDS);
     }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java?rev=817620&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java Tue Sep 22 11:59:22 2009
@@ -0,0 +1,59 @@
+/**
+ * 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.apache.camel.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.impl.DefaultExchange;
+
+/**
+ * @version $Revision$
+ */
+public class ExpressionListComparatorTest extends ContextTestSupport {
+
+    private class MyFooExpression implements Expression {
+
+        public <T> T evaluate(Exchange exchange, Class<T> type) {
+            return (T) "foo";
+        }
+    }
+
+    private class MyBarExpression implements Expression {
+
+        public <T> T evaluate(Exchange exchange, Class<T> type) {
+            return (T) "bar";
+        }
+    }
+
+    public void testExpressionListComparator() {
+        List<Expression> list = new ArrayList<Expression>();
+        list.add(new MyFooExpression());
+        list.add(new MyBarExpression());
+
+        ExpressionListComparator comp = new ExpressionListComparator(list);
+
+        Exchange e1 = new DefaultExchange(context);
+        Exchange e2 = new DefaultExchange(context);
+        int out = comp.compare(e1, e2);
+
+        assertEquals(0, out);
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExpressionListComparatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java?rev=817620&r1=817619&r2=817620&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java Tue Sep 22 11:59:22 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.util;
 
+import java.io.File;
+
 import junit.framework.TestCase;
 
 /**
@@ -23,8 +25,82 @@
  */
 public class FileUtilTest extends TestCase {
 
-    public void testMe() {
+    public void testNormalizePath() {
+        if (FileUtil.isWindows()) {
+            assertEquals("foo\\bar", FileUtil.normalizePath("foo/bar"));
+        } else {
+            assertEquals("foo/bar", FileUtil.normalizePath("foo/bar"));
+        }
+    }
+
+    public void testStripLeadingSeparator() {
+        assertEquals(null, FileUtil.stripLeadingSeparator(null));
+        assertEquals("foo", FileUtil.stripLeadingSeparator("foo"));
+        assertEquals("foo/bar", FileUtil.stripLeadingSeparator("foo/bar"));
+        assertEquals("foo", FileUtil.stripLeadingSeparator("foo"));
+        assertEquals("foo/bar", FileUtil.stripLeadingSeparator("/foo/bar"));
+    }
+
+    public void testStripTrailingSeparator() {
+        assertEquals(null, FileUtil.stripTrailingSeparator(null));
+        assertEquals("foo", FileUtil.stripTrailingSeparator("foo"));
+        assertEquals("foo/bar", FileUtil.stripTrailingSeparator("foo/bar"));
+        assertEquals("foo", FileUtil.stripTrailingSeparator("foo/"));
+        assertEquals("foo/bar", FileUtil.stripTrailingSeparator("foo/bar/"));
+        assertEquals("/foo/bar", FileUtil.stripTrailingSeparator("/foo/bar/"));
+        assertEquals("/foo/bar", FileUtil.stripTrailingSeparator("/foo/bar"));
+    }
+
+    public void testStripPath() {
+        assertEquals(null, FileUtil.stripPath(null));
+        assertEquals("foo", FileUtil.stripPath("foo"));
+        assertEquals("bar", FileUtil.stripPath("foo/bar"));
+        assertEquals("bar", FileUtil.stripPath("/foo/bar"));
+    }
+
+    public void testStripExt() {
+        assertEquals(null, FileUtil.stripExt(null));
+        assertEquals("foo", FileUtil.stripExt("foo"));
+        assertEquals("foo", FileUtil.stripExt("foo.xml"));
+        assertEquals("/foo/bar", FileUtil.stripExt("/foo/bar.xml"));
+    }
+
+    public void testOnlyPath() {
+        assertEquals(null, FileUtil.onlyPath(null));
+        assertEquals(null, FileUtil.onlyPath("foo"));
+        assertEquals(null, FileUtil.onlyPath("foo.xml"));
+        assertEquals("foo", FileUtil.onlyPath("foo/bar.xml"));
+        assertEquals("/foo", FileUtil.onlyPath("/foo/bar.xml"));
+        assertEquals("/foo/bar", FileUtil.onlyPath("/foo/bar/baz.xml"));
+    }
+
+    public void testCompactPath() {
+        assertEquals(null, FileUtil.compactPath(null));
+        assertEquals("foo", FileUtil.compactPath("foo"));
+        assertEquals("bar", FileUtil.compactPath("foo/../bar"));
+        assertEquals("bar/baz", FileUtil.compactPath("foo/../bar/baz"));
+        assertEquals("foo/baz", FileUtil.compactPath("foo/bar/../baz"));
+        assertEquals("baz", FileUtil.compactPath("foo/bar/../../baz"));
+        assertEquals("../baz", FileUtil.compactPath("foo/bar/../../../baz"));
+        assertEquals("../foo/bar", FileUtil.compactPath("../foo/bar"));
+    }
+
+    public void testDefaultTempFileSuffixAndPrefix() throws Exception {
+        File tmp = FileUtil.createTempFile("tmp-", ".tmp");
+        assertNotNull(tmp);
+        assertTrue("Should be a file", tmp.isFile());
+    }
+
+    public void testDefaultTempFile() throws Exception {
+        File tmp = FileUtil.createTempFile(null, null);
+        assertNotNull(tmp);
+        assertTrue("Should be a file", tmp.isFile());
+    }
 
+    public void testDefaultTempFileParent() throws Exception {
+        File tmp = FileUtil.createTempFile(null, null, new File("target"));
+        assertNotNull(tmp);
+        assertTrue("Should be a file", tmp.isFile());
     }
 
 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java?rev=817620&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java Tue Sep 22 11:59:22 2009
@@ -0,0 +1,30 @@
+/**
+ * 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.apache.camel.util;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class PackageHelperTest extends TestCase {
+
+    public void testIsValidPackage() {
+        boolean valid = PackageHelper.isValidVersion("org.apache.camel", 2.0);
+        assertEquals("Should be Camel 2.0 or higher", true, valid);
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/PackageHelperTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java?rev=817620&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java Tue Sep 22 11:59:22 2009
@@ -0,0 +1,67 @@
+/**
+ * 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.apache.camel.util;
+
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class TimeTest extends TestCase {
+
+    public void testTimeSeconds() {
+        Time time = Time.seconds(5);
+        assertNotNull(time);
+
+        assertEquals(5, time.getNumber());
+        assertEquals(TimeUnit.SECONDS, time.getTimeUnit());
+
+        assertNotNull(time.toDate());
+        assertNotNull(time.toMillis());
+        assertNotNull(time.toString());
+    }
+
+    public void testTimeMinutes() {
+        Time time = Time.minutes(3);
+        assertNotNull(time);
+
+        assertNotNull(time.toDate());
+        assertNotNull(time.toMillis());
+        assertNotNull(time.toString());
+    }
+
+    public void testTimeHours() {
+        Time time = Time.hours(4);
+        assertNotNull(time);
+
+        assertNotNull(time.toDate());
+        assertNotNull(time.toMillis());
+        assertNotNull(time.toString());
+    }
+
+    public void testTimeDays() {
+        Time time = Time.days(2);
+        assertNotNull(time);
+
+        assertNotNull(time.toDate());
+        assertNotNull(time.toMillis());
+        assertNotNull(time.toString());
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/TimeTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date