1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.base;
18 
19 import static com.google.common.base.Functions.toStringFunction;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.Lists;
24 import com.google.common.primitives.Longs;
25 import com.google.common.testing.EqualsTester;
26 import com.google.common.testing.SerializableTester;
27 
28 import junit.framework.TestCase;
29 
30 import java.util.Iterator;
31 import java.util.List;
32 
33 /**
34  * Unit tests for {@link Converter}.
35  */
36 @GwtCompatible
37 public class ConverterTest extends TestCase {
38 
39   private static final Converter<String, Long> STR_TO_LONG =
40       new Converter<String, Long>() {
41         @Override public Long doForward(String object) {
42           return Long.valueOf(object);
43         }
44 
45         @Override public String doBackward(Long object) {
46           return String.valueOf(object);
47         }
48 
49         @Override public String toString() {
50           return "string2long";
51         }
52       };
53 
54   private static final Long LONG_VAL = 12345L;
55   private static final String STR_VAL = "12345";
56 
57   private static final ImmutableList<String> STRINGS = ImmutableList.of("123", "456");
58   private static final ImmutableList<Long> LONGS = ImmutableList.of(123L, 456L);
59 
testConverter()60   public void testConverter() {
61     assertEquals(LONG_VAL, STR_TO_LONG.convert(STR_VAL));
62     assertEquals(STR_VAL, STR_TO_LONG.reverse().convert(LONG_VAL));
63 
64     Iterable<Long> convertedValues = STR_TO_LONG.convertAll(STRINGS);
65     assertEquals(LONGS, ImmutableList.copyOf(convertedValues));
66   }
67 
testConvertAllIsView()68   public void testConvertAllIsView() {
69     List<String> mutableList = Lists.newArrayList("789", "123");
70     Iterable<Long> convertedValues = STR_TO_LONG.convertAll(mutableList);
71     assertEquals(ImmutableList.of(789L, 123L), ImmutableList.copyOf(convertedValues));
72 
73     Iterator<Long> iterator = convertedValues.iterator();
74     iterator.next();
75     iterator.remove();
76     assertEquals(ImmutableList.of("123"), mutableList);
77   }
78 
testReverse()79   public void testReverse() {
80     Converter<Long, String> reverseConverter = STR_TO_LONG.reverse();
81 
82     assertEquals(STR_VAL, reverseConverter.convert(LONG_VAL));
83     assertEquals(LONG_VAL, reverseConverter.reverse().convert(STR_VAL));
84 
85     Iterable<String> convertedValues = reverseConverter.convertAll(LONGS);
86     assertEquals(STRINGS, ImmutableList.copyOf(convertedValues));
87 
88     assertSame(STR_TO_LONG, reverseConverter.reverse());
89 
90     assertEquals("string2long.reverse()", reverseConverter.toString());
91 
92     new EqualsTester()
93         .addEqualityGroup(STR_TO_LONG, STR_TO_LONG.reverse().reverse())
94         .addEqualityGroup(STR_TO_LONG.reverse(), STR_TO_LONG.reverse())
95         .testEquals();
96   }
97 
testReverseReverse()98   public void testReverseReverse() {
99     Converter<String, Long> converter = STR_TO_LONG;
100     assertEquals(converter, converter.reverse().reverse());
101   }
102 
testApply()103   public void testApply() {
104     assertEquals(LONG_VAL, STR_TO_LONG.apply(STR_VAL));
105   }
106 
107   private static class StringWrapper {
108     private final String value;
109 
StringWrapper(String value)110     public StringWrapper(String value) {
111       this.value = value;
112     }
113   }
114 
testAndThen()115   public void testAndThen() {
116     Converter<StringWrapper, String> first = new Converter<StringWrapper, String>() {
117       @Override public String doForward(StringWrapper object) {
118         return object.value;
119       }
120 
121       @Override public StringWrapper doBackward(String object) {
122         return new StringWrapper(object);
123       }
124 
125       @Override public String toString() {
126         return "StringWrapper";
127       }
128     };
129 
130     Converter<StringWrapper, Long> converter = first.andThen(STR_TO_LONG);
131 
132     assertEquals(LONG_VAL, converter.convert(new StringWrapper(STR_VAL)));
133     assertEquals(STR_VAL, converter.reverse().convert(LONG_VAL).value);
134 
135     assertEquals("StringWrapper.andThen(string2long)", converter.toString());
136 
137     assertEquals(first.andThen(STR_TO_LONG), first.andThen(STR_TO_LONG));
138   }
139 
testIdentityConverter()140   public void testIdentityConverter() {
141     Converter<String, String> stringIdentityConverter = Converter.identity();
142 
143     assertSame(stringIdentityConverter, stringIdentityConverter.reverse());
144     assertSame(STR_TO_LONG, stringIdentityConverter.andThen(STR_TO_LONG));
145 
146     assertSame(STR_VAL, stringIdentityConverter.convert(STR_VAL));
147     assertSame(STR_VAL, stringIdentityConverter.reverse().convert(STR_VAL));
148 
149     assertEquals("Converter.identity()", stringIdentityConverter.toString());
150 
151     assertSame(Converter.identity(), Converter.identity());
152   }
153 
testFrom()154   public void testFrom() {
155     Function<String, Integer> forward = new Function<String, Integer>() {
156       @Override public Integer apply(String input) {
157         return Integer.parseInt(input);
158       }
159     };
160     Function<Object, String> backward = toStringFunction();
161 
162     Converter<String, Number> converter = Converter.<String, Number>from(forward, backward);
163 
164     assertNull(converter.convert(null));
165     assertNull(converter.reverse().convert(null));
166 
167     assertEquals((Integer) 5, converter.convert("5"));
168     assertEquals("5", converter.reverse().convert(5));
169   }
170 
testNullIsPassedThrough()171   public void testNullIsPassedThrough() {
172     Converter<String, String> nullsArePassed = sillyConverter(false);
173     assertEquals("forward", nullsArePassed.convert("foo"));
174     assertEquals("forward", nullsArePassed.convert(null));
175     assertEquals("backward", nullsArePassed.reverse().convert("foo"));
176     assertEquals("backward", nullsArePassed.reverse().convert(null));
177   }
178 
testNullIsNotPassedThrough()179   public void testNullIsNotPassedThrough() {
180     Converter<String, String> nullsAreHandled = sillyConverter(true);
181     assertEquals("forward", nullsAreHandled.convert("foo"));
182     assertEquals(null, nullsAreHandled.convert(null));
183     assertEquals("backward", nullsAreHandled.reverse().convert("foo"));
184     assertEquals(null, nullsAreHandled.reverse().convert(null));
185   }
186 
sillyConverter(final boolean handleNullAutomatically)187   private static Converter<String, String> sillyConverter(final boolean handleNullAutomatically) {
188     return new Converter<String, String>(handleNullAutomatically) {
189       @Override public String doForward(String string) {
190         return "forward";
191       }
192       @Override public String doBackward(String string) {
193         return "backward";
194       }
195     };
196   }
197 
198   public void testSerialization_identity() {
199     Converter<String, String> identityConverter = Converter.identity();
200     SerializableTester.reserializeAndAssert(identityConverter);
201   }
202 
203   public void testSerialization_reverse() {
204     Converter<Long, String> reverseConverter = Longs.stringConverter().reverse();
205     SerializableTester.reserializeAndAssert(reverseConverter);
206   }
207 
208   public void testSerialization_andThen() {
209     Converter<String, Long> converterA = Longs.stringConverter();
210     Converter<Long, String> reverseConverter = Longs.stringConverter().reverse();
211     Converter<String, String> composedConverter = converterA.andThen(reverseConverter);
212     SerializableTester.reserializeAndAssert(composedConverter);
213   }
214 
215   public void testSerialization_from() {
216     Converter<String, String> dumb = Converter.from(toStringFunction(), toStringFunction());
217     SerializableTester.reserializeAndAssert(dumb);
218   }
219 }
220