You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/05/27 10:11:33 UTC
svn commit: r779045 -
/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
Author: davsclaus
Date: Wed May 27 08:11:33 2009
New Revision: 779045
URL: http://svn.apache.org/viewvc?rev=779045&view=rev
Log:
CAMEL-1647: ServicePool is now a SPI. Added unit test.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java (with props)
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java?rev=779045&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java Wed May 27 08:11:33 2009
@@ -0,0 +1,168 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.ServicePoolAware;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.ServicePool;
+
+/**
+ * Unit test for a custom ServicePool for producer.
+ *
+ * @version $Revision$
+ */
+public class CustomProducerServicePoolTest extends ContextTestSupport {
+
+ private static int COUNTER;
+
+ private class MyEndpoint extends DefaultEndpoint {
+
+ public Producer createProducer() throws Exception {
+ return new MyProducer(this);
+ }
+
+ public Consumer createConsumer(Processor processor) throws Exception {
+ return null;
+ }
+
+ public boolean isSingleton() {
+ return true;
+ }
+
+ @Override
+ protected String createEndpointUri() {
+ return "my";
+ }
+ }
+
+ private class MyProducer extends DefaultProducer implements ServicePoolAware {
+
+ public MyProducer(Endpoint endpoint) {
+ super(endpoint);
+ }
+
+ public void process(Exchange exchange) throws Exception {
+ COUNTER++;
+ }
+ }
+
+ private class MyPool implements ServicePool<Endpoint, Producer> {
+
+ private Producer producer;
+
+ public Producer addAndAcquire(Endpoint endpoint, Producer producer) {
+ if (endpoint instanceof MyEndpoint) {
+ return producer;
+ } else {
+ return null;
+ }
+ }
+
+ public Producer acquire(Endpoint endpoint) {
+ if (endpoint instanceof MyEndpoint) {
+ Producer answer = producer;
+ producer = null;
+ return answer;
+ } else {
+ return null;
+ }
+ }
+
+ public void release(Endpoint endpoint, Producer producer) {
+ this.producer = producer;
+ }
+
+ public void start() throws Exception {
+ }
+
+ public void stop() throws Exception {
+ }
+
+ public int size() {
+ return producer != null ? 1 : 0;
+ }
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testCustomProducerServicePool() throws Exception {
+ MyPool pool = new MyPool();
+ pool.start();
+ context.setProducerServicePool(pool);
+
+ context.addEndpoint("my", new MyEndpoint());
+
+ Endpoint endpoint = context.getEndpoint("my");
+
+ assertNull(pool.acquire(endpoint));
+ assertEquals(0, pool.size());
+
+ Producer producer = new MyProducer(endpoint);
+ producer = pool.addAndAcquire(endpoint, producer);
+ assertEquals(0, pool.size());
+
+ pool.release(endpoint, producer);
+ assertEquals(1, pool.size());
+
+ producer = pool.acquire(endpoint);
+ assertNotNull(producer);
+ assertEquals(0, pool.size());
+
+ pool.release(endpoint, producer);
+ assertEquals(1, pool.size());
+
+ assertIsInstanceOf(MyPool.class, context.getProducerServicePool());
+ }
+
+ public void testCustomProducerServicePoolInRoute() throws Exception {
+ context.addEndpoint("my", new MyEndpoint());
+
+ MyPool pool = new MyPool();
+ pool.start();
+ context.setProducerServicePool(pool);
+
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").to("my", "mock:result");
+ }
+ });
+ context.start();
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(2);
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start", "Bye World");
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(2, COUNTER);
+ assertEquals(1, pool.size());
+ }
+
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomProducerServicePoolTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date