StateSensor extends Sensor { @Override public String toString() { return "Sensor (Name = '" + getName() + "', ID = '" + getSensorID() + "', State Mappings: " + states.toString() + ")"; } StateSensor(String name, int sensorID, StatusCache cache, EventProducer producer, int commandID, DistinctStates states); protected  StateSensor(String name, int sensorID, StatusCache cache, EnumSensorType type, EventProducer producer,
                        int commandID, DistinctStates states, boolean includeStatesAsProperties); @Override Event processEvent(String value); void setStrictStateMapping(boolean strictStateMapping); @Override String toString(); final static boolean DEFAULT_STRICT_STATE_MAPPING; }
@Test public void testMappingToNull() throws Exception { StateSensor.DistinctStates states = new StateSensor.DistinctStates(); states.addStateMapping("0", "zero"); states.addStateMapping("1", ""); states.addStateMapping("2", "two"); states.addStateMapping(" ", "three"); states.addStateMapping("", "four"); MixedStateReadCommand readCommand = new MixedStateReadCommand("0", "1", "2", "", " "); StateSensor s1 = new StateSensor("empty", 40, cache, readCommand, 1, states); cache.registerSensor(s1); s1.start(); Assert.assertTrue(getSensorValueFromCache(40).equals("zero")); Assert.assertTrue(s1.getSensorID() == 40); Assert.assertTrue(s1.isPolling()); Assert.assertFalse(s1.isEventListener()); Assert.assertTrue(s1.getName().equals("empty")); Assert.assertTrue(s1.getProperties().size() == 5); Assert.assertTrue(s1.getProperties().keySet().contains("state-1")); Assert.assertTrue(s1.getProperties().keySet().contains("state-2")); Assert.assertTrue(s1.getProperties().keySet().contains("state-3")); Assert.assertTrue(s1.getProperties().keySet().contains("state-4")); Assert.assertTrue(s1.getProperties().keySet().contains("state-5")); Assert.assertTrue(s1.getProperties().values().contains("0")); Assert.assertTrue(s1.getProperties().values().contains("1")); Assert.assertTrue(s1.getProperties().values().contains("2")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(40).equals("")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(40).equals("two")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(40).equals("four")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(40).equals("three")); states = new StateSensor.DistinctStates(); states.addStateMapping("3", null); states.addStateMapping("4", null); readCommand = new MixedStateReadCommand("3", "4"); StateSensor s2 = new StateSensor("null mapping", 41, cache, readCommand, 2, states); cache.registerSensor(s2); s2.start(); Assert.assertTrue(getSensorValueFromCache(41).equals("3")); Assert.assertTrue(s2.getSensorID() == 41); Assert.assertTrue(s2.isPolling()); Assert.assertFalse(s2.isEventListener()); Assert.assertTrue(s2.getName().equals("null mapping")); Assert.assertTrue(s2.getProperties().size() == 2); Assert.assertTrue(s2.getProperties().keySet().contains("state-1")); Assert.assertTrue(s2.getProperties().keySet().contains("state-2")); Assert.assertTrue(s2.getProperties().values().contains("3")); Assert.assertTrue(s2.getProperties().values().contains("4")); Assert.assertTrue(s2.toString().contains("4")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(41).equals("4")); states = new StateSensor.DistinctStates(); states.addStateMapping("0", "zero"); states.addStateMapping(null, "one"); Assert.assertTrue(states.hasState("0")); readCommand = new MixedStateReadCommand("0", "1"); StateSensor s3 = new StateSensor("null mapping", 42, cache, readCommand, 3, states); cache.registerSensor(s3); s3.start(); Assert.assertTrue(getSensorValueFromCache(42).equals("zero")); Assert.assertTrue(s3.getSensorID() == 42); Assert.assertTrue(s3.isPolling()); Assert.assertFalse(s3.isEventListener()); Assert.assertTrue(s3.getName().equals("null mapping")); Assert.assertTrue(s3.getProperties().size() == 2); Assert.assertTrue(s3.getProperties().keySet().contains("state-1")); Assert.assertTrue(s3.getProperties().keySet().contains("state-2")); Assert.assertTrue(s3.getProperties().values().contains("0")); Assert.assertTrue(s3.toString().contains("zero")); readCommand.nextValue(); Assert.assertTrue(getSensorValueFromCache(42).equals(Sensor.UNKNOWN_STATUS)); }
@Test public void testToString() { StateSensor.DistinctStates states = new StateSensor.DistinctStates(); states.addState("foo"); StateSensor s1 = new StateSensor("single", 1, cache, new StateReadCommand("foo"), 1, states); Assert.assertTrue(s1.toString().contains("single")); Assert.assertTrue(s1.toString().contains("foo")); states = new StateSensor.DistinctStates(); states.addState("foo"); states.addState("bar"); s1 = new StateSensor("twostate", 2, cache, new StateReadCommand("foo", "bar"), 1, states); Assert.assertTrue(s1.toString().contains("twostate")); Assert.assertTrue(s1.toString().contains("foo")); Assert.assertTrue(s1.toString().contains("bar")); }