camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aka...@apache.org
Subject svn commit: r938630 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/ test/java/org/apache/camel/converter/
Date Tue, 27 Apr 2010 20:02:22 GMT
Author: akarpe
Date: Tue Apr 27 20:02:22 2010
New Revision: 938630

URL: http://svn.apache.org/viewvc?rev=938630&view=rev
Log:
CAMEL-2631: Add time millis converter which can convert from String to long

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java
  (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternConverterTest.java
  (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternTypeConversionTest.java
  (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimerDrivenTimePatternConverterTest.java
  (with props)

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java?rev=938630&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java
Tue Apr 27 20:02:22 2010
@@ -0,0 +1,104 @@
+/**
+ * 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.converter;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.camel.Converter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+@Converter
+public final class TimePatternConverter {   
+    private static final transient Log LOG = LogFactory.getLog(TimePatternConverter.class);
+    private static final String NUMBERS_ONLY_STRING_PATTERN = "^[-]?(\\d)+$";
+    private static final String REPLACEMENT_PATTERN = "[our|inute|econd](s)?";
+    private static final String HOUR_REGEX_PATTERN = "((\\d)*(\\d))[h|H]";
+    private static final String MINUTES_REGEX_PATTERN = "((\\d)*(\\d))[m|M]";
+    private static final String SECONDS_REGEX_PATTERN = "((\\d)*(\\d))[s|S]";
+
+    /**
+     * Utility classes should not have a public constructor.
+     */
+    private TimePatternConverter() {
+    }
+    
+    @Converter
+    public static long toMilliSeconds(String source) throws IllegalArgumentException {
+        long milliseconds = 0;
+        boolean foundFlag = false;
+        Matcher matcher;
+
+        matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source);
+        if (matcher.find()) {
+            // Note: This will also be used for regular numeric strings. 
+            //       This String -> long converter will be used for all strings.
+            milliseconds = Long.valueOf(source).longValue();
+        } else {            
+            matcher = createMatcher(REPLACEMENT_PATTERN, source);
+            String replacedSource = matcher.replaceAll(""); 
+            
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Replaced original source " + source + " to " + replacedSource);
+            }
+            
+            matcher = createMatcher(HOUR_REGEX_PATTERN, replacedSource);
+            if (matcher.find()) {
+                milliseconds = milliseconds + (3600000 * Long.valueOf(matcher.group(1)).longValue());
+                foundFlag = true;
+            }
+            
+            matcher = createMatcher(MINUTES_REGEX_PATTERN, replacedSource);            
+            if (matcher.find()) {
+                long minutes = Long.valueOf(matcher.group(1)).longValue();
+                if ((minutes > 59) && foundFlag) {
+                    throw new IllegalArgumentException("Minutes should contain a valid value
between 0 and 59: " + source);
+                }
+                foundFlag = true;
+                milliseconds = milliseconds + (60000 * minutes);
+            }
+               
+            matcher = createMatcher(SECONDS_REGEX_PATTERN, replacedSource);
+            if (matcher.find()) {
+                long seconds = Long.valueOf(matcher.group(1)).longValue();
+                if ((seconds > 59) && foundFlag) {
+                    throw new IllegalArgumentException("Seconds should contain a valid value
between 0 and 59: " + source);
+                }
+                foundFlag = true;
+                milliseconds = milliseconds + (1000 * seconds);
+            }      
+            
+            // No pattern matched... initiating fallback check and conversion (if required).

+            // The source at this point may contain illegal values or special characters

+            if (!foundFlag) {
+                milliseconds = Long.valueOf(source).longValue();
+            }
+        }       
+        
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("source: " + source + " milliseconds: " + milliseconds);
+        }
+        
+        return milliseconds;
+    }
+
+    private static Matcher createMatcher(String regexPattern, String source) {
+        Pattern pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
+        return pattern.matcher(source);        
+    }    
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/TimePatternConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternConverterTest.java?rev=938630&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternConverterTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternConverterTest.java
Tue Apr 27 20:02:22 2010
@@ -0,0 +1,150 @@
+/**
+ * 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.converter;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TimePatternConverterTest extends ContextTestSupport {
+    private static final transient Log LOG = LogFactory.getLog(TimePatternConverterTest.class);
+
+    public void testMillisecondsTimePattern() throws Exception {
+        String source = new String("444");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(444, milliseconds);
+    }
+    
+    public void testMilliseconds2TimePattern() throws Exception {
+        String source = new String("-72");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(-72, milliseconds);
+    }
+    
+    public void testSTimePattern() throws Exception {
+        String source = new String("35s");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(35000, milliseconds);
+    }
+    
+    public void testConsiderLegalSTimePattern() throws Exception {
+        String source = new String("89s");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(89000, milliseconds);
+    }
+
+    public void testMTimePattern() throws Exception {
+        String source = new String("28m");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(1680000, milliseconds);
+    }
+
+    public void testConsiderLegalMTimePattern() throws Exception {
+        String source = new String("89m");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(5340000, milliseconds);
+    }
+
+    public void testHMSTimePattern() throws Exception {
+        String source = new String("1h3m5s");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(3785000, milliseconds);
+    }
+
+    public void testHMSTimePattern2() throws Exception {
+        String source = new String("1hours30minutes1second");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(5401000, milliseconds);
+    }
+    
+    public void testHMSTimePattern3() throws Exception {
+        String source = new String("1HOUR3m5s");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(3785000, milliseconds);
+    }
+
+    public void testMSTimePattern() throws Exception {
+        String source = new String("30m55s");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(1855000, milliseconds);        
+    }
+    
+    public void testHMTimePattern() throws Exception {
+        String source = new String("1h30m");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(5400000, milliseconds);
+    }
+
+    public void testSTimePattern2() throws Exception {
+        String source = new String("15sec");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(15000, milliseconds);
+    }
+
+    public void testMTimePattern2() throws Exception {
+        String source = new String("5min");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(300000, milliseconds);
+    }
+    
+    public void testMTimePattern3() throws Exception {
+        String source = new String("5MIN");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(300000, milliseconds);
+    }
+    
+    public void testMSTimePattern2() throws Exception {
+        String source = new String("4min2sec");
+        long milliseconds = TimePatternConverter.toMilliSeconds(source);
+        assertEquals(242000, milliseconds);
+    }    
+    
+    //Negative Tests    
+    public void testIllegalHMSTimePattern() {
+        String source = new String("36h88m5s");
+        try {
+            long milliseconds = TimePatternConverter.toMilliSeconds(source);
+            fail("Should throw IllegalArgumentException");
+        } catch (Exception e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e);
+            assertEquals("Minutes should contain a valid value between 0 and 59: 36h88m5s",
e.getMessage());
+        }
+    }
+
+    public void testIllegalMSTimePattern() {
+        String source = new String("55m75s");
+        try {
+            long milliseconds = TimePatternConverter.toMilliSeconds(source);
+            fail("Should throw IllegalArgumentException");
+        } catch (Exception e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e);
+            assertEquals("Seconds should contain a valid value between 0 and 59: 55m75s",
e.getMessage());
+        }
+    }
+    
+    public void testIllegalHMTimePattern() throws Exception {
+        String source = new String("1h89s");
+        try {
+            long milliseconds = TimePatternConverter.toMilliSeconds(source);
+            fail("Should throw IllegalArgumentException");
+        } catch (Exception e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e);
+            assertEquals("Seconds should contain a valid value between 0 and 59: 1h89s",
e.getMessage());
+        }
+    }    
+    
+}

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

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternTypeConversionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternTypeConversionTest.java?rev=938630&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternTypeConversionTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimePatternTypeConversionTest.java
Tue Apr 27 20:02:22 2010
@@ -0,0 +1,42 @@
+/**
+ * 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.converter;
+
+import org.apache.camel.ContextTestSupport;
+
+public class TimePatternTypeConversionTest extends ContextTestSupport {
+
+    public void testForNoSideEffects() throws Exception {
+        long milliseconds = context.getTypeConverter().convertTo(long.class, "444");
+        assertEquals(Long.valueOf("444").longValue(), milliseconds);
+    }
+    
+    public void testForNoSideEffects2() throws Exception {
+        long milliseconds = context.getTypeConverter().convertTo(long.class, "-72");
+        assertEquals(Long.valueOf("-72").longValue(), milliseconds);
+    }
+    
+    public void testHMSTimePattern() throws Exception {
+        long milliseconds = context.getTypeConverter().convertTo(long.class, "1hours30m1s");
+        assertEquals(5401000, milliseconds);
+    }
+    
+    public void testMTimePattern() throws Exception {
+        long milliseconds = context.getTypeConverter().convertTo(long.class, "30m55s");
+        assertEquals(1855000, milliseconds);
+    }
+}

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

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimerDrivenTimePatternConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimerDrivenTimePatternConverterTest.java?rev=938630&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimerDrivenTimePatternConverterTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/TimerDrivenTimePatternConverterTest.java
Tue Apr 27 20:02:22 2010
@@ -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.converter;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.StopWatch;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TimerDrivenTimePatternConverterTest extends ContextTestSupport {
+    private static final transient Log LOG = LogFactory.getLog(TimerDrivenTimePatternConverterTest.class);
+    
+    //Timer test
+    public void testTimerInvocation() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result"); 
+        mock.expectedMinimumMessageCount(2);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testTimerUsingStopWatch() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result"); 
+        mock.expectedMinimumMessageCount(2);
+
+        StopWatch watch = new StopWatch();
+        assertMockEndpointsSatisfied();
+        long interval = watch.stop();
+        
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Should take approx 5000 milliseconds, was: " + interval);
+        }
+        assertTrue("Should take approx 5000 milliseconds, was: " + interval, interval >=
4995);
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("timer://foo?fixedRate=true&delay=0&period=5s").to("mock:result");
+            }
+        };
+    } 
+}

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



Mime
View raw message