Author: davsclaus
Date: Sun Jul 13 05:31:43 2008
New Revision: 676309
URL: http://svn.apache.org/viewvc?rev=676309&view=rev
Log:
CAMEL-713: FileProducer had faulty code that would consume the file if it was a
out capable exchange. Introduced new header
FileComponent.HEADER_FILE_NAME_PRODUCED containing the absolute filepath for
the file produced.
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileHeaderFileNameProducedTest.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileMEPInOutTest.java
- copied, changed from r676219,
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/BeanToFileTest.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SimulatorTest.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java?rev=676309&r1=676308&r2=676309&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
Sun Jul 13 05:31:43 2008
@@ -30,8 +30,18 @@
* @version $Revision$
*/
public class FileComponent extends DefaultComponent<FileExchange> {
+
+ /**
+ * Header key hold the value: the fixed filename to use for prodcuing
files.
+ */
public static final String HEADER_FILE_NAME = "org.apache.camel.file.name";
+
+ /**
+ * Header key holding the value: absolute filepath for the file produced.
+ */
+ public static final String HEADER_FILE_NAME_PRODUCED =
"org.apache.camel.file.name.produced";
+
public FileComponent() {
}
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?rev=676309&r1=676308&r2=676309&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
Sun Jul 13 05:31:43 2008
@@ -57,13 +57,6 @@
}
public void process(FileExchange exchange) throws Exception {
- if (ExchangeHelper.isOutCapable(exchange)) {
- // lets poll the file
- Message out = exchange.getOut(true);
- endpoint.configureMessage(endpoint.getFile(), out);
- return;
- }
-
InputStream in = ExchangeHelper.getMandatoryInBody(exchange,
InputStream.class);
File file = createFileName(exchange.getIn());
buildDirectory(file);
@@ -128,8 +121,8 @@
}
}
- // TODO lets store the name in the header?
- //message.setHeader(FileComponent.HEADER_FILE_NAME, answer.toString());
+ // lets store the name we really used in the header, so end-users can
retrieve it
+ message.setHeader(FileComponent.HEADER_FILE_NAME_PRODUCED,
answer.getAbsolutePath());
return answer;
}
@@ -144,5 +137,4 @@
}
}
-
}
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileHeaderFileNameProducedTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileHeaderFileNameProducedTest.java?rev=676309&view=auto
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileHeaderFileNameProducedTest.java
(added)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileHeaderFileNameProducedTest.java
Sun Jul 13 05:31:43 2008
@@ -0,0 +1,51 @@
+/**
+ * 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.component.file;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test that tests that the header contains the absolute path to the
filename it used.
+ */
+public class FileHeaderFileNameProducedTest extends ContextTestSupport {
+
+ public void testHeaderFileNameProduced() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ // the absolute path to the file written should contain the target
folder
+
mock.message(0).header(FileComponent.HEADER_FILE_NAME_PRODUCED).contains("target");
+
+ template.requestBodyAndHeader("direct:in", "Hello World",
FileComponent.HEADER_FILE_NAME,
+ "FileHeaderFileNameProducedTest.txt");
+
+ mock.assertIsSatisfied();
+ }
+
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:in")
+ .to("file://target/?append=false")
+ .to("mock:result");
+ }
+ };
+ }
+
+}
\ No newline at end of file
Copied:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileMEPInOutTest.java
(from r676219,
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/BeanToFileTest.java)
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileMEPInOutTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileMEPInOutTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/BeanToFileTest.java&r1=676219&r2=676309&rev=676309&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/BeanToFileTest.java
(original)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileMEPInOutTest.java
Sun Jul 13 05:31:43 2008
@@ -17,50 +17,44 @@
package org.apache.camel.component.file;
import java.io.File;
-import javax.naming.Context;
import org.apache.camel.ContextTestSupport;
-import org.apache.camel.converter.IOConverter;
+import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.util.jndi.JndiContext;
+import org.apache.camel.converter.IOConverter;
/**
- * Unit test that we can chain bean and file producer.
+ * Unit test that we can produce files even for InOut MEP.
*/
-public class BeanToFileTest extends ContextTestSupport {
+public class FileMEPInOutTest extends ContextTestSupport {
- public void testBeanToFile() throws Exception {
- template.sendBody("direct:in", "World");
+ public void testMEPInOutTest() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+
+ // request is InOut
+ template.requestBodyAndHeader("direct:in", "Hello World",
FileComponent.HEADER_FILE_NAME,
+ "FileMEPInOutTest.txt");
// give Camel time to create the file
Thread.sleep(1000);
- File file = new File("target/BeanToFileTest.txt");
+ File file = new File("target/FileMEPInOutTest.txt");
file = file.getAbsoluteFile();
- assertEquals("Bye World", IOConverter.toString(file));
- }
+ assertEquals("Hello World", IOConverter.toString(file));
- protected Context createJndiContext() throws Exception {
- JndiContext answer = new JndiContext();
- answer.bind("myBean", new MyBean());
- return answer;
+ mock.assertIsSatisfied();
}
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
- from("direct:in").
- to("bean:myBean").
- setHeader(FileComponent.HEADER_FILE_NAME,
"BeanToFileTest.txt").
- to("file://target/?append=false");
+ from("direct:in")
+ .to("file://target/?append=false")
+ .to("mock:result");
}
};
}
- public static class MyBean {
- public String doSomething(String input) {
- return "Bye " + input;
- }
- }
-
-}
+}
\ No newline at end of file
Modified:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SimulatorTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SimulatorTest.java?rev=676309&r1=676308&r2=676309&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SimulatorTest.java
(original)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SimulatorTest.java
Sun Jul 13 05:31:43 2008
@@ -17,29 +17,40 @@
package org.apache.camel.processor;
+import javax.naming.Context;
+
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.util.ExchangeHelper;
import static org.apache.camel.language.simple.SimpleLanguage.simple;
+import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.jndi.JndiContext;
/**
* @version $Revision$
*/
public class SimulatorTest extends ContextTestSupport {
+ protected Context createJndiContext() throws Exception {
+ JndiContext answer = new JndiContext();
+ answer.bind("foo", new MyBean("foo"));
+ answer.bind("bar", new MyBean("bar"));
+ return answer;
+ }
+
public void testReceivesFooResponse() throws Exception {
- assertRespondsWith("foo", "<hello>foo</hello>");
+ assertRespondsWith("foo", "Bye said foo");
}
public void testReceivesBarResponse() throws Exception {
- assertRespondsWith("bar", "<hello>bar</hello>");
+ assertRespondsWith("bar", "Bye said bar");
}
- protected void assertRespondsWith(final String value, String
containedText) throws InvalidPayloadException {
+ protected void assertRespondsWith(final String value, String containedText)
+ throws InvalidPayloadException {
Exchange response = template.request("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
@@ -60,9 +71,21 @@
public void configure() {
// START SNIPPET: example
from("direct:a").
-
recipientList(simple("file:src/main/data/${in.headers.cheese}.xml"));
+ recipientList(simple("bean:${in.header.cheese}"));
// END SNIPPET: example
}
};
}
+
+ public static class MyBean {
+ private String value;
+
+ public MyBean(String value) {
+ this.value = value;
+ }
+
+ public String doSomething(String in) {
+ return "Bye said " + value;
+ }
+ }
}
\ No newline at end of file