camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject git commit: CAMEL-7369: Fix the not-working readPreference option and polish the tests a bit Conflicts: components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
Date Tue, 15 Apr 2014 19:32:28 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.12.x 1c79aef8d -> a45e51938


CAMEL-7369: Fix the not-working readPreference option and polish the tests a bit
Conflicts:
	components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java


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

Branch: refs/heads/camel-2.12.x
Commit: a45e519382e59ef8de6b3d6f2e6482ba8203506e
Parents: 1c79aef
Author: Babak Vahdat <bvahdat@apache.org>
Authored: Tue Apr 15 16:10:08 2014 +0200
Committer: Babak Vahdat <bvahdat@apache.org>
Committed: Tue Apr 15 21:30:17 2014 +0200

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


http://git-wip-us.apache.org/repos/asf/camel/blob/a45e5193/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 0ee3554..4912f63 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,29 +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((Class<?>)
null).newInstance((Object[]) null);
-                } catch (Exception 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/a45e5193/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/a45e5193/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/a45e5193/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();


Mime
View raw message