• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.testing;
18 
19 import com.google.common.base.CharMatcher;
20 import com.google.common.base.Equivalence;
21 import com.google.common.base.Function;
22 import com.google.common.base.Joiner;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Predicate;
25 import com.google.common.base.Splitter;
26 import com.google.common.base.Ticker;
27 import com.google.common.collect.ArrayListMultimap;
28 import com.google.common.collect.BiMap;
29 import com.google.common.collect.HashBasedTable;
30 import com.google.common.collect.HashBiMap;
31 import com.google.common.collect.HashMultimap;
32 import com.google.common.collect.HashMultiset;
33 import com.google.common.collect.ImmutableBiMap;
34 import com.google.common.collect.ImmutableCollection;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableListMultimap;
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.ImmutableMultimap;
39 import com.google.common.collect.ImmutableMultiset;
40 import com.google.common.collect.ImmutableSet;
41 import com.google.common.collect.ImmutableSetMultimap;
42 import com.google.common.collect.ImmutableSortedMap;
43 import com.google.common.collect.ImmutableSortedMultiset;
44 import com.google.common.collect.ImmutableSortedSet;
45 import com.google.common.collect.ImmutableTable;
46 import com.google.common.collect.LinkedHashMultimap;
47 import com.google.common.collect.LinkedHashMultiset;
48 import com.google.common.collect.ListMultimap;
49 import com.google.common.collect.Lists;
50 import com.google.common.collect.Maps;
51 import com.google.common.collect.Multimap;
52 import com.google.common.collect.Multiset;
53 import com.google.common.collect.Ordering;
54 import com.google.common.collect.Range;
55 import com.google.common.collect.RowSortedTable;
56 import com.google.common.collect.SetMultimap;
57 import com.google.common.collect.Sets;
58 import com.google.common.collect.SortedMultiset;
59 import com.google.common.collect.Table;
60 import com.google.common.collect.TreeBasedTable;
61 import com.google.common.collect.TreeMultiset;
62 import com.google.common.primitives.UnsignedInteger;
63 import com.google.common.primitives.UnsignedLong;
64 import com.google.common.reflect.TypeToken;
65 
66 import junit.framework.TestCase;
67 
68 import java.io.ByteArrayInputStream;
69 import java.io.ByteArrayOutputStream;
70 import java.io.File;
71 import java.io.InputStream;
72 import java.io.OutputStream;
73 import java.io.Reader;
74 import java.io.StringReader;
75 import java.io.StringWriter;
76 import java.io.Writer;
77 import java.lang.reflect.Method;
78 import java.lang.reflect.Type;
79 import java.math.BigDecimal;
80 import java.math.BigInteger;
81 import java.nio.Buffer;
82 import java.nio.ByteBuffer;
83 import java.nio.CharBuffer;
84 import java.nio.DoubleBuffer;
85 import java.nio.FloatBuffer;
86 import java.nio.IntBuffer;
87 import java.nio.LongBuffer;
88 import java.nio.ShortBuffer;
89 import java.nio.charset.Charset;
90 import java.util.ArrayList;
91 import java.util.Collection;
92 import java.util.Comparator;
93 import java.util.Currency;
94 import java.util.HashMap;
95 import java.util.HashSet;
96 import java.util.LinkedHashMap;
97 import java.util.LinkedHashSet;
98 import java.util.LinkedList;
99 import java.util.List;
100 import java.util.Locale;
101 import java.util.Map;
102 import java.util.NavigableMap;
103 import java.util.NavigableSet;
104 import java.util.Set;
105 import java.util.SortedMap;
106 import java.util.SortedSet;
107 import java.util.TreeMap;
108 import java.util.TreeSet;
109 import java.util.concurrent.ConcurrentMap;
110 import java.util.concurrent.TimeUnit;
111 import java.util.regex.MatchResult;
112 import java.util.regex.Pattern;
113 
114 /**
115  * Tests for {@link FreshValueGenerator}.
116  *
117  * @author Ben Yu
118  */
119 public class FreshValueGeneratorTest extends TestCase {
120 
testFreshInstance()121   public void testFreshInstance() {
122     assertFreshInstances(
123         String.class, CharSequence.class,
124         Appendable.class, StringBuffer.class, StringBuilder.class,
125         Pattern.class, MatchResult.class,
126         Number.class, int.class, Integer.class,
127         long.class, Long.class,
128         short.class, Short.class,
129         byte.class, Byte.class,
130         boolean.class, Boolean.class,
131         char.class, Character.class,
132         int[].class, Object[].class,
133         UnsignedInteger.class, UnsignedLong.class,
134         BigInteger.class, BigDecimal.class,
135         Throwable.class, Error.class, Exception.class, RuntimeException.class,
136         Charset.class, Locale.class, Currency.class,
137         List.class, Map.Entry.class,
138         Object.class,
139         Equivalence.class, Predicate.class, Function.class,
140         Comparable.class, Comparator.class, Ordering.class,
141         Class.class, Type.class, TypeToken.class,
142         TimeUnit.class, Ticker.class,
143         Joiner.class, Splitter.class, CharMatcher.class,
144         InputStream.class, ByteArrayInputStream.class,
145         Reader.class, Readable.class, StringReader.class,
146         OutputStream.class, ByteArrayOutputStream.class,
147         Writer.class, StringWriter.class, File.class,
148         Buffer.class, ByteBuffer.class, CharBuffer.class,
149         ShortBuffer.class, IntBuffer.class, LongBuffer.class,
150         FloatBuffer.class, DoubleBuffer.class,
151         String[].class, Object[].class, int[].class);
152   }
153 
testStringArray()154   public void testStringArray() {
155     FreshValueGenerator generator = new FreshValueGenerator();
156     String[] a1 = generator.generate(String[].class);
157     String[] a2 = generator.generate(String[].class);
158     assertFalse(a1[0].equals(a2[0]));
159   }
160 
testPrimitiveArray()161   public void testPrimitiveArray() {
162     FreshValueGenerator generator = new FreshValueGenerator();
163     int[] a1 = generator.generate(int[].class);
164     int[] a2 = generator.generate(int[].class);
165     assertTrue(a1[0] != a2[0]);
166   }
167 
testRange()168   public void testRange() {
169     assertFreshInstance(new TypeToken<Range<String>>() {});
170   }
171 
testImmutableList()172   public void testImmutableList() {
173     assertFreshInstance(new TypeToken<ImmutableList<String>>() {});
174     assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)),
175         new FreshValueGenerator().generate(new TypeToken<ImmutableList<String>>() {}));
176     assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(int.class)),
177         new FreshValueGenerator().generate(new TypeToken<ImmutableList<Integer>>() {}));
178     assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)),
179         new FreshValueGenerator().generate(new TypeToken<ImmutableList<?>>() {}));
180   }
181 
testImmutableSet()182   public void testImmutableSet() {
183     assertFreshInstance(new TypeToken<ImmutableSet<String>>() {});
184     assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(String.class)),
185         new FreshValueGenerator().generate(new TypeToken<ImmutableSet<String>>() {}));
186     assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(Number.class)),
187         new FreshValueGenerator().generate(new TypeToken<ImmutableSet<Number>>() {}));
188     assertValueAndTypeEquals(ImmutableSet.of(new FreshValueGenerator().generate(Number.class)),
189         new FreshValueGenerator().generate(new TypeToken<ImmutableSet<? extends Number>>() {}));
190   }
191 
testImmutableSortedSet()192   public void testImmutableSortedSet() {
193     assertFreshInstance(new TypeToken<ImmutableSortedSet<String>>() {});
194     assertValueAndTypeEquals(
195         ImmutableSortedSet.of(new FreshValueGenerator().generate(String.class)),
196         new FreshValueGenerator().generate(new TypeToken<ImmutableSortedSet<String>>() {}));
197   }
198 
testImmutableMultiset()199   public void testImmutableMultiset() {
200     assertFreshInstance(new TypeToken<ImmutableSortedSet<String>>() {});
201     assertValueAndTypeEquals(ImmutableMultiset.of(new FreshValueGenerator().generate(String.class)),
202         new FreshValueGenerator().generate(new TypeToken<ImmutableMultiset<String>>() {}));
203     assertValueAndTypeEquals(ImmutableMultiset.of(new FreshValueGenerator().generate(Number.class)),
204         new FreshValueGenerator().generate(new TypeToken<ImmutableMultiset<Number>>() {}));
205     assertNotInstantiable(new TypeToken<ImmutableMultiset<EmptyEnum>>() {});
206   }
207 
testImmutableCollection()208   public void testImmutableCollection() {
209     assertFreshInstance(new TypeToken<ImmutableCollection<String>>() {});
210     assertValueAndTypeEquals(ImmutableList.of(new FreshValueGenerator().generate(String.class)),
211         new FreshValueGenerator().generate(new TypeToken<ImmutableCollection<String>>() {}));
212     assertNotInstantiable(new TypeToken<ImmutableCollection<EmptyEnum>>() {});
213   }
214 
testImmutableMap()215   public void testImmutableMap() {
216     assertFreshInstance(new TypeToken<ImmutableMap<String, Integer>>() {});
217     FreshValueGenerator generator = new FreshValueGenerator();
218     assertValueAndTypeEquals(
219         ImmutableMap.of(generator.generate(String.class), generator.generate(int.class)),
220         new FreshValueGenerator().generate(new TypeToken<ImmutableMap<String, Integer>>() {}));
221   }
222 
testImmutableSortedMap()223   public void testImmutableSortedMap() {
224     assertFreshInstance(new TypeToken<ImmutableSortedMap<String, Integer>>() {});
225     FreshValueGenerator generator = new FreshValueGenerator();
226     assertValueAndTypeEquals(
227         ImmutableSortedMap.of(generator.generate(String.class), generator.generate(int.class)),
228         new FreshValueGenerator().generate(
229             new TypeToken<ImmutableSortedMap<String, Integer>>() {}));
230   }
231 
testImmutableMultimap()232   public void testImmutableMultimap() {
233     assertFreshInstance(new TypeToken<ImmutableMultimap<String, Integer>>() {});
234     FreshValueGenerator generator = new FreshValueGenerator();
235     assertValueAndTypeEquals(
236         ImmutableMultimap.of(generator.generate(String.class), generator.generate(int.class)),
237         new FreshValueGenerator().generate(new TypeToken<ImmutableMultimap<String, Integer>>() {}));
238     assertNotInstantiable(new TypeToken<ImmutableMultimap<EmptyEnum, String>>() {});
239   }
240 
testImmutableListMultimap()241   public void testImmutableListMultimap() {
242     assertFreshInstance(new TypeToken<ImmutableListMultimap<String, Integer>>() {});
243     FreshValueGenerator generator = new FreshValueGenerator();
244     assertValueAndTypeEquals(
245         ImmutableListMultimap.of(generator.generate(String.class), generator.generate(int.class)),
246         new FreshValueGenerator().generate(
247             new TypeToken<ImmutableListMultimap<String, Integer>>() {}));
248   }
249 
testImmutableSetMultimap()250   public void testImmutableSetMultimap() {
251     assertFreshInstance(new TypeToken<ImmutableSetMultimap<String, Integer>>() {});
252     FreshValueGenerator generator = new FreshValueGenerator();
253     assertValueAndTypeEquals(
254         ImmutableSetMultimap.of(generator.generate(String.class), generator.generate(int.class)),
255         new FreshValueGenerator().generate(
256             new TypeToken<ImmutableSetMultimap<String, Integer>>() {}));
257   }
258 
testImmutableBiMap()259   public void testImmutableBiMap() {
260     assertFreshInstance(new TypeToken<ImmutableBiMap<String, Integer>>() {});
261     FreshValueGenerator generator = new FreshValueGenerator();
262     assertValueAndTypeEquals(
263         ImmutableBiMap.of(generator.generate(String.class), generator.generate(int.class)),
264         new FreshValueGenerator().generate(
265             new TypeToken<ImmutableBiMap<String, Integer>>() {}));
266   }
267 
testImmutableTable()268   public void testImmutableTable() {
269     assertFreshInstance(new TypeToken<ImmutableTable<String, Integer, ImmutableList<String>>>() {});
270     FreshValueGenerator generator = new FreshValueGenerator();
271     assertValueAndTypeEquals(
272         ImmutableTable.of(
273             generator.generate(String.class), generator.generate(int.class),
274             generator.generate(new TypeToken<ImmutableList<String>>() {})),
275         new FreshValueGenerator().generate(
276             new TypeToken<ImmutableTable<String, Integer, ImmutableList<String>>>() {}));
277   }
278 
testList()279   public void testList() {
280     assertFreshInstance(new TypeToken<List<String>>() {});
281     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
282         new FreshValueGenerator().generate(new TypeToken<List<String>>() {}));
283     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(int.class)),
284         new FreshValueGenerator().generate(new TypeToken<List<Integer>>() {}));
285     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
286         new FreshValueGenerator().generate(new TypeToken<List<?>>() {}));
287     assertNotInstantiable(new TypeToken<List<EmptyEnum>>() {});
288   }
289 
testArrayList()290   public void testArrayList() {
291     assertFreshInstance(new TypeToken<ArrayList<String>>() {});
292     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
293         new FreshValueGenerator().generate(new TypeToken<ArrayList<String>>() {}));
294     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(int.class)),
295         new FreshValueGenerator().generate(new TypeToken<ArrayList<Integer>>() {}));
296     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
297         new FreshValueGenerator().generate(new TypeToken<ArrayList<?>>() {}));
298     assertNotInstantiable(new TypeToken<ArrayList<EmptyEnum>>() {});
299   }
300 
testLinkedList()301   public void testLinkedList() {
302     assertFreshInstance(new TypeToken<LinkedList<String>>() {});
303     assertValueAndTypeEquals(newLinkedList(new FreshValueGenerator().generate(String.class)),
304         new FreshValueGenerator().generate(new TypeToken<LinkedList<String>>() {}));
305   }
306 
testSet()307   public void testSet() {
308     assertFreshInstance(new TypeToken<Set<String>>() {});
309     assertValueAndTypeEquals(
310         newLinkedHashSet(new FreshValueGenerator().generate(Number.class)),
311         new FreshValueGenerator().generate(new TypeToken<Set<? extends Number>>() {}));
312     assertNotInstantiable(new TypeToken<Set<EmptyEnum>>() {});
313   }
314 
testHashSet()315   public void testHashSet() {
316     assertFreshInstance(new TypeToken<HashSet<String>>() {});
317     assertValueAndTypeEquals(
318         newLinkedHashSet(new FreshValueGenerator().generate(Number.class)),
319         new FreshValueGenerator().generate(new TypeToken<HashSet<? extends Number>>() {}));
320   }
321 
testLinkedHashSet()322   public void testLinkedHashSet() {
323     assertFreshInstance(new TypeToken<LinkedHashSet<String>>() {});
324     assertValueAndTypeEquals(
325         newLinkedHashSet(new FreshValueGenerator().generate(Number.class)),
326         new FreshValueGenerator().generate(new TypeToken<LinkedHashSet<? extends Number>>() {}));
327   }
328 
testTreeSet()329   public void testTreeSet() {
330     assertFreshInstance(new TypeToken<TreeSet<String>>() {});
331     TreeSet<String> expected = Sets.newTreeSet();
332     expected.add(new FreshValueGenerator().generate(String.class));
333     assertValueAndTypeEquals(expected,
334         new FreshValueGenerator().generate(new TypeToken<TreeSet<? extends CharSequence>>() {}));
335     assertNotInstantiable(new TypeToken<TreeSet<EmptyEnum>>() {});
336   }
337 
testSortedSet()338   public void testSortedSet() {
339     assertFreshInstance(new TypeToken<SortedSet<String>>() {});
340     TreeSet<String> expected = Sets.newTreeSet();
341     expected.add(new FreshValueGenerator().generate(String.class));
342     assertValueAndTypeEquals(expected,
343         new FreshValueGenerator().generate(new TypeToken<SortedSet<String>>() {}));
344     assertNotInstantiable(new TypeToken<SortedSet<EmptyEnum>>() {});
345   }
346 
testNavigableSet()347   public void testNavigableSet() {
348     assertFreshInstance(new TypeToken<NavigableSet<String>>() {});
349     TreeSet<String> expected = Sets.newTreeSet();
350     expected.add(new FreshValueGenerator().generate(String.class));
351     assertValueAndTypeEquals(expected,
352         new FreshValueGenerator().generate(new TypeToken<NavigableSet<String>>() {}));
353     assertNotInstantiable(new TypeToken<NavigableSet<EmptyEnum>>() {});
354   }
355 
testMultiset()356   public void testMultiset() {
357     assertFreshInstance(new TypeToken<Multiset<String>>() {});
358     Multiset<String> expected = HashMultiset.create();
359     expected.add(new FreshValueGenerator().generate(String.class));
360     assertValueAndTypeEquals(expected,
361         new FreshValueGenerator().generate(new TypeToken<Multiset<String>>() {}));
362     assertNotInstantiable(new TypeToken<Multiset<EmptyEnum>>() {});
363   }
364 
testSortedMultiset()365   public void testSortedMultiset() {
366     assertFreshInstance(new TypeToken<SortedMultiset<String>>() {});
367     SortedMultiset<String> expected = TreeMultiset.create();
368     expected.add(new FreshValueGenerator().generate(String.class));
369     assertValueAndTypeEquals(expected,
370         new FreshValueGenerator().generate(new TypeToken<SortedMultiset<String>>() {}));
371     assertNotInstantiable(new TypeToken<Multiset<EmptyEnum>>() {});
372   }
373 
testHashMultiset()374   public void testHashMultiset() {
375     assertFreshInstance(new TypeToken<HashMultiset<String>>() {});
376     HashMultiset<String> expected = HashMultiset.create();
377     expected.add(new FreshValueGenerator().generate(String.class));
378     assertValueAndTypeEquals(expected,
379         new FreshValueGenerator().generate(new TypeToken<HashMultiset<String>>() {}));
380   }
381 
testLinkedHashMultiset()382   public void testLinkedHashMultiset() {
383     assertFreshInstance(new TypeToken<LinkedHashMultiset<String>>() {});
384     LinkedHashMultiset<String> expected = LinkedHashMultiset.create();
385     expected.add(new FreshValueGenerator().generate(String.class));
386     assertValueAndTypeEquals(expected,
387         new FreshValueGenerator().generate(new TypeToken<LinkedHashMultiset<String>>() {}));
388   }
389 
testTreeMultiset()390   public void testTreeMultiset() {
391     assertFreshInstance(new TypeToken<TreeMultiset<String>>() {});
392     TreeMultiset<String> expected = TreeMultiset.create();
393     expected.add(new FreshValueGenerator().generate(String.class));
394     assertValueAndTypeEquals(expected,
395         new FreshValueGenerator().generate(new TypeToken<TreeMultiset<String>>() {}));
396   }
397 
testImmutableSortedMultiset()398   public void testImmutableSortedMultiset() {
399     assertFreshInstance(new TypeToken<ImmutableSortedMultiset<String>>() {});
400     assertValueAndTypeEquals(
401         ImmutableSortedMultiset.of(new FreshValueGenerator().generate(String.class)),
402         new FreshValueGenerator().generate(new TypeToken<ImmutableSortedMultiset<String>>() {}));
403     assertNotInstantiable(new TypeToken<Multiset<EmptyEnum>>() {});
404   }
405 
testCollection()406   public void testCollection() {
407     assertFreshInstance(new TypeToken<Collection<String>>() {});
408     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
409         new FreshValueGenerator().generate(new TypeToken<Collection<String>>() {}));
410     assertNotInstantiable(new TypeToken<Collection<EmptyEnum>>() {});
411   }
412 
testIterable()413   public void testIterable() {
414     assertFreshInstance(new TypeToken<Iterable<String>>() {});
415     assertValueAndTypeEquals(Lists.newArrayList(new FreshValueGenerator().generate(String.class)),
416         new FreshValueGenerator().generate(new TypeToken<Iterable<String>>() {}));
417     assertNotInstantiable(new TypeToken<Iterable<EmptyEnum>>() {});
418   }
419 
testMap()420   public void testMap() {
421     assertFreshInstance(new TypeToken<Map<String, ?>>() {});
422     FreshValueGenerator generator = new FreshValueGenerator();
423     Map<String, Integer> expected = Maps.newLinkedHashMap();
424     expected.put(generator.generate(String.class), generator.generate(int.class));
425     assertValueAndTypeEquals(expected,
426         new FreshValueGenerator().generate(new TypeToken<Map<String, Integer>>() {}));
427     assertNotInstantiable(new TypeToken<Map<EmptyEnum, String>>() {});
428   }
429 
testHashMap()430   public void testHashMap() {
431     assertFreshInstance(new TypeToken<HashMap<String, ?>>() {});
432     FreshValueGenerator generator = new FreshValueGenerator();
433     HashMap<String, Integer> expected = Maps.newLinkedHashMap();
434     expected.put(generator.generate(String.class), generator.generate(int.class));
435     assertValueAndTypeEquals(expected,
436         new FreshValueGenerator().generate(new TypeToken<HashMap<String, Integer>>() {}));
437   }
438 
testLinkedHashMap()439   public void testLinkedHashMap() {
440     assertFreshInstance(new TypeToken<LinkedHashMap<String, ?>>() {});
441     FreshValueGenerator generator = new FreshValueGenerator();
442     LinkedHashMap<String, Integer> expected = Maps.newLinkedHashMap();
443     expected.put(generator.generate(String.class), generator.generate(int.class));
444     assertValueAndTypeEquals(expected,
445         new FreshValueGenerator().generate(new TypeToken<LinkedHashMap<String, Integer>>() {}));
446   }
447 
testTreeMap()448   public void testTreeMap() {
449     assertFreshInstance(new TypeToken<TreeMap<String, ?>>() {});
450     FreshValueGenerator generator = new FreshValueGenerator();
451     TreeMap<String, Integer> expected = Maps.newTreeMap();
452     expected.put(generator.generate(String.class), generator.generate(int.class));
453     assertValueAndTypeEquals(expected,
454         new FreshValueGenerator().generate(new TypeToken<TreeMap<String, Integer>>() {}));
455     assertNotInstantiable(new TypeToken<LinkedHashSet<EmptyEnum>>() {});
456   }
457 
testSortedMap()458   public void testSortedMap() {
459     assertFreshInstance(new TypeToken<SortedMap<?, String>>() {});
460     FreshValueGenerator generator = new FreshValueGenerator();
461     TreeMap<String, Integer> expected = Maps.newTreeMap();
462     expected.put(generator.generate(String.class), generator.generate(int.class));
463     assertValueAndTypeEquals(expected,
464         new FreshValueGenerator().generate(
465             new TypeToken<SortedMap<String, Integer>>() {}));
466     assertNotInstantiable(new TypeToken<SortedMap<EmptyEnum, String>>() {});
467   }
468 
testNavigableMap()469   public void testNavigableMap() {
470     assertFreshInstance(new TypeToken<NavigableMap<?, ?>>() {});
471     FreshValueGenerator generator = new FreshValueGenerator();
472     TreeMap<String, Integer> expected = Maps.newTreeMap();
473     expected.put(generator.generate(String.class), generator.generate(int.class));
474     assertValueAndTypeEquals(expected,
475         new FreshValueGenerator().generate(
476             new TypeToken<NavigableMap<String, Integer>>() {}));
477     assertNotInstantiable(new TypeToken<NavigableMap<EmptyEnum, String>>() {});
478   }
479 
testConcurrentMap()480   public void testConcurrentMap() {
481     assertFreshInstance(new TypeToken<ConcurrentMap<String, ?>>() {});
482     FreshValueGenerator generator = new FreshValueGenerator();
483     ConcurrentMap<String, Integer> expected = Maps.newConcurrentMap();
484     expected.put(generator.generate(String.class), generator.generate(int.class));
485     assertValueAndTypeEquals(expected,
486         new FreshValueGenerator().generate(new TypeToken<ConcurrentMap<String, Integer>>() {}));
487     assertNotInstantiable(new TypeToken<ConcurrentMap<EmptyEnum, String>>() {});
488   }
489 
testMultimap()490   public void testMultimap() {
491     assertFreshInstance(new TypeToken<Multimap<String, ?>>() {});
492     FreshValueGenerator generator = new FreshValueGenerator();
493     Multimap<String, Integer> expected = ArrayListMultimap.create();
494     expected.put(generator.generate(String.class), generator.generate(int.class));
495     assertValueAndTypeEquals(expected,
496         new FreshValueGenerator().generate(new TypeToken<Multimap<String, Integer>>() {}));
497     assertNotInstantiable(new TypeToken<Multimap<EmptyEnum, String>>() {});
498   }
499 
testHashMultimap()500   public void testHashMultimap() {
501     assertFreshInstance(new TypeToken<HashMultimap<String, ?>>() {});
502     FreshValueGenerator generator = new FreshValueGenerator();
503     HashMultimap<String, Integer> expected = HashMultimap.create();
504     expected.put(generator.generate(String.class), generator.generate(int.class));
505     assertValueAndTypeEquals(expected,
506         new FreshValueGenerator().generate(new TypeToken<HashMultimap<String, Integer>>() {}));
507   }
508 
testLinkedHashMultimap()509   public void testLinkedHashMultimap() {
510     assertFreshInstance(new TypeToken<LinkedHashMultimap<String, ?>>() {});
511     FreshValueGenerator generator = new FreshValueGenerator();
512     LinkedHashMultimap<String, Integer> expected = LinkedHashMultimap.create();
513     expected.put(generator.generate(String.class), generator.generate(int.class));
514     assertValueAndTypeEquals(expected,
515         new FreshValueGenerator().generate(
516             new TypeToken<LinkedHashMultimap<String, Integer>>() {}));
517   }
518 
testListMultimap()519   public void testListMultimap() {
520     assertFreshInstance(new TypeToken<ListMultimap<String, ?>>() {});
521     FreshValueGenerator generator = new FreshValueGenerator();
522     ListMultimap<String, Integer> expected = ArrayListMultimap.create();
523     expected.put(generator.generate(String.class), generator.generate(int.class));
524     assertValueAndTypeEquals(expected,
525         new FreshValueGenerator().generate(
526             new TypeToken<ListMultimap<String, Integer>>() {}));
527     assertNotInstantiable(new TypeToken<ListMultimap<EmptyEnum, String>>() {});
528   }
529 
testArrayListMultimap()530   public void testArrayListMultimap() {
531     assertFreshInstance(new TypeToken<ArrayListMultimap<String, ?>>() {});
532     FreshValueGenerator generator = new FreshValueGenerator();
533     ArrayListMultimap<String, Integer> expected = ArrayListMultimap.create();
534     expected.put(generator.generate(String.class), generator.generate(int.class));
535     assertValueAndTypeEquals(expected,
536         new FreshValueGenerator().generate(
537             new TypeToken<ArrayListMultimap<String, Integer>>() {}));
538   }
539 
testSetMultimap()540   public void testSetMultimap() {
541     assertFreshInstance(new TypeToken<SetMultimap<String, ?>>() {});
542     FreshValueGenerator generator = new FreshValueGenerator();
543     SetMultimap<String, Integer> expected = LinkedHashMultimap.create();
544     expected.put(generator.generate(String.class), generator.generate(int.class));
545     assertValueAndTypeEquals(expected,
546         new FreshValueGenerator().generate(
547             new TypeToken<SetMultimap<String, Integer>>() {}));
548     assertNotInstantiable(new TypeToken<SetMultimap<EmptyEnum, String>>() {});
549   }
550 
testBiMap()551   public void testBiMap() {
552     assertFreshInstance(new TypeToken<BiMap<String, ?>>() {});
553     FreshValueGenerator generator = new FreshValueGenerator();
554     BiMap<String, Integer> expected = HashBiMap.create();
555     expected.put(generator.generate(String.class), generator.generate(int.class));
556     assertValueAndTypeEquals(expected,
557         new FreshValueGenerator().generate(
558             new TypeToken<BiMap<String, Integer>>() {}));
559     assertNotInstantiable(new TypeToken<BiMap<EmptyEnum, String>>() {});
560   }
561 
testHashBiMap()562   public void testHashBiMap() {
563     assertFreshInstance(new TypeToken<HashBiMap<String, ?>>() {});
564     FreshValueGenerator generator = new FreshValueGenerator();
565     HashBiMap<String, Integer> expected = HashBiMap.create();
566     expected.put(generator.generate(String.class), generator.generate(int.class));
567     assertValueAndTypeEquals(expected,
568         new FreshValueGenerator().generate(
569             new TypeToken<HashBiMap<String, Integer>>() {}));
570   }
571 
testTable()572   public void testTable() {
573     assertFreshInstance(new TypeToken<Table<String, ?, ?>>() {});
574     FreshValueGenerator generator = new FreshValueGenerator();
575     Table<String, Integer, Object> expected = HashBasedTable.create();
576     expected.put(generator.generate(String.class), generator.generate(int.class),
577             generator.generate(new TypeToken<List<String>>() {}));
578     assertValueAndTypeEquals(expected,
579         new FreshValueGenerator().generate(
580             new TypeToken<Table<String, Integer, List<String>>>() {}));
581     assertNotInstantiable(new TypeToken<Table<EmptyEnum, String, Integer>>() {});
582   }
583 
testHashBasedTable()584   public void testHashBasedTable() {
585     assertFreshInstance(new TypeToken<HashBasedTable<String, ?, ?>>() {});
586     FreshValueGenerator generator = new FreshValueGenerator();
587     HashBasedTable<String, Integer, Object> expected = HashBasedTable.create();
588     expected.put(generator.generate(String.class), generator.generate(int.class),
589             generator.generate(new TypeToken<List<String>>() {}));
590     assertValueAndTypeEquals(expected,
591         new FreshValueGenerator().generate(
592             new TypeToken<HashBasedTable<String, Integer, List<String>>>() {}));
593   }
594 
testRowSortedTable()595   public void testRowSortedTable() {
596     assertFreshInstance(new TypeToken<RowSortedTable<String, ?, ?>>() {});
597     FreshValueGenerator generator = new FreshValueGenerator();
598     RowSortedTable<String, Integer, Object> expected = TreeBasedTable.create();
599     expected.put(generator.generate(String.class), generator.generate(int.class),
600             generator.generate(new TypeToken<List<String>>() {}));
601     assertValueAndTypeEquals(expected,
602         new FreshValueGenerator().generate(
603             new TypeToken<RowSortedTable<String, Integer, List<String>>>() {}));
604     assertNotInstantiable(new TypeToken<RowSortedTable<EmptyEnum, String, Integer>>() {});
605   }
606 
testTreeBasedTable()607   public void testTreeBasedTable() {
608     assertFreshInstance(new TypeToken<TreeBasedTable<String, ?, ?>>() {});
609     FreshValueGenerator generator = new FreshValueGenerator();
610     TreeBasedTable<String, Integer, Object> expected = TreeBasedTable.create();
611     expected.put(generator.generate(String.class), generator.generate(int.class),
612             generator.generate(new TypeToken<List<String>>() {}));
613     assertValueAndTypeEquals(expected,
614         new FreshValueGenerator().generate(
615             new TypeToken<TreeBasedTable<String, Integer, List<String>>>() {}));
616   }
617 
testObject()618   public void testObject() {
619     assertEquals(new FreshValueGenerator().generate(String.class),
620         new FreshValueGenerator().generate(Object.class));
621   }
622 
testEnums()623   public void testEnums() {
624     assertEqualInstance(EmptyEnum.class, null);
625     assertEqualInstance(OneConstantEnum.class, OneConstantEnum.CONSTANT1);
626     assertFreshInstance(TwoConstantEnum.class);
627     assertFreshInstance(new TypeToken<Optional<OneConstantEnum>>() {});
628   }
629 
testOptional()630   public void testOptional() {
631     FreshValueGenerator generator = new FreshValueGenerator();
632     assertEquals(Optional.absent(), generator.generate(new TypeToken<Optional<String>>() {}));
633     assertEquals(Optional.of("1"), generator.generate(new TypeToken<Optional<String>>() {}));
634     // Test that the first generated instance for different Optional<T> is always absent().
635     // Having generated Optional<String> instances doesn't prevent absent() from being generated for
636     // other Optional types.
637     assertEquals(Optional.absent(),
638         generator.generate(new TypeToken<Optional<OneConstantEnum>>() {}));
639     assertEquals(Optional.of(OneConstantEnum.CONSTANT1),
640         generator.generate(new TypeToken<Optional<OneConstantEnum>>() {}));
641   }
642 
testAddSampleInstances_twoInstances()643   public void testAddSampleInstances_twoInstances() {
644     FreshValueGenerator generator = new FreshValueGenerator();
645     generator.addSampleInstances(String.class, ImmutableList.of("a", "b"));
646     assertEquals("a", generator.generate(String.class));
647     assertEquals("b", generator.generate(String.class));
648     assertEquals("a", generator.generate(String.class));
649   }
650 
testAddSampleInstances_oneInstance()651   public void testAddSampleInstances_oneInstance() {
652     FreshValueGenerator generator = new FreshValueGenerator();
653     generator.addSampleInstances(String.class, ImmutableList.of("a"));
654     assertEquals("a", generator.generate(String.class));
655     assertEquals("a", generator.generate(String.class));
656   }
657 
testAddSampleInstances_noInstance()658   public void testAddSampleInstances_noInstance() {
659     FreshValueGenerator generator = new FreshValueGenerator();
660     generator.addSampleInstances(String.class, ImmutableList.<String>of());
661     assertEquals(new FreshValueGenerator().generate(String.class),
662         generator.generate(String.class));
663   }
664 
testFreshCurrency()665   public void testFreshCurrency() {
666     FreshValueGenerator generator = new FreshValueGenerator();
667     // repeat a few times to make sure we don't stumble upon a bad Locale
668     assertNotNull(generator.generate(Currency.class));
669     assertNotNull(generator.generate(Currency.class));
670     assertNotNull(generator.generate(Currency.class));
671   }
672 
testNulls()673   public void testNulls() throws Exception {
674     new ClassSanityTester()
675         .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls"))
676         .testNulls(FreshValueGenerator.class);
677   }
678 
assertFreshInstances(Class<?>.... types)679   private static void assertFreshInstances(Class<?>... types) {
680     for (Class<?> type : types) {
681       assertFreshInstance(type);
682     }
683   }
684 
assertFreshInstance(TypeToken<?> type)685   private static void assertFreshInstance(TypeToken<?> type) {
686     FreshValueGenerator generator = new FreshValueGenerator();
687     Object value1 = generator.generate(type);
688     Object value2 = generator.generate(type);
689     assertNotNull("Null returned for " + type, value1);
690     assertFalse("Equal instance " + value1 + " returned for " + type, value1.equals(value2));
691   }
692 
assertFreshInstance(Class<T> type)693   private static <T> void assertFreshInstance(Class<T> type) {
694     assertFreshInstance(TypeToken.of(type));
695   }
696 
assertEqualInstance(Class<T> type, T value)697   private static <T> void assertEqualInstance(Class<T> type, T value) {
698     FreshValueGenerator generator = new FreshValueGenerator();
699     assertEquals(value, generator.generate(type));
700     assertEquals(value, generator.generate(type));
701   }
702 
703   private enum EmptyEnum {}
704 
705   private enum OneConstantEnum {
706     CONSTANT1
707   }
708 
709   private enum TwoConstantEnum {
710     CONSTANT1, CONSTANT2
711   }
712 
assertValueAndTypeEquals(Object expected, Object actual)713   private static void assertValueAndTypeEquals(Object expected, Object actual) {
714     assertEquals(expected, actual);
715     assertEquals(expected.getClass(), actual.getClass());
716   }
717 
assertNotInstantiable(TypeToken<?> type)718   private static void assertNotInstantiable(TypeToken<?> type) {
719     assertNull(new FreshValueGenerator().generate(type));
720   }
721 
newLinkedHashSet(E element)722   private static <E> LinkedHashSet<E> newLinkedHashSet(E element) {
723     LinkedHashSet<E> set = Sets.newLinkedHashSet();
724     set.add(element);
725     return set;
726   }
727 
newLinkedList(E element)728   private static <E> LinkedList<E> newLinkedList(E element) {
729     LinkedList<E> list = Lists.newLinkedList();
730     list.add(element);
731     return list;
732   }
733 }
734