You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/05/14 11:04:39 UTC
git commit: CAMEL-6359: Bindy now supports enums. Thanks to Daniel
Lorych for the patch.
Updated Branches:
refs/heads/master deb0b3bfa -> 00bdbfc7a
CAMEL-6359: Bindy now supports enums. Thanks to Daniel Lorych for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/00bdbfc7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/00bdbfc7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/00bdbfc7
Branch: refs/heads/master
Commit: 00bdbfc7a667c40584cf66bf33030552e7e43e68
Parents: deb0b3b
Author: Claus Ibsen <da...@apache.org>
Authored: Tue May 14 11:03:52 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue May 14 11:03:52 2013 +0200
----------------------------------------------------------------------
.../camel/dataformat/bindy/FormatFactory.java | 6 +-
.../camel/dataformat/bindy/format/EnumFormat.java | 36 ++++++
.../csv/BindyCarQuoteAndCommaDelimiterTest.java | 92 ++++++++++++---
.../camel/dataformat/bindy/model/car/Car.java | 13 ++-
4 files changed, 124 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/00bdbfc7/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
index 312f4b7..6e18f62 100755
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
@@ -32,6 +32,7 @@ import org.apache.camel.dataformat.bindy.format.CharacterFormat;
import org.apache.camel.dataformat.bindy.format.DatePatternFormat;
import org.apache.camel.dataformat.bindy.format.DoubleFormat;
import org.apache.camel.dataformat.bindy.format.DoublePatternFormat;
+import org.apache.camel.dataformat.bindy.format.EnumFormat;
import org.apache.camel.dataformat.bindy.format.FloatFormat;
import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
import org.apache.camel.dataformat.bindy.format.IntegerFormat;
@@ -54,7 +55,7 @@ public final class FormatFactory {
/**
* Retrieves the format to use for the given type
- *
+ *
* @param clazz represents the type of the format (String, Integer, Byte)
* @param pattern is the pattern to be used during the formatting of the data
* @param locale optional locale for NumberFormat and DateFormat parsing.
@@ -63,6 +64,7 @@ public final class FormatFactory {
* @return Format the formatter
* @throws IllegalArgumentException if not suitable formatter is found
*/
+ @SuppressWarnings("unchcecked")
private static Format<?> doGetFormat(Class<?> clazz, String pattern, String locale, int precision, boolean impliedDecimalSeparator) throws Exception {
if (clazz == byte.class || clazz == Byte.class) {
return ObjectHelper.isNotEmpty(pattern)
@@ -98,6 +100,8 @@ public final class FormatFactory {
return new DatePatternFormat(pattern, getLocale(locale));
} else if (clazz == char.class || clazz == Character.class) {
return new CharacterFormat();
+ } else if (clazz.isEnum()) {
+ return new EnumFormat(clazz);
} else {
throw new IllegalArgumentException("Can not find a suitable formatter for the type: " + clazz.getCanonicalName());
}
http://git-wip-us.apache.org/repos/asf/camel/blob/00bdbfc7/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/EnumFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/EnumFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/EnumFormat.java
new file mode 100644
index 0000000..4245222
--- /dev/null
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/EnumFormat.java
@@ -0,0 +1,36 @@
+/**
+ * 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.dataformat.bindy.format;
+
+import org.apache.camel.dataformat.bindy.Format;
+
+public class EnumFormat<T extends Enum<T>> implements Format<T> {
+
+ private final Class<T> clazz;
+
+ public EnumFormat(Class<T> clazz) {
+ this.clazz = clazz;
+ }
+
+ public String format(final T object) throws Exception {
+ return object.name();
+ }
+
+ public T parse(final String string) throws Exception {
+ return Enum.valueOf(clazz, string);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/00bdbfc7/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyCarQuoteAndCommaDelimiterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyCarQuoteAndCommaDelimiterTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyCarQuoteAndCommaDelimiterTest.java
index 6c960e5..5a4d051 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyCarQuoteAndCommaDelimiterTest.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyCarQuoteAndCommaDelimiterTest.java
@@ -22,32 +22,33 @@ import java.util.Map;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.dataformat.bindy.model.car.Car;
+import org.apache.camel.dataformat.bindy.model.car.Car.Colour;
import org.apache.camel.model.dataformat.BindyType;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
/**
- * @version
+ * @version
*/
public class BindyCarQuoteAndCommaDelimiterTest extends CamelTestSupport {
+ private static final String HEADER = "\"stockid\";\"make\";\"model\";\"deriv\";\"series\";\"registration\";\"chassis\";\"engine\";\"year\""
+ + ";\"klms\";\"body\";\"colour\";\"enginesize\";\"trans\";\"fuel\";\"options\";\"desc\";\"status\";\"Reserve_price\";\"nvic\"";
+ private static final String ROW = "\"SS552\";\"TOYOTA\";\"KLUGER\";\"CV 4X4\";\"MCU28R UPGRADE\";\"TBA\";\"\";\"\";\"2005\";\"155000.0\";\"4D WAGON\""
+ + ";\"BLACK\";\"3.3 LTR\";\"5 Sp Auto\";\"MULTI POINT FINJ\";\"POWER MIRRORS, POWER STEERING, POWER WINDOWS, CRUISE CONTROL,"
+ + " ENGINE IMMOBILISER, BRAKE ASSIST, DUAL AIRBAG PACKAGE, ANTI-LOCK BRAKING, CENTRAL LOCKING REMOTE CONTROL, ALARM SYSTEM/REMOTE"
+ + " ANTI THEFT, AUTOMATIC AIR CON / CLIMATE CONTROL, ELECTRONIC BRAKE FORCE DISTRIBUTION, CLOTH TRIM, LIMITED SLIP DIFFERENTIAL,"
+ + " RADIO CD WITH 6 SPEAKERS\";\"Dual Airbag Package, Anti-lock Braking, Automatic Air Con / Climate Control, Alarm System/Remote"
+ + " Anti Theft, Brake Assist, Cruise Control, Central Locking Remote Control, Cloth Trim, Electronic Brake Force Distribution,"
+ + " Engine Immobiliser, Limited Slip Differential, Power Mirrors, Power Steering, Power Windows, Radio CD with 6 Speakers"
+ + " CV GOOD KLMS AUTO POWER OPTIONS GOOD KLMS \";\"Used\";\"0.0\";\"EZR05I\"\n";
+
@Test
- public void testBindyQuoteAndCommaDelimiter() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
+ public void testBindyUnmarshalQuoteAndCommaDelimiter() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:out");
mock.expectedMessageCount(1);
- String header = "\"stockid\";\"make\";\"model\";\"deriv\";\"series\";\"registration\";\"chassis\";\"engine\";\"year\""
- + ";\"klms\";\"body\";\"colour\";\"enginesize\";\"trans\";\"fuel\";\"options\";\"desc\";\"status\";\"Reserve_price\";\"nvic\"";
- String row = "\"SS552\";\"TOYOTA\";\"KLUGER\";\"CV 4X4\";\"MCU28R UPGRADE\";\"TBA\";\"\";\"\";\"2005\";\"155000\";\"4D WAGON\""
- + ";\"BLACK\";\"3.3 LTR\";\"5 Sp Auto\";\"MULTI POINT FINJ\";\"POWER MIRRORS, POWER STEERING, POWER WINDOWS, CRUISE CONTROL,"
- + " ENGINE IMMOBILISER, BRAKE ASSIST, DUAL AIRBAG PACKAGE, ANTI-LOCK BRAKING, CENTRAL LOCKING REMOTE CONTROL, ALARM SYSTEM/REMOTE"
- + " ANTI THEFT, AUTOMATIC AIR CON / CLIMATE CONTROL, ELECTRONIC BRAKE FORCE DISTRIBUTION, CLOTH TRIM, LIMITED SLIP DIFFERENTIAL,"
- + " RADIO CD WITH 6 SPEAKERS\";\"Dual Airbag Package, Anti-lock Braking, Automatic Air Con / Climate Control, Alarm System/Remote"
- + " Anti Theft, Brake Assist, Cruise Control, Central Locking Remote Control, Cloth Trim, Electronic Brake Force Distribution,"
- + " Engine Immobiliser, Limited Slip Differential, Power Mirrors, Power Steering, Power Windows, Radio CD with 6 Speakers"
- + " CV GOOD KLMS AUTO POWER OPTIONS GOOD KLMS \";\"Used\";\"\";\"EZR05I\" ";
-
- template.sendBody("direct:start", header + "\n" + row);
+ template.sendBody("direct:out", HEADER + "\n" + ROW);
assertMockEndpointsSatisfied();
@@ -62,6 +63,22 @@ public class BindyCarQuoteAndCommaDelimiterTest extends CamelTestSupport {
assertEquals(Double.valueOf("155000.0"), rec1.getKlms(), 0.0001);
assertEquals("EZR05I", rec1.getNvic());
assertEquals("Used", rec1.getStatus());
+ assertEquals(Car.Colour.BLACK, rec1.getColour());
+ }
+
+ @Test
+ public void testBindyMarshalQuoteAndCommaDelimiter() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:in");
+ mock.expectedMessageCount(1);
+
+ Car car = getCar();
+
+ template.sendBody("direct:in", car);
+
+ assertMockEndpointsSatisfied();
+
+ String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+ assertEquals(ROW, body);
}
@Override
@@ -69,11 +86,50 @@ public class BindyCarQuoteAndCommaDelimiterTest extends CamelTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:start")
- .unmarshal().bindy(BindyType.Csv, "org.apache.camel.dataformat.bindy.model.car")
- .to("mock:result");
+
+ BindyCsvDataFormat camelDataFormat =
+ new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.car");
+ camelDataFormat.setLocale("en");
+
+ from("direct:out")
+ .unmarshal().bindy(BindyType.Csv, "org.apache.camel.dataformat.bindy.model.car")
+ .to("mock:out");
+
+ from("direct:in")
+ .marshal(camelDataFormat)
+ .to("mock:in");
}
};
}
+ private Car getCar() {
+ Car car = new Car();
+ car.setStockid("SS552");
+ car.setMake("TOYOTA");
+ car.setModel("KLUGER");
+ car.setDeriv("CV 4X4");
+ car.setSeries("MCU28R UPGRADE");
+ car.setRegistration("TBA");
+ car.setChassis("");
+ car.setEngine("");
+ car.setYear(2005);
+ car.setKlms(155000);
+ car.setBody("4D WAGON");
+ car.setColour(Colour.BLACK);
+ car.setEnginesize("3.3 LTR");
+ car.setTrans("5 Sp Auto");
+ car.setFuel("MULTI POINT FINJ");
+ car.setOptions("POWER MIRRORS, POWER STEERING, POWER WINDOWS, CRUISE CONTROL,"
+ + " ENGINE IMMOBILISER, BRAKE ASSIST, DUAL AIRBAG PACKAGE, ANTI-LOCK BRAKING, CENTRAL LOCKING REMOTE CONTROL, ALARM SYSTEM/REMOTE"
+ + " ANTI THEFT, AUTOMATIC AIR CON / CLIMATE CONTROL, ELECTRONIC BRAKE FORCE DISTRIBUTION, CLOTH TRIM, LIMITED SLIP DIFFERENTIAL,"
+ + " RADIO CD WITH 6 SPEAKERS");
+ car.setDesc("Dual Airbag Package, Anti-lock Braking, Automatic Air Con / Climate Control, Alarm System/Remote"
+ + " Anti Theft, Brake Assist, Cruise Control, Central Locking Remote Control, Cloth Trim, Electronic Brake Force Distribution,"
+ + " Engine Immobiliser, Limited Slip Differential, Power Mirrors, Power Steering, Power Windows, Radio CD with 6 Speakers"
+ + " CV GOOD KLMS AUTO POWER OPTIONS GOOD KLMS ");
+ car.setStatus("Used");
+ car.setNvic("EZR05I");
+ return car;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/00bdbfc7/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/car/Car.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/car/Car.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/car/Car.java
index e230c0b..7be8c4d 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/car/Car.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/car/Car.java
@@ -22,7 +22,7 @@ import org.apache.camel.dataformat.bindy.annotation.DataField;
/**
*
*/
-@CsvRecord(separator = ";", skipFirstLine = true)
+@CsvRecord(separator = ";", skipFirstLine = true, quoting = true, crlf = "UNIX")
public class Car {
@DataField(pos = 1)
@@ -48,7 +48,7 @@ public class Car {
@DataField(pos = 11)
private String body;
@DataField(pos = 12)
- private String colour;
+ private Colour colour;
@DataField(pos = 13)
private String enginesize;
@DataField(pos = 14)
@@ -154,11 +154,11 @@ public class Car {
this.body = body;
}
- public String getColour() {
+ public Colour getColour() {
return colour;
}
- public void setColour(String colour) {
+ public void setColour(Colour colour) {
this.colour = colour;
}
@@ -225,4 +225,9 @@ public class Car {
public void setNvic(String nvic) {
this.nvic = nvic;
}
+
+ public static enum Colour {
+ BLACK
+ }
+
}