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.annotations.GwtIncompatible; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.collect.Lists; 25 import com.google.common.primitives.Longs; 26 import com.google.common.testing.EqualsTester; 27 import com.google.common.testing.SerializableTester; 28 import java.util.Iterator; 29 import java.util.List; 30 import junit.framework.TestCase; 31 32 /** Unit tests for {@link Converter}. */ 33 @GwtCompatible(emulated = true) 34 public class ConverterTest extends TestCase { 35 36 private static final Converter<String, Long> STR_TO_LONG = 37 new Converter<String, Long>() { 38 @Override 39 protected Long doForward(String object) { 40 return Long.valueOf(object); 41 } 42 43 @Override 44 protected String doBackward(Long object) { 45 return String.valueOf(object); 46 } 47 48 @Override 49 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 115 @GwtIncompatible // J2CL generics problem testAndThen()116 public void testAndThen() { 117 Converter<StringWrapper, String> first = 118 new Converter<StringWrapper, String>() { 119 @Override 120 protected String doForward(StringWrapper object) { 121 return object.value; 122 } 123 124 @Override 125 protected StringWrapper doBackward(String object) { 126 return new StringWrapper(object); 127 } 128 129 @Override 130 public String toString() { 131 return "StringWrapper"; 132 } 133 }; 134 135 Converter<StringWrapper, Long> converter = first.andThen(STR_TO_LONG); 136 137 assertEquals(LONG_VAL, converter.convert(new StringWrapper(STR_VAL))); 138 assertEquals(STR_VAL, converter.reverse().convert(LONG_VAL).value); 139 140 assertEquals("StringWrapper.andThen(string2long)", converter.toString()); 141 142 assertEquals(first.andThen(STR_TO_LONG), first.andThen(STR_TO_LONG)); 143 } 144 145 @GwtIncompatible // J2CL generics problem testIdentityConverter()146 public void testIdentityConverter() { 147 Converter<String, String> stringIdentityConverter = Converter.identity(); 148 149 assertSame(stringIdentityConverter, stringIdentityConverter.reverse()); 150 assertSame(STR_TO_LONG, stringIdentityConverter.andThen(STR_TO_LONG)); 151 152 assertSame(STR_VAL, stringIdentityConverter.convert(STR_VAL)); 153 assertSame(STR_VAL, stringIdentityConverter.reverse().convert(STR_VAL)); 154 155 assertEquals("Converter.identity()", stringIdentityConverter.toString()); 156 157 assertSame(Converter.identity(), Converter.identity()); 158 } 159 testFrom()160 public void testFrom() { 161 Function<String, Integer> forward = 162 new Function<String, Integer>() { 163 @Override 164 public Integer apply(String input) { 165 return Integer.parseInt(input); 166 } 167 }; 168 Function<Object, String> backward = toStringFunction(); 169 170 Converter<String, Number> converter = Converter.<String, Number>from(forward, backward); 171 172 assertNull(converter.convert(null)); 173 assertNull(converter.reverse().convert(null)); 174 175 assertEquals((Integer) 5, converter.convert("5")); 176 assertEquals("5", converter.reverse().convert(5)); 177 } 178 testNullIsPassedThrough()179 public void testNullIsPassedThrough() { 180 Converter<String, String> nullsArePassed = sillyConverter(false); 181 assertEquals("forward", nullsArePassed.convert("foo")); 182 assertEquals("forward", nullsArePassed.convert(null)); 183 assertEquals("backward", nullsArePassed.reverse().convert("foo")); 184 assertEquals("backward", nullsArePassed.reverse().convert(null)); 185 } 186 testNullIsNotPassedThrough()187 public void testNullIsNotPassedThrough() { 188 Converter<String, String> nullsAreHandled = sillyConverter(true); 189 assertEquals("forward", nullsAreHandled.convert("foo")); 190 assertEquals(null, nullsAreHandled.convert(null)); 191 assertEquals("backward", nullsAreHandled.reverse().convert("foo")); 192 assertEquals(null, nullsAreHandled.reverse().convert(null)); 193 } 194 sillyConverter(final boolean handleNullAutomatically)195 private static Converter<String, String> sillyConverter(final boolean handleNullAutomatically) { 196 return new Converter<String, String>(handleNullAutomatically) { 197 @Override 198 protected String doForward(String string) { 199 return "forward"; 200 } 201 202 @Override 203 protected String doBackward(String string) { 204 return "backward"; 205 } 206 }; 207 } 208 209 public void testSerialization_identity() { 210 Converter<String, String> identityConverter = Converter.identity(); 211 SerializableTester.reserializeAndAssert(identityConverter); 212 } 213 214 public void testSerialization_reverse() { 215 Converter<Long, String> reverseConverter = Longs.stringConverter().reverse(); 216 SerializableTester.reserializeAndAssert(reverseConverter); 217 } 218 219 @GwtIncompatible // J2CL generics problem 220 public void testSerialization_andThen() { 221 Converter<String, Long> converterA = Longs.stringConverter(); 222 Converter<Long, String> reverseConverter = Longs.stringConverter().reverse(); 223 Converter<String, String> composedConverter = converterA.andThen(reverseConverter); 224 SerializableTester.reserializeAndAssert(composedConverter); 225 } 226 227 public void testSerialization_from() { 228 Converter<String, String> dumb = Converter.from(toStringFunction(), toStringFunction()); 229 SerializableTester.reserializeAndAssert(dumb); 230 } 231 } 232