You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/10/10 10:31:28 UTC
svn commit: r703352 - in
/servicemix/components/bindings/servicemix-file/trunk/src:
main/java/org/apache/servicemix/file/FilePollerEndpoint.java
test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
test/resources/test-data.xml
Author: gertv
Date: Fri Oct 10 01:31:27 2008
New Revision: 703352
URL: http://svn.apache.org/viewvc?rev=703352&view=rev
Log:
SMX4-121, SM-1605: Adding a unit test
Added:
servicemix/components/bindings/servicemix-file/trunk/src/test/resources/test-data.xml
Modified:
servicemix/components/bindings/servicemix-file/trunk/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java
servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
Modified: servicemix/components/bindings/servicemix-file/trunk/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java?rev=703352&r1=703351&r2=703352&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java (original)
+++ servicemix/components/bindings/servicemix-file/trunk/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java Fri Oct 10 01:31:27 2008
@@ -62,7 +62,7 @@
private File archive;
private FileMarshaler marshaler = new DefaultFileMarshaler();
private LockManager lockManager;
- private ConcurrentMap<String, InputStream> openExchanges;
+ private ConcurrentMap<String, InputStream> openExchanges = new ConcurrentHashMap<String, InputStream>();
public FilePollerEndpoint() {
}
@@ -82,7 +82,7 @@
public synchronized void start() throws Exception {
super.start();
- // create the openExchanges map
+ // re-create the openExchanges map
this.openExchanges = new ConcurrentHashMap<String, InputStream>();
}
Modified: servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java?rev=703352&r1=703351&r2=703352&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java (original)
+++ servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java Fri Oct 10 01:31:27 2008
@@ -17,22 +17,70 @@
package org.apache.servicemix.file;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.LinkedList;
+import java.util.List;
import javax.jbi.management.DeploymentException;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessageExchangeFactory;
+import javax.jbi.messaging.MessagingException;
import javax.xml.namespace.QName;
import junit.framework.TestCase;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.executors.Executor;
+import org.apache.servicemix.jbi.util.FileUtil;
+import org.apache.servicemix.tck.mock.MockExchangeFactory;
+
public class FilePollerEndpointTest extends TestCase {
private static final File DATA = new File("target/test/data");
private static final File ARCHIVE = new File("target/test/archive");
+ private final List<MessageExchange> exchanges = new LinkedList<MessageExchange>();
private FilePollerEndpoint endpoint;
@Override
protected void setUp() throws Exception {
- endpoint = new FilePollerEndpoint();
+ exchanges.clear();
+ endpoint = new FilePollerEndpoint() {
+ {
+ logger = LogFactory.getLog(this.getClass());
+
+ }
+ @Override
+ protected void send(MessageExchange me) throws MessagingException {
+ exchanges.add(me);
+ }
+ @Override
+ public Executor getExecutor() {
+ return new MockExecutor();
+ }
+ @Override
+ public MessageExchangeFactory getExchangeFactory() {
+ return new MockExchangeFactory() {
+ @Override
+ public InOnly createInOnlyExchange() throws MessagingException {
+ return new MockExchangeFactory.MockInOnly() {
+ private final String exchangeId = "id" + System.nanoTime();
+ @Override
+ public String getExchangeId() {
+ return exchangeId;
+ }
+ };
+ }
+ };
+ }
+ };
endpoint.setTargetService(new QName("urn:test", "service"));
+ endpoint.setLockManager(new org.apache.servicemix.common.locks.impl.SimpleLockManager());
}
public void testValidateNoFile() throws Exception {
@@ -72,4 +120,66 @@
//test succeeds
}
}
+
+ public void testProcessError() throws Exception {
+ createTestFile();
+ endpoint.setFile(DATA);
+ endpoint.pollFile(new File(DATA, "test-data.xml"));
+ MessageExchange exchange = exchanges.get(0);
+ exchange.setStatus(ExchangeStatus.ERROR);
+ exchange.setError(new TestException());
+ try {
+ endpoint.process(exchange);
+ } catch (TestException e) {
+ //this is OK
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.out.println(e);
+ fail("we shouldn't be getting any other exceptions at this point");
+ }
+ }
+
+ public void testProcessSuccess() throws Exception {
+ createTestFile();
+ endpoint.setFile(DATA);
+ File file = new File(DATA, "test-data.xml");
+ endpoint.pollFile(file);
+ MessageExchange exchange = exchanges.get(0);
+ exchange.setStatus(ExchangeStatus.DONE);
+ endpoint.process(exchange);
+ assertFalse(file.exists());
+ }
+
+ private void createTestFile() throws IOException {
+ DATA.mkdirs();
+ InputStream fis = new FileInputStream("target/test-classes/test-data.xml");
+ OutputStream fos = new FileOutputStream(new File(DATA, "test-data.xml"));
+ FileUtil.copyInputStream(fis, fos);
+ fis.close();
+ fos.close();
+ }
+
+ private static class TestException extends Exception {
+ //nothing to do here
+ }
+
+ private static class MockExecutor implements Executor {
+
+ public int capacity() {
+ return 0;
+ }
+
+ public void execute(Runnable command) {
+ command.run();
+ }
+
+ public void shutdown() {
+ //graciously do nothing
+ }
+
+ public int size() {
+ return 0;
+ }
+
+ }
}
Added: servicemix/components/bindings/servicemix-file/trunk/src/test/resources/test-data.xml
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/test/resources/test-data.xml?rev=703352&view=auto
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/test/resources/test-data.xml (added)
+++ servicemix/components/bindings/servicemix-file/trunk/src/test/resources/test-data.xml Fri Oct 10 01:31:27 2008
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!--
+
+ 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.
+
+-->
+<just>
+ <a>
+ <message>for testing purposes</messages>
+ </a>
+</just>