This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 32b11cf CAMEL-14805: Use doInit for iniitalizing. Fixed tests 32b11cf is described below commit 32b11cf54c6359757cf60dde9930fc77ec3c932e Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Mar 30 10:34:12 2020 +0200 CAMEL-14805: Use doInit for iniitalizing. Fixed tests --- components/camel-mongodb-gridfs/pom.xml | 5 +--- .../component/mongodb/gridfs/GridFsComponent.java | 7 ------ .../component/mongodb/gridfs/GridFsConsumer.java | 24 +++++++------------ .../component/mongodb/gridfs/GridFsConverter.java | 5 +--- .../component/mongodb/gridfs/GridFsEndpoint.java | 24 +++++++------------ .../mongodb/gridfs/AbstractMongoDbTest.java | 28 +++++++++------------- .../mongodb/gridfs/EmbedMongoConfiguration.java | 11 +++------ .../mongodb/gridfs/GridFsConsumerTest.java | 1 + 8 files changed, 34 insertions(+), 71 deletions(-) diff --git a/components/camel-mongodb-gridfs/pom.xml b/components/camel-mongodb-gridfs/pom.xml index 5b4cc12..9e50f84 100644 --- a/components/camel-mongodb-gridfs/pom.xml +++ b/components/camel-mongodb-gridfs/pom.xml @@ -32,9 +32,6 @@ <name>Camel :: MongoDB GridFS</name> <description>Camel MongoDB GridFS component</description> - <properties> - </properties> - <dependencies> <dependency> @@ -59,7 +56,7 @@ </dependency> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-test-spring</artifactId> + <artifactId>camel-test</artifactId> <scope>test</scope> </dependency> diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsComponent.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsComponent.java index 1e9a638..d5b11bf 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsComponent.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsComponent.java @@ -30,17 +30,10 @@ public class GridFsComponent extends DefaultComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - GridFsEndpoint endpoint = new GridFsEndpoint(uri, this); endpoint.setConnectionBean(remaining); setProperties(endpoint, parameters); - return endpoint; } - @Override - protected void doShutdown() throws Exception { - super.doShutdown(); - } - } diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java index d9381eb..6f031a4 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumer.java @@ -30,15 +30,12 @@ import com.mongodb.util.JSON; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.support.DefaultConsumer; +import org.apache.camel.util.IOHelper; public class GridFsConsumer extends DefaultConsumer implements Runnable { - final GridFsEndpoint endpoint; - private ExecutorService executor; + private final GridFsEndpoint endpoint; + private volatile ExecutorService executor; - /** - * @param endpoint - * @param processor - */ public GridFsConsumer(GridFsEndpoint endpoint, Processor processor) { super(endpoint, processor); this.endpoint = endpoint; @@ -48,7 +45,7 @@ public class GridFsConsumer extends DefaultConsumer implements Runnable { protected void doStop() throws Exception { super.doStop(); if (executor != null) { - executor.shutdown(); + endpoint.getCamelContext().getExecutorServiceManager().shutdown(executor); executor = null; } } @@ -76,13 +73,13 @@ public class GridFsConsumer extends DefaultConsumer implements Runnable { DBObject persistentTimestamp = null; if (persistsTimestamp) { ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection()); - // ensure standard indexes as long as collections are small + // ensure standard indexes as long as collections are small try { if (ptsCollection.count() < 1000) { ptsCollection.createIndex(new BasicDBObject("id", 1)); } } catch (MongoException e) { - //TODO: Logging + // ignore } persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject())); if (persistentTimestamp == null) { @@ -137,7 +134,6 @@ public class GridFsConsumer extends DefaultConsumer implements Runnable { exchange.getIn().setBody(file.getInputStream(), InputStream.class); try { getProcessor().process(exchange); - //System.out.println("Processing " + file.getFilename()); if (usesAttribute) { forig.put(endpoint.getFileAttributeName(), "done"); endpoint.getFilesCollection().save(forig); @@ -149,8 +145,7 @@ public class GridFsConsumer extends DefaultConsumer implements Runnable { } } } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // ignore } } } @@ -161,11 +156,10 @@ public class GridFsConsumer extends DefaultConsumer implements Runnable { Thread.sleep(endpoint.getDelay()); } } catch (Throwable e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); + // ignore } if (c != null) { - c.close(); + IOHelper.close(c); } } diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConverter.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConverter.java index e3d8644..ee10cfb 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConverter.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsConverter.java @@ -22,10 +22,7 @@ import org.apache.camel.Converter; import org.apache.camel.Exchange; @Converter(generateLoader = true) -public final class GridFsConverter { - - private GridFsConverter() { - } +public class GridFsConverter { @Converter public static WriteConcern toWriteConcern(String value, Exchange exchange) { diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsEndpoint.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsEndpoint.java index d728154..46f3d31 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsEndpoint.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/mongodb/gridfs/GridFsEndpoint.java @@ -69,7 +69,6 @@ public class GridFsEndpoint extends DefaultEndpoint { private long initialDelay = 1000; @UriParam(label = "consumer", defaultValue = "500") private long delay = 500; - @UriParam(label = "consumer", defaultValue = "TimeStamp") private QueryStrategy queryStrategy = QueryStrategy.TimeStamp; @UriParam(label = "consumer", defaultValue = "camel-timestamps") @@ -79,7 +78,6 @@ public class GridFsEndpoint extends DefaultEndpoint { @UriParam(label = "consumer", defaultValue = "camel-processed") private String fileAttributeName = "camel-processed"; - private MongoClient mongoConnection; private DB db; private GridFS gridFs; @@ -117,18 +115,17 @@ public class GridFsEndpoint extends DefaultEndpoint { }; } - @Override - protected void doStart() throws Exception { + protected void doInit() throws Exception { mongoConnection = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class); LOG.debug("Resolved the connection with the name {} as {}", connectionBean, mongoConnection); setWriteReadOptionsOnConnection(); - super.doStart(); + super.doInit(); } @Override - protected void doStop() throws Exception { - super.doStop(); + protected void doShutdown() throws Exception { + super.doShutdown(); if (mongoConnection != null) { LOG.debug("Closing connection"); mongoConnection.close(); @@ -148,6 +145,7 @@ public class GridFsEndpoint extends DefaultEndpoint { } // ======= Getters and setters =============================================== + public String getConnectionBean() { return connectionBean; } @@ -207,8 +205,6 @@ public class GridFsEndpoint extends DefaultEndpoint { /** * Additional query parameters (in JSON) that are used to configure the query used for finding * files in the GridFsConsumer - * - * @param query */ public void setQuery(String query) { this.query = query; @@ -219,9 +215,7 @@ public class GridFsEndpoint extends DefaultEndpoint { } /** - * Sets the delay between polls within the Consumer. Default is 500ms - * - * @param delay + * Sets the delay between polls within the Consumer. Default is 500ms */ public void setDelay(long delay) { this.delay = delay; @@ -232,9 +226,7 @@ public class GridFsEndpoint extends DefaultEndpoint { } /** - * Sets the initialDelay before the consumer will start polling. Default is 1000ms - * - * @param initialDelay + * Sets the initialDelay before the consumer will start polling. Default is 1000ms */ public void setInitialDelay(long initialDelay) { this.initialDelay = delay; @@ -248,7 +240,7 @@ public class GridFsEndpoint extends DefaultEndpoint { } /** - * Sets the QueryStrategy that is used for polling for new files. Default is Timestamp + * Sets the QueryStrategy that is used for polling for new files. Default is Timestamp */ public void setQueryStrategy(QueryStrategy queryStrategy) { this.queryStrategy = queryStrategy; diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java index 143b202..d94b4e0 100644 --- a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java @@ -19,26 +19,16 @@ package org.apache.camel.component.mongodb.gridfs; import com.mongodb.MongoClient; import com.mongodb.gridfs.GridFS; import org.apache.camel.CamelContext; -import org.apache.camel.spring.SpringCamelContext; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.After; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.apache.camel.component.mongodb.gridfs.EmbedMongoConfiguration.createMongoClient; public abstract class AbstractMongoDbTest extends CamelTestSupport { protected MongoClient mongo; protected GridFS gridfs; - protected ApplicationContext applicationContext; - - @SuppressWarnings("deprecation") - @Override - public void doPostSetup() { - mongo = applicationContext.getBean(MongoClient.class); - gridfs = new GridFS(mongo.getDB("test"), getBucket()); - } - public String getBucket() { return this.getClass().getSimpleName(); } @@ -52,10 +42,14 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport { @Override protected CamelContext createCamelContext() throws Exception { - applicationContext = new AnnotationConfigApplicationContext(EmbedMongoConfiguration.class); - CamelContext ctx = new SpringCamelContext(applicationContext); - ctx.init(); - ctx.getPropertiesComponent().setLocation("classpath:mongodb.test.properties"); - return ctx; + mongo = createMongoClient(); + gridfs = new GridFS(mongo.getDB("test"), getBucket()); + + CamelContext context = super.createCamelContext(); + context.getPropertiesComponent().setLocation("classpath:mongodb.test.properties"); + context.getRegistry().bind("test", gridfs); + context.getRegistry().bind("myDb", mongo); + + return context; } } diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/EmbedMongoConfiguration.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/EmbedMongoConfiguration.java index d6f1c36..81bb485 100644 --- a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/EmbedMongoConfiguration.java +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/EmbedMongoConfiguration.java @@ -17,7 +17,6 @@ package org.apache.camel.component.mongodb.gridfs; import java.io.IOException; -import java.net.UnknownHostException; import com.mongodb.MongoClient; import de.flapdoodle.embed.mongo.Command; @@ -27,20 +26,17 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; import de.flapdoodle.embed.mongo.config.Net; import de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder; import de.flapdoodle.embed.process.config.IRuntimeConfig; +import org.apache.camel.test.AvailablePortFinder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import static com.mongodb.ServerAddress.defaultHost; import static de.flapdoodle.embed.mongo.distribution.Version.Main.PRODUCTION; import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6; -import static org.springframework.util.SocketUtils.findAvailableTcpPort; -@Configuration public class EmbedMongoConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(EmbedMongoConfiguration.class); - private static final int PORT = findAvailableTcpPort(18500); // 1024 is too low on CI servers to find free ports + private static final int PORT = AvailablePortFinder.getNextAvailable(18500, 19000); static { try { @@ -59,8 +55,7 @@ public class EmbedMongoConfiguration { } } - @Bean - public MongoClient myDb() throws UnknownHostException { + public static MongoClient createMongoClient() { return new MongoClient(defaultHost(), PORT); } } diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTest.java index c7f23e9..979dc0c 100644 --- a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTest.java +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/GridFsConsumerTest.java @@ -26,6 +26,7 @@ import org.apache.camel.component.mock.MockEndpoint; import org.junit.Test; public class GridFsConsumerTest extends AbstractMongoDbTest { + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() {