You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by rm...@apache.org on 2013/11/20 14:09:25 UTC

git commit: DELTASPIKE-444 adding SimpleQueryInOutMapperBase

Updated Branches:
  refs/heads/master c75adbb95 -> 5f2e3a610


DELTASPIKE-444 adding SimpleQueryInOutMapperBase


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/5f2e3a61
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/5f2e3a61
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/5f2e3a61

Branch: refs/heads/master
Commit: 5f2e3a610710f658d41b7ae627fe43dc1ce83e56
Parents: c75adbb
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Nov 20 14:09:11 2013 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Nov 20 14:09:11 2013 +0100

----------------------------------------------------------------------
 deltaspike/modules/data/api/pom.xml             | 13 ----
 .../api/mapping/SimpleQueryInOutMapperBase.java | 69 ++++++++++++++++++++
 .../mapping/SimpleQueryInOutMapperBaseTest.java | 66 +++++++++++++++++++
 3 files changed, 135 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5f2e3a61/deltaspike/modules/data/api/pom.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/pom.xml b/deltaspike/modules/data/api/pom.xml
index efb2243..f6afd6c 100755
--- a/deltaspike/modules/data/api/pom.xml
+++ b/deltaspike/modules/data/api/pom.xml
@@ -61,17 +61,4 @@
 
     </dependencies>
 
-    <build>
-        <plugins>
-            <!-- no test phase for API module -->
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-surefire-plugin</artifactId>
-                <configuration>
-                    <skip>true</skip>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
-
 </project>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5f2e3a61/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBase.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBase.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBase.java
new file mode 100644
index 0000000..a065755
--- /dev/null
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBase.java
@@ -0,0 +1,69 @@
+/*
+ * 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.deltaspike.data.api.mapping;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class SimpleQueryInOutMapperBase<Entity, Dto> implements QueryInOutMapper<Entity>
+{
+    protected abstract Dto toDto(Entity entity);
+    protected abstract Entity toEntity(Dto dto);
+
+    @Override
+    public Object mapResult(final Entity result)
+    {
+        if (result == null)
+        {
+            return null;
+        }
+        return toDto(result);
+    }
+
+    @Override
+    public Object mapResultList(final List<Entity> result)
+    {
+        final List<Object> mapped = new ArrayList<Object>(result.size());
+        if (result != null)
+        {
+            for (final Entity a : result)
+            {
+                mapped.add(mapResult(a));
+            }
+        }
+        return mapped;
+    }
+
+    @Override
+    public boolean mapsParameter(final Object parameter)
+    {
+        if (parameter == null)
+        {
+            return false;
+        }
+        final String name = parameter.getClass().getName();
+        return Object.class.isInstance(parameter) && !(name.startsWith("java.") || name.startsWith("javax."));
+    }
+
+    @Override
+    public Object mapParameter(final Object parameter)
+    {
+        return toEntity((Dto) parameter);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5f2e3a61/deltaspike/modules/data/api/src/test/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBaseTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/test/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBaseTest.java b/deltaspike/modules/data/api/src/test/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBaseTest.java
new file mode 100644
index 0000000..2bc4349
--- /dev/null
+++ b/deltaspike/modules/data/api/src/test/java/org/apache/deltaspike/data/api/mapping/SimpleQueryInOutMapperBaseTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.deltaspike.data.api.mapping;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class SimpleQueryInOutMapperBaseTest
+{
+    @Test
+    public void checkDefault() {
+        final SimpleQueryInOutMapperBase<String, Integer> mapper = new SimpleQueryInOutMapperBase<String, Integer>()
+        {
+            @Override
+            public Integer toDto(final String result) {
+                return result.length();
+            }
+
+            @Override
+            protected String toEntity(Integer b) {
+                return "ok";
+            }
+        };
+
+        assertNull(mapper.mapResult(null));
+        assertEquals(2, mapper.mapResult("ab"));
+
+        final List<Integer> collection = List.class.cast(mapper.mapResultList(Arrays.asList(null, "a", "bc")));
+        assertEquals(3, collection.size());
+        assertEquals(Arrays.asList(null, 1, 2), collection);
+
+        assertFalse(mapper.mapsParameter(null));
+        assertFalse(mapper.mapsParameter("foo"));
+        assertFalse(mapper.mapsParameter(true));
+        assertFalse(mapper.mapsParameter(2));
+        assertTrue(mapper.mapsParameter(new Object() {
+        }));
+
+        // this test is particular cause we refuse String but forcing call to it we having the mapper behavior
+        // but at least we test what is expected!
+        assertEquals("ok", mapper.mapParameter(2));
+    }
+}