StatusCache implements DeployerCommandListener, DeployerSensorListener { public synchronized void registerSensor(Sensor sensor) { if (sensor == null) { throw new IllegalArgumentException("Null sensor parameter is not allowed."); } if (isShutdownInProcess) { return; } Sensor previous = sensors.put(sensor.getSensorID(), sensor); Logger initLog = Logger.getLogger(Constants.SENSOR_INIT_LOG_CATEGORY); if (previous != null) { initLog.error( "Duplicate registration of sensor ID {0}. Sensor ''{1}'' has replaced ''{2}''.", sensor.getSensorID(), sensor.getName(), previous.getName() ); } sensorMap.init(sensor); initLog.info("Registered sensor : {0}", sensor.toString()); } StatusCache();  StatusCache(ChangedStatusTable cst, EventProcessorChain epc); @Override void onCommandsDeployed(Set<Command> commands); @Override void onSensorsDeployed(Set<Sensor> sensors); void start(); synchronized void registerSensor(Sensor sensor); Sensor getSensor(int id); synchronized void shutdown(); synchronized void update(Event event); Map<Integer, String> queryStatus(Set<Integer> sensorIDs); String queryStatus(Integer sensorID); Event queryStatus(String name); Iterator<Event> getStateSnapshot(); void initializeEventContext(Set<Command> commands); Integer sensorIDFromName(String sensorName);  }
@Test public void testRegisterSensor() { StatusCache cache = new StatusCache(); DummyCommand cmd1 = new DummyCommand("0"); Sensor s1 = new RangeSensor("test1", 1, cache, cmd1, 1, -10, 10); cache.registerSensor(s1); Assert.assertTrue(cache.getSensor(1).equals(s1)); Assert.assertTrue(Sensor.isUnknownSensorValue(cache.queryStatus(1))); s1.start(); waitForUpdate(); Assert.assertTrue(cache.queryStatus(1).equals("0")); DummyCommand cmd2 = new DummyCommand("10"); Sensor s2 = new LevelSensor("test2", 2, cache, cmd2, 1); cache.registerSensor(s2); Assert.assertTrue(cache.getSensor(2).equals(s2)); Assert.assertTrue(Sensor.isUnknownSensorValue(cache.queryStatus(2))); s2.start(); waitForUpdate(); Assert.assertTrue(cache.queryStatus(2).equals("10")); }
@Test (expected = IllegalArgumentException.class) public void testNullRegistration() { StatusCache cache = new StatusCache(); cache.registerSensor(null); }