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 2011/08/22 12:20:07 UTC

svn commit: r1160189 - /camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java

Author: ningjiang
Date: Mon Aug 22 10:20:06 2011
New Revision: 1160189

URL: http://svn.apache.org/viewvc?rev=1160189&view=rev
Log:
Added an unit test of camel-guice according the camel user mailing list

Added:
    camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java   (with props)

Added: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java?rev=1160189&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java (added)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java Mon Aug 22 10:20:06 2011
@@ -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.guice;
+
+import java.util.Hashtable;
+import java.util.Set;
+
+import javax.naming.InitialContext;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Provides;
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileFilter;
+import org.apache.camel.component.mock.MockComponent;
+import org.apache.camel.guice.GuiceRouteWithNamedKeysTest.MyModule;
+import org.guiceyfruit.Injectors;
+import org.guiceyfruit.jndi.GuiceInitialContextFactory;
+import org.guiceyfruit.jndi.JndiBind;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class FileEndpointReferenceRouteTest extends Assert {
+    
+    public static class RouteInstaller extends RouteBuilder {
+
+        public void configure() throws Exception {
+            // lets add some other route builders
+            includeRoutes(new MyConfigurableRoute(endpoint("file://src/test/resources?noop=true&filter=#fileFilter"), endpoint("direct:b")));
+        }
+    }
+    
+    public static class MyFileFilter implements GenericFileFilter {
+
+        public boolean accept(GenericFile file) {
+            // we only want report files 
+            return file.getFileName().startsWith("report");
+        }
+    }
+
+    
+    public static class MyModule extends CamelModuleWithRouteTypes {
+       
+        @SuppressWarnings("unchecked")
+        public MyModule() {
+            super(RouteInstaller.class);
+        }
+        
+        @Provides
+        @JndiBind("fileFilter")
+        public GenericFileFilter getfileFilter() {
+            return new MyFileFilter();
+        }
+        
+
+    }
+    
+    public static void assertCamelContextRunningThenCloseInjector(Injector injector) throws Exception {
+        CamelContext camelContext = injector.getInstance(CamelContext.class);
+
+        org.hamcrest.MatcherAssert.assertThat(camelContext, org.hamcrest.Matchers.is(GuiceCamelContext.class));
+        GuiceCamelContext guiceContext = (GuiceCamelContext) camelContext;
+        assertTrue("is started!", guiceContext.isStarted());
+
+        Thread.sleep(1000);
+
+        Injectors.close(injector);
+    }
+    
+    @Test
+    public void runTest() throws Exception {
+        Hashtable<String, Object> env = new Hashtable<String, Object>();
+        env.put(InitialContext.PROVIDER_URL, GuiceInitialContextFactory.class.getName());
+        env.put(Injectors.MODULE_CLASS_NAMES, MyModule.class.getName());
+
+        InitialContext context = new InitialContext(env);
+        Injector injector = (Injector) context.lookup(Injector.class.getName());
+        assertNotNull("Found injector", injector);
+
+        Object value = context.lookup("fileFilter");
+        assertNotNull("Should have found a value for foo!", value);
+        
+        assertCamelContextRunningThenCloseInjector(injector);
+    }
+
+
+}

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

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