Skip to content
Permalink
Browse files

Don't register a bean that already is in the manager

  • Loading branch information...
danielb987 committed Aug 3, 2019
1 parent 5e5ac47 commit 8ed6dba5dcbb7387b35e08b132c2dbf83b746467
@@ -13,8 +13,11 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import jmri.util.NamedBeanComparator;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.OverridingMethodsMustInvokeSuper;
@@ -206,6 +209,25 @@ public void register(E s) {
log.error("systemName is already registered: {}", systemName);
throw new IllegalArgumentException("systemName is already registered: " + systemName);
}
} else {
// Check if the manager already has a bean with a system name that is
// not equal to the system name of the new bean, but there the two
// system names are treated as the same. For example LT1 and LT01.
if (_beans.contains(s)) {
final AtomicReference<String> oldSysName = new AtomicReference<>();
NamedBeanComparator<NamedBean> c = new NamedBeanComparator<>();
_beans.forEach((NamedBean t) -> {
if (c.compare(s, t) == 0) {
oldSysName.set(t.getSystemName());
}
});
if (!systemName.equals(oldSysName.get())) {
String msg = String.format("systemName is already registered. Current system name: %s. New system name: %s",
oldSysName, systemName);
log.error(msg);
throw new IllegalArgumentException(msg);
}
}
}

// clear caches
@@ -0,0 +1,107 @@
package jmri.managers;

import jmri.InstanceManager;
import jmri.JmriException;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import jmri.util.JUnitAppender;
import jmri.util.JUnitUtil;
import org.junit.*;
import org.junit.rules.ExpectedException;

/**
* Test NamedBeanComparator
* @author Daniel Bergqvist Copyright (C) 2019
*/
public class AbstractManagerTest {

@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void testRegister() {
MyBean a1 = new MyBean("IT1");
MyBean b1 = new MyBean("IT01");
MyBean a2 = new MyBean("IT02");
MyBean b2 = new MyBean("IT2");

MyManager m = new MyManager();

boolean exceptionThrown = false;
m.register(a1);
try {
m.register(b1);
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
Assert.assertTrue("exception thrown", exceptionThrown);
JUnitAppender.assertErrorMessage("systemName is already registered. Current system name: IT1. New system name: IT01");

exceptionThrown = false;
m.register(a2);
try {
m.register(b2);
} catch (IllegalArgumentException e) {
exceptionThrown = true;
}
Assert.assertTrue("exception thrown", exceptionThrown);
JUnitAppender.assertErrorMessage("systemName is already registered. Current system name: IT02. New system name: IT2");
}

@Before
public void setUp() {
JUnitUtil.setUp();
}

@After
public void tearDown() {
JUnitUtil.tearDown();
}


private class MyManager extends AbstractManager<MyBean> {

MyManager() {
super(InstanceManager.getDefault(InternalSystemConnectionMemo.class));
}

@Override
public int getXMLOrder() {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public String getBeanTypeHandled(boolean plural) {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public char typeLetter() {
throw new UnsupportedOperationException("Not supported.");
}

}

private class MyBean extends jmri.implementation.AbstractNamedBean {

MyBean(String sysName) {
super(sysName);
}

@Override
public void setState(int s) throws JmriException {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public int getState() {
throw new UnsupportedOperationException("Not supported.");
}

@Override
public String getBeanType() {
throw new UnsupportedOperationException("Not supported.");
}

}

}
@@ -5,6 +5,7 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
AbstractManagerTest.class,
DefaultConditionalManagerTest.class,
DefaultIdTagManagerTest.class,
DefaultLogixManagerTest.class,

0 comments on commit 8ed6dba

Please sign in to comment.
You can’t perform that action at this time.