Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 java.io.File;
 import java.io.Reader;
 import java.io.Writer;
 import java.util.List;
Unit test for ArbitraryInstances.

Author(s):
Ben Yu
public class ArbitraryInstancesTest extends TestCase {
  public void testGet_primitives() {
    assertNull(ArbitraryInstances.get(void.class));
    assertNull(ArbitraryInstances.get(Void.class));
    assertEquals(., ArbitraryInstances.get(boolean.class));
    assertEquals(., ArbitraryInstances.get(Boolean.class));
    assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(char.class));
    assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(Character.class));
    assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(byte.class));
    assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(Byte.class));
    assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(short.class));
    assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(Short.class));
    assertEquals(Integer.valueOf(0), ArbitraryInstances.get(int.class));
    assertEquals(Integer.valueOf(0), ArbitraryInstances.get(Integer.class));
    assertEquals(Long.valueOf(0), ArbitraryInstances.get(long.class));
    assertEquals(Long.valueOf(0), ArbitraryInstances.get(Long.class));
    assertEquals(Float.valueOf(0), ArbitraryInstances.get(float.class));
    assertEquals(Float.valueOf(0), ArbitraryInstances.get(Float.class));
    assertEquals(Double.valueOf(0), ArbitraryInstances.get(double.class));
    assertEquals(Double.valueOf(0), ArbitraryInstances.get(Double.class));
    assertEquals(., ArbitraryInstances.get(UnsignedInteger.class));
    assertEquals(., ArbitraryInstances.get(UnsignedLong.class));
    assertEquals(0, ArbitraryInstances.get(BigDecimal.class).intValue());
    assertEquals(0, ArbitraryInstances.get(BigInteger.class).intValue());
    assertEquals("", ArbitraryInstances.get(String.class));
    assertEquals("", ArbitraryInstances.get(CharSequence.class));
    assertEquals(., ArbitraryInstances.get(TimeUnit.class));
    assertNotNull(ArbitraryInstances.get(Object.class));
    assertEquals(0, ArbitraryInstances.get(Number.class));
    assertEquals(., ArbitraryInstances.get(Charset.class));
  }
  public void testGet_collections() {
    assertEquals(Iterators.emptyIterator(), ArbitraryInstances.get(Iterator.class));
    assertFalse(ArbitraryInstances.get(PeekingIterator.class).hasNext());
    assertFalse(ArbitraryInstances.get(ListIterator.class).hasNext());
    assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Iterable.class));
    assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Set.class));
    assertEquals(ImmutableSet.of(), ArbitraryInstances.get(ImmutableSet.class));
    assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(SortedSet.class));
    assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(ImmutableSortedSet.class));
    assertEquals(ImmutableList.of(), ArbitraryInstances.get(Collection.class));
    assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableCollection.class));
    assertEquals(ImmutableList.of(), ArbitraryInstances.get(List.class));
    assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableList.class));
    assertEquals(ImmutableMap.of(), ArbitraryInstances.get(Map.class));
    assertEquals(ImmutableMap.of(), ArbitraryInstances.get(ImmutableMap.class));
    assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(SortedMap.class));
    assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(ImmutableSortedMap.class));
    assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(Multiset.class));
    assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(ImmutableMultiset.class));
    assertTrue(ArbitraryInstances.get(SortedMultiset.class).isEmpty());
    assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(Multimap.class));
    assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(ImmutableMultimap.class));
    assertTrue(ArbitraryInstances.get(SortedSetMultimap.class).isEmpty());
    assertEquals(ImmutableTable.of(), ArbitraryInstances.get(Table.class));
    assertEquals(ImmutableTable.of(), ArbitraryInstances.get(ImmutableTable.class));
    assertTrue(ArbitraryInstances.get(RowSortedTable.class).isEmpty());
    assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(BiMap.class));
    assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(ImmutableBiMap.class));
    assertTrue(ArbitraryInstances.get(ImmutableClassToInstanceMap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(ClassToInstanceMap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(ListMultimap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(ImmutableListMultimap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(SetMultimap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(ImmutableSetMultimap.class).isEmpty());
    assertTrue(ArbitraryInstances.get(MapDifference.class).areEqual());
    assertTrue(ArbitraryInstances.get(SortedMapDifference.class).areEqual());
    assertEquals(Range.all(), ArbitraryInstances.get(Range.class));
    assertTrue(ArbitraryInstances.get(LinkedList.class).isEmpty());
    assertTrue(ArbitraryInstances.get(PriorityQueue.class).isEmpty());
    assertTrue(ArbitraryInstances.get(BitSet.class).isEmpty());
    assertTrue(ArbitraryInstances.get(TreeSet.class).isEmpty());
    assertTrue(ArbitraryInstances.get(TreeMap.class).isEmpty());
        LinkedList.classPriorityQueue.classBitSet.class,
        TreeSet.classTreeMap.class);
  }
  public void testGet_misc() {
    assertNotNull(ArbitraryInstances.get(CharMatcher.class));
    assertNotNull(ArbitraryInstances.get(Currency.class).getCurrencyCode());
    assertNotNull(ArbitraryInstances.get(Locale.class));
    ArbitraryInstances.get(Joiner.class).join(ImmutableList.of("a"));
    ArbitraryInstances.get(Splitter.class).split("a,b");
    assertFalse(ArbitraryInstances.get(Optional.class).isPresent());
    ArbitraryInstances.get(Stopwatch.class).start();
    assertNotNull(ArbitraryInstances.get(Ticker.class));
    assertNotNull(ArbitraryInstances.get(MapConstraint.class));
    assertEquals(ArbitraryInstances.get(Random.class).nextInt(),
        ArbitraryInstances.get(Random.class).nextInt());
  }
  public void testGet_concurrent() {
    assertTrue(ArbitraryInstances.get(BlockingQueue.class).isEmpty());
    assertTrue(ArbitraryInstances.get(DelayQueue.class).isEmpty());
    assertTrue(ArbitraryInstances.get(SynchronousQueue.class).isEmpty());
    assertTrue(ArbitraryInstances.get(PriorityBlockingQueue.class).isEmpty());
    assertTrue(ArbitraryInstances.get(ConcurrentMap.class).isEmpty());
    ArbitraryInstances.get(Executor.class).execute(ArbitraryInstances.get(Runnable.class));
    assertNotNull(ArbitraryInstances.get(ThreadFactory.class));
        BlockingQueue.classPriorityBlockingQueue.class,
        DelayQueue.classSynchronousQueue.class,
        ConcurrentMap.class,
        AtomicReference.classAtomicBoolean.class,
        AtomicInteger.classAtomicLong.classAtomicDouble.class);
  }
  @SuppressWarnings("unchecked"// functor classes have no type parameters
  public void testGet_functors() {
    assertEquals(0, ArbitraryInstances.get(Comparator.class).compare("abc", 123));
    assertTrue(ArbitraryInstances.get(Predicate.class).apply("abc"));
    assertTrue(ArbitraryInstances.get(Equivalence.class).equivalent(1, 1));
    assertFalse(ArbitraryInstances.get(Equivalence.class).equivalent(1, 2));
  }
  public void testGet_comparable() {
    @SuppressWarnings("unchecked"// The null value can compare with any Object
    Comparable<Objectcomparable = ArbitraryInstances.get(Comparable.class);
    assertEquals(0, comparable.compareTo(comparable));
    assertTrue(comparable.compareTo("") > 0);
    try {
      comparable.compareTo(null);
      fail();
    } catch (NullPointerException expected) {}
  }
  public void testGet_array() {
    assertEquals(0, ArbitraryInstances.get(int[].class).length);
    assertEquals(0, ArbitraryInstances.get(Object[].class).length);
    assertEquals(0, ArbitraryInstances.get(String[].class).length);
  }
  public void testGet_enum() {
    assertNull(ArbitraryInstances.get(EmptyEnum.class));
    assertEquals(., ArbitraryInstances.get(Direction.class));
  }
  public void testGet_interface() {
    assertNull(ArbitraryInstances.get(SomeInterface.class));
  }
  public void testGet_runnable() {
    ArbitraryInstances.get(Runnable.class).run();
  }
  public void testGet_class() {
    assertSame(., ArbitraryInstances.get(SomeAbstractClass.class));
        ArbitraryInstances.get(WithPrivateConstructor.class));
    assertNull(ArbitraryInstances.get(NoDefaultConstructor.class));
        ArbitraryInstances.get(WithExceptionalConstructor.class));
    assertNull(ArbitraryInstances.get(NonPublicClass.class));
  }
  public void testGet_mutable() {
    assertEquals(0, ArbitraryInstances.get(ArrayList.class).size());
    assertEquals(0, ArbitraryInstances.get(HashMap.class).size());
    assertEquals("", ArbitraryInstances.get(Appendable.class).toString());
    assertEquals("", ArbitraryInstances.get(StringBuilder.class).toString());
    assertEquals("", ArbitraryInstances.get(StringBuffer.class).toString());
        ArrayList.classHashMap.class,
        Appendable.classStringBuilder.classStringBuffer.class,
        Throwable.classException.class);
  }
  public void testGet_io() throws IOException {
    assertEquals(-1, ArbitraryInstances.get(InputStream.class).read());
    assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read());
    assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1)));
    assertEquals(-1, ArbitraryInstances.get(Reader.class).read());
    assertEquals(-1, ArbitraryInstances.get(StringReader.class).read());
    assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity());
    assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity());
    ArbitraryInstances.get(PrintStream.class).println("test");
    ArbitraryInstances.get(PrintWriter.class).println("test");
    assertNotNull(ArbitraryInstances.get(File.class));
        ByteArrayOutputStream.classOutputStream.class,
        Writer.classStringWriter.class,
        PrintStream.classPrintWriter.class);
  }
  public void testGet_reflect() {
    assertNotNull(ArbitraryInstances.get(Type.class));
    assertNotNull(ArbitraryInstances.get(AnnotatedElement.class));
    assertNotNull(ArbitraryInstances.get(GenericDeclaration.class));
  }
  public void testGet_regex() {
    assertEquals(Pattern.compile("").pattern(),
        ArbitraryInstances.get(Pattern.class).pattern());
    assertEquals(0, ArbitraryInstances.get(MatchResult.class).groupCount());
  }
  public void testGet_usePublicConstant() {
        ArbitraryInstances.get(WithPublicConstant.class));
  }
  public void testGet_useFirstPublicConstant() {
        ArbitraryInstances.get(WithPublicConstants.class));
  }
  public void testGet_nullConstantIgnored() {
        ArbitraryInstances.get(FirstConstantIsNull.class));
  }
    assertNull(ArbitraryInstances.get(WithGenericConstant.class));
  }
  public void testGet_nullConstant() {
    assertNull(ArbitraryInstances.get(WithNullConstant.class));
  }
    assertNull(ArbitraryInstances.get(ParentClassHasConstant.class));
  }
    assertNull(ArbitraryInstances.get(NonPublicConstantIgnored.class));
  }
  public void testGet_nonStaticFieldNotUsed() {
    assertNull(ArbitraryInstances.get(NonStaticFieldIgnored.class));
  }
    assertNotNull(ArbitraryInstances.get(WithPublicConstructorAndConstant.class));
    assertTrue(ArbitraryInstances.get(WithPublicConstructorAndConstant.class)
        != ArbitraryInstances.get(WithPublicConstructorAndConstant.class));
  }
  public void testGet_nonFinalFieldNotUsed() {
    assertNull(ArbitraryInstances.get(NonFinalFieldIgnored.class));
  }
  private static void assertFreshInstanceReturned(Class<?>... mutableClasses) {
    for (Class<?> mutableClass : mutableClasses) {
      Object instance = ArbitraryInstances.get(mutableClass);
      assertNotNull("Expected to return non-null for: " + mutableClassinstance);
      assertNotSame("Expected to return fresh instance for: " + mutableClass,
          instance, ArbitraryInstances.get(mutableClass));
    }
  }
  private enum EmptyEnum {}
  private enum Direction {
    UP, DOWN
  }
  public interface SomeInterface {}
  public static abstract class SomeAbstractClass {
    public static final SomeAbstractClass INSTANCE = new SomeAbstractClass() {};
    public SomeAbstractClass() {}
  }
  static class NonPublicClass {
    public NonPublicClass() {}
  }
  private static class WithPrivateConstructor {
    public static final WithPrivateConstructor INSTANCE = new WithPrivateConstructor();
  }
  public static class NoDefaultConstructor {
    public NoDefaultConstructor(@SuppressWarnings("unused"int i) {}
  }
  public static class WithExceptionalConstructor {
    public static final WithExceptionalConstructor INSTANCE =
        new WithExceptionalConstructor("whatever");
    public WithExceptionalConstructor() {
      throw new RuntimeException();
    }
    private WithExceptionalConstructor(String unused) {}
  }
  private static class WithPublicConstant {
    public static final WithPublicConstant INSTANCE = new WithPublicConstant();
  }
  private static class ParentClassHasConstant
      extends WithPublicConstant {}
  public static class WithGenericConstant<T> {
    public static final WithGenericConstant<StringSTRING_CONSTANT =
        new WithGenericConstant<String>();
  
    private WithGenericConstant() {}
  }
  public static class WithNullConstant {
    public static final WithNullConstant NULL = null;
  
    private WithNullConstant() {}
  }
  public static class WithPublicConstructorAndConstant {
    public static final WithPublicConstructorAndConstant INSTANCE =
        new WithPublicConstructorAndConstant();
  
  }
  private static class WithPublicConstants {
    public static final WithPublicConstants FIRST = new WithPublicConstants();
    // To test that we pick the first constant alphabetically
    @SuppressWarnings("unused")
    public static final WithPublicConstants SECOND = new WithPublicConstants();
  }
  private static class FirstConstantIsNull {
    // To test that null constant is ignored
    @SuppressWarnings("unused")
    public static final FirstConstantIsNull FIRST = null;
    public static final FirstConstantIsNull SECOND = new FirstConstantIsNull();
  }
  public static class NonFinalFieldIgnored {
    public static NonFinalFieldIgnored instance =
        new NonFinalFieldIgnored();
  
    private NonFinalFieldIgnored() {}
  }
  public static class NonPublicConstantIgnored {
    static final NonPublicConstantIgnored INSTANCE =
        new NonPublicConstantIgnored();
  
    private NonPublicConstantIgnored() {}
  }
  public static class NonStaticFieldIgnored {
    // This should cause infinite recursion. But it shouldn't be used anyway.
    public final NonStaticFieldIgnored instance =
        new NonStaticFieldIgnored();
  
    private NonStaticFieldIgnored() {}
  }
New to GrepCode? Check out our FAQ X