You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@batchee.apache.org by rm...@apache.org on 2015/03/24 22:03:22 UTC
incubator-batchee git commit: BATCHEE-62 matchingStrategy in
ModelMapperItemProcessor
Repository: incubator-batchee
Updated Branches:
refs/heads/master 716954137 -> d3dd38e9f
BATCHEE-62 matchingStrategy in ModelMapperItemProcessor
Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/d3dd38e9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/d3dd38e9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/d3dd38e9
Branch: refs/heads/master
Commit: d3dd38e9fc9d21eb3dc440802dd7a11e14f7e86d
Parents: 7169541
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Tue Mar 24 22:03:14 2015 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Tue Mar 24 22:03:14 2015 +0100
----------------------------------------------------------------------
.../modelmapper/ModelMapperItemProcessor.java | 38 +++-
.../LooseModelMapperProcessorTest.java | 194 +++++++++++++++++++
.../batch-jobs/modelmapper-processor-loose.xml | 29 +++
3 files changed, 256 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/d3dd38e9/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java b/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
index 1d1d9bb..d52cace 100644
--- a/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
+++ b/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
@@ -17,7 +17,11 @@
package org.apache.batchee.modelmapper;
import org.modelmapper.ModelMapper;
+import org.modelmapper.config.Configuration;
+import org.modelmapper.convention.MatchingStrategies;
+import org.modelmapper.spi.MatchingStrategy;
+import java.util.Locale;
import javax.batch.api.BatchProperty;
import javax.batch.api.chunk.ItemProcessor;
import javax.inject.Inject;
@@ -27,6 +31,10 @@ public class ModelMapperItemProcessor implements ItemProcessor {
@Inject
@BatchProperty
+ private String matchingStrategy;
+
+ @Inject
+ @BatchProperty
private String destinationType;
private volatile Class<?> destinationTypeClass = null;
@@ -48,11 +56,7 @@ public class ModelMapperItemProcessor implements ItemProcessor {
synchronized (this) {
if (destinationTypeClass == null) {
try {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (loader == null) {
- loader = ModelMapperItemProcessor.class.getClassLoader();
- }
-
+ final ClassLoader loader = currentLoader();
destinationTypeClass = loader.loadClass(destinationType);
} catch (final ClassNotFoundException e) {
throw new IllegalArgumentException("Can't load: '" + destinationType + "'", e);
@@ -69,9 +73,33 @@ public class ModelMapperItemProcessor implements ItemProcessor {
synchronized (this) {
if (mapper == null) {
mapper = newMapper();
+ if (matchingStrategy != null) {
+ final Configuration configuration = mapper.getConfiguration();
+ try {
+ configuration.setMatchingStrategy(MatchingStrategy.class.cast(
+ MatchingStrategies.class.getDeclaredField(matchingStrategy.toUpperCase(Locale.ENGLISH)).get(null)));
+ } catch (final Exception e) {
+ try {
+ configuration.setMatchingStrategy(MatchingStrategy.class.cast(currentLoader().loadClass(matchingStrategy)));
+ } catch (final Exception e1) {
+ if (RuntimeException.class.isInstance(e)) {
+ throw RuntimeException.class.cast(e);
+ }
+ throw new IllegalStateException(e);
+ }
+ }
+ }
}
}
}
return mapper;
}
+
+ private static ClassLoader currentLoader() {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null) {
+ loader = ModelMapperItemProcessor.class.getClassLoader();
+ }
+ return loader;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/d3dd38e9/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/LooseModelMapperProcessorTest.java
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/LooseModelMapperProcessorTest.java b/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/LooseModelMapperProcessorTest.java
new file mode 100644
index 0000000..6f66230
--- /dev/null
+++ b/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/LooseModelMapperProcessorTest.java
@@ -0,0 +1,194 @@
+/*
+ * 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.batchee.modelmapper;
+
+import org.apache.batchee.util.Batches;
+import org.testng.annotations.Test;
+
+import java.io.Serializable;
+import java.util.List;
+import javax.batch.api.chunk.ItemReader;
+import javax.batch.api.chunk.ItemWriter;
+import javax.batch.operations.JobOperator;
+import javax.batch.runtime.BatchRuntime;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+public class LooseModelMapperProcessorTest {
+ @Test
+ public void map() {
+ final JobOperator operator = BatchRuntime.getJobOperator();
+ Batches.waitForEnd(operator, operator.start("modelmapper-processor-loose", null));
+ assertNotNull(Writer.items);
+ assertEquals(2, Writer.items.size());
+ assertTrue(Writer.items.contains(new B("1", new Nested("#1"))));
+ assertTrue(Writer.items.contains(new B("2", new Nested("#2"))));
+ }
+
+ public static class Reader implements ItemReader {
+ private int count = 0;
+
+ @Override
+ public void open(final Serializable serializable) throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void close() throws Exception {
+ // no-op
+ }
+
+ @Override
+ public Object readItem() throws Exception {
+ if (count++ < 2) {
+ return new A("" + count, "#" + count);
+ }
+ return null;
+ }
+
+ @Override
+ public Serializable checkpointInfo() throws Exception {
+ return null;
+ }
+ }
+
+ public static class Writer implements ItemWriter {
+ private static List<B> items;
+
+ @Override
+ public void open(final Serializable serializable) throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void close() throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void writeItems(final List objects) throws Exception {
+ items = objects;
+ }
+
+ @Override
+ public Serializable checkpointInfo() throws Exception {
+ return null;
+ }
+ }
+
+ public static class A {
+ private String a;
+ private String b;
+
+ public A(final String a, final String b) {
+ this.a = a;
+ this.b = b;
+ }
+
+ public String getA() {
+ return a;
+ }
+
+ public String getB() {
+ return b;
+ }
+ }
+
+ public static class B {
+ private String a;
+ private Nested nested;
+
+ public B(final String s, final Nested s1) {
+ this.a = s;
+ this.nested = s1;
+ }
+
+ public B() {
+ // no-op
+ }
+
+ public void setA(final String a) {
+ this.a = a;
+ }
+
+ public String getA() {
+ return a;
+ }
+
+ public Nested getNested() {
+ return nested;
+ }
+
+ public void setNested(final Nested nested) {
+ this.nested = nested;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ B b1 = (B) o;
+
+ return !(a != null ? !a.equals(b1.a) : b1.a != null) && !(nested != null ? !nested.equals(b1.nested) : b1.nested != null);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = a != null ? a.hashCode() : 0;
+ result = 31 * result + (nested != null ? nested.hashCode() : 0);
+ return result;
+ }
+ }
+
+ public static class Nested {
+ private String b;
+
+ public Nested() {
+ // no-op
+ }
+
+ public Nested(final String b) {
+ this.b = b;
+ }
+
+ public void setB(final String b) {
+ this.b = b;
+ }
+
+ public String getB() {
+ return b;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Nested nested = (Nested) o;
+
+ return !(b != null ? !b.equals(nested.b) : nested.b != null);
+ }
+
+ @Override
+ public int hashCode() {
+ return b != null ? b.hashCode() : 0;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/d3dd38e9/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor-loose.xml
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor-loose.xml b/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor-loose.xml
new file mode 100644
index 0000000..a314502
--- /dev/null
+++ b/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor-loose.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ See the NOTICE file distributed with this work for additional information
+ regarding copyright ownership. Licensed 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.
+-->
+<job id="modelmapper-processor" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
+ <step id="step1">
+ <chunk>
+ <reader ref="org.apache.batchee.modelmapper.LooseModelMapperProcessorTest$Reader" />
+ <processor ref="modelMapperProcessor">
+ <properties>
+ <property name="destinationType" value="org.apache.batchee.modelmapper.LooseModelMapperProcessorTest$B" />
+ <property name="matchingStrategy" value="LOOSE" />
+ </properties>
+ </processor>
+ <writer ref="org.apache.batchee.modelmapper.LooseModelMapperProcessorTest$Writer" />
+ </chunk>
+ </step>
+</job>