• 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.Predicate;
24 import com.google.common.base.Splitter;
25 import com.google.common.base.Ticker;
26 import com.google.common.collect.ArrayListMultimap;
27 import com.google.common.collect.BiMap;
28 import com.google.common.collect.HashBasedTable;
29 import com.google.common.collect.HashBiMap;
30 import com.google.common.collect.HashMultimap;
31 import com.google.common.collect.HashMultiset;
32 import com.google.common.collect.ImmutableBiMap;
33 import com.google.common.collect.ImmutableCollection;
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.collect.ImmutableListMultimap;
36 import com.google.common.collect.ImmutableMap;
37 import com.google.common.collect.ImmutableMultimap;
38 import com.google.common.collect.ImmutableMultiset;
39 import com.google.common.collect.ImmutableSet;
40 import com.google.common.collect.ImmutableSetMultimap;
41 import com.google.common.collect.ImmutableSortedMap;
42 import com.google.common.collect.ImmutableSortedMultiset;
43 import com.google.common.collect.ImmutableSortedSet;
44 import com.google.common.collect.ImmutableTable;
45 import com.google.common.collect.LinkedHashMultimap;
46 import com.google.common.collect.LinkedHashMultiset;
47 import com.google.common.collect.ListMultimap;
48 import com.google.common.collect.Maps;
49 import com.google.common.collect.Multimap;
50 import com.google.common.collect.Multiset;
51 import com.google.common.collect.Ordering;
52 import com.google.common.collect.Range;
53 import com.google.common.collect.RowSortedTable;
54 import com.google.common.collect.SetMultimap;
55 import com.google.common.collect.SortedMultiset;
56 import com.google.common.collect.Table;
57 import com.google.common.collect.TreeBasedTable;
58 import com.google.common.collect.TreeMultiset;
59 import com.google.common.primitives.UnsignedInteger;
60 import com.google.common.primitives.UnsignedLong;
61 import com.google.common.reflect.TypeToken;
62 import java.io.ByteArrayInputStream;
63 import java.io.ByteArrayOutputStream;
64 import java.io.File;
65 import java.io.InputStream;
66 import java.io.OutputStream;
67 import java.io.Reader;
68 import java.io.StringReader;
69 import java.io.StringWriter;
70 import java.io.Writer;
71 import java.lang.reflect.Method;
72 import java.lang.reflect.Type;
73 import java.math.BigDecimal;
74 import java.math.BigInteger;
75 import java.nio.Buffer;
76 import java.nio.ByteBuffer;
77 import java.nio.CharBuffer;
78 import java.nio.DoubleBuffer;
79 import java.nio.FloatBuffer;
80 import java.nio.IntBuffer;
81 import java.nio.LongBuffer;
82 import java.nio.ShortBuffer;
83 import java.nio.charset.Charset;
84 import java.util.ArrayList;
85 import java.util.Collection;
86 import java.util.Comparator;
87 import java.util.Currency;
88 import java.util.HashMap;
89 import java.util.HashSet;
90 import java.util.LinkedHashMap;
91 import java.util.LinkedHashSet;
92 import java.util.LinkedList;
93 import java.util.List;
94 import java.util.Locale;
95 import java.util.Map;
96 import java.util.Map.Entry;
97 import java.util.NavigableMap;
98 import java.util.NavigableSet;
99 import java.util.Set;
100 import java.util.SortedMap;
101 import java.util.SortedSet;
102 import java.util.TreeMap;
103 import java.util.TreeSet;
104 import java.util.concurrent.ConcurrentMap;
105 import java.util.concurrent.TimeUnit;
106 import java.util.regex.MatchResult;
107 import java.util.regex.Pattern;
108 import junit.framework.TestCase;
109 
110 /**
111  * Tests for {@link FreshValueGenerator}.
112  *
113  * @author Ben Yu
114  */
115 public class FreshValueGeneratorTest extends TestCase {
116 
117   @AndroidIncompatible // problem with equality of Type objects?
testFreshInstance()118   public void testFreshInstance() {
119     assertFreshInstances(
120         String.class,
121         CharSequence.class,
122         Appendable.class,
123         StringBuffer.class,
124         StringBuilder.class,
125         Pattern.class,
126         MatchResult.class,
127         Number.class,
128         int.class,
129         Integer.class,
130         long.class,
131         Long.class,
132         short.class,
133         Short.class,
134         byte.class,
135         Byte.class,
136         boolean.class,
137         Boolean.class,
138         char.class,
139         Character.class,
140         int[].class,
141         Object[].class,
142         UnsignedInteger.class,
143         UnsignedLong.class,
144         BigInteger.class,
145         BigDecimal.class,
146         Throwable.class,
147         Error.class,
148         Exception.class,
149         RuntimeException.class,
150         Charset.class,
151         Locale.class,
152         Currency.class,
153         List.class,
154         Entry.class,
155         Object.class,
156         Equivalence.class,
157         Predicate.class,
158         Function.class,
159         Comparable.class,
160         Comparator.class,
161         Ordering.class,
162         Class.class,
163         Type.class,
164         TypeToken.class,
165         TimeUnit.class,
166         Ticker.class,
167         Joiner.class,
168         Splitter.class,
169         CharMatcher.class,
170         InputStream.class,
171         ByteArrayInputStream.class,
172         Reader.class,
173         Readable.class,
174         StringReader.class,
175         OutputStream.class,
176         ByteArrayOutputStream.class,
177         Writer.class,
178         StringWriter.class,
179         File.class,
180         Buffer.class,
181         ByteBuffer.class,
182         CharBuffer.class,
183         ShortBuffer.class,
184         IntBuffer.class,
185         LongBuffer.class,
186         FloatBuffer.class,
187         DoubleBuffer.class,
188         String[].class,
189         Object[].class,
190         int[].class);
191   }
192 
testStringArray()193   public void testStringArray() {
194     FreshValueGenerator generator = new FreshValueGenerator();
195     String[] a1 = generator.generateFresh(String[].class);
196     String[] a2 = generator.generateFresh(String[].class);
197     assertFalse(a1[0].equals(a2[0]));
198   }
199 
testPrimitiveArray()200   public void testPrimitiveArray() {
201     FreshValueGenerator generator = new FreshValueGenerator();
202     int[] a1 = generator.generateFresh(int[].class);
203     int[] a2 = generator.generateFresh(int[].class);
204     assertTrue(a1[0] != a2[0]);
205   }
206 
testRange()207   public void testRange() {
208     assertFreshInstance(new TypeToken<Range<String>>() {});
209   }
210 
testImmutableList()211   public void testImmutableList() {
212     assertFreshInstance(new TypeToken<ImmutableList<String>>() {});
213   }
214 
testImmutableSet()215   public void testImmutableSet() {
216     assertFreshInstance(new TypeToken<ImmutableSet<String>>() {});
217   }
218 
testImmutableSortedSet()219   public void testImmutableSortedSet() {
220     assertFreshInstance(new TypeToken<ImmutableSortedSet<String>>() {});
221   }
222 
testImmutableMultiset()223   public void testImmutableMultiset() {
224     assertFreshInstance(new TypeToken<ImmutableSortedSet<String>>() {});
225     assertNotInstantiable(new TypeToken<ImmutableMultiset<EmptyEnum>>() {});
226   }
227 
testImmutableCollection()228   public void testImmutableCollection() {
229     assertFreshInstance(new TypeToken<ImmutableCollection<String>>() {});
230     assertNotInstantiable(new TypeToken<ImmutableCollection<EmptyEnum>>() {});
231   }
232 
testImmutableMap()233   public void testImmutableMap() {
234     assertFreshInstance(new TypeToken<ImmutableMap<String, Integer>>() {});
235   }
236 
testImmutableSortedMap()237   public void testImmutableSortedMap() {
238     assertFreshInstance(new TypeToken<ImmutableSortedMap<String, Integer>>() {});
239   }
240 
testImmutableMultimap()241   public void testImmutableMultimap() {
242     assertFreshInstance(new TypeToken<ImmutableMultimap<String, Integer>>() {});
243     assertNotInstantiable(new TypeToken<ImmutableMultimap<EmptyEnum, String>>() {});
244   }
245 
testImmutableListMultimap()246   public void testImmutableListMultimap() {
247     assertFreshInstance(new TypeToken<ImmutableListMultimap<String, Integer>>() {});
248   }
249 
testImmutableSetMultimap()250   public void testImmutableSetMultimap() {
251     assertFreshInstance(new TypeToken<ImmutableSetMultimap<String, Integer>>() {});
252   }
253 
testImmutableBiMap()254   public void testImmutableBiMap() {
255     assertFreshInstance(new TypeToken<ImmutableBiMap<String, Integer>>() {});
256   }
257 
testImmutableTable()258   public void testImmutableTable() {
259     assertFreshInstance(new TypeToken<ImmutableTable<String, Integer, ImmutableList<String>>>() {});
260   }
261 
testList()262   public void testList() {
263     assertFreshInstance(new TypeToken<List<String>>() {});
264     assertNotInstantiable(new TypeToken<List<EmptyEnum>>() {});
265   }
266 
testArrayList()267   public void testArrayList() {
268     assertFreshInstance(new TypeToken<ArrayList<String>>() {});
269     assertNotInstantiable(new TypeToken<ArrayList<EmptyEnum>>() {});
270   }
271 
testLinkedList()272   public void testLinkedList() {
273     assertFreshInstance(new TypeToken<LinkedList<String>>() {});
274   }
275 
testSet()276   public void testSet() {
277     assertFreshInstance(new TypeToken<Set<String>>() {});
278     assertNotInstantiable(new TypeToken<Set<EmptyEnum>>() {});
279   }
280 
testHashSet()281   public void testHashSet() {
282     assertFreshInstance(new TypeToken<HashSet<String>>() {});
283   }
284 
testLinkedHashSet()285   public void testLinkedHashSet() {
286     assertFreshInstance(new TypeToken<LinkedHashSet<String>>() {});
287   }
288 
testTreeSet()289   public void testTreeSet() {
290     assertFreshInstance(new TypeToken<TreeSet<String>>() {});
291   }
292 
testSortedSet()293   public void testSortedSet() {
294     assertFreshInstance(new TypeToken<SortedSet<String>>() {});
295   }
296 
testNavigableSet()297   public void testNavigableSet() {
298     assertFreshInstance(new TypeToken<NavigableSet<String>>() {});
299   }
300 
testMultiset()301   public void testMultiset() {
302     assertFreshInstance(new TypeToken<Multiset<String>>() {});
303   }
304 
testSortedMultiset()305   public void testSortedMultiset() {
306     assertFreshInstance(new TypeToken<SortedMultiset<String>>() {});
307   }
308 
testHashMultiset()309   public void testHashMultiset() {
310     assertFreshInstance(new TypeToken<HashMultiset<String>>() {});
311   }
312 
testLinkedHashMultiset()313   public void testLinkedHashMultiset() {
314     assertFreshInstance(new TypeToken<LinkedHashMultiset<String>>() {});
315   }
316 
testTreeMultiset()317   public void testTreeMultiset() {
318     assertFreshInstance(new TypeToken<TreeMultiset<String>>() {});
319   }
320 
testImmutableSortedMultiset()321   public void testImmutableSortedMultiset() {
322     assertFreshInstance(new TypeToken<ImmutableSortedMultiset<String>>() {});
323   }
324 
testCollection()325   public void testCollection() {
326     assertFreshInstance(new TypeToken<Collection<String>>() {});
327   }
328 
testIterable()329   public void testIterable() {
330     assertFreshInstance(new TypeToken<Iterable<String>>() {});
331   }
332 
testMap()333   public void testMap() {
334     assertFreshInstance(new TypeToken<Map<String, ?>>() {});
335   }
336 
testHashMap()337   public void testHashMap() {
338     assertFreshInstance(new TypeToken<HashMap<String, ?>>() {});
339   }
340 
testLinkedHashMap()341   public void testLinkedHashMap() {
342     assertFreshInstance(new TypeToken<LinkedHashMap<String, ?>>() {});
343   }
344 
testTreeMap()345   public void testTreeMap() {
346     assertFreshInstance(new TypeToken<TreeMap<String, ?>>() {});
347   }
348 
testSortedMap()349   public void testSortedMap() {
350     assertFreshInstance(new TypeToken<SortedMap<?, String>>() {});
351   }
352 
testNavigableMap()353   public void testNavigableMap() {
354     assertFreshInstance(new TypeToken<NavigableMap<?, ?>>() {});
355   }
356 
testConcurrentMap()357   public void testConcurrentMap() {
358     assertFreshInstance(new TypeToken<ConcurrentMap<String, ?>>() {});
359     assertCanGenerateOnly(
360         new TypeToken<ConcurrentMap<EmptyEnum, String>>() {}, Maps.newConcurrentMap());
361   }
362 
testMultimap()363   public void testMultimap() {
364     assertFreshInstance(new TypeToken<Multimap<String, ?>>() {});
365   }
366 
testHashMultimap()367   public void testHashMultimap() {
368     assertFreshInstance(new TypeToken<HashMultimap<String, ?>>() {});
369   }
370 
testLinkedHashMultimap()371   public void testLinkedHashMultimap() {
372     assertFreshInstance(new TypeToken<LinkedHashMultimap<String, ?>>() {});
373   }
374 
testListMultimap()375   public void testListMultimap() {
376     assertFreshInstance(new TypeToken<ListMultimap<String, ?>>() {});
377   }
378 
testArrayListMultimap()379   public void testArrayListMultimap() {
380     assertFreshInstance(new TypeToken<ArrayListMultimap<String, ?>>() {});
381   }
382 
testSetMultimap()383   public void testSetMultimap() {
384     assertFreshInstance(new TypeToken<SetMultimap<String, ?>>() {});
385   }
386 
testBiMap()387   public void testBiMap() {
388     assertFreshInstance(new TypeToken<BiMap<String, ?>>() {});
389     assertNotInstantiable(new TypeToken<BiMap<EmptyEnum, String>>() {});
390   }
391 
testHashBiMap()392   public void testHashBiMap() {
393     assertFreshInstance(new TypeToken<HashBiMap<String, ?>>() {});
394   }
395 
testTable()396   public void testTable() {
397     assertFreshInstance(new TypeToken<Table<String, ?, ?>>() {});
398     assertNotInstantiable(new TypeToken<Table<EmptyEnum, String, Integer>>() {});
399   }
400 
testHashBasedTable()401   public void testHashBasedTable() {
402     assertFreshInstance(new TypeToken<HashBasedTable<String, ?, ?>>() {});
403   }
404 
testRowSortedTable()405   public void testRowSortedTable() {
406     assertFreshInstance(new TypeToken<RowSortedTable<String, ?, ?>>() {});
407   }
408 
testTreeBasedTable()409   public void testTreeBasedTable() {
410     assertFreshInstance(new TypeToken<TreeBasedTable<String, ?, ?>>() {});
411   }
412 
testObject()413   public void testObject() {
414     assertEquals(
415         new FreshValueGenerator().generateFresh(String.class),
416         new FreshValueGenerator().generateFresh(Object.class));
417   }
418 
testEnums()419   public void testEnums() {
420     assertEqualInstance(EmptyEnum.class, null);
421     assertEqualInstance(OneConstantEnum.class, OneConstantEnum.CONSTANT1);
422     assertFreshInstance(TwoConstantEnum.class, 2);
423     assertFreshInstance(new TypeToken<com.google.common.base.Optional<OneConstantEnum>>() {}, 2);
424     assertFreshInstance(new TypeToken<List<OneConstantEnum>>() {}, 1);
425     assertFreshInstance(new TypeToken<List<TwoConstantEnum>>() {}, 2);
426   }
427 
428   @AndroidIncompatible // problem with equality of Type objects?
testGoogleOptional()429   public void testGoogleOptional() {
430     FreshValueGenerator generator = new FreshValueGenerator();
431     assertEquals(
432         com.google.common.base.Optional.absent(),
433         generator.generateFresh(new TypeToken<com.google.common.base.Optional<String>>() {}));
434     assertEquals(
435         com.google.common.base.Optional.of("2"),
436         generator.generateFresh(new TypeToken<com.google.common.base.Optional<String>>() {}));
437     // Test that the first generated instance for different cgcb.Optional<T> is always absent().
438     // Having generated cgcb.Optional<String> instances doesn't prevent absent() from being
439     // generated for other cgcb.Optional types.
440     assertEquals(
441         com.google.common.base.Optional.absent(),
442         generator.generateFresh(
443             new TypeToken<com.google.common.base.Optional<OneConstantEnum>>() {}));
444     assertEquals(
445         com.google.common.base.Optional.of(OneConstantEnum.CONSTANT1),
446         generator.generateFresh(
447             new TypeToken<com.google.common.base.Optional<OneConstantEnum>>() {}));
448   }
449 
testAddSampleInstances_twoInstances()450   public void testAddSampleInstances_twoInstances() {
451     FreshValueGenerator generator = new FreshValueGenerator();
452     generator.addSampleInstances(String.class, ImmutableList.of("a", "b"));
453     assertEquals("a", generator.generateFresh(String.class));
454     assertEquals("b", generator.generateFresh(String.class));
455     assertEquals("a", generator.generateFresh(String.class));
456   }
457 
testAddSampleInstances_oneInstance()458   public void testAddSampleInstances_oneInstance() {
459     FreshValueGenerator generator = new FreshValueGenerator();
460     generator.addSampleInstances(String.class, ImmutableList.of("a"));
461     assertEquals("a", generator.generateFresh(String.class));
462     assertEquals("a", generator.generateFresh(String.class));
463   }
464 
testAddSampleInstances_noInstance()465   public void testAddSampleInstances_noInstance() {
466     FreshValueGenerator generator = new FreshValueGenerator();
467     generator.addSampleInstances(String.class, ImmutableList.<String>of());
468     assertEquals(
469         new FreshValueGenerator().generateFresh(String.class),
470         generator.generateFresh(String.class));
471   }
472 
testFreshCurrency()473   public void testFreshCurrency() {
474     FreshValueGenerator generator = new FreshValueGenerator();
475     // repeat a few times to make sure we don't stumble upon a bad Locale
476     assertNotNull(generator.generateFresh(Currency.class));
477     assertNotNull(generator.generateFresh(Currency.class));
478     assertNotNull(generator.generateFresh(Currency.class));
479   }
480 
testNulls()481   public void testNulls() throws Exception {
482     new ClassSanityTester()
483         .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls"))
484         .testNulls(FreshValueGenerator.class);
485   }
486 
assertFreshInstances(Class<?>.... types)487   private static void assertFreshInstances(Class<?>... types) {
488     for (Class<?> type : types) {
489       assertFreshInstance(type, 2);
490     }
491   }
492 
assertFreshInstance(TypeToken<?> type)493   private static void assertFreshInstance(TypeToken<?> type) {
494     assertFreshInstance(type, 3);
495   }
496 
assertFreshInstance(Class<?> type, int instances)497   private static void assertFreshInstance(Class<?> type, int instances) {
498     assertFreshInstance(TypeToken.of(type), instances);
499   }
500 
assertFreshInstance(TypeToken<?> type, int instances)501   private static void assertFreshInstance(TypeToken<?> type, int instances) {
502     FreshValueGenerator generator = new FreshValueGenerator();
503     EqualsTester tester = new EqualsTester();
504     for (int i = 0; i < instances; i++) {
505       tester.addEqualityGroup(generator.generateFresh(type));
506     }
507     tester.testEquals();
508   }
509 
assertEqualInstance(Class<T> type, T value)510   private static <T> void assertEqualInstance(Class<T> type, T value) {
511     FreshValueGenerator generator = new FreshValueGenerator();
512     assertEquals(value, generator.generateFresh(type));
513     assertEquals(value, generator.generateFresh(type));
514   }
515 
516   private enum EmptyEnum {}
517 
518   private enum OneConstantEnum {
519     CONSTANT1
520   }
521 
522   private enum TwoConstantEnum {
523     CONSTANT1,
524     CONSTANT2
525   }
526 
assertCanGenerateOnly(TypeToken<?> type, Object expected)527   private static void assertCanGenerateOnly(TypeToken<?> type, Object expected) {
528     FreshValueGenerator generator = new FreshValueGenerator();
529     assertValueAndTypeEquals(expected, generator.generateFresh(type));
530     assertNull(generator.generateFresh(type));
531   }
532 
assertNotInstantiable(TypeToken<?> type)533   private static void assertNotInstantiable(TypeToken<?> type) {
534     assertNull(new FreshValueGenerator().generateFresh(type));
535   }
536 
assertValueAndTypeEquals(Object expected, Object actual)537   private static void assertValueAndTypeEquals(Object expected, Object actual) {
538     assertEquals(expected, actual);
539     assertEquals(expected.getClass(), actual.getClass());
540   }
541 }
542