|
Java example source code file (ConverterTest.java)
The ConverterTest.java Java example source code
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed 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 com.google.common.base;
import static com.google.common.base.Functions.toStringFunction;
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.primitives.Longs;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.SerializableTester;
import junit.framework.TestCase;
import java.util.Iterator;
import java.util.List;
/**
* Unit tests for {@link Converter}.
*/
@GwtCompatible
public class ConverterTest extends TestCase {
private static final Converter<String, Long> STR_TO_LONG =
new Converter<String, Long>() {
@Override public Long doForward(String object) {
return Long.valueOf(object);
}
@Override public String doBackward(Long object) {
return String.valueOf(object);
}
@Override public String toString() {
return "string2long";
}
};
private static final Long LONG_VAL = 12345L;
private static final String STR_VAL = "12345";
private static final ImmutableList<String> STRINGS = ImmutableList.of("123", "456");
private static final ImmutableList<Long> LONGS = ImmutableList.of(123L, 456L);
public void testConverter() {
assertEquals(LONG_VAL, STR_TO_LONG.convert(STR_VAL));
assertEquals(STR_VAL, STR_TO_LONG.reverse().convert(LONG_VAL));
Iterable<Long> convertedValues = STR_TO_LONG.convertAll(STRINGS);
assertEquals(LONGS, ImmutableList.copyOf(convertedValues));
}
public void testConvertAllIsView() {
List<String> mutableList = Lists.newArrayList("789", "123");
Iterable<Long> convertedValues = STR_TO_LONG.convertAll(mutableList);
assertEquals(ImmutableList.of(789L, 123L), ImmutableList.copyOf(convertedValues));
Iterator<Long> iterator = convertedValues.iterator();
iterator.next();
iterator.remove();
assertEquals(ImmutableList.of("123"), mutableList);
}
public void testReverse() {
Converter<Long, String> reverseConverter = STR_TO_LONG.reverse();
assertEquals(STR_VAL, reverseConverter.convert(LONG_VAL));
assertEquals(LONG_VAL, reverseConverter.reverse().convert(STR_VAL));
Iterable<String> convertedValues = reverseConverter.convertAll(LONGS);
assertEquals(STRINGS, ImmutableList.copyOf(convertedValues));
assertSame(STR_TO_LONG, reverseConverter.reverse());
assertEquals("string2long.reverse()", reverseConverter.toString());
new EqualsTester()
.addEqualityGroup(STR_TO_LONG, STR_TO_LONG.reverse().reverse())
.addEqualityGroup(STR_TO_LONG.reverse(), STR_TO_LONG.reverse())
.testEquals();
}
public void testReverseReverse() {
Converter<String, Long> converter = STR_TO_LONG;
assertEquals(converter, converter.reverse().reverse());
}
public void testApply() {
assertEquals(LONG_VAL, STR_TO_LONG.apply(STR_VAL));
}
private static class StringWrapper {
private final String value;
public StringWrapper(String value) {
this.value = value;
}
}
public void testAndThen() {
Converter<StringWrapper, String> first = new Converter
Other Java examples (source code examples)Here is a short list of links related to this Java ConverterTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.