/* * Copyright (C) 2012 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.testing; import com.google.common.base.CharMatcher; import com.google.common.base.Equivalence; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.base.Ticker; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.BiMap; import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBiMap; import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultiset; import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultiset; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedMultiset; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.ImmutableTable; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.LinkedHashMultiset; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multiset; import com.google.common.collect.Ordering; import com.google.common.collect.Range; import com.google.common.collect.RowSortedTable; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.SortedMultiset; import com.google.common.collect.Table; import com.google.common.collect.TreeBasedTable; import com.google.common.collect.TreeMultiset; import com.google.common.primitives.UnsignedInteger; import com.google.common.primitives.UnsignedLong; import com.google.common.reflect.TypeToken; import junit.framework.TestCase; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.LongBuffer; import java.nio.ShortBuffer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Currency; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import java.util.regex.MatchResult; import java.util.regex.Pattern; /** * Tests for {@link FreshValueGenerator}. * * @author Ben Yu */ public class FreshValueGeneratorTest extends TestCase { public void testFreshInstance() { assertFreshInstances( String.class, CharSequence.class, Appendable.class, StringBuffer.class, StringBuilder.class, Pattern.class, MatchResult.class, Number.class, int.class, Integer.class, long.class, Long.class, short.class, Short.class, byte.class, Byte.class, boolean.class, Boolean.class, char.class, Character.class, int[].class, Object[].class, UnsignedInteger.class, UnsignedLong.class, BigInteger.class, BigDecimal.class, Throwable.class, Error.class, Exception.class, RuntimeException.class, Charset.class, Locale.class, Currency.class, List.class, Map.Entry.class, Object.class, Equivalence.class, Predicate.class, Function.class, Comparable.class, Comparator.class, Ordering.class, Class.class, Type.class, TypeToken.class, TimeUnit.class, Ticker.class, Joiner.class, Splitter.class, CharMatcher.class, InputStream.class, ByteArrayInputStream.class, Reader.class, Readable.class, StringReader.class, OutputStream.class, ByteArrayOutputStream.class, Writer.class, StringWriter.class, File.class, Buffer.class, ByteBuffer.class, CharBuffer.class, ShortBuffer.class, IntBuffer.class, LongBuffer.class, FloatBuffer.class, DoubleBuffer.class, String[].class, Object[].class, int[].class); } public void testStringArray() { FreshValueGenerator generator = new FreshValueGenerator(); String[] a1 = generator.generate(String[].class); String[] a2 = generator.generate(String[].class); assertFalse(a1[0].equals(a2[0])); } public void testPrimitiveArray() { FreshValueGenerator generator = new FreshValueGenerator(); int[] a1 = generator.generate(int[].class); int[] a2 = generator.generate(int[].class); assertTrue(a1[0] != a2[0]); } public void testRange() { assertFreshInstance(new TypeToken>() {}); } public void testImmutableList() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(int.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testImmutableSet() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testImmutableSortedSet() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals( ImmutableSortedSet.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testImmutableMultiset() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(ImmutableMultiset.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(ImmutableMultiset.of(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testImmutableCollection() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testImmutableMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableMap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testImmutableSortedMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableSortedMap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate( new TypeToken>() {})); } public void testImmutableMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableMultimap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testImmutableListMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableListMultimap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate( new TypeToken>() {})); } public void testImmutableSetMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableSetMultimap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate( new TypeToken>() {})); } public void testImmutableBiMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableBiMap.of(generator.generate(String.class), generator.generate(int.class)), new FreshValueGenerator().generate( new TypeToken>() {})); } public void testImmutableTable() { assertFreshInstance(new TypeToken>>() {}); FreshValueGenerator generator = new FreshValueGenerator(); assertValueAndTypeEquals( ImmutableTable.of( generator.generate(String.class), generator.generate(int.class), generator.generate(new TypeToken>() {})), new FreshValueGenerator().generate( new TypeToken>>() {})); } public void testList() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(int.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testArrayList() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(int.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testLinkedList() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(newLinkedList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testSet() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals( newLinkedHashSet(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashSet() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals( newLinkedHashSet(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testLinkedHashSet() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals( newLinkedHashSet(new FreshValueGenerator().generate(Number.class)), new FreshValueGenerator().generate(new TypeToken>() {})); } public void testTreeSet() { assertFreshInstance(new TypeToken>() {}); TreeSet expected = Sets.newTreeSet(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testSortedSet() { assertFreshInstance(new TypeToken>() {}); TreeSet expected = Sets.newTreeSet(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testNavigableSet() { assertFreshInstance(new TypeToken>() {}); TreeSet expected = Sets.newTreeSet(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testMultiset() { assertFreshInstance(new TypeToken>() {}); Multiset expected = HashMultiset.create(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testSortedMultiset() { assertFreshInstance(new TypeToken>() {}); SortedMultiset expected = TreeMultiset.create(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashMultiset() { assertFreshInstance(new TypeToken>() {}); HashMultiset expected = HashMultiset.create(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testLinkedHashMultiset() { assertFreshInstance(new TypeToken>() {}); LinkedHashMultiset expected = LinkedHashMultiset.create(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testTreeMultiset() { assertFreshInstance(new TypeToken>() {}); TreeMultiset expected = TreeMultiset.create(); expected.add(new FreshValueGenerator().generate(String.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testImmutableSortedMultiset() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals( ImmutableSortedMultiset.of(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testCollection() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testIterable() { assertFreshInstance(new TypeToken>() {}); assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)), new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); Map expected = Maps.newLinkedHashMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); HashMap expected = Maps.newLinkedHashMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testLinkedHashMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); LinkedHashMap expected = Maps.newLinkedHashMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testTreeMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); TreeMap expected = Maps.newTreeMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testSortedMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); TreeMap expected = Maps.newTreeMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testNavigableMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); TreeMap expected = Maps.newTreeMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testConcurrentMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); ConcurrentMap expected = Maps.newConcurrentMap(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); Multimap expected = ArrayListMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); HashMultimap expected = HashMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate(new TypeToken>() {})); } public void testLinkedHashMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); LinkedHashMultimap expected = LinkedHashMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); } public void testListMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); ListMultimap expected = ArrayListMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testArrayListMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); ArrayListMultimap expected = ArrayListMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); } public void testSetMultimap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); SetMultimap expected = LinkedHashMultimap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testBiMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); BiMap expected = HashBiMap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashBiMap() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); HashBiMap expected = HashBiMap.create(); expected.put(generator.generate(String.class), generator.generate(int.class)); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>() {})); } public void testTable() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); Table expected = HashBasedTable.create(); expected.put(generator.generate(String.class), generator.generate(int.class), generator.generate(new TypeToken>() {})); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testHashBasedTable() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); HashBasedTable expected = HashBasedTable.create(); expected.put(generator.generate(String.class), generator.generate(int.class), generator.generate(new TypeToken>() {})); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>>() {})); } public void testRowSortedTable() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); RowSortedTable expected = TreeBasedTable.create(); expected.put(generator.generate(String.class), generator.generate(int.class), generator.generate(new TypeToken>() {})); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>>() {})); assertNotInstantiable(new TypeToken>() {}); } public void testTreeBasedTable() { assertFreshInstance(new TypeToken>() {}); FreshValueGenerator generator = new FreshValueGenerator(); TreeBasedTable expected = TreeBasedTable.create(); expected.put(generator.generate(String.class), generator.generate(int.class), generator.generate(new TypeToken>() {})); assertValueAndTypeEquals(expected, new FreshValueGenerator().generate( new TypeToken>>() {})); } public void testObject() { assertEquals(new FreshValueGenerator().generate(String.class), new FreshValueGenerator().generate(Object.class)); } public void testEnums() { assertEqualInstance(EmptyEnum.class, null); assertEqualInstance(OneConstantEnum.class, OneConstantEnum.CONSTANT1); assertFreshInstance(TwoConstantEnum.class); assertFreshInstance(new TypeToken>() {}); } public void testOptional() { FreshValueGenerator generator = new FreshValueGenerator(); assertEquals(Optional.absent(), generator.generate(new TypeToken>() {})); assertEquals(Optional.of("1"), generator.generate(new TypeToken>() {})); // Test that the first generated instance for different Optional is always absent(). // Having generated Optional instances doesn't prevent absent() from being generated for // other Optional types. assertEquals(Optional.absent(), generator.generate(new TypeToken>() {})); assertEquals(Optional.of(OneConstantEnum.CONSTANT1), generator.generate(new TypeToken>() {})); } public void testAddSampleInstances_twoInstances() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.of("a", "b")); assertEquals("a", generator.generate(String.class)); assertEquals("b", generator.generate(String.class)); assertEquals("a", generator.generate(String.class)); } public void testAddSampleInstances_oneInstance() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.of("a")); assertEquals("a", generator.generate(String.class)); assertEquals("a", generator.generate(String.class)); } public void testAddSampleInstances_noInstance() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.of()); assertEquals(new FreshValueGenerator().generate(String.class), generator.generate(String.class)); } public void testFreshCurrency() { FreshValueGenerator generator = new FreshValueGenerator(); // repeat a few times to make sure we don't stumble upon a bad Locale assertNotNull(generator.generate(Currency.class)); assertNotNull(generator.generate(Currency.class)); assertNotNull(generator.generate(Currency.class)); } public void testNulls() throws Exception { new ClassSanityTester() .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls")) .testNulls(FreshValueGenerator.class); } private static void assertFreshInstances(Class... types) { for (Class type : types) { assertFreshInstance(type); } } private static void assertFreshInstance(TypeToken type) { FreshValueGenerator generator = new FreshValueGenerator(); Object value1 = generator.generate(type); Object value2 = generator.generate(type); assertNotNull("Null returned for " + type, value1); assertFalse("Equal instance " + value1 + " returned for " + type, value1.equals(value2)); } private static void assertFreshInstance(Class type) { assertFreshInstance(TypeToken.of(type)); } private static void assertEqualInstance(Class type, T value) { FreshValueGenerator generator = new FreshValueGenerator(); assertEquals(value, generator.generate(type)); assertEquals(value, generator.generate(type)); } private enum EmptyEnum {} private enum OneConstantEnum { CONSTANT1 } private enum TwoConstantEnum { CONSTANT1, CONSTANT2 } private static void assertValueAndTypeEquals(Object expected, Object actual) { assertEquals(expected, actual); assertEquals(expected.getClass(), actual.getClass()); } private static void assertNotInstantiable(TypeToken type) { assertNull(new FreshValueGenerator().generate(type)); } private static LinkedHashSet newLinkedHashSet(E element) { LinkedHashSet set = Sets.newLinkedHashSet(); set.add(element); return set; } private static LinkedList newLinkedList(E element) { LinkedList list = Lists.newLinkedList(); list.add(element); return list; } }