Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 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 static com.google.common.base.Preconditions.checkNotNull;
 
 
 
 import java.util.List;

Implementation helper for EqualsTester and EquivalenceTester that tests for equivalence classes.

Author(s):
Gregory Kick
 
 final class RelationshipTester<T> {
 
   static class ItemReporter {
     String reportItem(Item<?> item) {
       return item.toString();
     }
   }

  
A word about using com.google.common.base.Equivalence, which automatically checks for null and identical inputs: This sounds like it ought to be a problem here, since the goals of this class include testing that equals() is reflexive and is tolerant of null. However, there's no problem. The reason: EqualsTester tests null and identical inputs directly against equals() rather than through the Equivalence.
 
   private final Equivalence<? super T> equivalence;
   private final String relationshipName;
   private final String hashName;
   private final ItemReporter itemReporter;
   private final List<ImmutableList<T>> groups = Lists.newArrayList();
 
   RelationshipTester(Equivalence<? super T> equivalenceString relationshipNameString hashName,
       ItemReporter itemReporter) {
     this. = checkNotNull(equivalence);
     this. = checkNotNull(relationshipName);
     this. = checkNotNull(hashName);
     this. = checkNotNull(itemReporter);
   }
 
   // TODO(cpovirk): should we reject null items, since the tests already check null automatically?
   public RelationshipTester<T> addRelatedGroup(Iterable<? extends T> group) {
     .add(ImmutableList.copyOf(group));
     return this;
   }
 
   public void test() {
     for (int groupNumber = 0; groupNumber < .size(); groupNumber++) {
       ImmutableList<T> group = .get(groupNumber);
       for (int itemNumber = 0; itemNumber < group.size(); itemNumber++) {
         // check related items in same group
         for (int relatedItemNumber = 0; relatedItemNumber < group.size(); relatedItemNumber++) {
           if (itemNumber != relatedItemNumber) {
             assertRelated(groupNumberitemNumberrelatedItemNumber);
           }
         }
         // check unrelated items in all other groups
         for (int unrelatedGroupNumber = 0; unrelatedGroupNumber < .size();
             unrelatedGroupNumber++) {
           if (groupNumber != unrelatedGroupNumber) {
             ImmutableList<T> unrelatedGroup = .get(unrelatedGroupNumber);
             for (int unrelatedItemNumber = 0; unrelatedItemNumber < unrelatedGroup.size();
                 unrelatedItemNumber++) {
               assertUnrelated(groupNumberitemNumberunrelatedGroupNumberunrelatedItemNumber);
             }
           }
         }
       }
     }
   }
 
   private void assertRelated(int groupNumberint itemNumberint relatedItemNumber) {
     Item<T> itemInfo = getItem(groupNumberitemNumber);
     Item<T> relatedInfo = getItem(groupNumberrelatedItemNumber);
    T item = itemInfo.value;
    T related = relatedInfo.value;
    assertWithTemplate("$ITEM must be $RELATIONSHIP to $OTHER"itemInforelatedInfo,
        .equivalent(itemrelated));
    
    int itemHash = .hash(item);
    int relatedHash = .hash(related);
    assertWithTemplate("the $HASH (" + itemHash + ") of $ITEM must be equal to the $HASH ("
        + relatedHash + ") of $OTHER"itemInforelatedInfoitemHash == relatedHash);
  }
  private void assertUnrelated(int groupNumberint itemNumberint unrelatedGroupNumber,
      int unrelatedItemNumber) {
    Item<T> itemInfo = getItem(groupNumberitemNumber);
    Item<T> unrelatedInfo = getItem(unrelatedGroupNumberunrelatedItemNumber);
    assertWithTemplate("$ITEM must not be $RELATIONSHIP to $OTHER"itemInfounrelatedInfo,
        !.equivalent(itemInfo.valueunrelatedInfo.value));
  }
  private void assertWithTemplate(String templateItem<T> itemItem<T> otherboolean condition) {
    if (!condition) {
      throw new AssertionFailedError(template
          .replace("$RELATIONSHIP")
          .replace("$HASH")
          .replace("$ITEM".reportItem(item))
          .replace("$OTHER".reportItem(other)));
    }
  }
  private Item<T> getItem(int groupNumberint itemNumber) {
    return new Item<T>(.get(groupNumber).get(itemNumber), groupNumberitemNumber);
  }
  static final class Item<T> {
    final T value;
    final int groupNumber;
    final int itemNumber;
    Item(T valueint groupNumberint itemNumber) {
      this. = value;
      this. = groupNumber;
      this. = itemNumber;
    }
    @Override public String toString() {
      return new StringBuilder()
          .append()
          .append(" [group ")
          .append( + 1)
          .append(", item ")
          .append( + 1)
          .append(']')
          .toString();
    }
  }
New to GrepCode? Check out our FAQ X