Author: davsclaus
Date: Wed May 23 09:55:39 2012
New Revision: 1341811
URL: http://svn.apache.org/viewvc?rev=1341811&view=rev
Log:
CAMEL-5300: Flatpack now throws exception if failure during parsing. Thanks to
Chris Geer for the patch.
Added:
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackException.java
camel/trunk/components/camel-flatpack/src/test/data/fixedLong/
camel/trunk/components/camel-flatpack/src/test/data/fixedLong/PEOPLE-FixedLength.txt
camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/InvalidFixedLengthTest.java
camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/InvalidFixedLengthTest-context.xml
Modified:
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackProducer.java
Added:
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackException.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackException.java?rev=1341811&view=auto
==============================================================================
---
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackException.java
(added)
+++
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackException.java
Wed May 23 09:55:39 2012
@@ -0,0 +1,55 @@
+/**
+ * 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.flatpack;
+
+import java.util.List;
+
+import net.sf.flatpack.DataError;
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+
+/**
+ * Flatpack exception.
+ */
+public class FlatpackException extends CamelExchangeException {
+
+ private final List<DataError> errors;
+
+ public FlatpackException(String message, Exchange exchange,
List<DataError> errors) {
+ super(message, exchange);
+ this.errors = errors;
+ }
+
+ public List<DataError> getErrors() {
+ return errors;
+ }
+
+ @Override
+ public String getMessage() {
+ StringBuffer sb = new StringBuffer();
+ sb.append(super.getMessage());
+ if (errors != null && !errors.isEmpty()) {
+ sb.append("\n");
+ for (Object error : errors) {
+ DataError e = (DataError) error;
+ sb.append(e.toString());
+ }
+ }
+ return sb.toString();
+ }
+
+}
Modified:
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackProducer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackProducer.java?rev=1341811&r1=1341810&r2=1341811&view=diff
==============================================================================
---
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackProducer.java
(original)
+++
camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackProducer.java
Wed May 23 09:55:39 2012
@@ -16,13 +16,14 @@
*/
package org.apache.camel.component.flatpack;
+import net.sf.flatpack.DataError;
import net.sf.flatpack.DataSet;
import net.sf.flatpack.Parser;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
/**
- * @version
+ * @version
*/
class FlatpackProducer extends DefaultProducer {
private FixedLengthEndpoint endpoint;
@@ -32,10 +33,17 @@ class FlatpackProducer extends DefaultPr
this.endpoint = endpoint;
}
+ @SuppressWarnings("unchecked")
public void process(Exchange exchange) throws Exception {
Parser parser = endpoint.createParser(exchange);
DataSet dataSet = parser.parse();
+ if (dataSet.getErrorCount() > 0) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(String.format("Flatpack has found %s errors while
parsing", dataSet.getErrorCount()));
+ throw new FlatpackException(sb.toString(), exchange,
dataSet.getErrors());
+ }
+
if (endpoint.isSplitRows()) {
int counter = 0;
while (dataSet.next()) {
Added:
camel/trunk/components/camel-flatpack/src/test/data/fixedLong/PEOPLE-FixedLength.txt
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/data/fixedLong/PEOPLE-FixedLength.txt?rev=1341811&view=auto
==============================================================================
---
camel/trunk/components/camel-flatpack/src/test/data/fixedLong/PEOPLE-FixedLength.txt
(added)
+++
camel/trunk/components/camel-flatpack/src/test/data/fixedLong/PEOPLE-FixedLength.txt
Wed May 23 09:55:39 2012
@@ -0,0 +1,4 @@
+JOHN DOE 1234
CIRCLE CT
ELYRIA
OH44035*
+JIMMY SMITH 180 SOME
ST
AVON
OH44011*
+JANE DOE 111
MILKY WY
AMHERST
OH44001*
+FRED FLINTSTONE 123
ROCKY WY
BEDROCK
AZ12345*
Added:
camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/InvalidFixedLengthTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/InvalidFixedLengthTest.java?rev=1341811&view=auto
==============================================================================
---
camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/InvalidFixedLengthTest.java
(added)
+++
camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/InvalidFixedLengthTest.java
Wed May 23 09:55:39 2012
@@ -0,0 +1,57 @@
+/**
+ * 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.flatpack;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.context.ContextConfiguration;
+import
org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.util.Assert;
+
+/**
+ * @version
+ */
+@ContextConfiguration
+public class InvalidFixedLengthTest extends AbstractJUnit4SpringContextTests {
+ private static final transient Logger LOG =
LoggerFactory.getLogger(FixedLengthTest.class);
+
+ @EndpointInject(uri = "mock:results")
+ protected MockEndpoint results;
+
+ @EndpointInject(uri = "mock:error")
+ protected MockEndpoint error;
+
+ @Test
+ public void testCamel() throws Exception {
+ results.expectedMessageCount(0);
+ results.assertIsSatisfied();
+
+ error.expectedMessageCount(1);
+ error.assertIsSatisfied();
+
+ Exchange e = error.getReceivedExchanges().get(0);
+ FlatpackException cause = e.getProperty(Exchange.EXCEPTION_CAUGHT,
FlatpackException.class);
+ Assert.notNull(cause);
+
+ Assert.hasText("Flatpack has found 4 errors while parsing.
Exchange[PEOPLE-FixedLength.txt]", cause.getMessage());
+ Assert.hasText("Line:4 Level:2 Desc:LINE TOO LONG. LINE IS 278 LONG.
SHOULD BE 277", cause.getMessage());
+ }
+}
Added:
camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/InvalidFixedLengthTest-context.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/InvalidFixedLengthTest-context.xml?rev=1341811&view=auto
==============================================================================
---
camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/InvalidFixedLengthTest-context.xml
(added)
+++
camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/InvalidFixedLengthTest-context.xml
Wed May 23 09:55:39 2012
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
+ ">
+
+ <!-- START SNIPPET: example -->
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+ <onException>
+
<exception>org.apache.camel.component.flatpack.FlatpackException</exception>
+ <redeliveryPolicy maximumRedeliveries="1"/>
+ <handled>
+ <constant>true</constant>
+ </handled>
+ <to uri="mock:error"/>
+ </onException>
+
+ <route>
+ <from uri="file://src/test/data/fixedLong?noop=true"/>
+ <to uri="flatpack:fixed:PEOPLE-FixedLength.pzmap.xml"/>
+ </route>
+
+ <route>
+ <from uri="flatpack:fixed:PEOPLE-FixedLength.pzmap.xml"/>
+ <convertBodyTo type="java.util.Map"/>
+ <to uri="mock:results"/>
+ </route>
+ </camelContext>
+ <!-- END SNIPPET: example -->
+
+</beans>
\ No newline at end of file