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 interface MyType extends Identifiable {
}
private static class MyTypeWrapper implements MyType {
- private Id<MyType> id;
private MyType type;
- public MyTypeWrapper(Id<MyType> aId, MyType aType) {
- id = aId;
+ public MyTypeWrapper(MyType aType) {
type = aType;
}
public MyType getType() {
return type;
}
+
+ @Override
+ public Id getId() {
+ return type.getId();
+ }
}
private AtomicLong sequence;
@Before
public void setUp() {
sequence = new AtomicLong(1L);
- config = new ConfigImpl<MyType>(sequence) {
+ config = new ConfigImpl<MyType>(new Id<Config>("mytype")) {
@Override
- public MyType wrap(Id<MyType> aId, MyType aT) {
- return new MyTypeWrapper(aId, aT);
+ public MyType wrap(MyType aT) {
+ return new MyTypeWrapper(aT);
}
};
}
@Test
public void testAdd() {
MyType type1 = mock(MyType.class);
+ 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());
+ assertEquals(1, config.values().size());
+ assertTrue(config.values().get(0) instanceof MyTypeWrapper);
+ assertSame(type1, ((MyTypeWrapper) config.values().get(0)).getType());
// 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));
+ 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"));
+
+ config.add(type1);
- assertNotNull(id1);
- assertEquals(1, config.map().size());
+ assertEquals(1, config.values().size());
- config.remove(id1);
- assertTrue(config.map().isEmpty());
+ 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));
}
}