@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()); }
@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()); }