import org.junit.Before;
import org.junit.Test;
import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Config;
+import org.wamblee.xmlrouter.config.Identifiable;
public class ConfigImplTest {
- private static interface MyType {
+ private static final String CONFIG_TYPE = "transformation";
+
+ private static interface MyType extends Identifiable<MyType> {
}
private static class MyTypeWrapper implements MyType {
- private Id<MyType> id;
+ private String prefix;
private MyType type;
- public MyTypeWrapper(Id<MyType> aId, MyType aType) {
- id = aId;
+ public MyTypeWrapper(String aPrefix, MyType aType) {
+ prefix = aPrefix;
type = aType;
}
public MyType getType() {
return type;
}
+
+ @Override
+ public Id getId() {
+ return new Id(prefix + type.getId().getId());
+ }
}
private AtomicLong sequence;
@Before
public void setUp() {
sequence = new AtomicLong(1L);
- config = new ConfigImpl<MyType>(sequence) {
+ config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
@Override
- public MyType wrap(Id<MyType> aId, MyType aT) {
- return new MyTypeWrapper(aId, aT);
+ public MyType wrap(String aPrefix, MyType aT) {
+ return new MyTypeWrapper(aPrefix, aT);
}
};
}
@Test
public void testAdd() {
MyType type1 = mock(MyType.class);
- assertFalse(config.isDirty());
+ when(type1.getId()).thenReturn(new Id("type1"));
- Id<MyType> id1 = config.add(type1);
+ config.add(type1);
- assertNotNull(id1);
- assertEquals(1, config.map().size());
- assertTrue(config.map().get(id1) instanceof MyTypeWrapper);
- assertSame(type1, ((MyTypeWrapper) config.map().get(id1)).getType());
- assertTrue(config.isDirty());
+ assertEquals(1, config.values().size());
+ MyType firstValue = config.values().iterator().next();
- config.resetDirty();
- assertFalse(config.isDirty());
+ assertTrue(firstValue instanceof MyTypeWrapper);
+ assertSame(type1, ((MyTypeWrapper) firstValue).getType());
+ assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue
+ .getId().getId());
// add another one.
MyType type2 = mock(MyType.class);
- Id<MyType> id2 = config.add(type2);
- assertNotNull(id2);
- assertEquals(2, config.map().size());
- assertFalse(id1.equals(id2));
- assertTrue(config.isDirty());
+ when(type2.getId()).thenReturn(new Id("type2"));
+ config.add(type2);
+ assertEquals(2, config.values().size());
}
@Test
public void testRemove() {
MyType type1 = mock(MyType.class);
- Id<MyType> id1 = config.add(type1);
+ when(type1.getId()).thenReturn(new Id("type1"));
- assertNotNull(id1);
- assertEquals(1, config.map().size());
+ config.add(type1);
- config.resetDirty();
- assertFalse(config.isDirty());
+ assertEquals(1, config.values().size());
- config.remove(id1);
- assertTrue(config.map().isEmpty());
- assertTrue(config.isDirty());
+ assertTrue(config.remove(new Id("type1")));
+ assertTrue(config.values().isEmpty());
}
@Test(expected = UnsupportedOperationException.class)
public void testUnmodifiable() {
- config.map().put(new Id<MyType>(100L), mock(MyType.class));
+ config.values().add(mock(MyType.class));
+ }
+
+ @Test
+ public void testEquals() {
+
+ Config<MyType> config1 = new ConfigImpl<MyType>(new Id<Config>(
+ CONFIG_TYPE)) {
+ @Override
+ public MyType wrap(String aPrefix, MyType aT) {
+ return new MyTypeWrapper(aPrefix, aT);
+ }
+ };
+ assertFalse(config1.equals(null));
+ assertFalse(config1.equals("hello"));
+ Config<MyType> config2 = new ConfigImpl<MyType>(new Id<Config>(
+ CONFIG_TYPE)) {
+ @Override
+ public MyType wrap(String aPrefix, MyType aT) {
+ return new MyTypeWrapper(aPrefix, aT);
+ }
+ };
+ assertEquals(config1, config2);
+ assertEquals(config1.hashCode(), config2.hashCode());
+
+ MyType type1 = mock(MyType.class);
+ when(type1.getId()).thenReturn(new Id("type1"));
+
+ config1.add(type1);
+ assertFalse(config1.equals(config2));
+
+ MyType type2 = mock(MyType.class);
+ when(type2.getId()).thenReturn(new Id("type1"));
+
+ config2.add(type2);
+ assertEquals(config1, config2);
+ assertEquals(config1.hashCode(), config2.hashCode());
+
+ assertTrue(config2.remove(type2.getId()));
+ assertFalse(config1.equals(config2));
}
}