Permalink
Browse files

Reworked unit testing to remove the requirment for a extrenal CIFS se…

…rver, moved these tests into integration test.
  • Loading branch information...
1 parent 124f0b3 commit dc4cb0972426f4113905049aff93c871dce83a8a @ullgren ullgren committed Jul 18, 2012
Showing with 621 additions and 97 deletions.
  1. +5 −3 README.md
  2. +21 −1 pom.xml
  3. BIN src/integrationtest/data/logo1.png
  4. BIN src/integrationtest/data/logo2.png
  5. +45 −0 src/integrationtest/java/com/redpill_linpro/component/smb/BaseSmbTestSupport.java
  6. +31 −0 src/integrationtest/java/com/redpill_linpro/component/smb/FromFileToSmbTest.java
  7. +56 −0 src/integrationtest/java/com/redpill_linpro/component/smb/FromSmbMoveFileTest.java
  8. +61 −0 src/integrationtest/java/com/redpill_linpro/component/smb/FromSmbToAsciiFileTest.java
  9. 0 src/{test → integrationtest}/resources/camelsmb.prp.template
  10. +22 −0 src/integrationtest/resources/log4j.properties
  11. +30 −0 src/main/java/com/redpill_linpro/component/smb/JcifsSmbApiFactory.java
  12. +23 −0 src/main/java/com/redpill_linpro/component/smb/SmbApiFactory.java
  13. +21 −11 src/main/java/com/redpill_linpro/component/smb/SmbClient.java
  14. +9 −2 src/main/java/com/redpill_linpro/component/smb/SmbComponent.java
  15. +11 −1 src/main/java/com/redpill_linpro/component/smb/SmbConfiguration.java
  16. +3 −5 src/main/java/com/redpill_linpro/component/smb/SmbEndpoint.java
  17. +23 −19 src/test/java/com/redpill_linpro/component/smb/BaseSmbTestSupport.java
  18. +58 −4 src/test/java/com/redpill_linpro/component/smb/FromFileToSmbTest.java
  19. +86 −30 src/test/java/com/redpill_linpro/component/smb/FromSmbMoveFileTest.java
  20. +64 −21 src/test/java/com/redpill_linpro/component/smb/FromSmbToAsciiFileTest.java
  21. +52 −0 src/test/java/com/redpill_linpro/component/smb/test/StubFileSmbApiFactory.java
View
@@ -6,9 +6,11 @@ It was originally developed by Redpill Linpro AB and Helsingborgs stad as part o
This component is licensed under the LGPL (http://www.gnu.org/licenses/lgpl-2.1.txt)
-Since we could not find a easy to integrate CIFS server implemented in Java the unit testing
-currently requires that a local CIFS server (Samba or MS Windows share).
-Also copy camelsmb.prp.template to your home directory, rename it to camelsmb.prp" and edit
+The unit testing is done by mocking out the jcifs library to be able to run automaticly.
+
+There are some manuell integration tests (src/integrationtest) that are meant to be run with a a
+local CIFS server (Samba or MS Windows share).
+To run these copy camelsmb.prp.template to your home directory, rename it to camelsmb.prp" and edit
to include correct details.
Contributor
View
22 pom.xml
@@ -18,7 +18,7 @@
<groupId>com.redpill_linpro.component.smb</groupId>
<artifactId>camel-smb</artifactId>
<packaging>jar</packaging>
- <version>2.9.0-1.0</version>
+ <version>2.9.0-1.1</version>
<name>A Camel SMB Component</name>
<url>https://github.com/Redpill-Linpro/camel-smb</url>
@@ -55,11 +55,31 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>3.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>cglib</groupId>
+ <artifactId>cglib</artifactId>
+ <version>2.2.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.objenesis</groupId>
+ <artifactId>objenesis</artifactId>
+ <version>1.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
+
+
</dependencies>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,45 @@
+package com.redpill_linpro.component.smb;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.Properties;
+
+import org.apache.camel.test.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Before;
+
+public class BaseSmbTestSupport extends CamelTestSupport {
+
+ private static Properties properties;
+
+ @Before
+ public void setUp() throws Exception {
+ properties = new Properties();
+ File configFile = new File(System.getProperty("user.home")+File.separator+"camelsmb.prp");
+ if ( !configFile.exists() ) {
+ Assert.fail("Copy src/test/resources/camelsmb.prp.template to " +System.getProperty("user.home")+File.separator+"camelsmb.prp and edit for correct details.");
+ }
+ properties.load(new FileInputStream(configFile));
+ super.setUp();
+ }
+
+ public String getUsername() {
+ return properties.getProperty("username");
+ }
+
+ public String getDomain() {
+ return properties.getProperty("domain");
+ }
+
+ public String getPassword() {
+ return properties.getProperty("password");
+ }
+
+ public String getShare() {
+ return properties.getProperty("share");
+ }
+
+ public String getLocalSharePath() {
+ return properties.getProperty("localpath");
+ }
+}
@@ -0,0 +1,31 @@
+package com.redpill_linpro.component.smb;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ * Unit test to verify that we can pool an ASCII file from a local file path and store it on the SMB Server
+ */
+public class FromFileToSmbTest extends BaseSmbTestSupport {
+ private String getSmbUrl() {
+ return "smb://"+getDomain()+";"+getUsername()+"@localhost/"+getShare()+"/camel/"+getClass().getSimpleName()+"?password="+getPassword()+"&fileExist=Override";
+ }
+
+ @Test
+ public void testFromFileToSmb() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(2);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from(getSmbUrl()).to("mock:result");
+ from("file:src/test/data?noop=true&consumer.delay=3000").to(getSmbUrl());
+ }
+ };
+ }
+}
@@ -0,0 +1,56 @@
+package com.redpill_linpro.component.smb;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to test both consumer.moveNamePrefix and consumer.moveNamePostfix options.
+ */
+public class FromSmbMoveFileTest extends BaseSmbTestSupport {
+ private String getSmbUrl() {
+ return "smb://"+getDomain()+";"+getUsername()+"@localhost/"+getShare()+"/camel/"
+ +getClass().getSimpleName()+"?password="+getPassword()
+ +"&move=done/sub2/${file:name}.old&consumer.delay=5000";
+ }
+
+ @Test
+ public void testPollFileAndShouldBeMoved() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World this file will be moved");
+ mock.expectedFileExists(getLocalSharePath() + "/camel/"
+ +getClass().getSimpleName()+"/done/sub2/hello.txt.old");
+
+ mock.assertIsSatisfied();
+ }
+
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ // prepares the FTP Server by creating a file on the server that we want to unit
+ // test that we can pool and store as a local file
+ Endpoint endpoint = context.getEndpoint(getSmbUrl());
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World this file will be moved");
+ exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from(getSmbUrl()).to("mock:result");
+ }
+ };
+ }
+}
@@ -0,0 +1,61 @@
+package com.redpill_linpro.component.smb;
+
+import java.io.File;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to verify that we can pool an ASCII file from the SMB Server and store it on a local file path
+ */
+public class FromSmbToAsciiFileTest extends BaseSmbTestSupport {
+
+ private String getSmbUrl() {
+ return "smb://"+getDomain()+";"+getUsername()+"@localhost/"+getShare()+"/camel/"+getClass().getSimpleName()+"?password="+getPassword()+"&fileExist=Override";
+ }
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ // prepares the FTP Server by creating a file on the server that we want to unit
+ // test that we can pool and store as a local file
+ Endpoint endpoint = context.getEndpoint(getSmbUrl());
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World from SMBServer");
+ exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ }
+
+ @Test
+ public void testFromSmbToFile() throws Exception {
+ MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
+ resultEndpoint.expectedMinimumMessageCount(1);
+ resultEndpoint.expectedBodiesReceived("Hello World from SMBServer");
+
+ resultEndpoint.assertIsSatisfied();
+
+ // assert the file
+ File file = new File("target/smbtest/deleteme.txt");
+ assertTrue("The ASCII file should exists", file.exists());
+ assertTrue("File size wrong", file.length() > 10);
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ String fileUrl = "file:target/smbtest/?fileExist=Override&noop=true";
+ from(getSmbUrl()).setHeader(Exchange.FILE_NAME, constant("deleteme.txt")).
+ convertBodyTo(String.class).to(fileUrl).to("mock:result");
+ }
+ };
+ }
+}
@@ -0,0 +1,22 @@
+
+#
+# The logging properties used for eclipse testing, We want to see debug output on the console.
+#
+log4j.rootLogger=INFO, out
+
+# uncomment the following line to turn on Camel debugging
+log4j.logger.org.apache.camel=DEBUG
+
+# uncomment the following line to turn on ActiveMQ debugging
+#log4j.logger.org.apache.activemq=DEBUG
+
+log4j.logger.org.springframework=DEBUG
+
+
+# CONSOLE appender not used by default
+log4j.appender.out=org.apache.log4j.ConsoleAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
+#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+log4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer
@@ -0,0 +1,30 @@
+package com.redpill_linpro.component.smb;
+
+import java.net.MalformedURLException;
+import java.net.UnknownHostException;
+
+import jcifs.smb.NtlmPasswordAuthentication;
+import jcifs.smb.SmbException;
+import jcifs.smb.SmbFile;
+import jcifs.smb.SmbFileOutputStream;
+
+/**
+ * Default implementation of the the {@link SmbApiFactory}
+ *
+ * @author Pontus Ullgren
+ *
+ */
+public class JcifsSmbApiFactory implements SmbApiFactory {
+
+ public SmbFile createSmbFile(String url,
+ NtlmPasswordAuthentication authentication)
+ throws MalformedURLException, SmbException {
+ return new SmbFile(url, authentication);
+ }
+
+ public SmbFileOutputStream createSmbFileOutputStream(SmbFile smbFile,
+ boolean b) throws SmbException, MalformedURLException, UnknownHostException {
+ return new SmbFileOutputStream(smbFile, b);
+ }
+
+}
@@ -0,0 +1,23 @@
+package com.redpill_linpro.component.smb;
+
+import java.net.MalformedURLException;
+import java.net.UnknownHostException;
+
+import jcifs.smb.NtlmPasswordAuthentication;
+import jcifs.smb.SmbException;
+import jcifs.smb.SmbFile;
+import jcifs.smb.SmbFileOutputStream;
+
+/**
+ * Factory interface for creating jcifs API objects.
+ *
+ * @author Pontus Ullgren
+ *
+ */
+public interface SmbApiFactory {
+
+ SmbFile createSmbFile(String url, NtlmPasswordAuthentication authentication) throws MalformedURLException, SmbException;
+
+ SmbFileOutputStream createSmbFileOutputStream(SmbFile smbFile, boolean b) throws SmbException, MalformedURLException, UnknownHostException;
+
+}
Oops, something went wrong.

0 comments on commit dc4cb09

Please sign in to comment.