Remove support for velocity Velocity was removed from core Gerrit in change I9942eb1a9. Change-Id: I5f4e166ff9d3fc62ffdefd7b80c083b6144c52eb
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java index 91b26a0..37b2fc6 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java
@@ -33,7 +33,6 @@ import com.googlesource.gerrit.plugins.its.base.workflow.action.AddComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddSoyComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddStandardComment; -import com.googlesource.gerrit.plugins.its.base.workflow.action.AddVelocityComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.LogEvent; public class ItsHookModule extends FactoryModule { @@ -61,7 +60,6 @@ factory(AddComment.Factory.class); factory(AddSoyComment.Factory.class); factory(AddStandardComment.Factory.class); - factory(AddVelocityComment.Factory.class); factory(LogEvent.Factory.class); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutor.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutor.java index 0e31b93..c3e0421 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutor.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutor.java
@@ -20,7 +20,6 @@ import com.googlesource.gerrit.plugins.its.base.workflow.action.AddComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddSoyComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddStandardComment; -import com.googlesource.gerrit.plugins.its.base.workflow.action.AddVelocityComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.LogEvent; import java.io.IOException; import java.util.Set; @@ -34,7 +33,6 @@ private final ItsFacade its; private final AddComment.Factory addCommentFactory; private final AddStandardComment.Factory addStandardCommentFactory; - private final AddVelocityComment.Factory addVelocityCommentFactory; private final AddSoyComment.Factory addSoyCommentFactory; private final LogEvent.Factory logEventFactory; @@ -43,13 +41,11 @@ ItsFacade its, AddComment.Factory addCommentFactory, AddStandardComment.Factory addStandardCommentFactory, - AddVelocityComment.Factory addVelocityCommentFactory, AddSoyComment.Factory addSoyCommentFactory, LogEvent.Factory logEventFactory) { this.its = its; this.addCommentFactory = addCommentFactory; this.addStandardCommentFactory = addStandardCommentFactory; - this.addVelocityCommentFactory = addVelocityCommentFactory; this.addSoyCommentFactory = addSoyCommentFactory; this.logEventFactory = logEventFactory; } @@ -62,8 +58,6 @@ action = addCommentFactory.create(); } else if ("add-standard-comment".equals(name)) { action = addStandardCommentFactory.create(); - } else if ("add-velocity-comment".equals(name)) { - action = addVelocityCommentFactory.create(); } else if ("add-soy-comment".equals(name)) { action = addSoyCommentFactory.create(); } else if ("log-event".equals(name)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityComment.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityComment.java deleted file mode 100644 index f3a1cc2..0000000 --- a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityComment.java +++ /dev/null
@@ -1,159 +0,0 @@ -// Copyright (C) 2013 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.googlesource.gerrit.plugins.its.base.workflow.action; - -import com.google.common.base.Strings; -import com.google.gerrit.server.config.SitePath; -import com.google.inject.Inject; - -import com.googlesource.gerrit.plugins.its.base.its.ItsFacade; -import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; -import com.googlesource.gerrit.plugins.its.base.workflow.Property; - -import org.apache.commons.lang.StringUtils; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.runtime.RuntimeInstance; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Set; - -/** - * Adds a short predefined comments to an issue. - * - * Comments are added for merging, abandoning, restoring of changes and adding - * of patch sets. - */ -public class AddVelocityComment implements Action { - private static final Logger log = LoggerFactory.getLogger( - AddVelocityComment.class); - - public interface Factory { - AddVelocityComment create(); - } - - /** - * Directory (relative to site) to search templates in - */ - private static final String ITS_TEMPLATE_DIR = "etc" + File.separator + - "its" + File.separator + "templates"; - - private final ItsFacade its; - private final Path sitePath; - private final RuntimeInstance velocityRuntime; - - @Inject - public AddVelocityComment(RuntimeInstance velocityRuntime, @SitePath Path sitePath, ItsFacade its) { - this.velocityRuntime = velocityRuntime; - this.sitePath = sitePath; - this.its = its; - } - - private VelocityContext getVelocityContext(Set<Property> properties) { - VelocityContext velocityContext = new VelocityContext(); - for (Property property : properties) { - String key = property.getKey(); - if (!Strings.isNullOrEmpty(key)) { - String value = property.getValue(); - if (!Strings.isNullOrEmpty(value)) { - velocityContext.put(key, value); - } - } - } - - velocityContext.put("its", new VelocityAdapterItsFacade(its)); - - return velocityContext; - } - - private String velocify(String template, Set<Property> properties) { - VelocityContext context = getVelocityContext(properties); - StringWriter w = new StringWriter(); - velocityRuntime.evaluate(context, w, "ItsComment", template); - return w.toString(); - } - - @Override - public void execute(String issue, ActionRequest actionRequest, - Set<Property> properties) throws IOException { - String template = null; - String templateName = actionRequest.getParameter(1); - if ("inline".equals(templateName)) { - String[] allParameters = actionRequest.getParameters(); - String[] templateParameters = - Arrays.copyOfRange(allParameters, 1, allParameters.length); - template = StringUtils.join(templateParameters, " "); - } else { - if (templateName.isEmpty()) { - log.error("No template name given in " + actionRequest); - } else { - Path templateDir = sitePath.resolve(ITS_TEMPLATE_DIR); - Path templatePath = templateDir.resolve(templateName + ".vm"); - if (Files.isReadable(templatePath)) { - template = new String(Files.readAllBytes(templatePath)); - } else { - log.error("Cannot read template " + templatePath); - } - } - } - if (!Strings.isNullOrEmpty(template)) { - String comment = velocify(template, properties); - its.addComment(issue, comment); - } - } - - /** - * Adapter for ItsFacade to be used through Velocity - */ - // Although we'd prefer to keep this class private, Velocity will only pick - // it up, if it is public. - public class VelocityAdapterItsFacade { - - private final ItsFacade facade; - - private VelocityAdapterItsFacade(ItsFacade facade) { - this.facade = facade; - } - - /** - * Format a link to a URL in the used Its' syntax. - * - * @param url URL to link to - * @param caption Text used to represent the link - * @return Link to the given URL in the used Its' syntax. - */ - public String formatLink(String url, String caption) { - return facade.createLinkForWebui(url, caption); - } - - /** - * Format a link to an URL. - * <p> - * The provided URL is used as caption for the formatted link. - * - * @param url URL to link to - * @return Link to the given URL in the used Its' syntax. - */ - public String formatLink(String url) { - return facade.createLinkForWebui(url, url); - } - } -}
diff --git a/src/main/resources/Documentation/config-rulebase-common.md b/src/main/resources/Documentation/config-rulebase-common.md index ca305a0..3426e4a 100644 --- a/src/main/resources/Documentation/config-rulebase-common.md +++ b/src/main/resources/Documentation/config-rulebase-common.md
@@ -523,67 +523,6 @@ (abandoner, merger, ...), the change's subject, a reason (if one has been given), and a link to the change. -[action-add-velocity-comment]: #action-add-velocity-comment -### <a name="action-add-velocity-comment">Action: add-velocity-comment</a> - -The `add-velocity-comment` action renders a Velocity template for the -event and adds the output as comment to any associated issue. - -So for example - -``` - action = add-velocity-comment TemplateName -``` - -would render the template `etc/its/templates/TemplateName.vm` add the -output as comment to associated issues. - -If 'TemplateName' is `inline`, the Velocity template to render is not -loaded from a file, but the template is built by joining the remaining -parameters. So for example - -``` - action = add-velocity-comment inline Sample template using $subject property. -``` - -would render “Sample template using $subject property.” as Velocity -template. - -If 'TemplateName' is not `inline`, further parameters get ignored. - -Any [property][event-properties] of the event may be used from -templates. So for example `$subject` in the above example refers to -the event's subject property, and `$changeNumber` would refer to the -change's number. - -Additionally, the context's `its` property provides an object that -allows to format links using the its' syntax: - -`formatLink( url )` -: Formats a link to a url. - - So for example upon adding a comment to a change, the - following rule formats a link to the change: - - ``` -[rule "formatLinkSampleRule"] - event-type = comment-added - action = add-velocity-comment inline Comment for change $change-number added. See ${its.formatLink($changeUrl)} -``` - -`formatLink( url, caption )` -: Formats a link to a url using 'caption' to represent the url. - - So for example upon adding a comment to a change, the following rule - formats a link to the change using the change number as link - capition: - - ``` -[rule "formatLinkSampleRule"] - event-type = comment-added - action = add-velocity-comment inline Comment for change ${its.formatLink($changeUrl, $changeNumber)} added. -``` - [action-add-soy-comment]: #action-add-soy-comment ### <a name="action-add-soy-comment">Action: add-soy-comment</a>
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutorTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutorTest.java index f3f5916..8cab7a4 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutorTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/ActionExecutorTest.java
@@ -26,7 +26,6 @@ import com.googlesource.gerrit.plugins.its.base.workflow.action.AddComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddSoyComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.AddStandardComment; -import com.googlesource.gerrit.plugins.its.base.workflow.action.AddVelocityComment; import com.googlesource.gerrit.plugins.its.base.workflow.action.LogEvent; import java.io.IOException; @@ -39,7 +38,6 @@ private ItsFacade its; private AddComment.Factory addCommentFactory; private AddStandardComment.Factory addStandardCommentFactory; - private AddVelocityComment.Factory addVelocityCommentFactory; private AddSoyComment.Factory addSoyCommentFactory; private LogEvent.Factory logEventFactory; @@ -181,24 +179,6 @@ actionExecutor.execute("4711", actionRequest, properties); } - public void testAddVelocityCommentDelegation() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getName()).andReturn("add-velocity-comment"); - - Set<Property> properties = Collections.emptySet(); - - AddVelocityComment addVelocityComment = - createMock(AddVelocityComment.class); - expect(addVelocityCommentFactory.create()).andReturn(addVelocityComment); - - addVelocityComment.execute("4711", actionRequest, properties); - - replayMocks(); - - ActionExecutor actionExecutor = createActionExecutor(); - actionExecutor.execute("4711", actionRequest, properties); - } - public void testLogEventDelegation() throws IOException { ActionRequest actionRequest = createMock(ActionRequest.class); expect(actionRequest.getName()).andReturn("log-event"); @@ -243,10 +223,6 @@ bind(AddStandardComment.Factory.class).toInstance( addStandardCommentFactory); - addVelocityCommentFactory = createMock(AddVelocityComment.Factory.class); - bind(AddVelocityComment.Factory.class).toInstance( - addVelocityCommentFactory); - logEventFactory = createMock(LogEvent.Factory.class); bind(LogEvent.Factory.class).toInstance(logEventFactory); }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityCommentTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityCommentTest.java deleted file mode 100644 index 5a33db6..0000000 --- a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddVelocityCommentTest.java +++ /dev/null
@@ -1,429 +0,0 @@ -// Copyright (C) 2013 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package com.googlesource.gerrit.plugins.its.base.workflow.action; - -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.capture; -import static org.easymock.EasyMock.eq; -import static org.easymock.EasyMock.expect; - -import com.google.common.collect.Sets; -import com.google.gerrit.extensions.config.FactoryModule; -import com.google.gerrit.server.config.SitePath; -import com.google.inject.Guice; -import com.google.inject.Injector; - -import com.googlesource.gerrit.plugins.its.base.its.ItsFacade; -import com.googlesource.gerrit.plugins.its.base.testutil.LoggingMockingTestCase; -import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; -import com.googlesource.gerrit.plugins.its.base.workflow.Property; -import com.googlesource.gerrit.plugins.its.base.workflow.action.AddVelocityComment.VelocityAdapterItsFacade; - -import org.apache.velocity.VelocityContext; -import org.apache.velocity.runtime.RuntimeInstance; -import org.easymock.Capture; -import org.easymock.EasyMock; -import org.easymock.IAnswer; -import org.eclipse.jgit.util.FileUtils; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -public class AddVelocityCommentTest extends LoggingMockingTestCase { - private Injector injector; - - private Path sitePath; - private ItsFacade its; - private RuntimeInstance velocityRuntime; - - private boolean cleanupSitePath; - - public void testWarnNoTemplateNameGiven() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn(""); - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - - assertLogMessageContains("No template name"); - } - - public void testInlinePlain() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "Simple-text"}); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Simple-text"); - expect(velocityRuntime.evaluate((VelocityContext)anyObject(), - (Writer)anyObject(), (String)anyObject(), eq("Simple-text"))) - .andAnswer(answer); - - its.addComment("4711", "Simple-text"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - } - - public void testInlineWithMultipleParameters() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "Param2", "Param3"}); - - Set<Property> properties = Sets.newHashSet(); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Param2 Param3"); - expect(velocityRuntime.evaluate((VelocityContext)anyObject(), - (Writer)anyObject(), (String)anyObject(), eq("Param2 Param3"))) - .andAnswer(answer); - - its.addComment("4711", "Param2 Param3"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, properties); - } - - public void testInlineWithSingleProperty() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "${subject}"}); - - Set<Property> properties = Sets.newHashSet(); - - Property propertySubject = createMock(Property.class); - expect(propertySubject.getKey()).andReturn("subject").anyTimes(); - expect(propertySubject.getValue()).andReturn("Rosebud").anyTimes(); - properties.add(propertySubject); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Rosebud"); - Capture<VelocityContext> contextCapture = createCapture(); - expect(velocityRuntime.evaluate(capture(contextCapture), - (Writer)anyObject(), (String)anyObject(), eq("${subject}"))) - .andAnswer(answer); - - its.addComment("4711", "Rosebud"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, properties); - - VelocityContext context = contextCapture.getValue(); - assertEquals("Subject property of context did not match", "Rosebud", - context.get("subject")); - } - - public void testInlineWithUnusedProperty() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "Test"}); - - Set<Property> properties = Sets.newHashSet(); - - Property propertySubject = createMock(Property.class); - expect(propertySubject.getKey()).andReturn("subject").anyTimes(); - expect(propertySubject.getValue()).andReturn("Rosebud").anyTimes(); - properties.add(propertySubject); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Test"); - expect(velocityRuntime.evaluate((VelocityContext)anyObject(), - (Writer)anyObject(), (String)anyObject(), eq("Test"))) - .andAnswer(answer); - - its.addComment("4711", "Test"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, properties); - } - - public void testInlineWithMultipleProperties() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "${subject}", "${reason}", "${subject}"}); - - Set<Property> properties = Sets.newHashSet(); - - Property propertySubject = createMock(Property.class); - expect(propertySubject.getKey()).andReturn("subject").anyTimes(); - expect(propertySubject.getValue()).andReturn("Rosebud").anyTimes(); - properties.add(propertySubject); - - Property propertyReason = createMock(Property.class); - expect(propertyReason.getKey()).andReturn("reason").anyTimes(); - expect(propertyReason.getValue()).andReturn("Life").anyTimes(); - properties.add(propertyReason); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Rosebud Life Rosebud"); - Capture<VelocityContext> contextCapture = createCapture(); - expect(velocityRuntime.evaluate(capture(contextCapture), - (Writer)anyObject(), (String)anyObject(), - eq("${subject} ${reason} ${subject}"))).andAnswer(answer); - - its.addComment("4711", "Rosebud Life Rosebud"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, properties); - - VelocityContext context = contextCapture.getValue(); - assertEquals("Subject property of context did not match", "Rosebud", - context.get("subject")); - assertEquals("Reason property of context did not match", "Life", - context.get("reason")); - } - - public void testItsWrapperFormatLink1Parameter() throws IOException, - SecurityException, IllegalArgumentException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "Simple-Text"}); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Simple-Text"); - Capture<VelocityContext> contextCapture = createCapture(); - expect(velocityRuntime.evaluate(capture(contextCapture), - (Writer)anyObject(), (String)anyObject(), eq("Simple-Text"))) - .andAnswer(answer); - - its.addComment("4711", "Simple-Text"); - - expect(its.createLinkForWebui("http://www.example.org/", - "http://www.example.org/")) .andReturn("Formatted Link"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - - VelocityContext context = contextCapture.getValue(); - Object itsAdapterObj = context.get("its"); - assertNotNull("its property is null", itsAdapterObj); - assertTrue("Its is not a VelocityAdapterItsFacade instance", - itsAdapterObj instanceof VelocityAdapterItsFacade); - VelocityAdapterItsFacade itsAdapter = - (VelocityAdapterItsFacade) itsAdapterObj; - String formattedLink = itsAdapter.formatLink("http://www.example.org/"); - assertEquals("Result of formatLink does not match", "Formatted Link", - formattedLink); - } - - public void testItsWrapperFormatLink2Parameters() throws IOException, - SecurityException, IllegalArgumentException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("inline"); - expect(actionRequest.getParameters()).andReturn( - new String[] {"inline", "Simple-Text"}); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Simple-Text"); - Capture<VelocityContext> contextCapture = createCapture(); - expect(velocityRuntime.evaluate(capture(contextCapture), - (Writer)anyObject(), (String)anyObject(), eq("Simple-Text"))) - .andAnswer(answer); - - its.addComment("4711", "Simple-Text"); - - expect(its.createLinkForWebui("http://www.example.org/", "Caption")) - .andReturn("Formatted Link"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - - VelocityContext context = contextCapture.getValue(); - Object itsAdapterObj = context.get("its"); - assertNotNull("its property is null", itsAdapterObj); - assertTrue("Its is not a VelocityAdapterItsFacade instance", - itsAdapterObj instanceof VelocityAdapterItsFacade); - VelocityAdapterItsFacade itsAdapter = - (VelocityAdapterItsFacade) itsAdapterObj; - String formattedLink = itsAdapter.formatLink("http://www.example.org/", - "Caption"); - assertEquals("Result of formatLink does not match", "Formatted Link", - formattedLink); - } - - public void testWarnTemplateNotFound() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("non-existing-template"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - - assertLogMessageContains("non-existing-template"); - } - - public void testTemplateSimple() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("test-template"); - - injectTestTemplate("Simple Test Template"); - - IAnswer<Boolean> answer = new VelocityWriterFiller("Simple Test Template"); - expect(velocityRuntime.evaluate((VelocityContext)anyObject(), - (Writer)anyObject(), (String)anyObject(), - eq("Simple Test Template"))).andAnswer(answer); - - its.addComment("4711", "Simple Test Template"); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, new HashSet<Property>()); - } - - public void testTemplateMultipleParametersAndProperties() throws IOException { - ActionRequest actionRequest = createMock(ActionRequest.class); - expect(actionRequest.getParameter(1)).andReturn("test-template"); - - Set<Property> properties = Sets.newHashSet(); - - Property propertySubject = createMock(Property.class); - expect(propertySubject.getKey()).andReturn("subject").anyTimes(); - expect(propertySubject.getValue()).andReturn("Rosebud").anyTimes(); - properties.add(propertySubject); - - Property propertyReason = createMock(Property.class); - expect(propertyReason.getKey()).andReturn("reason").anyTimes(); - expect(propertyReason.getValue()).andReturn("Life").anyTimes(); - properties.add(propertyReason); - - injectTestTemplate("Test Template with subject: ${subject}.\n" + - "${reason} is the reason for ${subject}."); - - IAnswer<Boolean> answer = new VelocityWriterFiller( - "Test Template with subject: Rosebud.\n" + - "Life is the reason for Rosebud."); - Capture<VelocityContext> contextCapture = createCapture(); - expect(velocityRuntime.evaluate(capture(contextCapture), - (Writer)anyObject(), (String)anyObject(), - eq("Test Template with subject: ${subject}.\n" + - "${reason} is the reason for ${subject}."))).andAnswer(answer); - - its.addComment("4711", "Test Template with subject: Rosebud.\n" + - "Life is the reason for Rosebud."); - - replayMocks(); - - AddVelocityComment addVelocityComment = createAddVelocityComment(); - addVelocityComment.execute("4711", actionRequest, properties); - - VelocityContext context = contextCapture.getValue(); - assertEquals("Subject property of context did not match", "Rosebud", - context.get("subject")); - assertEquals("Reason property of context did not match", "Life", - context.get("reason")); - } - - private AddVelocityComment createAddVelocityComment() { - return injector.getInstance(AddVelocityComment.class); - } - - private void injectTestTemplate(String template) throws IOException { - File templateParentFile = new File(sitePath.toFile(), "etc" + - File.separatorChar + "its" + File.separator + "templates"); - assertTrue("Failed to create parent (" + templateParentFile + ") for " + - "rule base", templateParentFile.mkdirs()); - File templateFile = new File(templateParentFile, "test-template.vm"); - - try (FileWriter unbufferedWriter = new FileWriter(templateFile); - BufferedWriter writer = new BufferedWriter(unbufferedWriter)) { - writer.write(template); - } - } - - @Override - public void setUp() throws Exception { - super.setUp(); - cleanupSitePath = false; - injector = Guice.createInjector(new TestModule()); - } - - @Override - public void tearDown() throws Exception { - if (cleanupSitePath) { - if (Files.exists(sitePath)) { - FileUtils.delete(sitePath.toFile(), FileUtils.RECURSIVE); - } - } - super.tearDown(); - } - - private Path randomTargetPath() { - return Paths.get("target", "random-name-" + UUID.randomUUID().toString()); - } - - private class TestModule extends FactoryModule { - @Override - protected void configure() { - sitePath = randomTargetPath(); - assertFalse("sitePath already (" + sitePath + ") already exists", - Files.exists(sitePath)); - cleanupSitePath = true; - - bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath); - - its = createMock(ItsFacade.class); - bind(ItsFacade.class).toInstance(its); - - velocityRuntime = createMock(RuntimeInstance.class); - bind(RuntimeInstance.class).toInstance(velocityRuntime); - } - } - - private class VelocityWriterFiller implements IAnswer<Boolean> { - private final String fill; - private final boolean returnValue; - - private VelocityWriterFiller(String fill, boolean returnValue) { - this.fill = fill; - this.returnValue = returnValue; - } - - private VelocityWriterFiller(String fill) { - this(fill, true); - } - - @Override - public Boolean answer() throws Throwable { - Object[] arguments = EasyMock.getCurrentArguments(); - Writer writer = (Writer) arguments[1]; - writer.write(fill); - return returnValue; - } - } -} \ No newline at end of file