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

svn commit: r749563 - in /camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi: CamelOsgiTestSupport.java OsgiAnnotationTypeConverterLoaderTest.java test/MockTypeConverterRegistry.java test/MyTypeConverter.java

Author: ningjiang
Date: Tue Mar  3 08:20:30 2009
New Revision: 749563

URL: http://svn.apache.org/viewvc?rev=749563&view=rev
Log:
Added the test for the OsgiAnnotationTypeConverterLoader

Added:
    camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java   (with props)
    camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java   (with props)
Modified:
    camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java
    camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java

Modified: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java?rev=749563&r1=749562&r2=749563&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java (original)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java Tue Mar  3 08:20:30 2009
@@ -17,6 +17,7 @@
 package org.apache.camel.osgi;
 
 import junit.framework.TestCase;
+import org.osgi.framework.BundleContext;
 import org.springframework.osgi.mock.MockBundle;
 import org.springframework.osgi.mock.MockBundleContext;
 
@@ -38,5 +39,9 @@
     public Activator getActivator() {
         return testActivator;
     }
+    
+    public BundleContext getBundleContext() {
+        return bundleContext;
+    }
 
 }

Added: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java?rev=749563&view=auto
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java (added)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java Tue Mar  3 08:20:30 2009
@@ -0,0 +1,31 @@
+/**
+ * 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.osgi;
+
+import org.apache.camel.osgi.test.MockTypeConverterRegistry;
+
+public class OsgiAnnotationTypeConverterLoaderTest extends CamelOsgiTestSupport {
+    
+    public void testLoad() throws Exception {
+        OsgiAnnotationTypeConverterLoader loader = new OsgiAnnotationTypeConverterLoader(getBundleContext());
+        MockTypeConverterRegistry registry = new MockTypeConverterRegistry();
+        loader.load(registry);
+        assertEquals("There should have a fallback converter", registry.getFallbackTypeConverters().size(), 1);
+        assertEquals("There should have a coverter", registry.getTypeConverters().size(), 1);
+    }
+
+}

Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java?rev=749563&view=auto
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java (added)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java Tue Mar  3 08:20:30 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.osgi.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.TypeConverter;
+import org.apache.camel.spi.Injector;
+import org.apache.camel.spi.TypeConverterRegistry;
+
+public class MockTypeConverterRegistry implements TypeConverterRegistry {
+    private List<TypeConverter> typeConverters = new ArrayList<TypeConverter>();
+    private List<TypeConverter> fallbackTypeConverters = new ArrayList<TypeConverter>();
+    
+    public List<TypeConverter> getTypeConverters() {
+        return typeConverters;
+    }
+    
+    public List<TypeConverter> getFallbackTypeConverters() {
+        return fallbackTypeConverters;
+    }
+    
+    public void addTypeConverter(Class toType, Class fromType, TypeConverter typeConverter) {
+        System.out.println(" Adding the type converter" + typeConverter);
+    }
+
+    public void addFallbackTypeConverter(TypeConverter typeConverter) {
+        System.out.println(" Adding the fall back type converter" + typeConverter);
+    }
+
+    public TypeConverter lookup(Class toType, Class fromType) {       
+        return null;
+    }
+
+    public void setInjector(Injector injector) {
+       // do nothing
+    }
+
+    public Injector getInjector() {
+        return null;
+    }
+
+}
+

Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MockTypeConverterRegistry.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java?rev=749563&r1=749562&r2=749563&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java (original)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java Tue Mar  3 08:20:30 2009
@@ -19,6 +19,11 @@
 import java.util.Collection;
 
 import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
 
 @Converter
@@ -48,7 +53,29 @@
             return answer.booleanValue();
         }
         return false;
-    }    
+    }
+    
+    @FallbackConverter
+    public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+        // use a fallback type converter so we can convert the embedded body if the value is GenericFile
+        if (GenericFile.class.isAssignableFrom(value.getClass())) {
+            GenericFile file = (GenericFile) value;
+            Class from = file.getBody().getClass();
+
+            // maybe from is already the type we want
+            if (from.isAssignableFrom(type)) {
+                return file.getBody();
+            }
+            // no then try to lookup a type converter
+            TypeConverter tc = registry.lookup(type, from);
+            if (tc != null) {
+                Object body = file.getBody();
+                return tc.convertTo(type, exchange, body);
+            }
+        }
+        
+        return null;
+    }
     
 
 }