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 class MyTypeWrapper implements MyType {
- private String prefix;
private MyType type;
- public MyTypeWrapper(String aPrefix, MyType aType) {
- prefix = aPrefix;
+ public MyTypeWrapper(MyType aType) {
type = aType;
}
@Override
public Id getId() {
- return new Id(prefix + type.getId().getId());
+ return type.getId();
+ }
+ }
+
+ public static final class MyTypeConfig extends ConfigImpl<MyType> {
+ public MyTypeConfig(String aId) {
+ super(MyType.class, aId);
+ }
+
+ public MyTypeConfig(MyTypeConfig aConfig) {
+ super(aConfig);
+ }
+
+ @Override
+ public MyType wrap(MyType aT) {
+ return new MyTypeWrapper(aT);
}
}
private AtomicLong sequence;
- private ExtendedConfig<MyType> config;
+ private MyTypeConfig config;
@Before
public void setUp() {
sequence = new AtomicLong(1L);
- config = new ConfigImpl<MyType>(new Id<Config>(CONFIG_TYPE)) {
- @Override
- public MyType wrap(String aPrefix, MyType aT) {
- return new MyTypeWrapper(aPrefix, aT);
- }
- };
+ config = new MyTypeConfig(CONFIG_TYPE);
}
@Test
assertTrue(firstValue instanceof MyTypeWrapper);
assertSame(type1, ((MyTypeWrapper) firstValue).getType());
- assertEquals(CONFIG_TYPE + "." + type1.getId().getId(), firstValue
- .getId().getId());
+ assertEquals(type1.getId().getId(), firstValue.getId().getId());
// add another one.
MyType type2 = 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);
- }
- };
+ Config<MyType> config1 = new MyTypeConfig(CONFIG_TYPE);
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);
- }
- };
+ Config<MyType> config2 = new MyTypeConfig(CONFIG_TYPE);
+
assertEquals(config1, config2);
assertEquals(config1.hashCode(), config2.hashCode());
assertTrue(config2.remove(type2.getId()));
assertFalse(config1.equals(config2));
}
+
+ @Test
+ public void testCopy() {
+ testAdd();
+ assertEquals(2, config.values().size());
+ MyTypeConfig copy = new MyTypeConfig(config);
+ assertEquals(config.getPrefix(), copy.getPrefix());
+ assertEquals(config, copy);
+
+ // verify the copy is not shallow
+ assertTrue(config.remove(new Id<MyType>("type1")));
+ assertEquals(1, config.values().size());
+ assertFalse(config.equals(copy));
+ }
}