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 2009/05/11 08:48:11 UTC

svn commit: r773478 - in /servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file: FilePollerEndpointTest.java FileSenderEndpointTest.java

Author: gertv
Date: Mon May 11 06:48:11 2009
New Revision: 773478

URL: http://svn.apache.org/viewvc?rev=773478&view=rev
Log:
SMXCOMP-525: Improve test coverage for servicemix-file

Added:
    servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java   (with props)
Modified:
    servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java

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=773478&r1=773477&r2=773478&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 Mon May 11 06:48:11 2009
@@ -41,7 +41,7 @@
 import org.apache.servicemix.util.FileUtil;
 
 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>();
@@ -53,16 +53,18 @@
         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() {
@@ -70,6 +72,7 @@
                     public InOnly createInOnlyExchange() throws MessagingException {
                         return new MockExchangeFactory.MockInOnly() {
                             private final String exchangeId = "id" + System.nanoTime();
+
                             @Override
                             public String getExchangeId() {
                                 return exchangeId;
@@ -82,13 +85,13 @@
         endpoint.setTargetService(new QName("urn:test", "service"));
         endpoint.setLockManager(new org.apache.servicemix.common.locks.impl.SimpleLockManager());
     }
-    
+
     public void testValidateNoFile() throws Exception {
         try {
             endpoint.validate();
             fail("validate() should throw an exception when file has not been set");
         } catch (DeploymentException e) {
-            //test succeeds
+            // test succeeds
         }
     }
 
@@ -101,14 +104,14 @@
             endpoint.validate();
             fail("validate() should throw an exception when archive doesn't refer to a directory");
         } catch (DeploymentException e) {
-            //test succeeds
+            // test succeeds
         } finally {
             if (archive != null) {
                 archive.delete();
             }
         }
     }
-    
+
     public void testValidateArchiveWithoutDelete() throws Exception {
         endpoint.setFile(DATA);
         endpoint.setArchive(ARCHIVE);
@@ -117,10 +120,10 @@
             endpoint.validate();
             fail("validate() should throw an exception when archive was set without delete");
         } catch (DeploymentException e) {
-            //test succeeds
+            // test succeeds
         }
     }
-    
+
     public void testProcessError() throws Exception {
         createTestFile();
         endpoint.setFile(DATA);
@@ -131,14 +134,14 @@
         try {
             endpoint.process(exchange);
         } catch (TestException e) {
-            //this is OK
+            // 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);
@@ -149,7 +152,7 @@
         endpoint.process(exchange);
         assertFalse(file.exists());
     }
-    
+
     private void createTestFile() throws IOException {
         DATA.mkdirs();
         InputStream fis = new FileInputStream("target/test-classes/test-data.xml");
@@ -158,11 +161,24 @@
         fis.close();
         fos.close();
     }
-    
+
+    public void testMoveFileToNonExistentDirectory() throws Exception {
+        File srcFile = File.createTempFile("poller-test-", ".tmp");
+        try {
+            FilePollerEndpoint.moveFile(srcFile, new File("bogus"));
+            fail("moveFile() should fail when moving to non-existent directory");
+        } catch (IOException ioe) {
+            // test succeeds
+        } finally {
+            srcFile.delete();
+        }
+    }
+
+    @SuppressWarnings("serial")
     private static class TestException extends Exception {
-        //nothing to do here
+        // nothing to do here
     }
-    
+
     private static class MockExecutor implements Executor {
 
         public int capacity() {
@@ -173,13 +189,12 @@
             command.run();
         }
 
-        public void shutdown() {  
-            //graciously do nothing
+        public void shutdown() {
+            // graciously do nothing
         }
 
         public int size() {
             return 0;
         }
-        
     }
 }

Added: servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java?rev=773478&view=auto
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java (added)
+++ servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java Mon May 11 06:48:11 2009
@@ -0,0 +1,243 @@
+/*
+ * 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.servicemix.file;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.jbi.management.DeploymentException;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.framework.ComponentNameSpace;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
+import org.apache.servicemix.util.FileUtil;
+import org.apache.servicemix.tck.mock.MockExchangeFactory;
+
+public class FileSenderEndpointTest extends TestCase {
+
+	private static final File OUT_DIR = new File("target/file-test");
+	private static final File OUT_FILE = new File(OUT_DIR, "file-exists.tmp");
+    public static final String FILE_NAME_PROPERTY = "org.apache.servicemix.file.name";
+    public static final String FILE_PATH_PROPERTY = "org.apache.servicemix.file.path";
+    private FileSenderEndpoint endpoint;
+    private FileComponent component;
+    private ComponentNameSpace cns;
+    private InternalEndpoint ie;
+
+    public FileSenderEndpointTest(final String name) {
+        super(name);
+    }
+
+    protected final void setUp() throws Exception {
+        super.setUp();
+        component = new FileComponent();
+        cns = new ComponentNameSpace("myContainer", "myName");
+        ie = new InternalEndpoint(cns, "myEndpoint", new QName("myService"));
+        endpoint = new FileSenderEndpoint(component, ie);
+    }
+
+    protected final void tearDown() throws Exception {
+        super.tearDown();
+
+        component = null;
+        cns = null;
+        ie = null;
+        endpoint = null;
+    }
+
+    // Test when no directory has been set on the endpoint.
+    public final void testValidateNoDirectory() throws Exception {
+        try {
+            endpoint.validate();
+            fail("validate() should fail when no directory is specified.");
+        } catch (DeploymentException de) {
+            // test succeeds
+        }
+    }
+
+    // Test when directory specified is not a directory.
+    public final void testValidateIsNotDirectory() throws Exception {
+        File tempFile = null;
+        try {
+            tempFile = File.createTempFile("servicemix", "test");
+            endpoint.setDirectory(tempFile);
+            endpoint.validate();
+            fail("validate() should fail when setDirectory is set to a file.");
+        } catch (DeploymentException de) {
+            // test succeeds
+        } finally {
+            if (tempFile != null) {
+                tempFile.delete();
+            }
+        }
+    }
+    
+    public final void testValidateOverwriteAndAppend() throws Exception {
+    	endpoint.setDirectory(OUT_DIR);
+    	endpoint.setAppend(true);
+    	endpoint.setOverwrite(true);
+    	try {
+    		endpoint.validate();
+    		fail("validate() should fail when isAppend and isOverwrite are both true.");
+    	} catch (DeploymentException de) {
+    		// test succeeds
+    	}
+    }
+
+    // Test when the output file does not exist.
+    public final void testProcessInOnlyNewFile()
+        throws Exception {
+        MockExchangeFactory mef = new MockExchangeFactory();
+        MessageExchange me = mef.createInOnlyExchange();
+        me.setOperation(new QName("uri", "op"));
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage msg = me.createMessage();
+        msg.setProperty("myMsgProp", "myMsgValue");
+        msg.setContent(new StringSource("<input>input message</input>"));
+        endpoint.setDirectory(OUT_DIR);
+        endpoint.setAutoCreateDirectory(true);
+        endpoint.validate();
+
+        endpoint.processInOnly(me, msg);
+
+        // Check to see if a file was written to the directory.
+        File[] fileList = endpoint.getDirectory().listFiles();
+        assertTrue("Directory should not be empty", fileList != null);
+
+        // cleanup
+        FileUtil.deleteFile(endpoint.getDirectory());
+    }
+    
+    // Test when output file exists but neither append nor overwrite are set.
+    public final void testProcessInOnlyFileExistsNoAppendNoOverwrite()
+        throws Exception {
+        MockExchangeFactory mef = new MockExchangeFactory();
+        MessageExchange me = mef.createInOnlyExchange();
+        me.setOperation(new QName("uri", "op"));
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage msg = me.createMessage();
+        msg.setProperty(FILE_PATH_PROPERTY, OUT_FILE.getAbsolutePath());
+        msg.setProperty(FILE_NAME_PROPERTY, OUT_FILE.getName());
+        msg.setContent(new StringSource("<input>input message</input>"));
+        endpoint.setDirectory(OUT_DIR);
+        endpoint.setAutoCreateDirectory(true);
+        endpoint.validate();
+        
+        // Create the initial file for later use.
+        endpoint.processInOnly(me, msg);
+        
+        endpoint.setAppend(false);
+        endpoint.setOverwrite(false);
+        
+        try {
+        	endpoint.processInOnly(me, msg);
+        	fail("processInOnly() should fail when file exists but append is false and " +
+        			"overwrite is false");
+        } catch (IOException ioe) {
+        	// test succeeds - file exists but cannot be appended to or overwritten
+        } finally {
+        	FileUtil.deleteFile(OUT_FILE);
+        }
+    }
+    
+    // Test when output file exists and overwrite is true.
+    public final void testProcessInOnlyFileExistsOverwrite() throws Exception {
+        MockExchangeFactory mef = new MockExchangeFactory();
+        MessageExchange me = mef.createInOnlyExchange();
+        me.setOperation(new QName("uri", "op"));
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage msg = me.createMessage();
+        msg.setProperty(FILE_PATH_PROPERTY, OUT_FILE.getAbsolutePath());
+        msg.setProperty(FILE_NAME_PROPERTY, OUT_FILE.getName());
+        msg.setContent(new StringSource("<input>input message</input>"));
+        endpoint.setDirectory(OUT_DIR);
+        endpoint.setAutoCreateDirectory(true);
+        endpoint.validate();
+        
+        // Create the initial file for later use.
+        endpoint.processInOnly(me, msg);
+        
+        long fileLength = OUT_FILE.length();
+        
+        endpoint.setOverwrite(true);
+        endpoint.setAppend(false);
+        
+        endpoint.processInOnly(me, msg);
+        
+        assertTrue("File was not overwritten: " + OUT_FILE.getAbsolutePath(), OUT_FILE.length() == fileLength);
+        
+        // clean up
+        FileUtil.deleteFile(OUT_FILE);
+    }
+
+    // Test when output file exists and append is true.
+    public final void testProcessInOnlyFileExistsAppend() throws Exception {
+        MockExchangeFactory mef = new MockExchangeFactory();
+        MessageExchange me = mef.createInOnlyExchange();
+        me.setOperation(new QName("uri", "op"));
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage msg = me.createMessage();
+        msg.setProperty(FILE_PATH_PROPERTY, OUT_FILE.getAbsolutePath());
+        msg.setProperty(FILE_NAME_PROPERTY, OUT_FILE.getName());
+        msg.setContent(new StringSource("<input>input message</input>"));
+        endpoint.setDirectory(OUT_DIR);
+        endpoint.setAutoCreateDirectory(true);
+        endpoint.validate();
+        
+        // Create the initial file for later use.
+        endpoint.processInOnly(me, msg);
+        
+        long fileLength = OUT_FILE.length();
+        
+        endpoint.setOverwrite(false);
+        endpoint.setAppend(true);
+        
+        endpoint.processInOnly(me, msg);
+        
+        assertTrue("File was not overwritten: " + OUT_FILE.getAbsolutePath(), OUT_FILE.length() > fileLength);
+        
+        // clean up
+        FileUtil.deleteFile(OUT_FILE);
+    }
+    
+    // Test when calling processInOut - not supported.
+    public final void testProcessInOutNotSupported() throws Exception {
+        MockExchangeFactory mef = new MockExchangeFactory();
+        MessageExchange me = mef.createInOutExchange();
+        me.setOperation(new QName("uri", "op"));
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage inMsg = me.createMessage();
+        inMsg.setProperty("myMsgProp", "myMsgValue");
+        inMsg.setContent(new StringSource("<input>input message</input>"));
+        NormalizedMessage outMsg = me.createMessage();
+        outMsg.setContent(new StringSource("<output>output message</output>"));
+        endpoint.setDirectory(new File("target/test"));
+
+        try {
+            endpoint.processInOut(me, inMsg, outMsg);
+            fail("processInOut is an unsupported operation");
+        } catch (UnsupportedOperationException uoe) {
+            // test succeeds
+        }
+    }
+
+}

Propchange: servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/FileSenderEndpointTest.java
------------------------------------------------------------------------------
    svn:eol-style = native