tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject [23/50] [abbrv] incubator-tamaya-sandbox git commit: TAMAYA-114: Tested and implemented base functionality.
Date Tue, 06 Sep 2016 17:18:19 GMT
TAMAYA-114: Tested and implemented base functionality.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/e37c2e0e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/e37c2e0e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/e37c2e0e

Branch: refs/heads/master
Commit: e37c2e0e0aef490ed5c9c24909fb184cebc9131a
Parents: 0d9afc2
Author: anatole <anatole@apache.org>
Authored: Mon Feb 15 17:24:19 2016 +0100
Committer: anatole <anatole@apache.org>
Committed: Mon Feb 15 17:24:19 2016 +0100

----------------------------------------------------------------------
 .../internal/ConcurrentHashMapConverter.java    |  96 ++++++++
 .../CollectionsTypedReadOnlyTests.java          | 220 +++++++++++++++++++
 .../collections/CollectionsTypedTests.java      | 209 ++++++++++++++++++
 3 files changed, 525 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e37c2e0e/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java
----------------------------------------------------------------------
diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java
b/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java
new file mode 100644
index 0000000..e425a27
--- /dev/null
+++ b/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java
@@ -0,0 +1,96 @@
+/*
+ * 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.tamaya.collections.internal;
+
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *  PropertyConverter for gnerating HashMap representation of a values.
+ */
+public class ConcurrentHashMapConverter implements PropertyConverter<HashMap> {
+    private static final Logger LOG = Logger.getLogger(ArrayListConverter.class.getName());
+
+    /** The shared instance, used by other collection converters in this package.*/
+    private static ConcurrentHashMapConverter INSTANCE = new ConcurrentHashMapConverter();
+
+    /**
+     * Provide a shared instance, used by other collection converters in this package.
+     * @return the shared instance, never null.
+     */
+    static ConcurrentHashMapConverter getInstance(){
+        return INSTANCE;
+    }
+
+    @Override
+    public HashMap convert(String value, ConversionContext context) {
+        List<String> rawList = ArrayListConverter.split(value);
+        String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser");
+        if(converterClass!=null){
+            try {
+                PropertyConverter<?> valueConverter = (PropertyConverter<?>)
Class.forName(converterClass).newInstance();
+                HashMap<String,Object> mlist = new HashMap<>();
+                ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(),
context.getKey(),
+                        TypeLiteral.of(context.getTargetType().getType())).build();
+                for(String raw:rawList){
+                    String[] items = splitItems(raw);
+                    Object convValue = valueConverter.convert(items[1], ctx);
+                    if(convValue!=null){
+                        mlist.put(items[0], convValue);
+                        continue;
+                    }
+                }
+                return mlist;
+
+            } catch (Exception e) {
+                LOG.log(Level.SEVERE, "Error convertion config to HashMap type.", e);
+            }
+        }
+        HashMap<String,String> result = new HashMap<>();
+        for(String raw:rawList){
+            String[] items = splitItems(raw);
+            if(items!=null){
+                result.put(items[0], items[1]);
+            }
+        }
+        return result;
+    }
+
+    static String[] splitItems(String raw) {
+        String[] items = raw.split("::");
+        if(items[0].trim().startsWith("[")){
+            items[0]= items[0].trim();
+            items[0] = items[0].substring(1);
+        }else{
+            items[0]= items[0].trim();
+        }
+        if(items[1].trim().endsWith("]")){
+            items[1] = items[1].substring(0,items[1].length()-1);
+        }else{
+            items[1]= items[1].trim();
+        }
+        return items;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e37c2e0e/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java
b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java
new file mode 100644
index 0000000..04b0564
--- /dev/null
+++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java
@@ -0,0 +1,220 @@
+package org.apache.tamaya.collections;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.TypeLiteral;
+import org.junit.Test;
+
+import java.util.*;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Basic tests for Tamaya collection support. Relevant configs for this tests:
+ * <pre>base.items=1,2,3,4,5,6,7,8,9,0
+ * base.map=1::a, 2::b, 3::c, [4:: ]
+ * </pre>
+ */
+public class CollectionsTypedReadOnlyTests {
+
+    @Test
+    public void testArrayListList_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        List<String> items = config.get("typed.arraylist", new TypeLiteral<List<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof ArrayList);
+        items = (List<String>) config.get("typed.arraylist", List.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof ArrayList);
+    }
+
+    @Test
+    public void testLinkedListList_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        List<String> items = config.get("typed.linkedlist", new TypeLiteral<List<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof LinkedList);
+        items = (List<String>) config.get("typed.linkedlist", List.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof LinkedList);
+    }
+
+
+    @Test
+    public void testHashSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Set<String> items = config.get("typed.hashset", new TypeLiteral<Set<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashSet);
+        items = (Set<String>) config.get("typed.hashset", Set.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashSet);
+    }
+
+    @Test
+    public void testTreeSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Set<String> items = config.get("typed.treeset", new TypeLiteral<Set<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeSet);
+        items = (Set<String>) config.get("typed.treeset", Set.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeSet);
+    }
+
+    @Test
+    public void testHashMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Map<String,String> items = config.get("typed.hashmap", new TypeLiteral<Map<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        assertTrue(items instanceof HashMap);
+        items = (Map<String,String>) config.get("typed.hashmap", Map.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        assertTrue(items instanceof HashMap);
+    }
+
+    @Test
+    public void testTreeMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Map<String,String> items = config.get("typed.treemap", new TypeLiteral<Map<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        assertTrue(items instanceof TreeMap);
+        items = (Map<String,String>) config.get("typed.treemap", Map.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        assertTrue(items instanceof TreeMap);
+    }
+
+
+    @Test
+    public void testCollection_TreeMap(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.treemap", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeMap);
+        items = (Collection<String>) config.get("typed.treemap", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeMap);
+    }
+
+    @Test
+    public void testCollection_HashMap(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.hashmap", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashMap);
+        items = (Collection<String>) config.get("typed.hashmap", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashMap);
+    }
+
+    @Test
+    public void testCollection_HashSet(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.hashset", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashSet);
+        items = (Collection<String>) config.get("typed.hashset", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof HashSet);
+    }
+
+    @Test
+    public void testCollection_TreeSet(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.treeset", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeSet);
+        items = (Collection<String>) config.get("typed.treeset", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof TreeSet);
+    }
+
+    @Test
+    public void testCollection_ArrayList(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.arraylist", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof ArrayList);
+        items = (Collection<String>) config.get("typed.arraylist", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof ArrayList);
+    }
+
+    @Test
+    public void testCollection_LinkedList(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("typed.linkedlist", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof LinkedList);
+        items = (Collection<String>) config.get("typed.linkedlist", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        assertTrue(items instanceof LinkedList);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e37c2e0e/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedTests.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedTests.java
b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedTests.java
new file mode 100644
index 0000000..71a86fb
--- /dev/null
+++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedTests.java
@@ -0,0 +1,209 @@
+package org.apache.tamaya.collections;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.TypeLiteral;
+import org.junit.Test;
+
+import java.util.*;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Basic tests for Tamaya collection support. Relevant configs for this tests:
+ * <pre>base.items=1,2,3,4,5,6,7,8,9,0
+ * base.map=1::a, 2::b, 3::c, [4:: ]
+ * </pre>
+ */
+public class CollectionsTypedTests {
+
+    @Test
+    public void testList_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        List<String> items = config.get("base.items", new TypeLiteral<List<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (List<String>) config.get("base.items", List.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testArrayList_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        ArrayList<String> items = config.get("base.items", new TypeLiteral<ArrayList<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (ArrayList<String>) config.get("base.items", ArrayList.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testLinkedList_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        LinkedList<String> items = config.get("base.items", new TypeLiteral<LinkedList<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (LinkedList<String>) config.get("base.items", LinkedList.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Set<String> items = config.get("base.items", new TypeLiteral<Set<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (Set<String>) config.get("base.items", Set.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testSortedSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Set<String> items = config.get("base.items", new TypeLiteral<SortedSet<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (SortedSet<String>) config.get("base.items", SortedSet.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testHashSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Set<String> items = config.get("base.items", new TypeLiteral<HashSet<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (HashSet<String>) config.get("base.items", HashSet.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testTreeSet_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        TreeSet<String> items = config.get("base.items", new TypeLiteral<TreeSet<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (TreeSet<String>) config.get("base.items", TreeSet.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+
+    @Test
+    public void testMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Map<String,String> items = config.get("base.map", new TypeLiteral<Map<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        items = (Map<String,String>) config.get("base.map", Map.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+    }
+
+    @Test
+    public void testHashMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Map<String,String> items = config.get("base.map", new TypeLiteral<HashMap<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        items = (HashMap<String,String>) config.get("base.map", HashMap.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+    }
+
+    @Test
+    public void testSortedMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Map<String,String> items = config.get("base.map", new TypeLiteral<SortedMap<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        items = (Map<String,String>) config.get("base.map", SortedMap.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+    }
+
+    @Test
+    public void testTreeMap_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        TreeMap<String,String> items = config.get("base.map", new TypeLiteral<TreeMap<String,String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+        items =  config.get("base.map", TreeMap.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(4, items.size());
+        assertEquals("a", items.get("1"));
+        assertEquals("b", items.get("2"));
+        assertEquals("c", items.get("3"));
+        assertEquals(" ", items.get("4"));
+    }
+
+    @Test
+    public void testCollection_String(){
+        Configuration config = ConfigurationProvider.getConfiguration();
+        Collection<String> items = config.get("base.items", new TypeLiteral<Collection<String>>(){});
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+        items = (Collection<String>) config.get("base.items", Collection.class);
+        assertNotNull(items);
+        assertFalse(items.isEmpty());
+        assertEquals(10, items.size());
+    }
+}


Mime
View raw message