org.junit.contrib.truth.Truth.ASSERT-s



API Populatity

1 Client projects

Project: org.junit

Project google/guava in file ...m.google.common.testing.EquivalenceTesterTest.java (2012-05-08)
@@ -17,6 +17,7 @@
 package com.google.common.testing;
 
 import static com.google.common.base.Preconditions.checkState;
+import static org.junit.contrib.truth.Truth.ASSERT;
 
 import com.google.common.annotations.GwtCompatible;
 import com.google.common.base.Equivalence;
@@ -101,8 +102,9 @@ public class EquivalenceTesterTest extends TestCase {
     try {
       tester.addEquivalenceGroup(group1Item1, group1Item2).test();
     } catch (AssertionFailedError expected) {
-      assertEquals("TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
-          + "TestObject{group=1, item=1} [group 1, item 1]", expected.getMessage());
+      ASSERT.that(expected.getMessage()).contains(
+          "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+          + "TestObject{group=1, item=1} [group 1, item 1]");
       return;
     }
     fail();
@@ -129,8 +131,9 @@ public class EquivalenceTesterTest extends TestCase {
     try {
       tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test();
     } catch (AssertionFailedError expected) {
-      assertEquals("TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
-          + "TestObject{group=1, item=3} [group 1, item 3]", expected.getMessage());
+      ASSERT.that(expected.getMessage()).contains(
+          "TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+          + "TestObject{group=1, item=3} [group 1, item 3]");
       return;
     }
     fail();
@@ -151,8 +154,9 @@ public class EquivalenceTesterTest extends TestCase {
     try {
       tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test();
     } catch (AssertionFailedError expected) {
-      assertEquals("TestObject{group=1, item=1} [group 1, item 1] must be inequivalent to "
-          + "TestObject{group=2, item=1} [group 2, item 1]", expected.getMessage());
+      ASSERT.that(expected.getMessage()).contains(
+          "TestObject{group=1, item=1} [group 1, item 1] must be inequivalent to "
+          + "TestObject{group=2, item=1} [group 2, item 1]");
       return;
     }
     fail();
Project google/guava in file ....com.google.common.collect.ImmutableTableTest.java (2011-10-03)
@@ -0,0 +1,347 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.collect;
+
+import static org.junit.contrib.truth.Truth.ASSERT;
+
+/**
+ * Tests common methods in {@link ImmutableTable}
+ *
+ * @author gak@google.com (Gregory Kick)
+ */
+public class ImmutableTableTest extends AbstractTableReadTest {
+  @Override protected Table<String, Integer, Character> create(Object... data) {
+    ImmutableTable.Builder<String, Integer, Character> builder =
+        ImmutableTable.builder();
+    for (int i = 0; i < data.length; i = i + 3) {
+      builder.put((String) data[i], (Integer) data[i + 1],
+          (Character) data[i + 2]);
+    }
+    return builder.build();
+  }
+
+  public void testBuilder() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>();
+    assertEquals(ImmutableTable.of(), builder.build());
+    assertEquals(ImmutableTable.of('a', 1, "foo"), builder
+        .put('a', 1, "foo")
+        .build());
+    Table<Character, Integer, String> expectedTable = HashBasedTable.create();
+    expectedTable.put('a', 1, "foo");
+    expectedTable.put('b', 1, "bar");
+    expectedTable.put('a', 2, "baz");
+    Table<Character, Integer, String> otherTable = HashBasedTable.create();
+    otherTable.put('b', 1, "bar");
+    otherTable.put('a', 2, "baz");
+    assertEquals(expectedTable, builder
+        .putAll(otherTable)
+        .build());
+  }
+
+  public void testBuilder_withImmutableCell() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>();
+    assertEquals(ImmutableTable.of('a', 1, "foo"), builder
+        .put(Tables.immutableCell('a', 1, "foo"))
+        .build());
+  }
+
+  public void testBuilder_withImmutableCellAndNullContents() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>();
+    try {
+      builder.put(Tables.immutableCell((Character) null, 1, "foo"));
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+    try {
+      builder.put(Tables.immutableCell('a', (Integer) null, "foo"));
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+    try {
+      builder.put(Tables.immutableCell('a', 1, (String) null));
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+  }
+
+  private static class StringHolder {
+    String string;
+  }
+
+  public void testBuilder_withMutableCell() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>();
+
+    final StringHolder holder = new StringHolder();
+    holder.string = "foo";
+    Table.Cell<Character, Integer, String> mutableCell =
+        new Tables.AbstractCell<Character, Integer, String>() {
+          @Override public Character getRowKey() {
+            return 'K';
+          }
+          @Override public Integer getColumnKey() {
+            return 42;
+          }
+          @Override public String getValue() {
+            return holder.string;
+          }
+        };
+
+    // Add the mutable cell to the builder
+    builder.put(mutableCell);
+
+    // Mutate the value
+    holder.string = "bar";
+
+    // Make sure it uses the original value.
+    assertEquals(ImmutableTable.of('K', 42, "foo"), builder.build());
+  }
+
+  public void testBuilder_noDuplicates() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>()
+            .put('a', 1, "foo")
+            .put('a', 1, "bar");
+    try {
+      builder.build();
+      fail();
+    } catch (IllegalArgumentException e) {
+      // success
+    }
+  }
+
+  public void testBuilder_noNulls() {
+    ImmutableTable.Builder<Character, Integer, String> builder =
+        new ImmutableTable.Builder<Character, Integer, String>();
+    try {
+      builder.put(null, 1, "foo");
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+    try {
+      builder.put('a', null, "foo");
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+    try {
+      builder.put('a', 1, null);
+      fail();
+    } catch (NullPointerException e) {
+      // success
+    }
+  }
+
+  private static <R, C, V> void validateTableCopies(Table<R, C, V> original) {
+    Table<R, C, V> copy = ImmutableTable.copyOf(original);
+    assertEquals(original, copy);
+    validateViewOrdering(original, copy);
+
+    Table<R, C, V> built
+        = ImmutableTable.<R, C, V>builder().putAll(original).build();
+    assertEquals(original, built);
+    validateViewOrdering(original, built);
+  }
+
+  private static <R, C, V> void validateViewOrdering(
+      Table<R, C, V> original, Table<R, C, V> copy) {
+    assertTrue(Iterables.elementsEqual(original.cellSet(), copy.cellSet()));
+    assertTrue(Iterables.elementsEqual(original.rowKeySet(), copy.rowKeySet()));
+    assertTrue(Iterables.elementsEqual(original.values(), copy.values()));
+  }
+
+  public void testCopyOf() {
+    Table<Character, Integer, String> table = TreeBasedTable.create();
+    validateTableCopies(table);
+    table.put('b', 2, "foo");
+    validateTableCopies(table);
+    table.put('b', 1, "bar");
+    table.put('a', 2, "baz");
+    validateTableCopies(table);
+    // Even though rowKeySet, columnKeySet, and cellSet have the same
+    // iteration ordering, row has an inconsistent ordering.
+    ASSERT.that(table.row('b').keySet()).hasContentsInOrder(1, 2);
+    ASSERT.that(ImmutableTable.copyOf(table).row('b').keySet())
+        .hasContentsInOrder(2, 1);
+  }
+
+  public void testCopyOfSparse() {
+    Table<Character, Integer, String> table = TreeBasedTable.create();
+    table.put('x', 2, "foo");
+    table.put('r', 1, "bar");
+    table.put('c', 3, "baz");
+    table.put('b', 7, "cat");
+    table.put('e', 5, "dog");
+    table.put('c', 0, "axe");
+    table.put('e', 3, "tub");
+    table.put('r', 4, "foo");
+    table.put('x', 5, "bar");
+    validateTableCopies(table);
+  }
+
+  public void testCopyOfDense() {
+    Table<Character, Integer, String> table = TreeBasedTable.create();
+    table.put('c', 3, "foo");
+    table.put('c', 2, "bar");
+    table.put('c', 1, "baz");
+    table.put('b', 3, "cat");
+    table.put('b', 1, "dog");
+    table.put('a', 3, "foo");
+    table.put('a', 2, "bar");
+    table.put('a', 1, "baz");
+    validateTableCopies(table);
+  }
+
+  public void testBuilder_orderRowsAndColumnsBy_putAll() {
+    Table<Character, Integer, String> table = HashBasedTable.create();
+    table.put('b', 2, "foo");
+    table.put('b', 1, "bar");
+    table.put('a', 2, "baz");
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    Table<Character, Integer, String> copy
+        = builder.orderRowsBy(Ordering.natural())
+            .orderColumnsBy(Ordering.natural())
+            .putAll(table).build();
+    ASSERT.that(copy.rowKeySet()).hasContentsInOrder('a', 'b');
+    ASSERT.that(copy.columnKeySet()).hasContentsInOrder(1, 2);
+    ASSERT.that(copy.values()).hasContentsInOrder("baz", "bar", "foo");
+    ASSERT.that(copy.row('b').keySet()).hasContentsInOrder(1, 2);
+  }
+
+  public void testBuilder_orderRowsAndColumnsBy_sparse() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderRowsBy(Ordering.natural());
+    builder.orderColumnsBy(Ordering.natural());
+    builder.put('x', 2, "foo");
+    builder.put('r', 1, "bar");
+    builder.put('c', 3, "baz");
+    builder.put('b', 7, "cat");
+    builder.put('e', 5, "dog");
+    builder.put('c', 0, "axe");
+    builder.put('e', 3, "tub");
+    builder.put('r', 4, "foo");
+    builder.put('x', 5, "bar");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.rowKeySet()).hasContentsInOrder('b', 'c', 'e', 'r', 'x');
+    ASSERT.that(table.columnKeySet()).hasContentsInOrder(0, 1, 2, 3, 4, 5, 7);
+    ASSERT.that(table.values()).hasContentsInOrder("cat", "axe", "baz", "tub",
+        "dog", "bar", "foo", "foo", "bar");
+    ASSERT.that(table.row('c').keySet()).hasContentsInOrder(0, 3);
+    ASSERT.that(table.column(5).keySet()).hasContentsInOrder('e', 'x');
+  }
+
+  public void testBuilder_orderRowsAndColumnsBy_dense() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderRowsBy(Ordering.natural());
+    builder.orderColumnsBy(Ordering.natural());
+    builder.put('c', 3, "foo");
+    builder.put('c', 2, "bar");
+    builder.put('c', 1, "baz");
+    builder.put('b', 3, "cat");
+    builder.put('b', 1, "dog");
+    builder.put('a', 3, "foo");
+    builder.put('a', 2, "bar");
+    builder.put('a', 1, "baz");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.rowKeySet()).hasContentsInOrder('a', 'b', 'c');
+    ASSERT.that(table.columnKeySet()).hasContentsInOrder(1, 2, 3);
+    ASSERT.that(table.values()).hasContentsInOrder("baz", "bar", "foo", "dog",
+        "cat", "baz", "bar", "foo");
+    ASSERT.that(table.row('c').keySet()).hasContentsInOrder(1, 2, 3);
+    ASSERT.that(table.column(1).keySet()).hasContentsInOrder('a', 'b', 'c');
+  }
+
+  public void testBuilder_orderRowsBy_sparse() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderRowsBy(Ordering.natural());
+    builder.put('x', 2, "foo");
+    builder.put('r', 1, "bar");
+    builder.put('c', 3, "baz");
+    builder.put('b', 7, "cat");
+    builder.put('e', 5, "dog");
+    builder.put('c', 0, "axe");
+    builder.put('e', 3, "tub");
+    builder.put('r', 4, "foo");
+    builder.put('x', 5, "bar");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.rowKeySet()).hasContentsInOrder('b', 'c', 'e', 'r', 'x');
+    ASSERT.that(table.column(5).keySet()).hasContentsInOrder('e', 'x');
+  }
+
+  public void testBuilder_orderRowsBy_dense() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderRowsBy(Ordering.natural());
+    builder.put('c', 3, "foo");
+    builder.put('c', 2, "bar");
+    builder.put('c', 1, "baz");
+    builder.put('b', 3, "cat");
+    builder.put('b', 1, "dog");
+    builder.put('a', 3, "foo");
+    builder.put('a', 2, "bar");
+    builder.put('a', 1, "baz");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.rowKeySet()).hasContentsInOrder('a', 'b', 'c');
+    ASSERT.that(table.column(1).keySet()).hasContentsInOrder('a', 'b', 'c');
+  }
+
+  public void testBuilder_orderColumnsBy_sparse() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderColumnsBy(Ordering.natural());
+    builder.put('x', 2, "foo");
+    builder.put('r', 1, "bar");
+    builder.put('c', 3, "baz");
+    builder.put('b', 7, "cat");
+    builder.put('e', 5, "dog");
+    builder.put('c', 0, "axe");
+    builder.put('e', 3, "tub");
+    builder.put('r', 4, "foo");
+    builder.put('x', 5, "bar");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.columnKeySet()).hasContentsInOrder(0, 1, 2, 3, 4, 5, 7);
+    ASSERT.that(table.row('c').keySet()).hasContentsInOrder(0, 3);
+  }
+
+  public void testBuilder_orderColumnsBy_dense() {
+    ImmutableTable.Builder<Character, Integer, String> builder
+        = ImmutableTable.builder();
+    builder.orderColumnsBy(Ordering.natural());
+    builder.put('c', 3, "foo");
+    builder.put('c', 2, "bar");
+    builder.put('c', 1, "baz");
+    builder.put('b', 3, "cat");
+    builder.put('b', 1, "dog");
+    builder.put('a', 3, "foo");
+    builder.put('a', 2, "bar");
+    builder.put('a', 1, "baz");
+    Table<Character, Integer, String> table = builder.build();
+    ASSERT.that(table.columnKeySet()).hasContentsInOrder(1, 2, 3);
+    ASSERT.that(table.row('c').keySet()).hasContentsInOrder(1, 2, 3);
+  }
+}
Project google/guava in file ...st.com.google.common.collect.Collections2Test.java (2012-10-29)
@@ -22,7 +22,6 @@ import static com.google.common.collect.Lists.newLinkedList;
 import static com.google.common.collect.testing.testers.CollectionIteratorTester.getIteratorKnownOrderRemoveSupportedMethod;
 import static java.util.Arrays.asList;
 import static java.util.Collections.nCopies;
-import static org.junit.contrib.truth.Truth.ASSERT;
 
 import com.google.common.annotations.GwtCompatible;
 import com.google.common.annotations.GwtIncompatible;
@@ -196,140 +195,6 @@ public class Collections2Test extends TestCase {
         .createTestSuite();
   }
 
-  public abstract static class FilterChangeTest extends TestCase {
-    protected abstract <E> List<E> newList();
-
-    public void testFilterIllegalAdd() {
-      List<String> unfiltered = newList();
-      Collection<String> filtered
-          = Collections2.filter(unfiltered, NOT_YYY_ZZZ);
-      filtered.add("a");
-      filtered.add("b");
-      ASSERT.that(filtered).hasContentsInOrder("a", "b");
-
-      try {
-        filtered.add("yyy");
-        fail();
-      } catch (IllegalArgumentException expected) {}
-
-      try {
-        filtered.addAll(asList("c", "zzz", "d"));
-        fail();
-      } catch (IllegalArgumentException expected) {}
-
-      ASSERT.that(filtered).hasContentsInOrder("a", "b");
-    }
-
-    public void testFilterChangeUnfiltered() {
-      List<String> unfiltered = newList();
-      Collection<String> filtered
-          = Collections2.filter(unfiltered, NOT_YYY_ZZZ);
-
-      unfiltered.add("a");
-      unfiltered.add("yyy");
-      unfiltered.add("b");
-      ASSERT.that(unfiltered).hasContentsInOrder("a", "yyy", "b");
-      ASSERT.that(filtered).hasContentsInOrder("a", "b");
-
-      unfiltered.remove("a");
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy", "b");
-      ASSERT.that(filtered).hasContentsInOrder("b");
-
-      unfiltered.clear();
-      ASSERT.that(unfiltered).isEmpty();
-      ASSERT.that(filtered).isEmpty();
-
-      unfiltered.add("yyy");
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy");
-      ASSERT.that(filtered).isEmpty();
-      filtered.clear();
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy");
-      ASSERT.that(filtered).isEmpty();
-
-      unfiltered.clear();
-      filtered.clear();
-      ASSERT.that(unfiltered).isEmpty();
-      ASSERT.that(filtered).isEmpty();
-
-      unfiltered.add("a");
-      ASSERT.that(unfiltered).hasContentsInOrder("a");
-      ASSERT.that(filtered).hasContentsInOrder("a");
-      filtered.clear();
-      ASSERT.that(unfiltered).isEmpty();
-      ASSERT.that(filtered).isEmpty();
-
-      unfiltered.clear();
-      Collections.addAll(unfiltered,
-          "a", "b", "yyy", "zzz", "c", "d", "yyy", "zzz");
-      ASSERT.that(unfiltered).hasContentsInOrder(
-          "a", "b", "yyy", "zzz", "c", "d", "yyy", "zzz");
-      ASSERT.that(filtered).hasContentsInOrder("a", "b", "c", "d");
-      filtered.clear();
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy", "zzz", "yyy", "zzz");
-      ASSERT.that(filtered).isEmpty();
-    }
-
-    public void testFilterChangeFiltered() {
-      List<String> unfiltered = newList();
-      Collection<String> filtered
-          = Collections2.filter(unfiltered, NOT_YYY_ZZZ);
-
-      unfiltered.add("a");
-      unfiltered.add("yyy");
-      filtered.add("b");
-      ASSERT.that(unfiltered).hasContentsInOrder("a", "yyy", "b");
-      ASSERT.that(filtered).hasContentsInOrder("a", "b");
-
-      filtered.remove("a");
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy", "b");
-      ASSERT.that(filtered).hasContentsInOrder("b");
-
-      filtered.clear();
-      ASSERT.that(unfiltered).hasContentsInOrder("yyy");
-      ASSERT.that(filtered);
-    }
-
-    public void testFilterFiltered() {
-      List<String> unfiltered = newList();
-      Collection<String> filtered = Collections2.filter(
-          Collections2.filter(unfiltered, LENGTH_1), STARTS_WITH_VOWEL);
-      unfiltered.add("a");
-      unfiltered.add("b");
-      unfiltered.add("apple");
-      unfiltered.add("banana");
-      unfiltered.add("e");
-      ASSERT.that(filtered).hasContentsInOrder("a", "e");
-      ASSERT.that(unfiltered).hasContentsInOrder("a", "b", "apple", "banana", "e");
-
-      try {
-        filtered.add("d");
-        fail();
-      } catch (IllegalArgumentException expected) {}
-      try {
-        filtered.add("egg");
-        fail();
-      } catch (IllegalArgumentException expected) {}
-      ASSERT.that(filtered).hasContentsInOrder("a", "e");
-      ASSERT.that(unfiltered).hasContentsInOrder("a", "b", "apple", "banana", "e");
-
-      filtered.clear();
-      ASSERT.that(filtered).isEmpty();
-      ASSERT.that(unfiltered).hasContentsInOrder("b", "apple", "banana");
-    }
-  }
-
-  public static class ArrayListFilterChangeTest extends FilterChangeTest {
-    @Override protected <E> List<E> newList() {
-      return Lists.newArrayList();
-    }
-  }
-
-  public static class LinkedListFilterChangeTest extends FilterChangeTest {
-    @Override protected <E> List<E> newList() {
-      return Lists.newLinkedList();
-    }
-  }
-
   private static final Function<String, String> REMOVE_FIRST_CHAR
       = new Function<String, String>() {
         @Override