@Test public void testLoadApplicationErrors() throws Exception { File tempDirectory = this.folder.newFolder(); Iterator<RoboconfError> iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_DESC_DIR, iterator.next().getErrorCode()); File appDir = new File( tempDirectory, Constants.PROJECT_DIR_DESC ); if( ! appDir.mkdir()) throw new IOException( "Failed to create the descriptor directory." ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_DESC_FILE, iterator.next().getErrorCode()); Properties props = new Properties(); props.setProperty( ApplicationTemplateDescriptor.APPLICATION_NAME, "app-name" ); props.setProperty( ApplicationTemplateDescriptor.APPLICATION_VERSION, "1.0" ); props.setProperty( ApplicationTemplateDescriptor.APPLICATION_DSL_ID, "roboconf-1.0" ); props.setProperty( ApplicationTemplateDescriptor.APPLICATION_GRAPH_EP, "main.graph" ); Utils.writePropertiesFile( props, new File( appDir, Constants.PROJECT_FILE_DESCRIPTOR )); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_GRAPH_DIR, iterator.next().getErrorCode()); File graphDir = new File( tempDirectory, Constants.PROJECT_DIR_GRAPH ); if( ! graphDir.mkdir()) throw new IOException( "Failed to create the graph directory." ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_MISSING_GRAPH_EP, iterator.next().getErrorCode()); File graphFile = new File( graphDir, "main.graph" ); if( ! graphFile.createNewFile()) throw new IOException( "Faild to create a graph file." ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.P_EMPTY_FILE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.RM_NO_ROOT_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( " \n \t\n\n ", graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.P_EMPTY_FILE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.RM_NO_ROOT_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( "[ not supported by our parser ]", graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.P_UNRECOGNIZED_BLOCK, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PM_INVALID_BLOCK_TYPE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_A_GRAPH, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( " \n\n [ not supported by our parser ] \n\n ", graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.P_UNRECOGNIZED_BLOCK, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PM_INVALID_BLOCK_TYPE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_A_GRAPH, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( "comp", graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.P_O_C_BRACKET_MISSING, iterator.next().getErrorCode()); String fileContent = "my Toto {\n\tname: toto;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PM_INVALID_NAME, iterator.next().getErrorCode()); fileContent = "instanceof Toto {\n\tname: toto;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PM_INVALID_NAME, iterator.next().getErrorCode()); fileContent = "instance of Toto {\n\tname: toto;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), graphFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.CO_NOT_A_GRAPH, iterator.next().getErrorCode()); fileContent = "facet MyFacet {\n}\n\nA {\n\tinstaller: target;\n\tfacets: MyFacet;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), graphFile ); Collection<RoboconfError> errors = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors; Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, errors.iterator().next().getErrorCode()); props.setProperty( ApplicationTemplateDescriptor.APPLICATION_INSTANCES_EP, "init.instances" ); Utils.writePropertiesFile( props, new File( appDir, Constants.PROJECT_FILE_DESCRIPTOR )); File instDir = new File( tempDirectory, Constants.PROJECT_DIR_INSTANCES ); if( ! instDir.mkdir()) throw new IOException( "Failed to create the instances directory." ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PROJ_MISSING_INSTANCE_EP, iterator.next().getErrorCode()); File instancesFile = new File( instDir, "init.instances" ); if( ! instancesFile.createNewFile()) throw new IOException( "Failed to create " + instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.P_EMPTY_FILE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( " \n \t\n\n ", instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.P_EMPTY_FILE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( "[ not supported by our parser ]", instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.P_UNRECOGNIZED_BLOCK, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PM_INVALID_BLOCK_TYPE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_INSTANCES, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( " \n\n [ not supported by our parser ] \n\n ", instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.P_UNRECOGNIZED_BLOCK, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PM_INVALID_BLOCK_TYPE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_INSTANCES, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); Utils.writeStringInto( " \n\n ip \n\n ", instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.P_O_C_BRACKET_MISSING, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.PM_INVALID_BLOCK_TYPE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_INSTANCES, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); fileContent = "facet MyFacet {\n}\n\nA {\n\tinstaller: script;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), instancesFile ); iterator = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors.iterator(); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_NOT_INSTANCES, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); fileContent = "instance of A {\n\tname: toto;\n}"; Utils.copyStream( new ByteArrayInputStream( fileContent.getBytes( "UTF-8" )), instancesFile ); errors = RuntimeModelIo.loadApplication( tempDirectory ).loadErrors; Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.PROJ_NO_RESOURCE_DIRECTORY, errors.iterator().next().getErrorCode()); }
public static ApplicationLoadResult loadApplication( File projectDirectory ) { ApplicationLoadResult result = new ApplicationLoadResult(); ApplicationTemplate app = new ApplicationTemplate(); result.applicationTemplate = app; ApplicationTemplateDescriptor appDescriptor = null; File descDirectory = new File( projectDirectory, Constants.PROJECT_DIR_DESC ); DESC: if( ! descDirectory.exists()) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_DESC_DIR, directory( projectDirectory )); result.loadErrors.add( error ); } else { File descriptorFile = new File( descDirectory, Constants.PROJECT_FILE_DESCRIPTOR ); if( ! descriptorFile.exists()) { result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_NO_DESC_FILE )); break DESC; } try { appDescriptor = ApplicationTemplateDescriptor.load( descriptorFile ); app.setName( appDescriptor.getName()); app.setDescription( appDescriptor.getDescription()); app.setVersion( appDescriptor.getVersion()); app.setDslId( appDescriptor.getDslId()); app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix()); app.setTags( appDescriptor.tags ); for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet()) app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue()); result.objectToSource.put( appDescriptor, new SourceReference( appDescriptor, descriptorFile, 1 )); for( Map.Entry<String,Integer> entry : appDescriptor.propertyToLine.entrySet()) { result.objectToSource.put( entry.getKey(), new SourceReference( entry.getKey(), descriptorFile, entry.getValue())); } Collection<ModelError> errors = RuntimeModelValidator.validate( appDescriptor ); result.loadErrors.addAll( errors ); } catch( IOException e ) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_READ_DESC_FILE, exception( e )); result.loadErrors.add( error ); } } return loadApplication( projectDirectory, appDescriptor, result ); }
RuntimeModelIo { public static ApplicationLoadResult loadApplication( File projectDirectory ) { ApplicationLoadResult result = new ApplicationLoadResult(); ApplicationTemplate app = new ApplicationTemplate(); result.applicationTemplate = app; ApplicationTemplateDescriptor appDescriptor = null; File descDirectory = new File( projectDirectory, Constants.PROJECT_DIR_DESC ); DESC: if( ! descDirectory.exists()) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_DESC_DIR, directory( projectDirectory )); result.loadErrors.add( error ); } else { File descriptorFile = new File( descDirectory, Constants.PROJECT_FILE_DESCRIPTOR ); if( ! descriptorFile.exists()) { result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_NO_DESC_FILE )); break DESC; } try { appDescriptor = ApplicationTemplateDescriptor.load( descriptorFile ); app.setName( appDescriptor.getName()); app.setDescription( appDescriptor.getDescription()); app.setVersion( appDescriptor.getVersion()); app.setDslId( appDescriptor.getDslId()); app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix()); app.setTags( appDescriptor.tags ); for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet()) app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue()); result.objectToSource.put( appDescriptor, new SourceReference( appDescriptor, descriptorFile, 1 )); for( Map.Entry<String,Integer> entry : appDescriptor.propertyToLine.entrySet()) { result.objectToSource.put( entry.getKey(), new SourceReference( entry.getKey(), descriptorFile, entry.getValue())); } Collection<ModelError> errors = RuntimeModelValidator.validate( appDescriptor ); result.loadErrors.addAll( errors ); } catch( IOException e ) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_READ_DESC_FILE, exception( e )); result.loadErrors.add( error ); } } return loadApplication( projectDirectory, appDescriptor, result ); } }
RuntimeModelIo { public static ApplicationLoadResult loadApplication( File projectDirectory ) { ApplicationLoadResult result = new ApplicationLoadResult(); ApplicationTemplate app = new ApplicationTemplate(); result.applicationTemplate = app; ApplicationTemplateDescriptor appDescriptor = null; File descDirectory = new File( projectDirectory, Constants.PROJECT_DIR_DESC ); DESC: if( ! descDirectory.exists()) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_DESC_DIR, directory( projectDirectory )); result.loadErrors.add( error ); } else { File descriptorFile = new File( descDirectory, Constants.PROJECT_FILE_DESCRIPTOR ); if( ! descriptorFile.exists()) { result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_NO_DESC_FILE )); break DESC; } try { appDescriptor = ApplicationTemplateDescriptor.load( descriptorFile ); app.setName( appDescriptor.getName()); app.setDescription( appDescriptor.getDescription()); app.setVersion( appDescriptor.getVersion()); app.setDslId( appDescriptor.getDslId()); app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix()); app.setTags( appDescriptor.tags ); for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet()) app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue()); result.objectToSource.put( appDescriptor, new SourceReference( appDescriptor, descriptorFile, 1 )); for( Map.Entry<String,Integer> entry : appDescriptor.propertyToLine.entrySet()) { result.objectToSource.put( entry.getKey(), new SourceReference( entry.getKey(), descriptorFile, entry.getValue())); } Collection<ModelError> errors = RuntimeModelValidator.validate( appDescriptor ); result.loadErrors.addAll( errors ); } catch( IOException e ) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_READ_DESC_FILE, exception( e )); result.loadErrors.add( error ); } } return loadApplication( projectDirectory, appDescriptor, result ); } private  RuntimeModelIo(); }
RuntimeModelIo { public static ApplicationLoadResult loadApplication( File projectDirectory ) { ApplicationLoadResult result = new ApplicationLoadResult(); ApplicationTemplate app = new ApplicationTemplate(); result.applicationTemplate = app; ApplicationTemplateDescriptor appDescriptor = null; File descDirectory = new File( projectDirectory, Constants.PROJECT_DIR_DESC ); DESC: if( ! descDirectory.exists()) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_DESC_DIR, directory( projectDirectory )); result.loadErrors.add( error ); } else { File descriptorFile = new File( descDirectory, Constants.PROJECT_FILE_DESCRIPTOR ); if( ! descriptorFile.exists()) { result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_NO_DESC_FILE )); break DESC; } try { appDescriptor = ApplicationTemplateDescriptor.load( descriptorFile ); app.setName( appDescriptor.getName()); app.setDescription( appDescriptor.getDescription()); app.setVersion( appDescriptor.getVersion()); app.setDslId( appDescriptor.getDslId()); app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix()); app.setTags( appDescriptor.tags ); for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet()) app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue()); result.objectToSource.put( appDescriptor, new SourceReference( appDescriptor, descriptorFile, 1 )); for( Map.Entry<String,Integer> entry : appDescriptor.propertyToLine.entrySet()) { result.objectToSource.put( entry.getKey(), new SourceReference( entry.getKey(), descriptorFile, entry.getValue())); } Collection<ModelError> errors = RuntimeModelValidator.validate( appDescriptor ); result.loadErrors.addAll( errors ); } catch( IOException e ) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_READ_DESC_FILE, exception( e )); result.loadErrors.add( error ); } } return loadApplication( projectDirectory, appDescriptor, result ); } private  RuntimeModelIo(); static ApplicationLoadResult loadApplication( File projectDirectory ); static ApplicationLoadResult loadApplicationFlexibly( File projectDirectory ); static Graphs loadGraph( File graphFile, File graphDirectory, ApplicationLoadResult alr ); static InstancesLoadResult loadInstances( File instancesFile, File rootDirectory, Graphs graph, String applicationName ); static void writeInstances( File targetFile, Collection<Instance> rootInstances ); }
RuntimeModelIo { public static ApplicationLoadResult loadApplication( File projectDirectory ) { ApplicationLoadResult result = new ApplicationLoadResult(); ApplicationTemplate app = new ApplicationTemplate(); result.applicationTemplate = app; ApplicationTemplateDescriptor appDescriptor = null; File descDirectory = new File( projectDirectory, Constants.PROJECT_DIR_DESC ); DESC: if( ! descDirectory.exists()) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_DESC_DIR, directory( projectDirectory )); result.loadErrors.add( error ); } else { File descriptorFile = new File( descDirectory, Constants.PROJECT_FILE_DESCRIPTOR ); if( ! descriptorFile.exists()) { result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_NO_DESC_FILE )); break DESC; } try { appDescriptor = ApplicationTemplateDescriptor.load( descriptorFile ); app.setName( appDescriptor.getName()); app.setDescription( appDescriptor.getDescription()); app.setVersion( appDescriptor.getVersion()); app.setDslId( appDescriptor.getDslId()); app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix()); app.setTags( appDescriptor.tags ); for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet()) app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue()); result.objectToSource.put( appDescriptor, new SourceReference( appDescriptor, descriptorFile, 1 )); for( Map.Entry<String,Integer> entry : appDescriptor.propertyToLine.entrySet()) { result.objectToSource.put( entry.getKey(), new SourceReference( entry.getKey(), descriptorFile, entry.getValue())); } Collection<ModelError> errors = RuntimeModelValidator.validate( appDescriptor ); result.loadErrors.addAll( errors ); } catch( IOException e ) { RoboconfError error = new RoboconfError( ErrorCode.PROJ_READ_DESC_FILE, exception( e )); result.loadErrors.add( error ); } } return loadApplication( projectDirectory, appDescriptor, result ); } private  RuntimeModelIo(); static ApplicationLoadResult loadApplication( File projectDirectory ); static ApplicationLoadResult loadApplicationFlexibly( File projectDirectory ); static Graphs loadGraph( File graphFile, File graphDirectory, ApplicationLoadResult alr ); static InstancesLoadResult loadInstances( File instancesFile, File rootDirectory, Graphs graph, String applicationName ); static void writeInstances( File targetFile, Collection<Instance> rootInstances );  }