コード例 #1
0
  @Test
  public void basicTests() throws Exception {
    final InstanceIdentifier<DataObject> instanceIdentifier1 =
        InstanceIdentifier.create(DataObject.class);
    final InstanceIdentifier<DataObject> instanceIdentifier2 =
        InstanceIdentifier.create(DataObject.class);
    final InstanceIdentifier<DataObject> instanceIdentifier4 =
        InstanceIdentifier.create(DataObject.class);
    final InstanceIdentifier<NodeChild> instanceIdentifier3 =
        InstanceIdentifier.builder(Nodes.class)
            .child(Node.class, new NodeKey(10))
            .child(NodeChild.class)
            .build();
    final Object object = new Object();

    assertTrue(instanceIdentifier1.equals(instanceIdentifier1));
    assertFalse(instanceIdentifier1.equals(null));
    assertFalse(instanceIdentifier1.equals(object));
    assertTrue(instanceIdentifier1.equals(instanceIdentifier2));

    final Field pathArgumentsField = InstanceIdentifier.class.getDeclaredField("pathArguments");
    final Field wildCardField = InstanceIdentifier.class.getDeclaredField("wildcarded");
    pathArgumentsField.setAccessible(true);
    wildCardField.setAccessible(true);
    pathArgumentsField.set(instanceIdentifier2, instanceIdentifier1.pathArguments);
    wildCardField.set(instanceIdentifier4, true);

    assertTrue(instanceIdentifier1.equals(instanceIdentifier2));

    assertFalse(instanceIdentifier1.equals(instanceIdentifier3));
    assertFalse(instanceIdentifier1.equals(instanceIdentifier4));
    final InstanceIdentifier instanceIdentifier5 = InstanceIdentifier.create(Node.class);
    final Field hashField = InstanceIdentifier.class.getDeclaredField("hash");
    hashField.setAccessible(true);
    hashField.set(instanceIdentifier5, instanceIdentifier1.hashCode());
    wildCardField.set(instanceIdentifier5, false);

    assertFalse(instanceIdentifier1.equals(instanceIdentifier5));
    assertNotNull(
        InstanceIdentifier.create(ImmutableList.copyOf(instanceIdentifier1.getPathArguments())));
    assertNotNull(InstanceIdentifier.create(Nodes.class).child(Node.class));
    assertNotNull(InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(5)));
    assertNotNull(instanceIdentifier5.augmentation(DataObject.class));
    assertNotNull(instanceIdentifier1.hashCode());
    assertNotNull(instanceIdentifier1.toString());

    final InstanceIdentifierBuilder instanceIdentifierBuilder = instanceIdentifier1.builder();
    assertEquals(instanceIdentifier1.hashCode(), instanceIdentifierBuilder.hashCode());
    assertNotNull(instanceIdentifierBuilder.augmentation(InstantiatedFoo.class));
    assertNotNull(instanceIdentifierBuilder.toInstance());
  }
コード例 #2
0
  @Test
  public void hashCodeTest() {
    final InstanceIdentifierBuilder<DataObject> builder1 =
        InstanceIdentifier.create(DataObject.class).builder();
    final InstanceIdentifierBuilder<DataObject> builder2 =
        InstanceIdentifier.create(DataObject.class).builder();
    final InstanceIdentifierBuilder<Nodes> builder3 =
        InstanceIdentifier.create(Nodes.class).builder();
    final InstanceIdentifierBuilder<Nodes> builder4 =
        InstanceIdentifier.create(Nodes.class).builder();
    final Object obj = new Object();

    assertTrue(builder1.hashCode() == builder2.hashCode());
    assertTrue(builder1.hashCode() != builder3.hashCode());
    assertTrue(builder3.hashCode() == builder4.hashCode());
    assertTrue(builder2.hashCode() != builder4.hashCode());
    assertTrue(builder1.hashCode() != obj.hashCode());

    builder3.child(Node.class, new NodeKey(10));

    assertTrue(builder3.hashCode() != builder4.hashCode());
  }
コード例 #3
0
  @Test
  public void equalsTest() {
    final InstanceIdentifierBuilder<DataObject> builder1 =
        InstanceIdentifier.create(DataObject.class).builder();
    final InstanceIdentifierBuilder<DataObject> builder2 =
        InstanceIdentifier.create(DataObject.class).builder();
    final InstanceIdentifierBuilder<Nodes> builder3 =
        InstanceIdentifier.create(Nodes.class).builder();
    final InstanceIdentifierBuilder<Nodes> builder4 =
        InstanceIdentifier.create(Nodes.class).builder();
    final Object obj = new Object();

    assertTrue(builder1.equals(builder2));
    assertTrue(builder2.equals(builder1));
    assertTrue(builder2.equals(builder2));
    assertTrue(builder3.equals(builder4));
    assertTrue(builder4.equals(builder4));
    assertFalse(builder3.equals(builder1));
    assertFalse(builder3.equals(null));
    assertFalse(builder4.equals(null));
    assertFalse(builder1.equals(obj));

    builder3.child(Node.class, new NodeKey(10));
    assertFalse(builder3.equals(builder4));
    assertFalse(builder4.equals(builder3));

    builder4.child(Node.class, new NodeKey(20));
    assertFalse(builder3.equals(builder4));
    assertFalse(builder4.equals(builder3));

    InstanceIdentifierBuilder<Nodes> iib1 = new InstanceIdentifierBuilderImpl(null, null, 31, true);
    InstanceIdentifierBuilder<Nodes> iib2 =
        new InstanceIdentifierBuilderImpl(null, null, 31, false);
    InstanceIdentifierBuilder<Nodes> iib3 = new InstanceIdentifierBuilderImpl(null, null, 31, true);
    assertFalse(iib1.equals(iib2));
    assertFalse(iib2.equals(iib1));
    assertTrue(iib1.equals(iib3));
    assertTrue(iib3.equals(iib1));
  }