Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id CA74D200B89 for ; Tue, 6 Sep 2016 19:18:08 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id C92E2160ACE; Tue, 6 Sep 2016 17:18:08 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 49CBF160AD4 for ; Tue, 6 Sep 2016 19:18:07 +0200 (CEST) Received: (qmail 60876 invoked by uid 500); 6 Sep 2016 17:18:06 -0000 Mailing-List: contact commits-help@tamaya.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tamaya.incubator.apache.org Delivered-To: mailing list commits@tamaya.incubator.apache.org Received: (qmail 60864 invoked by uid 99); 6 Sep 2016 17:18:06 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Sep 2016 17:18:06 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id D45CFCD976 for ; Tue, 6 Sep 2016 17:18:05 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.344 X-Spam-Level: X-Spam-Status: No, score=-4.344 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.124] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id spnvqkyX28Ak for ; Tue, 6 Sep 2016 17:18:03 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with SMTP id 1B5B75FC35 for ; Tue, 6 Sep 2016 17:18:00 +0000 (UTC) Received: (qmail 58362 invoked by uid 99); 6 Sep 2016 17:17:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Sep 2016 17:17:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C41E8E01C1; Tue, 6 Sep 2016 17:17:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: plexus@apache.org To: commits@tamaya.incubator.apache.org Date: Tue, 06 Sep 2016 17:18:21 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [25/50] [abbrv] incubator-tamaya-sandbox git commit: Added more complex scenarios and features for configuring of collections. Refactored parsing logic into separate utility class. archived-at: Tue, 06 Sep 2016 17:18:09 -0000 Added more complex scenarios and features for configuring of collections. Refactored parsing logic into separate utility class. 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/e94ac73d Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/e94ac73d Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/e94ac73d Branch: refs/heads/master Commit: e94ac73dfa88b9b974ec7b6eddb52a39fcbcdaeb Parents: dda281b Author: anatole Authored: Wed Feb 17 00:31:16 2016 +0100 Committer: anatole Committed: Wed Feb 17 00:31:16 2016 +0100 ---------------------------------------------------------------------- .../internal/AdaptiveCombinationPolicy.java | 3 +- .../internal/ArrayListConverter.java | 50 ++---- .../internal/CollectionConverter.java | 1 - .../internal/ConcurrentHashMapConverter.java | 25 +-- .../collections/internal/HashMapConverter.java | 6 +- .../collections/internal/HashSetConverter.java | 5 +- .../collections/internal/ItemTokenizer.java | 170 +++++++++++++++++++ .../internal/LinkedListConverter.java | 5 +- .../collections/internal/TreeSetConverter.java | 5 +- .../collections/CollectionAdvancedTests.java | 78 +++++++++ .../collections/MyUpperCaseConverter.java | 33 ++++ .../META-INF/javaconfiguration.properties | 30 ++-- 12 files changed, 324 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/AdaptiveCombinationPolicy.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/AdaptiveCombinationPolicy.java b/collections/src/main/java/org/apache/tamaya/collections/internal/AdaptiveCombinationPolicy.java index 63fea76..ac5af36 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/AdaptiveCombinationPolicy.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/AdaptiveCombinationPolicy.java @@ -68,6 +68,7 @@ public class AdaptiveCombinationPolicy implements PropertyValueCombinationPolicy } } // check for default collection combination policies for lists, sets, maps etc. + final String SEPARATOR = ConfigurationProvider.getConfiguration().getOrDefault('_' + key+".collection-separator", ","); String collectionType = ConfigurationProvider.getConfiguration().get('_' + key+".collection-type"); if(collectionType!=null) { if (collectionType.startsWith("java.util.")) { @@ -96,7 +97,7 @@ public class AdaptiveCombinationPolicy implements PropertyValueCombinationPolicy String oldVal = newMapValue.get(key); newMapValue.putAll(newValue.getConfigEntries()); if(oldVal!=null){ - newMapValue.put(key,oldVal + ',' + newValue.getValue()); + newMapValue.put(key,oldVal + SEPARATOR + newValue.getValue()); } return newMapValue; }else{ http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/ArrayListConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/ArrayListConverter.java b/collections/src/main/java/org/apache/tamaya/collections/internal/ArrayListConverter.java index e55b44a..3e95438 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/ArrayListConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/ArrayListConverter.java @@ -18,6 +18,7 @@ */ package org.apache.tamaya.collections.internal; +import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; @@ -47,47 +48,20 @@ public class ArrayListConverter implements PropertyConverter { @Override public ArrayList convert(String value, ConversionContext context) { - ArrayList rawList = split(value); - String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); - if(converterClass!=null){ - try { - PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); - ArrayList mlist = new ArrayList<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), - TypeLiteral.of(context.getTargetType().getType())).build(); - for(String raw:rawList){ - Object convValue = valueConverter.convert(raw, ctx); - if(convValue!=null){ - mlist.add(convValue); - continue; - } - } - return mlist; + final String SEPARATOR = ConfigurationProvider.getConfiguration().getOrDefault( + '_' + context.getKey()+".collection-separator", ","); + List rawList = ItemTokenizer.split(value, SEPARATOR); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); - } - } - return rawList; - } - - static ArrayList split(String value) { - ArrayList result = new ArrayList<>(); - int start = 0; - int end = value.indexOf(',',start); - while(end>0) { - if (end>0 && (value.charAt(end - 1) != '\\')) { - result.add(value.substring(start, end)); - start = end + 1; - end = value.indexOf(',',start); + ArrayList mlist = new ArrayList<>(); + for(String raw:rawList){ + Object convValue = ItemTokenizer.convertValue(raw, context); + if (convValue != null) { + mlist.add(convValue); }else{ - end = value.indexOf(',',end + 1); + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); } - end = value.indexOf(',',start); - } - if(start < value.length()){ - result.add(value.substring(start)); } - return result; + return mlist; } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/CollectionConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/CollectionConverter.java b/collections/src/main/java/org/apache/tamaya/collections/internal/CollectionConverter.java index ecd2878..0d9a799 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/CollectionConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/CollectionConverter.java @@ -23,7 +23,6 @@ import org.apache.tamaya.spi.PropertyConverter; import java.util.Collection; import java.util.Collections; -import java.util.List; /** * PropertyConverter for gnerating a LIST representation of values. http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/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 index f472c4c..07aecc8 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/ConcurrentHashMapConverter.java @@ -46,16 +46,17 @@ public class ConcurrentHashMapConverter implements PropertyConverter rawList = ArrayListConverter.split(value); + List rawList = ItemTokenizer.split(value, context); String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); if(converterClass!=null){ try { PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); ConcurrentHashMap mlist = new ConcurrentHashMap<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), TypeLiteral.of(context.getTargetType().getType())).build(); for(String raw:rawList){ - String[] items = splitItems(raw); + String[] items = ItemTokenizer.splitMapEntry(raw, context); Object convValue = valueConverter.convert(items[1], ctx); if(convValue!=null){ mlist.put(items[0], convValue); @@ -70,7 +71,7 @@ public class ConcurrentHashMapConverter implements PropertyConverter result = new ConcurrentHashMap<>(); for(String raw:rawList){ - String[] items = splitItems(raw); + String[] items = ItemTokenizer.splitMapEntry(raw, context); if(items!=null){ result.put(items[0], items[1]); } @@ -78,19 +79,5 @@ public class ConcurrentHashMapConverter implements PropertyConverter { @Override public HashMap convert(String value, ConversionContext context) { - List rawList = ArrayListConverter.split(value); + List rawList = ItemTokenizer.split(value, context); String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); if(converterClass!=null){ try { PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); HashMap mlist = new HashMap<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), TypeLiteral.of(context.getTargetType().getType())).build(); for(String raw:rawList){ String[] items = splitItems(raw); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/HashSetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/HashSetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/internal/HashSetConverter.java index 0551549..47c32f6 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/HashSetConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/HashSetConverter.java @@ -49,13 +49,14 @@ public class HashSetConverter implements PropertyConverter { @Override public HashSet convert(String value, ConversionContext context) { - List rawList = ArrayListConverter.split(value); + List rawList = ItemTokenizer.split(value, context); String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); if(converterClass!=null){ try { PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); HashSet mlist = new HashSet<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), TypeLiteral.of(context.getTargetType().getType())).build(); for(String raw:rawList){ Object convValue = valueConverter.convert(raw, ctx); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/ItemTokenizer.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/ItemTokenizer.java b/collections/src/main/java/org/apache/tamaya/collections/internal/ItemTokenizer.java new file mode 100644 index 0000000..37f7cf1 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/ItemTokenizer.java @@ -0,0 +1,170 @@ +/* + * 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.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Helper class that implements the tokenizing of the entries of a configuration value. + */ +final class ItemTokenizer { + + private static final Logger LOG = Logger.getLogger(ItemTokenizer.class.getName()); + + /** + * Private singleton. + */ + private ItemTokenizer(){} + + /** + * Splits the given value using the given separator. Matcjhing is done by traversing the String value using + * {@code indexOf} calls, one by one. The last unresolvable item (without any next separator token) + * is added at the end of the list. + * @param value the value, not null. + * @param context the conversion context. + * @return the tokenized value as list, in order of occurrence. + */ + public static List split(String value, ConversionContext context){ + return split(value, ConfigurationProvider.getConfiguration().getOrDefault( + '_' + context.getKey()+".collection-separator", ",")); + } + + /** + * Splits the given value using the given separator. Matcjhing is done by traversing the String value using + * {@code indexOf} calls, one by one. The last unresolvable item (without any next separator token) + * is added at the end of the list. + * @param value the value, not null. + * @param separator the separator to be used. + * @return the tokenized value as list, in order of occurrence. + */ + public static List split(String value, final String separator) { + ArrayList result = new ArrayList<>(); + int start = 0; + int end = value.indexOf(separator,start); + while(end>0) { + if (end>0 && (value.charAt(end - 1) != '\\')) { + result.add(value.substring(start, end)); + start = end + separator.length(); + end = value.indexOf(separator,start); + }else{ + end = value.indexOf(separator,end + separator.length()); + } + end = value.indexOf(separator,start); + } + if(start < value.length()){ + result.add(value.substring(start)); + } + return result; + } + + /** + * plits the given String value as a map entry, splitting it into key and value part with the given separator. + * If the value cannot be split then {@code key = value = mapEntry} is used for further processing. key or value + * parts are normally trimmed, unless they are enmcosed with brackets {@code []}. + * @param mapEntry the entry, not null. + * @param context the conversion context. + * @return an array of length 2, with the trimmed and parsed key/value pair. + */ + public static String[] splitMapEntry(String mapEntry, ConversionContext context){ + return splitMapEntry(mapEntry, ConfigurationProvider.getConfiguration().getOrDefault( + '_' + context.getKey()+".collection-map-separator", ",")); + } + + /** + * Splits the given String value as a map entry, splitting it into key and value part with the given separator. + * If the value cannot be split then {@code key = value = mapEntry} is used for further processing. key or value + * parts are normally trimmed, unless they are enmcosed with brackets {@code []}. + * @param mapEntry the entry, not null. + * @param separator the separator, not null. + * @return an array of length 2, with the trimmed and parsed key/value pair. + */ + public static String[] splitMapEntry(final String mapEntry, final String separator) { + int index = mapEntry.indexOf(separator); + String[] items; + if(index<0) { + items = new String[]{mapEntry, mapEntry}; + }else { + items = new String[]{mapEntry.substring(0,index), + mapEntry.substring(index+separator.length())}; + } + 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; + } + + /** + * Parses the given value into the required collection target type, defined by the context. + * @param value the raw String value. + * @param context the context + * @return the parsed value, or null. + */ + public static Object convertValue(String value, ConversionContext context) { + String converterClass = context.getConfiguration().get('_' + context.getKey() + ".item-converter"); + List> valueConverters = new ArrayList<>(1); + if (converterClass != null) { + try { + valueConverters.add((PropertyConverter) Class.forName(converterClass).newInstance()); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); + } + } + if (TypeLiteral.getTypeParameters(context.getTargetType().getType()).length>0) { + valueConverters.addAll(context.getConfigurationContext().getPropertyConverters( + TypeLiteral.of(TypeLiteral.getTypeParameters(context.getTargetType().getType())[0]))); + } + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), + TypeLiteral.of(context.getTargetType().getType())).build(); + Object result = null; + if (valueConverters.isEmpty()) { + return value; + } else { + for (PropertyConverter conv : valueConverters) { + try { + result = conv.convert(value, ctx); + if (result != null) { + return result; + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); + } + } + } + LOG.log(Level.SEVERE, "Failed to convert collection value type for '" + value + "'."); + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/LinkedListConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/LinkedListConverter.java b/collections/src/main/java/org/apache/tamaya/collections/internal/LinkedListConverter.java index f882c78..986a303 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/LinkedListConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/LinkedListConverter.java @@ -46,13 +46,14 @@ public class LinkedListConverter implements PropertyConverter { @Override public LinkedList convert(String value, ConversionContext context) { - List rawList = ArrayListConverter.split(value); + List rawList = ItemTokenizer.split(value, context); String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); if(converterClass!=null){ try { PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); LinkedList mlist = new LinkedList<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), TypeLiteral.of(context.getTargetType().getType())).build(); for(String raw:rawList){ Object convValue = valueConverter.convert(raw, ctx); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/main/java/org/apache/tamaya/collections/internal/TreeSetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/internal/TreeSetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/internal/TreeSetConverter.java index e9e2282..f9cba02 100644 --- a/collections/src/main/java/org/apache/tamaya/collections/internal/TreeSetConverter.java +++ b/collections/src/main/java/org/apache/tamaya/collections/internal/TreeSetConverter.java @@ -47,13 +47,14 @@ public class TreeSetConverter implements PropertyConverter { @Override public TreeSet convert(String value, ConversionContext context) { - List rawList = ArrayListConverter.split(value); + List rawList = ItemTokenizer.split(value, context); String converterClass = context.getConfiguration().get('_' + context.getKey()+".collection-parser"); if(converterClass!=null){ try { PropertyConverter valueConverter = (PropertyConverter) Class.forName(converterClass).newInstance(); TreeSet mlist = new TreeSet<>(); - ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), TypeLiteral.of(context.getTargetType().getType())).build(); for(String raw:rawList){ Object convValue = valueConverter.convert(raw, ctx); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java ---------------------------------------------------------------------- diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java b/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java new file mode 100644 index 0000000..435ed5d --- /dev/null +++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java @@ -0,0 +1,78 @@ +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.Currency; +import java.util.List; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * Created by atsticks on 16.02.16. + */ +public class CollectionAdvancedTests { + + /** + * Tests if a custom separator works, Config is + *
+     *  sep-list=a,b,c|d,e,f|g,h,i
+     *  _sep-list.collection-type=List
+     *  _sep-list.collection-separator=|
+     * 
+ */ + @Test + public void testCustomSeparator(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List items = config.get("sep-list", new TypeLiteral>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("a,b,c", items.get(0)); + assertEquals("d,e,f", items.get(1)); + assertEquals("g,h,i", items.get(2)); + } + + /** + * Test typed content. + *
+     *  currency-list=CHF,USD,YEN
+     *  _currency-list.collection-type=List
+     * 
+ */ + @Test + public void testTypedContent(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List items = config.get("currency-list", new TypeLiteral>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("CHF", items.get(0).getCurrencyCode()); + assertEquals("USD", items.get(1).getCurrencyCode()); + assertEquals("USS", items.get(2).getCurrencyCode()); + } + + /** + * Tests if a custom parser works, Config is + *
+     *  parser-list=a,b,c
+     *  _parser-list.collection-type=List
+     *  _parser-list.item-converter=org.apache.tamaya.collections.MyUpperCaseConverter
+     * 
+ */ + @Test + public void testCustomParser(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List items = config.get("parser-list", new TypeLiteral>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("(A)", items.get(0)); + assertEquals("(B)", items.get(1)); + assertEquals("(C)", items.get(2)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/test/java/org/apache/tamaya/collections/MyUpperCaseConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/test/java/org/apache/tamaya/collections/MyUpperCaseConverter.java b/collections/src/test/java/org/apache/tamaya/collections/MyUpperCaseConverter.java new file mode 100644 index 0000000..1c95261 --- /dev/null +++ b/collections/src/test/java/org/apache/tamaya/collections/MyUpperCaseConverter.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +/** + * Example converter that is used for testing the custom parsing functionality. It sorrounds values with () and + * converts them to uppercase. + */ +public class MyUpperCaseConverter implements PropertyConverter{ + @Override + public String convert(String value, ConversionContext context) { + return "("+value.toUpperCase()+")"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e94ac73d/collections/src/test/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/collections/src/test/resources/META-INF/javaconfiguration.properties b/collections/src/test/resources/META-INF/javaconfiguration.properties index 76e5e84..2bb8414 100644 --- a/collections/src/test/resources/META-INF/javaconfiguration.properties +++ b/collections/src/test/resources/META-INF/javaconfiguration.properties @@ -56,22 +56,14 @@ _typed.hashmap.collection-type=java.util.HashMap typed.treemap=1::a, 2::b, 3::c, [4:: ] _typed.treemap.collection-type=TreeMap -# Config for tests with combination policy -list=a,b,c -_list.collection-type=List -arrayList=d,e,f -_arrayList.collection-type=java.util.ArrayList -linkedList=g,h,i -_linkedList.collection-type=java.util.LinkedList -set=a,b,b -_set.collection-type=java.util.Set -sortedSet=a,c,b -_sortedSet.collection-type=java.util.TreeSet -map=1::a, 2::b, 3::c -_map.collection-type=Map -sortedmap=[3 :: c], 1 :: a, 2 :: b -_sortedmap.collection-type=SorteedMap -treemap=[3 :: c], 1 :: a, 2 :: b -_treemap.collection-type=java.util.TreeMap -concurrentMap=[3::c], 1::a, key::value -_concurrentMap.collection-type=java.util.ConcurrentHashMap \ No newline at end of file +# Config for advanced tests +sep-list=a,b,c|d,e,f|g,h,i +_sep-list.collection-type=List +_sep-list.collection-separator=| +currency-list=CHF,USD,USS +_currency-list.collection-type=List + +parser-list=a,b,c +_parser-list.collection-type=List +_parser-list.item-converter=org.apache.tamaya.collections.MyUpperCaseConverter +