Skip to content

Commit

Permalink
changes to make unit tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvanlaatum committed Sep 24, 2016
1 parent e35c93a commit fe7654a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public static String transformText(String origText, ExtendedEmailPublisherContex
List<TokenMacro> macros = new ArrayList<>(getPrivateMacros());
if(additionalMacros != null)
macros.addAll(additionalMacros);
if(context.getBuild() != null) {
newText = TokenMacro.expandAll(context.getBuild(), context.getListener(), newText, false, macros);
if(context.getRun() != null) {
newText = TokenMacro.expandAll(context.getRun(), context.getWorkspace(), context.getListener(), newText, false, macros);
} else {
context.getListener().getLogger().println("Job type does not allow token replacement.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import hudson.FilePath;
import hudson.Plugin;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.ExtendedEmailPublisher;

Expand Down Expand Up @@ -60,7 +61,13 @@ public AbstractEvalContent(String macroName) {
}

@Override
public abstract String evaluate(AbstractBuild<?, ?> ab, TaskListener tl, String string) throws MacroEvaluationException, IOException, InterruptedException;
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
return evaluate(build, build.getWorkspace(), listener, macroName);
}

@Override
public abstract String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException;

@Override
public boolean acceptsMacroName(String macroName) {
Expand Down Expand Up @@ -156,7 +163,7 @@ protected String generateMissingFile(String type, String fileName) {
return type + " file [" + fileName + "] was not found in $JENKINS_HOME/" + EMAIL_TEMPLATES_DIRECTORY + ".";
}

protected String getCharset(AbstractBuild<?, ?> build) {
protected String getCharset(Run<?, ?> build) {
return ExtendedEmailPublisher.descriptor().getCharset();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.plugins.emailext.plugins.content;

import hudson.model.AbstractBuild;
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.JellyTemplateConfig.JellyTemplateConfigProvider;
Expand Down Expand Up @@ -31,15 +32,14 @@ public class JellyScriptContent extends AbstractEvalContent {
public JellyScriptContent() {
super(MACRO_NAME);
}

@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
InputStream inputStream = null;

try {
inputStream = getFileInputStream(build.getWorkspace(), template, JELLY_EXTENSION);
return renderContent(build, inputStream, listener);
inputStream = getFileInputStream(workspace, template, JELLY_EXTENSION);
return renderContent(run, inputStream, listener);
} catch (JellyException e) {
return "JellyException: " + e.getMessage();
} catch (FileNotFoundException e) {
Expand All @@ -54,7 +54,7 @@ protected Class<? extends ConfigProvider> getProviderClass() {
return JellyTemplateConfigProvider.class;
}

private String renderContent(AbstractBuild<?, ?> build, InputStream inputStream, TaskListener listener)
private String renderContent(Run<?, ?> build, InputStream inputStream, TaskListener listener)
throws JellyException, IOException {
JellyContext context = createContext(new ScriptContentBuildWrapper(build), build, listener);
Script script = context.compileScript(new InputSource(inputStream));
Expand All @@ -64,7 +64,7 @@ private String renderContent(AbstractBuild<?, ?> build, InputStream inputStream,
return null;
}

private String convert(AbstractBuild<?, ?> build, JellyContext context, Script script)
private String convert(Run<?, ?> build, JellyContext context, Script script)
throws JellyTagException, IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream(16 * 1024);
XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
Expand All @@ -75,7 +75,7 @@ private String convert(AbstractBuild<?, ?> build, JellyContext context, Script s
return output.toString(getCharset(build));
}

private JellyContext createContext(Object it, AbstractBuild<?, ?> build, TaskListener listener) {
private JellyContext createContext(Object it, Run<?, ?> build, TaskListener listener) {
JellyContext context = new JellyContext();
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getActiveInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);
context.setVariable("it", it);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import hudson.model.AbstractBuild;
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.GroovyTemplateConfig.GroovyTemplateConfigProvider;
Expand Down Expand Up @@ -49,19 +51,17 @@ public ScriptContent() {
}

@Override
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {

public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
InputStream inputStream = null;
String result = "";

try {
if (!StringUtils.isEmpty(script)) {
inputStream = getFileInputStream(context.getWorkspace(), script, ".groovy");
result = executeScript(context, listener, inputStream);
inputStream = getFileInputStream(workspace, script, ".groovy");
result = executeScript(run, listener, inputStream);
} else {
inputStream = getFileInputStream(context.getWorkspace(), template, ".template");
result = renderTemplate(context, listener, inputStream);
inputStream = getFileInputStream(workspace, template, ".template");
result = renderTemplate(run, listener, inputStream);
}
} catch (FileNotFoundException e) {
String missingScriptError = "";
Expand Down Expand Up @@ -93,7 +93,7 @@ protected Class<? extends ConfigProvider> getProviderClass () {
* @return the rendered template content
* @throws IOException
*/
private String renderTemplate(AbstractBuild<?, ?> build, TaskListener listener, InputStream templateStream)
private String renderTemplate(Run<?, ?> build, TaskListener listener, InputStream templateStream)
throws IOException {

String result;
Expand Down Expand Up @@ -138,7 +138,7 @@ private String renderTemplate(AbstractBuild<?, ?> build, TaskListener listener,
* @return a String containing the toString of the last item in the script
* @throws IOException
*/
private String executeScript(AbstractBuild<?, ?> build, TaskListener listener, InputStream scriptStream)
private String executeScript(Run<?, ?> build, TaskListener listener, InputStream scriptStream)
throws IOException {
String result = "";
Map binding = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import hudson.Functions;
import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.tasks.junit.TestResult;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.test.AggregatedTestResultAction;
Expand All @@ -14,9 +14,9 @@

public class ScriptContentBuildWrapper {

private AbstractBuild<?, ?> build;
private Run<?, ?> build;

public ScriptContentBuildWrapper(AbstractBuild<?, ?> build) {
public ScriptContentBuildWrapper(Run<?, ?> build) {
this.build = build;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package hudson.plugins.emailext.plugins.content;

import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.MavenResultAction;
import hudson.plugins.analysis.core.AbstractResultAction;
import hudson.plugins.analysis.core.ResultAction;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,9 +26,9 @@ public class StaticAnalysisUtilities {
* @return The static analysis actions for the specified build. The returned
* list might be empty if there are no such actions.
*/
public List<Action> getActions(AbstractBuild<?, ?> build) {
public List<Action> getActions(Run<?, ?> build) {
ArrayList<Action> actions = new ArrayList<>();
for (Action action : build.getActions()) {
for (Action action : build.getActions(ResultAction.class)) {
if (AbstractResultAction.class.isInstance(action) || MavenResultAction.class.isInstance(action)) {
actions.add(action);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hudson.plugins.emailext.plugins.content;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailToken;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -32,15 +34,20 @@ public TemplateContent() {
@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
return evaluate(build, build.getWorkspace(), listener, macroName);
}

@Override
public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
InputStream inputStream = null;
String result = "";

try {
if (!StringUtils.isEmpty(file)) {
result = IOUtils.toString(getFileInputStream(workspace, file, ".txt"));
inputStream = getFileInputStream(build.getWorkspace(), file, ".txt");
result = IOUtils.toString(inputStream);
}
}
} catch (FileNotFoundException e) {
String missingFileError = generateMissingFile("Plain Text", file);
LOGGER.log(Level.SEVERE, missingFileError, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -158,7 +159,9 @@ public void testEscapedToken() throws IOException, InterruptedException {
@Test
public void testRuntimeMacro() throws IOException, InterruptedException {
RuntimeContent content = new RuntimeContent("Hello, world");
assertEquals("Hello, world", ContentBuilder.transformText("${RUNTIME}", new ExtendedEmailPublisherContext(publisher, build, j.createLocalLauncher(), listener), Collections.singletonList((TokenMacro)content)));
assertEquals("Hello, world", ContentBuilder.transformText("${RUNTIME}",
new ExtendedEmailPublisherContext(publisher, build, build.getWorkspace(), j.createLocalLauncher(), listener),
Collections.singletonList((TokenMacro) content)));
}

public class RuntimeContent extends TokenMacro {
Expand All @@ -175,6 +178,11 @@ public boolean acceptsMacroName(String name) {
return name.equals(MACRO_NAME);
}

@Override
public List<String> getAcceptedMacroNames() {
return Collections.singletonList(MACRO_NAME);
}

@Override
public String evaluate(AbstractBuild<?, ?> ab, TaskListener tl, String string, Map<String, String> map, ListMultimap<String, String> lm) throws MacroEvaluationException, IOException, InterruptedException {
return replacement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,6 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
private void mockChangeSet(final AbstractBuild build) {
ScriptContentChangeLogSet changeLog = new ScriptContentChangeLogSet(build);
Mockito.when(build.getChangeSet()).thenReturn(changeLog);
Mockito.when(build.getChangeSets()).thenReturn(Collections.singletonList(changeLog));
}
}

0 comments on commit fe7654a

Please sign in to comment.