GroovySnippetExecutor extends AbstractSnippetExecutor<GroovySnippetExecutorOptions> { protected String buildSourceCode(final CodeSnippet codeSnippet) throws IOException { final StringBuilder sourceCode = new StringBuilder(); boolean someImportsPresent = false; if (makeScript(codeSnippet)) { sourceCode.append(getScriptImport()).append("\n"); someImportsPresent = true; } if (hasImports(codeSnippet)) { sourceCode.append(getImports(codeSnippet)).append("\n"); someImportsPresent = true; } if (someImportsPresent) sourceCode.append("\n"); sourceCode.append(getStartClassDefinition(codeSnippet)).append("\n"); if (mustBeWrappedIn(codeSnippet, WRAP_IN_METHOD_RUNNER)) { sourceCode.append("\t").append(getStartMainMethod(codeSnippet)).append("\n") .append(codeSnippet.getCode()) .append("\n\t}"); } else { sourceCode.append(codeSnippet.getCode()); } sourceCode.append("\n}"); return sourceCode.toString(); } GroovySnippetExecutor(); @Override Parent getUI(final CodeSnippet codeSnippet); @Override Node getConfigurationUI(); @Override void saveNewOptions(); @Override ObservableList<String> execute(final CodeSnippet codeSnippet);  }
@Test public void buildSourceCodeWithoutImportsAndWithoutWrapInMethodRunnerAndWithoutClassName() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.setCode("def static main(String ... args) {\n\tprintln(\"Hello\")\n}"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCodeWithoutImportsAndWithoutWrapInMethodRunnerAndWithoutClassName_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }
@Test public void buildSourceCodeWithoutImportsAndWithoutWrapInMethodRunner() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.getProperties().put(CLASS_NAME_PROPERTY, "TestGroovy"); snippet.setCode("def static main(String ... args) {\n\tprintln(\"Hello\")\n}"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCodeWithoutImportsAndWithoutWrapInMethodRunner_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }
@Test public void buildSourceCodeWithoutWrapInMethodRunner() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.getProperties().put(CLASS_NAME_PROPERTY, "TestGroovy"); snippet.getProperties().put(IMPORTS_PROPERTY, "import mypackage\nmysecondpackage"); snippet.setCode("def static main(String ... args) {\n\tprintln(\"Hello\")\n}"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCodeWithoutWrapInMethodRunner_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }
@Test public void buildSourceCodeWithoutWrapInMethodRunnerButMakeScript() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.getProperties().put(CLASS_NAME_PROPERTY, "TestGroovy"); snippet.getProperties().put(IMPORTS_PROPERTY, "import mypackage\nmysecondpackage"); snippet.getProperties().put(MAKE_SCRIPT, "true"); snippet.setCode("def run() {\n\tprintln(\"Hello\")\n}"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCodeWithoutWrapInMethodRunnerButMakeScript_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }
@Test public void buildSourceCode() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.getProperties().put(CLASS_NAME_PROPERTY, "TestGroovy"); snippet.getProperties().put(IMPORTS_PROPERTY, "import mypackage\nmysecondpackage"); snippet.getProperties().put(WRAP_IN_METHOD_RUNNER, "true"); snippet.setCode("println(\"Hello\")"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCode_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }
@Test public void buildSourceCodeAsScript() throws IOException { final CodeSnippet snippet = new CodeSnippet(); snippet.getProperties().put(CLASS_NAME_PROPERTY, "TestGroovy"); snippet.getProperties().put(IMPORTS_PROPERTY, "import mypackage\nmysecondpackage"); snippet.getProperties().put(WRAP_IN_METHOD_RUNNER, "true"); snippet.getProperties().put(MAKE_SCRIPT, "true"); snippet.setCode("println(\"Hello\")"); final String expected = IOUtils.read(GroovySnippetExecutorTest.class.getResourceAsStream("/com/twasyl/slideshowfx/snippet/executor/groovy/buildSourceCodeAsScript_expected.txt")); assertEquals(expected, snippetExecutor.buildSourceCode(snippet)); }