You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bv...@apache.org on 2014/04/15 16:10:13 UTC

git commit: CAMEL-7369: Fix the non-working readPreference option and polish tests a bit

Repository: camel
Updated Branches:
  refs/heads/master 576ea5bda -> d152c8515


CAMEL-7369: Fix the non-working readPreference option and polish tests a bit

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d152c851
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d152c851
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d152c851

Branch: refs/heads/master
Commit: d152c85153dbbf685287a3aa47cb653f1b848303
Parents: 576ea5b
Author: Babak Vahdat <bv...@apache.org>
Authored: Tue Apr 15 16:10:08 2014 +0200
Committer: Babak Vahdat <bv...@apache.org>
Committed: Tue Apr 15 16:10:08 2014 +0200

----------------------------------------------------------------------
 .../component/mongodb/MongoDbEndpoint.java      | 27 ++------
 .../component/mongodb/AbstractMongoDbTest.java  | 12 ++--
 .../MongoDbReadPreferenceOptionTest.java        | 73 ++++++++++++++++++++
 .../MongoDbTailableCursorConsumerTest.java      |  6 +-
 4 files changed, 87 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d152c851/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
index 3f14c99..78978b8 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
@@ -249,7 +249,6 @@ public class MongoDbEndpoint extends DefaultEndpoint {
         message.setHeader(MongoDbConstants.DATABASE, database);
         message.setHeader(MongoDbConstants.COLLECTION, collection);
         message.setHeader(MongoDbConstants.FROM_TAILABLE, true);
-
         message.setBody(dbObj);
         exchange.setIn(message);
         return exchange;
@@ -415,30 +414,14 @@ public class MongoDbEndpoint extends DefaultEndpoint {
     /** 
      * Sets a MongoDB {@link ReadPreference} on the Mongo connection. Read preferences set directly on the connection will be
      * overridden by this setting.
+     * <p/>
+     * The {@link com.mongodb.ReadPreference#valueOf(String)} utility method is used to resolve the passed {@code readPreference}
+     * value. Some examples for the possible values are {@code nearest}, {@code primary} or {@code secondary} etc.
      * 
-     * @param readPreference the bean name of the read preference to set
+     * @param readPreference the name of the read preference to set
      */
     public void setReadPreference(String readPreference) {
-        Class<?>[] innerClasses = ReadPreference.class.getDeclaredClasses();
-        for (Class<?> inClass : innerClasses) {
-            if (inClass.getSuperclass() == ReadPreference.class && inClass.getName().equals(readPreference)) {
-                try {
-                    this.readPreference = (ReadPreference) inClass.getConstructor().newInstance();
-                } catch (Exception e) {
-                    LOG.debug("Error setting the read preference. This exception will be ignored.", e);
-                    continue;
-                }
-                // break the loop as we could successfully set the read preference property
-                break;
-            }
-        }
-
-        // were we able to set the read preference?
-        if (getReadPreference() == null) {
-            String msg = "Could not resolve specified ReadPreference of type " + readPreference
-                    + ". Read preferences are resolved from inner classes of com.mongodb.ReadPreference.";
-            throw new IllegalArgumentException(msg);
-        }
+        this.readPreference = ReadPreference.valueOf(readPreference);
     }
 
     public ReadPreference getReadPreference() {

http://git-wip-us.apache.org/repos/asf/camel/blob/d152c851/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
index 66f2a5c..9bc7fb2 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
@@ -37,9 +37,7 @@ import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
-import org.junit.After;
 import org.junit.Assume;
-import org.junit.Before;
 import org.junit.BeforeClass;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -81,8 +79,8 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
         
     }
 
-    @Before
-    public void initTestCase() {
+    @Override
+    public void doPostSetup() {
         // Refresh the test collection - drop it and recreate it. We don't do this for the database because MongoDB would create large
         // store files each time
         testCollectionName = properties.getProperty("mongodb.testCollection");
@@ -97,10 +95,12 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
 
     }
 
-    @After
-    public void cleanup() {
+    @Override
+    public void tearDown() throws Exception {
         testCollection.drop();
         dynamicCollection.drop();
+
+        super.tearDown();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/d152c851/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbReadPreferenceOptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbReadPreferenceOptionTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbReadPreferenceOptionTest.java
new file mode 100644
index 0000000..49e27d2
--- /dev/null
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbReadPreferenceOptionTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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.component.mongodb;
+
+import com.mongodb.ReadPreference;
+import org.apache.camel.Endpoint;
+import org.junit.Test;
+
+public class MongoDbReadPreferenceOptionTest extends AbstractMongoDbTest {
+    
+    private MongoDbEndpoint endpoint;
+
+    @Test
+    public void testInvalidReadPreferenceOptionValue() throws Exception {
+        try {
+            createMongoDbEndpoint("mongodb:myDb?database={{mongodb.testDb}}&readPreference=foo");
+            fail("Should have thrown exception");
+        } catch (Exception iae) {
+            assertTrue(iae.getMessage(), iae.getMessage().endsWith("No match for read preference of foo"));
+        }
+    }
+
+    @Test
+    public void testNoReadPreferenceOptionValue() throws Exception {
+        endpoint = createMongoDbEndpoint("mongodb:myDb?database={{mongodb.testDb}}");
+        assertNull(endpoint.getReadPreference());
+        assertSame(ReadPreference.primary(), endpoint.getMongoConnection().getReadPreference()); // the default is primary
+    }
+
+    @Test
+    public void testPrimaryReadPreferenceOptionValue() throws Exception {
+        endpoint = createMongoDbEndpoint("mongodb:myDb?database={{mongodb.testDb}}&readPreference=primary");
+        assertSame(ReadPreference.primary(), endpoint.getReadPreference());
+        assertSame(ReadPreference.primary(), endpoint.getMongoConnection().getReadPreference());
+    }
+
+    @Test
+    public void testSecondaryReadPreferenceOptionValue() throws Exception {
+        endpoint = createMongoDbEndpoint("mongodb:myDb?database={{mongodb.testDb}}&readPreference=secondary");
+        assertSame(ReadPreference.secondary(), endpoint.getReadPreference());
+        assertSame(ReadPreference.secondary(), endpoint.getMongoConnection().getReadPreference());
+    }
+
+    @Test
+    public void testNearestReadPreferenceOptionValue() throws Exception {
+        endpoint = createMongoDbEndpoint("mongodb:myDb?database={{mongodb.testDb}}&readPreference=nearest");
+        assertSame(ReadPreference.nearest(), endpoint.getReadPreference());
+        assertSame(ReadPreference.nearest(), endpoint.getMongoConnection().getReadPreference());
+    }
+
+    private MongoDbEndpoint createMongoDbEndpoint(String uri) throws Exception {
+        MongoDbComponent component = context().getComponent("mongodb", MongoDbComponent.class);
+        Endpoint endpoint = component.createEndpoint(uri);
+        endpoint.start();
+        return (MongoDbEndpoint) endpoint;
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d152c851/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbTailableCursorConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbTailableCursorConsumerTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbTailableCursorConsumerTest.java
index e674021..fb65f22 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbTailableCursorConsumerTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbTailableCursorConsumerTest.java
@@ -373,9 +373,9 @@ public class MongoDbTailableCursorConsumerTest extends AbstractMongoDbTest {
     }
     
     @Override
-    public void initTestCase() {
-        super.initTestCase();
-        // drop the capped collection and let each test create what they need
+    public void doPostSetup() {
+        super.doPostSetup();
+        // drop the capped collection and let each test create what it needs
         cappedTestCollectionName = properties.getProperty("mongodb.cappedTestCollection");
         cappedTestCollection = db.getCollection(cappedTestCollectionName);
         cappedTestCollection.drop();