CommandsExecutor { public void execute() throws CommandException { try { CommandsParser parser = new CommandsParser( this.app, this.commandsFile ); if( RoboconfErrorHelpers.containsCriticalErrors( parser.getParsingErrors())) throw new CommandException( "Invalid command file. " + this.commandsFile.getName() + " contains errors." ); for( AbstractCommandInstruction instr : parser.getInstructions()) { if( instr.isDisabled()) { this.logger.fine( "Skipping disabled instruction: " + instr.getClass().getSimpleName()); this.instructionSkipped = true; continue; } AbstractCommandExecution executor = findExecutor( instr ); if( executor == null ) { this.logger.fine( "Skipping non-executable instruction: " + instr.getClass().getSimpleName()); continue; } executor.setExecutionContext( this.executionContext ); executor.execute(); } } catch( CommandException e ) { throw e; } catch( Exception e ) { throw new CommandException( e ); } } CommandsExecutor( Manager manager, Application app, File commandsFile );  CommandsExecutor(
			Manager manager,
			Application app,
			File commandsFile,
			CommandExecutionContext executionContext ); void execute(); boolean wereInstructionSkipped();  }
@Test public void testAllCommands() throws Exception { File f = TestUtils.findTestFile( "/commands/create-and-delete.commands" ); CommandsExecutor executor = new CommandsExecutor( this.manager, this.app, f ); String targetId = this.manager.targetsMngr().createTarget( "id: tid\nhandler: h" ); Assert.assertEquals( "tid", targetId ); List<String> instancePaths = new ArrayList<> (); for( Instance inst : InstanceHelpers.getAllInstances( this.app )) instancePaths.add( InstanceHelpers.computeInstancePath( inst )); int instancesCount = InstanceHelpers.getAllInstances( this.app ).size(); Assert.assertTrue( instancePaths.contains( "/tomcat-vm" )); executor.execute(); instancePaths.clear();; for( Instance inst : InstanceHelpers.getAllInstances( this.app )) instancePaths.add( InstanceHelpers.computeInstancePath( inst )); Assert.assertEquals( instancesCount + 5, instancePaths.size()); Assert.assertTrue( instancePaths.contains( "/tomcat 1" )); Assert.assertTrue( instancePaths.contains( "/tomcat 1/tomcat-server" )); Assert.assertTrue( instancePaths.contains( "/tomcat 1/tomcat-server/hello-world" )); Assert.assertTrue( instancePaths.contains( "/tomcat 2" )); Assert.assertTrue( instancePaths.contains( "/tomcat 2/tomcat-server" )); Assert.assertTrue( instancePaths.contains( "/tomcat 2/tomcat-server/hello-world" )); Assert.assertTrue( instancePaths.contains( "/tomcat 3" )); Assert.assertTrue( instancePaths.contains( "/tomcat 3/my-tomcat-server" )); Assert.assertFalse( instancePaths.contains( "/tomcat 3/tomcat-server/hello-world" )); Assert.assertFalse( instancePaths.contains( "/tomcat 3/my-tomcat-server/hello-world" )); Assert.assertFalse( instancePaths.contains( "/tomcat-vm" )); Instance instance = InstanceHelpers.findInstanceByPath( this.app, "/tomcat 1" ); Assert.assertNotNull( instance ); Assert.assertNull( this.manager.targetsMngr().findTargetId( this.app, "/tomcat 1" )); instance = InstanceHelpers.findInstanceByPath( this.app, "/tomcat 2" ); Assert.assertNotNull( instance ); Assert.assertNull( this.manager.targetsMngr().findTargetId( this.app, "/tomcat 2" )); instance = InstanceHelpers.findInstanceByPath( this.app, "/tomcat 3" ); Assert.assertNotNull( instance ); Assert.assertEquals( targetId, this.manager.targetsMngr().findTargetId( this.app, "/tomcat 3" )); }
@Test( expected = CommandException.class ) public void testExecutionFailure1() throws Exception { CommandsExecutor executor = new CommandsExecutor( this.manager, this.app, new File( "inexisting" )); executor.execute(); }
@Test( expected = CommandException.class ) public void testExecutionFailure2() throws Exception { CommandsExecutor executor = new CommandsExecutor( this.manager, null, this.folder.newFile()); executor.execute(); }
@Test public void testExecution_append_ok() throws Exception { File commandFile = this.folder.newFile(); File targetFile = this.folder.newFile(); Utils.deleteFilesRecursively( targetFile ); Utils.writeStringInto( "define FIRST_VM = vm $(EXISTING_INDEX MIN)\n", commandFile ); Utils.appendStringInto( "append $(FIRST_VM) into " + targetFile.getAbsolutePath(), commandFile ); CommandsExecutor executor = new CommandsExecutor( this.manager, this.app, commandFile ); Assert.assertFalse( targetFile.exists()); executor.execute(); Assert.assertTrue( targetFile.exists()); Assert.assertEquals( "vm 1", Utils.readFileContent( targetFile )); }
@Test public void testExecution_append_instructionWasDisabled() throws Exception { File commandFile = this.folder.newFile(); File targetFile = this.folder.newFile(); Utils.deleteFilesRecursively( targetFile ); Utils.writeStringInto( "define FIRST_VM = vm $(EXISTING_INDEX MIN > 4)\n", commandFile ); Utils.appendStringInto( "append $(FIRST_VM) into " + targetFile.getAbsolutePath(), commandFile ); CommandsExecutor executor = new CommandsExecutor( this.manager, this.app, commandFile ); Assert.assertFalse( targetFile.exists()); executor.execute(); Assert.assertFalse( targetFile.exists()); }