You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2017/04/03 11:36:48 UTC

svn commit: r1789967 - in /felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl: ConverterBuilderTest.java ConverterTest.java Helper.java

Author: gnodet
Date: Mon Apr  3 11:36:48 2017
New Revision: 1789967

URL: http://svn.apache.org/viewvc?rev=1789967&view=rev
Log:
Fix converter implementation

Added:
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/Helper.java
Modified:
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterBuilderTest.java
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterBuilderTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterBuilderTest.java?rev=1789967&r1=1789966&r2=1789967&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterBuilderTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterBuilderTest.java Mon Apr  3 11:36:48 2017
@@ -31,13 +31,13 @@ import java.util.stream.Stream;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.osgi.util.converter.ConvertFunction;
 import org.osgi.util.converter.Converter;
 import org.osgi.util.converter.ConverterBuilder;
 import org.osgi.util.converter.Rule;
 import org.osgi.util.converter.TypeRule;
 import org.osgi.util.function.Function;
 
+import static org.apache.felix.converter.impl.Helper.convert;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
@@ -85,8 +85,8 @@ public class ConverterBuilderTest {
     public void testSecondLevelAdapter() {
         ConverterBuilder cb = converter.newConverterBuilder();
         cb.rule(new TypeRule<>(char[].class, String.class, ConverterBuilderTest::convertToString));
-        cb.rule(Integer.class, (f,t) -> -1);
-        cb.rule(Long.class, (f,t) -> -1L);
+        cb.rule(Integer.class, convert((f,t) -> -1));
+        cb.rule(Long.class, convert((f,t) -> -1L));
         Converter ca = cb.build();
 
         assertEquals("hi", ca.convert(new char[] {'h', 'i'}).to(String.class));
@@ -121,7 +121,7 @@ public class ConverterBuilderTest {
 
     @Test
     public void testWildcardAdapter() {
-        ConvertFunction<Object> foo = new ConvertFunction<Object>() {
+        Helper.ConvertFunctionConverter<Object> foo = new Helper.ConvertFunctionConverter<Object>() {
             @Override
             public Object convert(Object obj, Type type) throws Exception {
                 if (!(obj instanceof List))
@@ -137,8 +137,8 @@ public class ConverterBuilderTest {
         };
 
         ConverterBuilder cb = converter.newConverterBuilder();
-        cb.rule(foo);
-        cb.rule((v,t) -> v.toString());
+        cb.rule(convert(foo));
+        cb.rule(convert((v,t) -> v.toString()));
         Converter ca = cb.build();
 
         assertEquals(3L, (long) ca.convert(Arrays.asList("a", "b", "c")).to(Long.class));
@@ -148,7 +148,7 @@ public class ConverterBuilderTest {
 
     @Test
     public void testWildcardAdapter1() {
-        ConvertFunction<Object> foo = new ConvertFunction<Object>() {
+        Helper.ConvertFunctionConverter<Object> foo = new Helper.ConvertFunctionConverter<Object>() {
             @Override
             public Object convert(Object obj, Type type) throws Exception {
                 if (!(obj instanceof List))
@@ -164,8 +164,8 @@ public class ConverterBuilderTest {
         };
 
         ConverterBuilder cb = converter.newConverterBuilder();
-        cb.rule((v,t) -> converter.convert(1).to(t));
-        cb.rule(foo);
+        cb.rule(convert((v,t) -> converter.convert(1).to(t)));
+        cb.rule(convert(foo));
         Converter ca = cb.build();
 
         // The catchall converter should be called always because it can handle all and was registered first
@@ -186,7 +186,7 @@ public class ConverterBuilderTest {
                 Arrays.sort(v, Collections.reverseOrder());
                 return new CopyOnWriteArrayList<>(Arrays.asList(v));
             }) {});
-        cb.rule((v,t) -> { snooped.put(v,t); return null;});
+        cb.rule(convert((v,t) -> { snooped.put(v,t); return null;}));
         Converter ca = cb.build();
 
         assertEquals(new ArrayList<>(Arrays.asList("c", "b", "a")), ca.convert(

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1789967&r1=1789966&r2=1789967&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java Mon Apr  3 11:36:48 2017
@@ -62,6 +62,7 @@ import org.osgi.util.converter.Rule;
 import org.osgi.util.converter.StandardConverter;
 import org.osgi.util.converter.TypeReference;
 
+import static org.apache.felix.converter.impl.Helper.convert;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -335,8 +336,8 @@ public class ConverterTest {
     @Test
     public void testCustomIntArrayConversion() {
         ConverterBuilder cb = converter.newConverterBuilder();
-        cb.rule(String.class, (f,t) -> f instanceof int[] ? Arrays.stream((int []) f).mapToObj(Integer::toString).collect(Collectors.joining(",")) : null);
-        cb.rule(int[].class, (f,t) -> f instanceof String ? Arrays.stream(((String) f).split(",")).mapToInt(Integer::parseInt).toArray() : null);
+        cb.rule(String.class, convert((f,t) -> f instanceof int[] ? Arrays.stream((int []) f).mapToObj(Integer::toString).collect(Collectors.joining(",")) : null));
+        cb.rule(int[].class, convert((f,t) -> f instanceof String ? Arrays.stream(((String) f).split(",")).mapToInt(Integer::parseInt).toArray() : null));
         Converter adapted = cb.build();
 
         int[] ia = {1, 2};

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/Helper.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/Helper.java?rev=1789967&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/Helper.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/Helper.java Mon Apr  3 11:36:48 2017
@@ -0,0 +1,53 @@
+/*
+ * 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.felix.converter.impl;
+
+import org.osgi.util.converter.ConvertFunction;
+
+import java.lang.reflect.Type;
+
+public class Helper {
+
+    @FunctionalInterface
+    public interface ConvertFunctionConverter<T> {
+        T convert(Object obj, Type targetType) throws Exception;
+    }
+
+    @FunctionalInterface
+    public interface ConvertFunctionErrorHandler<T> {
+        T handleError(Object obj, Type targetType);
+    }
+
+    public static <T> ConvertFunction<T> convert(ConvertFunctionConverter<T> converter) {
+        return convert(converter, null);
+    }
+
+    public static <T> ConvertFunction<T> convert(ConvertFunctionConverter<T> converter, ConvertFunctionErrorHandler<T> errorHandler) {
+        return new ConvertFunction<T>() {
+            @Override
+            public T convert(Object obj, Type targetType) throws Exception {
+                return converter != null ? converter.convert(obj, targetType) : null;
+            }
+
+            @Override
+            public T handleError(Object obj, Type targetType) {
+                return errorHandler != null ? errorHandler.handleError(obj, targetType) : null;
+            }
+        };
+    }
+
+}