ScriptBuilder implements Statement, AcceptsStatementVisitor { @Override public String render(OsFamily osFamily) { Map<String, String> functions = Maps.newLinkedHashMap(); functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily)); for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) { functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry .getValue(), osFamily))); } StringBuilder builder = new StringBuilder(); builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily)); builder.append(Utils.writeUnsetVariables(variablesToUnset, osFamily)); Map<String, String> functionsToWrite = resolveFunctionDependenciesForStatements(functions, statements, osFamily); writeFunctions(functionsToWrite, osFamily, builder); builder.append(Utils.writeZeroPath(osFamily)); StringBuilder statementBuilder = new StringBuilder(); for (Statement statement : statements) { statementBuilder.append(statement.render(osFamily)); } builder.append(statementBuilder.toString()); builder.append(ShellToken.END_SCRIPT.to(osFamily)); return builder.toString(); }  ScriptBuilder addStatement(Statement statement); ScriptBuilder unsetEnvironmentVariable(String name); ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables); static Statement forget(String instanceName, String script, String logDir); static Statement findPid(String pid); static Statement call(String fn, String... args); @Override String render(OsFamily osFamily); static void writeFunctions(Map<String, String> functionsToWrite, OsFamily osFamily, StringBuilder builder); @VisibleForTesting static Map<String, String> resolveFunctionDependenciesForStatements(Map<String, String> knownFunctions,
         Iterable<Statement> statements, final OsFamily osFamily); @Override Iterable<String> functionDependencies(OsFamily family); @Override void accept(StatementVisitor visitor);  }
@Test public void testBuildSimpleWindows() throws MalformedURLException, IOException { assertEquals( testScriptBuilder.render(OsFamily.WINDOWS), Resources.toString(Resources.getResource("test_script." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)); }
@Test public void testBuildSimpleUNIX() throws MalformedURLException, IOException { assertEquals( testScriptBuilder.render(OsFamily.UNIX), Resources.toString(Resources.getResource("test_script." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)); }
@Test public void testFindPidWindows() throws MalformedURLException, IOException { assertEquals( findPidBuilder.render(OsFamily.WINDOWS), Resources.toString(Resources.getResource("test_find_pid." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)); }
@Test public void testFindPidUNIX() throws MalformedURLException, IOException { assertEquals( findPidBuilder.render(OsFamily.UNIX), Resources.toString(Resources.getResource("test_find_pid." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)); }
@Test public void testSeekAndDestroyWindows() throws MalformedURLException, IOException { assertEquals(seekAndDestroyBuilder.render(OsFamily.WINDOWS), Resources.toString( Resources.getResource("test_seek_and_destroy." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)); }
@Test public void testSeekAndDestroyUNIX() throws MalformedURLException, IOException { assertEquals( seekAndDestroyBuilder.render(OsFamily.UNIX), Resources.toString(Resources.getResource("test_seek_and_destroy." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)); }