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 17:16:54 UTC

svn commit: r773597 - in /servicemix/components/bindings/servicemix-file/trunk/src/test: java/org/apache/servicemix/file/PollDirectoryTest.java resources/spring-polling.xml

Author: gertv
Date: Mon May 11 15:16:52 2009
New Revision: 773597

URL: http://svn.apache.org/viewvc?rev=773597&view=rev
Log:
SMXCOMP-534: Two new tests for the servicemix-file sender endpoint

Modified:
    servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
    servicemix/components/bindings/servicemix-file/trunk/src/test/resources/spring-polling.xml

Modified: servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java?rev=773597&r1=773596&r2=773597&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java (original)
+++ servicemix/components/bindings/servicemix-file/trunk/src/test/java/org/apache/servicemix/file/PollDirectoryTest.java Mon May 11 15:16:52 2009
@@ -16,7 +16,12 @@
  */
 package org.apache.servicemix.file;
 
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.InOnly;
@@ -97,6 +102,64 @@
         numFiles = dir.listFiles().length;
         assertEquals("There shouldn't be any files in the poll folder...but I found " + numFiles + " files there..." , 0, numFiles);
     }
+
+    // Testing the "append=false" 
+    public void testSendToWriterNotAppend() throws Exception {
+        FileUtil.deleteFile(new File("target/pollerFilesNotAppend"));
+     
+        // now lets make a request on this endpoint
+        DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
+
+        // lets send a request to be written to a file
+        InOnly me = client.createInOnlyExchange();
+        me.setService(new QName("urn:test", "serviceNotAppend"));
+        NormalizedMessage message = me.getInMessage();
+        message.setProperty(DefaultFileMarshaler.FILE_NAME_PROPERTY, "test.xml");
+        message.setContent(new StringSource("<hello>world</hello>"));
+        client.sendSync(me);
+
+        // send a second request so that it should overwrite the first 
+        // in the test.xml file
+        InOnly me2 = client.createInOnlyExchange();
+        me2.setService(new QName("urn:test", "serviceNotAppend"));
+        NormalizedMessage message2 = me2.getInMessage();
+        message2.setProperty(DefaultFileMarshaler.FILE_NAME_PROPERTY, "test.xml");
+        message2.setContent(new StringSource("<Goodbye>world</Goodbye>"));
+        client.sendSync(me2);            
+
+        File resultFile = new File("target/pollerFilesNotAppend/test.xml");
+        String content = getFileContent(resultFile);
+        assertEquals(content.indexOf("hello"), -1);
+    }
+        
+    // Testing the "append=true" 
+    public void testSendToWriterAppend() throws Exception {
+        FileUtil.deleteFile(new File("target/pollerFilesAppend"));
+     
+        // now lets make a request on this endpoint
+        DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
+
+        // lets send a request to be written to a file        
+        InOnly me = client.createInOnlyExchange();
+        me.setService(new QName("urn:test", "serviceAppend"));
+        NormalizedMessage message = me.getInMessage();
+        message.setProperty(DefaultFileMarshaler.FILE_NAME_PROPERTY, "test.xml");
+        message.setContent(new StringSource("<hello>world</hello>"));
+        client.sendSync(me);
+
+        // send a second request so that it append to the first 
+        // request in the test.xml file
+        InOnly me2 = client.createInOnlyExchange();
+        me2.setService(new QName("urn:test", "serviceAppend"));
+        NormalizedMessage message2 = me2.getInMessage();
+        message2.setProperty(DefaultFileMarshaler.FILE_NAME_PROPERTY, "test.xml");
+        message2.setContent(new StringSource("<Goodbye>world</Goodbye>"));
+        client.sendSync(me2);            
+
+        File resultFile = new File("target/pollerFilesAppend/test.xml");       
+        String content = getFileContent(resultFile);
+        assertTrue(content.indexOf("hello") != 0);
+    }    
    
     protected void assertExchangeWorked(MessageExchange me) throws Exception {
         if (me.getStatus() == ExchangeStatus.ERROR) {
@@ -114,4 +177,34 @@
         return new ClassPathXmlApplicationContext("spring-polling.xml");
     }
 
+    private String getFileContent(File file) {
+        FileInputStream fis = null;
+        BufferedInputStream bis = null;
+        DataInputStream dis = null;
+        String str = "";
+
+        try {
+            fis = new FileInputStream(file);
+            // Here BufferedInputStream is added for fast reading.
+            bis = new BufferedInputStream(fis);
+            dis = new DataInputStream(bis);
+
+            // dis.available() returns 0 if the file does not have more lines.
+            while (dis.available() != 0) {
+                // this statement reads the line from the file and print it to
+                // the console.
+                str = str.concat(dis.readLine());
+            }  
+            // dispose all the resources after using them.
+            fis.close();
+            bis.close();
+            dis.close();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return str;
+    }            
+
 }

Modified: servicemix/components/bindings/servicemix-file/trunk/src/test/resources/spring-polling.xml
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-file/trunk/src/test/resources/spring-polling.xml?rev=773597&r1=773596&r2=773597&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-file/trunk/src/test/resources/spring-polling.xml (original)
+++ servicemix/components/bindings/servicemix-file/trunk/src/test/resources/spring-polling.xml Mon May 11 15:16:52 2009
@@ -66,7 +66,27 @@
           <bean class="org.apache.servicemix.tck.ReceiverComponent" />
         </sm:component>
       </sm:activationSpec>
-      
+
+      <sm:activationSpec>
+        <sm:component>
+          <file:component>
+            <file:endpoints>
+              <file:sender service="test:serviceNotAppend"
+                           endpoint="endpoint"
+                           directory="file:target/pollerFilesNotAppend"
+                           append="false"
+                           autoCreateDirectory="true"/>
+              
+              <file:sender service="test:serviceAppend"
+                           endpoint="endpoint"
+                           directory="file:target/pollerFilesAppend" 
+                           append="true"
+                           autoCreateDirectory="true"/>
+            </file:endpoints>
+          </file:component>
+        </sm:component>
+      </sm:activationSpec>      
+
     </sm:activationSpecs>
   </sm:container>