use of jmri.JmriException in project JMRI by JMRI.
the class StartupActionsManager method initialize.
/**
* {@inheritDoc}
*
* Loads the startup action preferences and, if all required managers have
* initialized without exceptions, performs those actions. Startup actions
* are only performed if {@link apps.startup.StartupModel#isValid()} is true
* for the action. It is assumed that the action has retained an Exception
* that can be used to explain why isValid() is false.
*/
@Override
public void initialize(Profile profile) throws InitializationException {
if (!this.isInitialized(profile)) {
boolean perform = true;
try {
this.requiresNoInitializedWithExceptions(profile, Bundle.getMessage("StartupActionsManager.RefusalToInitialize"));
} catch (InitializationException ex) {
perform = false;
}
try {
Element startup;
try {
startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE, true));
} catch (NullPointerException ex) {
log.debug("Reading element from version 2.9.6 namespace...");
startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE_OLD, true));
}
for (Element action : startup.getChildren()) {
// NOI18N
String adapter = action.getAttributeValue("class");
// NOI18N
String name = action.getAttributeValue("name");
String override = StartupActionModelUtil.getDefault().getOverride(name);
if (override != null) {
action.setAttribute("name", override);
log.info("Overridding statup action class {} with {}", name, override);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override), Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override)));
// after logging difference and creating error message
name = override;
}
// NOI18N
String type = action.getAttributeValue("type");
log.debug("Read {} {} adapter {}", type, name, adapter);
try {
log.debug("Creating {} {} adapter {}...", type, name, adapter);
// no perNode preferences
((XmlAdapter) Class.forName(adapter).newInstance()).load(action, null);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
log.error("Unable to create {} for {}", adapter, action, ex);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsCreationError", adapter, name), // NOI18N
Bundle.getMessage("StartupActionsCreationError", adapter, name)));
} catch (Exception ex) {
log.error("Unable to load {} into {}", action, adapter, ex);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsLoadError", adapter, name), // NOI18N
Bundle.getMessage("StartupActionsLoadError", adapter, name)));
}
}
} catch (NullPointerException ex) {
// ignore - this indicates migration has not occured
log.debug("No element to read");
}
if (perform) {
this.actions.stream().filter((action) -> (action.isValid())).forEachOrdered((action) -> {
try {
action.performAction();
} catch (JmriException ex) {
this.addInitializationException(profile, ex);
}
});
}
this.isDirty = false;
this.restartRequired = false;
this.setInitialized(profile, true);
List<Exception> exceptions = this.getInitializationExceptions(profile);
if (exceptions.size() == 1) {
throw new InitializationException(exceptions.get(0));
} else if (exceptions.size() > 1) {
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsMultipleErrors"), // NOI18N
Bundle.getMessage("StartupActionsMultipleErrors"));
}
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonSensorSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonSensorSocketService service = new JsonSensorSocketService(connection);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
Sensor sensor1 = manager.provideSensor("IS1");
// Sensor INACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
// Sensor ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
// Sensor UNKNOWN - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
sensor1.setKnownState(Sensor.ACTIVE);
// Sensor INCONSISTENT - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
sensor1.setKnownState(Sensor.ACTIVE);
// Sensor no value
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
JsonException exception = null;
try {
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
Assert.assertNull(exception);
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonSensorSocketServiceTest method testSensorChange.
public void testSensorChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
JsonSensorSocketService service = new JsonSensorSocketService(connection);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
Sensor sensor1 = manager.provideSensor("IS1");
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
// TODO: test that service is listener in SensorManager
// -1 not possible value
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.ACTIVE);
JUnitUtil.waitFor(() -> {
return sensor1.getKnownState() == Sensor.ACTIVE;
}, "Sensor ACTIVE");
Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.INACTIVE);
JUnitUtil.waitFor(() -> {
return sensor1.getKnownState() == Sensor.INACTIVE;
}, "Sensor INACTIVE");
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
service.onClose();
// TODO: test that service is no longer a listener in SensorManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testOnMessageChange.
@Test
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
try {
// SignalHead Yellow
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getState());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
use of jmri.JmriException in project JMRI by JMRI.
the class JsonSignalMastSocketServiceTest method testOnMessageChange.
@Test
@Ignore("Needs setup completed")
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
//create a signalmast for testing
String sysName = "IF$shsm:basic:one-searchlight:SM2";
String userName = "SM2";
SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
SignalMast s = manager.provideSignalMast(sysName);
s.setUserName(userName);
try {
// SignalMast Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, "Stop");
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
//aspect should be Stop
Assert.assertEquals("Stop", s.getAspect());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalMast to Unknown, should throw error, remain at Stop
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
Assert.assertEquals("Stop", s.getAspect());
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
// set SignalMast no value, should throw error, remain at Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
exception = null;
try {
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
} catch (IOException | JmriException ex) {
Assert.fail(ex.getMessage());
}
Assert.assertNull(exception);
Assert.assertEquals("Stop", s.getAspect());
}
Aggregations