Version20ModelBuilder extends AbstractModelBuilder { @Override protected Document readControllerXMLDocument() throws InitializationException { SAXBuilder builder = new SAXBuilder(); File controllerXMLFile = getControllerDefinitionFile(config); if (!checkControllerDefinitionExists(config)) { try { throw new ControllerDefinitionNotFoundException( "Controller.xml not found -- make sure it's in " + controllerXMLFile.getAbsoluteFile() ); } catch (SecurityException e) { throw new InitializationException("Cannot access controller.xml : {0}", e, e.getMessage()); } } String xsdPath = CONTROLLER_XSD_PATH; try { URL xsdResource = Version20ModelBuilder.class.getResource(CONTROLLER_XSD_PATH); if (xsdResource == null) { log.error("Cannot find XSD schema ''{0}''. Disabling validation...", CONTROLLER_XSD_PATH); } else { xsdPath = xsdResource.getPath(); builder.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); builder.setProperty(JAXP_SCHEMA_SOURCE, xsdPath); builder.setValidation(true); } Document doc = builder.build(controllerXMLFile); lastTimeStamp = getControllerXMLTimeStamp(); return doc; } catch (Throwable t) { throw new XMLParsingException( "Unable to parse controller definition from " + "''{0}'' (accessing schema from ''{1}'') : {2}", t, controllerXMLFile.getAbsoluteFile(), xsdPath, t.getMessage() ); } } Version20ModelBuilder(StatusCache cache, ControllerConfiguration config,
                               SensorBuilder<Version20ModelBuilder> sensorBuilder,
                               DeviceProtocolBuilder deviceProtocolBuilder,
                               CommandFactory commandFactory); static boolean checkControllerDefinitionExists(ControllerConfiguration config); static File getControllerDefinitionFile(ControllerConfiguration config); Map<String, String> getConfigurationProperties(); StatusCache getDeviceStateCache(); void setCommandListeners(List<DeployerCommandListener> listeners); void setSensorListeners(List<DeployerSensorListener> listeners); @Override boolean hasControllerDefinitionChanged(); final static String CONTROLLER_XML; }
@Test public void testReadControllerXMLDocument() throws Exception { String resourcePath = AllTests.getAbsoluteFixturePath() .resolve("deployment/version20/example1/").toString(); ControllerConfiguration config = new ControllerConfiguration(); config.setResourcePath(resourcePath); CommandFactory commandFactory = DeployerTest.createCommandFactory(); Version20ModelBuilder builder = new Version20ModelBuilder( new StatusCache(), config, new Version20SensorBuilder(), new Version20CommandBuilder(commandFactory), commandFactory ); Document doc = builder.readControllerXMLDocument(); Assert.assertTrue(doc != null); Assert.assertTrue(doc.getRootElement().getName().equals("openremote")); Assert.assertTrue(doc.getRootElement().getNamespaceURI().equals("http: Assert.assertTrue(doc.getRootElement().getChild("components", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("sensors", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("commands", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("config", doc.getRootElement().getNamespace()) != null); }
@Test public void testReadControllerXMLDocumentWhichDoesntExist() throws Exception { String resourcePath = AllTests.getAbsoluteFixturePath() .resolve("deployment/version20/doesnotexist/").toString(); ControllerConfiguration config = new ControllerConfiguration(); config.setResourcePath(resourcePath); CommandFactory commandFactory = DeployerTest.createCommandFactory(); Version20ModelBuilder builder = new Version20ModelBuilder( new StatusCache(), config, new Version20SensorBuilder(), new Version20CommandBuilder(commandFactory), commandFactory ); try { Document doc = builder.readControllerXMLDocument(); Assert.fail("Was expecting ControllerDefinitionNotFoundException..."); } catch (ControllerDefinitionNotFoundException e) { } }
@Test public void testReadControllerXMLDocumentFromPathWithSpace() throws Exception { String resourcePath = AllTests.getAbsoluteFixturePath() .resolve("deployment/version20/white%20space/").toString(); ControllerConfiguration config = new ControllerConfiguration(); config.setResourcePath(resourcePath); CommandFactory commandFactory = DeployerTest.createCommandFactory(); Version20ModelBuilder builder = new Version20ModelBuilder( new StatusCache(), config, new Version20SensorBuilder(), new Version20CommandBuilder(commandFactory), commandFactory ); Document doc = builder.readControllerXMLDocument(); Assert.assertTrue(doc != null); Assert.assertTrue(doc.getRootElement().getName().equals("openremote")); Assert.assertTrue(doc.getRootElement().getNamespaceURI().equals("http: Assert.assertTrue(doc.getRootElement().getChild("components", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("sensors", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("commands", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("config", doc.getRootElement().getNamespace()) != null); }
@Test public void testReadControllerXMLDocumentFromPathWithSpaceNonURI() throws Exception { File resourcePath = new File(AllTests.getAbsoluteFixturePath()); resourcePath = new File(resourcePath, "deployment/version20/white space/"); ControllerConfiguration config = new ControllerConfiguration(); config.setResourcePath(resourcePath.toString()); CommandFactory commandFactory = DeployerTest.createCommandFactory(); Version20ModelBuilder builder = new Version20ModelBuilder( new StatusCache(), config, new Version20SensorBuilder(), new Version20CommandBuilder(commandFactory), commandFactory ); Document doc = builder.readControllerXMLDocument(); Assert.assertTrue(doc != null); Assert.assertTrue(doc.getRootElement().getName().equals("openremote")); Assert.assertTrue(doc.getRootElement().getNamespaceURI().equals("http: Assert.assertTrue(doc.getRootElement().getChild("components", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("sensors", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("commands", doc.getRootElement().getNamespace()) != null); Assert.assertTrue(doc.getRootElement().getChild("config", doc.getRootElement().getNamespace()) != null); }
@Test public void testReadControllerXMLDocumentWithInvalidSchema() throws Exception { File resourcePath = new File(AllTests.getAbsoluteFixturePath()); resourcePath = new File(resourcePath, "deployment/version20/invalidschema/"); ControllerConfiguration config = new ControllerConfiguration(); config.setResourcePath(resourcePath.toString()); CommandFactory commandFactory = DeployerTest.createCommandFactory(); Version20ModelBuilder builder = new Version20ModelBuilder( new StatusCache(), config, new Version20SensorBuilder(), new Version20CommandBuilder(commandFactory), commandFactory ); try { Document doc = builder.readControllerXMLDocument(); Assert.fail("was expecting XMLParsingException..."); } catch (XMLParsingException e) { } }