Search in sources :

Example 76 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JmriUserPreferencesManager method readUserPreferences.

public final void readUserPreferences() {
    this.allowSave = false;
    this.loading = true;
    File perNodeConfig = null;
    try {
        // NOI18N
        perNodeConfig = FileUtil.getFile(FileUtil.PROFILE + Profile.PROFILE + "/" + NodeIdentity.identity() + "/" + Profile.UI_CONFIG);
        if (!perNodeConfig.canRead()) {
            perNodeConfig = null;
        }
    } catch (FileNotFoundException ex) {
    // ignore - this only means that sharedConfig does not exist.
    }
    if (perNodeConfig != null) {
        file = perNodeConfig;
        this.readComboBoxLastSelections();
        this.readPreferencesState();
        this.readSimplePreferenceState();
        this.readWindowDetails();
    } else {
        try {
            file = FileUtil.getFile(FileUtil.PROFILE + Profile.UI_CONFIG_FILENAME);
            if (file.exists()) {
                log.debug("start load user pref file: {}", file.getPath());
                try {
                    InstanceManager.getDefault(ConfigureManager.class).load(file, true);
                    this.allowSave = true;
                    // write new preferences format immediately
                    this.savePreferences();
                } catch (JmriException e) {
                    log.error("Unhandled problem loading configuration: " + e);
                } catch (NullPointerException e) {
                    log.error("NPE when trying to load user pref " + file);
                }
            } else {
                // if we got here, there is no saved user preferences
                log.info("No saved user preferences file");
            }
        } catch (FileNotFoundException ex) {
        // ignore - this only means that UserPrefsProfileConfig.xml does not exist.
        }
    }
    this.loading = false;
    this.allowSave = true;
}
Also used : ConfigureManager(jmri.ConfigureManager) FileNotFoundException(java.io.FileNotFoundException) JmriException(jmri.JmriException) File(java.io.File)

Example 77 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonReporterSocketServiceTest method testReporterChange.

@Test
public void testReporterChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonReporterSocketService service = new JsonReporterSocketService(connection);
        ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
        Reporter memory1 = manager.provideReporter("IR1");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        // TODO: test that service is listener in ReporterManager
        // default null value of memory1 has text representation "null" in JSON
        Assert.assertEquals("null", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        memory1.setReport("throw");
        JUnitUtil.waitFor(() -> {
            return memory1.getCurrentReport().equals("throw");
        }, "Reporter to throw");
        Assert.assertEquals("throw", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        memory1.setReport("close");
        JUnitUtil.waitFor(() -> {
            return memory1.getCurrentReport().equals("close");
        }, "Reporter to close");
        Assert.assertEquals("close", memory1.getCurrentReport());
        Assert.assertEquals("close", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        service.onClose();
    // TODO: test that service is no longer a listener in ReporterManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Reporter(jmri.Reporter) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 78 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonReporterSocketServiceTest method testOnMessageChange.

@Test
public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonReporterSocketService service = new JsonReporterSocketService(connection);
        ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
        Reporter memory1 = manager.provideReporter("IR1");
        // Reporter "close"
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals("close", memory1.getCurrentReport());
        // Reporter "throw"
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals("throw", memory1.getCurrentReport());
        // Reporter UNKNOWN - remains ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals(null, memory1.getCurrentReport());
        memory1.setReport("throw");
        // Reporter no value
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonException exception = null;
        try {
            service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals("throw", memory1.getCurrentReport());
        Assert.assertNull(exception);
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Reporter(jmri.Reporter) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 79 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonMemorySocketServiceTest method testMemoryChange.

public void testMemoryChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
        JsonMemorySocketService service = new JsonMemorySocketService(connection);
        MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
        Memory memory1 = manager.provideMemory("IM1");
        service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
        // TODO: test that service is listener in MemoryManager
        // default null value of memory1 has text representation "null" in JSON
        Assert.assertEquals("null", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        memory1.setValue("throw");
        JUnitUtil.waitFor(() -> {
            return memory1.getValue().equals("throw");
        }, "Memory to throw");
        Assert.assertEquals("throw", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        memory1.setValue("close");
        JUnitUtil.waitFor(() -> {
            return memory1.getValue().equals("close");
        }, "Memory to close");
        Assert.assertEquals("close", memory1.getValue());
        Assert.assertEquals("close", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        service.onClose();
    // TODO: test that service is no longer a listener in MemoryManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Memory(jmri.Memory) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MemoryManager(jmri.MemoryManager)

Example 80 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonPowerSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        // Power ON
        JsonNode message = connection.getObjectMapper().readTree("{\"state\":2}");
        JsonPowerSocketService service = new JsonPowerSocketService(connection);
        PowerManager power = InstanceManager.getDefault(PowerManager.class);
        power.setPower(PowerManager.UNKNOWN);
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.ON, power.getPower());
        // Power OFF
        message = connection.getObjectMapper().readTree("{\"state\":4}");
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        // JSON Power UNKNOWN
        message = connection.getObjectMapper().readTree("{\"state\":0}");
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        // did not change
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        // JSON Invalid
        message = connection.getObjectMapper().readTree("{\"state\":1}");
        JsonException exception = null;
        try {
            service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        // did not change
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        log.error("testOnMessageChange threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Aggregations

JmriException (jmri.JmriException)80 Sensor (jmri.Sensor)22 JsonException (jmri.server.json.JsonException)22 IOException (java.io.IOException)20 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 JsonMockConnection (jmri.server.json.JsonMockConnection)18 ArrayList (java.util.ArrayList)10 NamedBean (jmri.NamedBean)10 Turnout (jmri.Turnout)10 Test (org.junit.Test)7 SignalMast (jmri.SignalMast)6 ConfigureManager (jmri.ConfigureManager)5 SensorManager (jmri.SensorManager)5 File (java.io.File)4 PowerManager (jmri.PowerManager)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Light (jmri.Light)3 SignalHead (jmri.SignalHead)3 FileNotFoundException (java.io.FileNotFoundException)2 Hashtable (java.util.Hashtable)2