diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java index 56c00415a..4b09edbd5 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTrigger.java @@ -67,6 +67,7 @@ import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefUpdated; import com.sonymobile.tools.gerrit.gerritevents.dto.rest.Notify; +import hudson.EnvVars; import hudson.Extension; import hudson.ExtensionList; import hudson.Util; @@ -85,8 +86,12 @@ import hudson.model.Queue; import hudson.model.Run; import hudson.model.Result; +import hudson.slaves.EnvironmentVariablesNodeProperty; +import hudson.slaves.NodeProperty; +import hudson.slaves.NodePropertyDescriptor; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; +import hudson.util.DescribableList; import hudson.util.FormValidation; import hudson.util.ListBoxModel; import hudson.util.ListBoxModel.Option; @@ -178,6 +183,9 @@ public class GerritTrigger extends Trigger { private List triggerOnEvents; private boolean dynamicTriggerConfiguration; private String triggerConfigURL; + private final EnvVars envVars; + private int globalHashVarsHash; + private int systemEnvVarsHash; private GerritTriggerTimerTask gerritTriggerTimerTask; private GerritTriggerInformationAction triggerInformationAction; @@ -194,8 +202,10 @@ public GerritTrigger(List gerritProjects) { this.skipVote = new SkipVote(false, false, false, false); this.escapeQuotes = true; this.serverName = ANY_SERVER; + this.envVars = initEnvVars(); + try { - DescriptorImpl descriptor = (DescriptorImpl)getDescriptor(); + DescriptorImpl descriptor = (DescriptorImpl) getDescriptor(); if (descriptor != null) { ListBoxModel options = descriptor.doFillNotificationLevelItems(this.serverName); if (!options.isEmpty()) { @@ -334,6 +344,23 @@ public GerritTrigger(List gerritProjects, SkipVote skipVote, Inte this.gerritTriggerTimerTask = null; this.triggerInformationAction = new GerritTriggerInformationAction(); this.notificationLevel = notificationLevel; + this.envVars = initEnvVars(); + } + + /** + * Initializes the environment variables used for expansion. + */ + private EnvVars initEnvVars() { + EnvVars result = new EnvVars(EnvVars.masterEnvVars); + DescribableList, NodePropertyDescriptor> globalProps = + Jenkins.getActiveInstance().getGlobalNodeProperties(); + EnvironmentVariablesNodeProperty envVarsProp = globalProps.get(EnvironmentVariablesNodeProperty.class); + EnvVars globalEnvVars = envVarsProp.getEnvVars(); + result.putAll(globalEnvVars); + + systemEnvVarsHash = EnvVars.masterEnvVars.hashCode(); + globalHashVarsHash = globalEnvVars.hashCode(); + return result; } /** @@ -960,14 +987,15 @@ public boolean equals(Object obj) { } logger.trace("entering isInteresting projects configured: {} the event: {}", allGerritProjects.size(), event); + updateEnvVarsIfNecessary(); for (GerritProject p : allGerritProjects) { try { if (event instanceof ChangeBasedEvent) { ChangeBasedEvent changeBasedEvent = (ChangeBasedEvent)event; if (isServerInteresting(event) - && p.isInteresting(changeBasedEvent.getChange().getProject(), - changeBasedEvent.getChange().getBranch(), - changeBasedEvent.getChange().getTopic())) { + && p.isInteresting(changeBasedEvent.getChange().getProject(), + changeBasedEvent.getChange().getBranch(), + changeBasedEvent.getChange().getTopic(), envVars)) { boolean containsFilePathsOrForbiddenFilePaths = ((p.getFilePaths() != null && p.getFilePaths().size() > 0) @@ -975,11 +1003,11 @@ public boolean equals(Object obj) { if (isFileTriggerEnabled() && containsFilePathsOrForbiddenFilePaths) { if (isServerInteresting(event) - && p.isInteresting(changeBasedEvent.getChange().getProject(), - changeBasedEvent.getChange().getBranch(), - changeBasedEvent.getChange().getTopic(), - changeBasedEvent.getFiles( - new GerritQueryHandler(getServerConfig(event))))) { + && p.isInteresting(changeBasedEvent.getChange().getProject(), + changeBasedEvent.getChange().getBranch(), + changeBasedEvent.getChange().getTopic(), + changeBasedEvent.getFiles( + new GerritQueryHandler(getServerConfig(event))), envVars)) { logger.trace("According to {} the event is interesting.", p); return true; } @@ -991,20 +1019,39 @@ public boolean equals(Object obj) { } else if (event instanceof RefUpdated) { RefUpdated refUpdated = (RefUpdated)event; if (isServerInteresting(event) && p.isInteresting(refUpdated.getRefUpdate().getProject(), - refUpdated.getRefUpdate().getRefName(), null)) { + refUpdated.getRefUpdate().getRefName(), null, envVars)) { logger.trace("According to {} the event is interesting.", p); return true; } } } catch (PatternSyntaxException pse) { logger.error(MessageFormat.format("Exception caught for project {0} and pattern {1}, message: {2}", - new Object[]{job.getName(), p.getPattern(), pse.getMessage()})); + new Object[]{job.getName(), p.getPattern(), pse.getMessage()})); } } logger.trace("Nothing interesting here, move along folks!"); return false; } + /** + * Checks if the current system env vars are still the same and updates our local copy if necessary + */ + private synchronized void updateEnvVarsIfNecessary() { + EnvironmentVariablesNodeProperty envVarsProp = + Jenkins.getActiveInstance().getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class); + EnvVars globalEnvVars = envVarsProp.getEnvVars(); + int newSystemEnvVarsHash = EnvVars.masterEnvVars.hashCode(); + int newGlobalEnvVarsHash = globalEnvVars.hashCode(); + + if (newSystemEnvVarsHash != systemEnvVarsHash || newGlobalEnvVarsHash != globalHashVarsHash) { + this.envVars.clear(); + this.envVars.putAll(EnvVars.masterEnvVars); + this.envVars.putAll(globalEnvVars); + systemEnvVarsHash = newSystemEnvVarsHash; + globalHashVarsHash = newGlobalEnvVarsHash; + } + } + /** * Check whether the event provider contains the same server name as the serverName field. * diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Branch.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Branch.java index d884dfb8d..1831d11c1 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Branch.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Branch.java @@ -23,6 +23,7 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; +import hudson.EnvVars; import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; @@ -90,10 +91,11 @@ public void setPattern(String pattern) { /** * Tells if the given branch is matched by this rule. * @param branch the branch + * @param envVars the environment variables exisiting on the jenkins host. * @return true if the branch matches. */ - public boolean isInteresting(String branch) { - return compareType.matches(pattern, branch); + public boolean isInteresting(String branch, EnvVars envVars) { + return compareType.matches(pattern, branch, envVars); } /** diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareType.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareType.java index 451efef4e..0fe1a403e 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareType.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareType.java @@ -26,6 +26,8 @@ import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareUtil.AntCompareUtil; import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareUtil.PlainCompareUtil; import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareUtil.RegExpCompareUtil; +import hudson.EnvVars; + import java.util.LinkedList; import java.util.List; @@ -88,7 +90,7 @@ public static CompareType findByOperator(char operator) { return PLAIN; } - private CompareUtil util; + CompareUtil util; /** * Private Constructor. @@ -102,10 +104,11 @@ private CompareType(CompareUtil util) { * Tells if the given string matches the given pattern based on the algorithm of this CompareType instance. * @param pattern the pattern * @param str the string + * @param envVars the environment variables exisiting on the jenkins host. * @return true if the string matches the pattern. */ - public boolean matches(String pattern, String str) { - return util.matches(pattern, str); + public boolean matches(String pattern, String str, EnvVars envVars) { + return util.matches(pattern, str, envVars); } /** diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareUtil.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareUtil.java index b8df9df82..fc6b32c94 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareUtil.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/CompareUtil.java @@ -23,11 +23,14 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; -import java.io.File; +import hudson.EnvVars; import org.apache.tools.ant.types.selectors.SelectorUtils; +import java.io.File; + /** * Base interface for the compare-algorithms. + * * @author Robert Sandell <robert.sandell@sonyericsson.com> */ public interface CompareUtil { @@ -37,9 +40,19 @@ public interface CompareUtil { * @param pattern the pattern to use. * @param str the string to match on. * @return true if the string matches the pattern. + * @deprecated use {@link #matches(String, String, EnvVars)} instead. */ boolean matches(String pattern, String str); + /** + * Tells if the given pattern matches the string according to the implemented comparer/algorithm. + * @param pattern the pattern to use. + * @param str the string to match on. + * @param envVars the environment variables exisiting on the jenkins host. Can be {@code null}. + * @return true if the string matches the pattern. + */ + boolean matches(String pattern, String str, EnvVars envVars); + /** * Returns the human-readable name of the util. * @return the name. @@ -52,20 +65,42 @@ public interface CompareUtil { */ char getOperator(); + abstract class AbstractCompareUtil implements CompareUtil { + @Override + public boolean matches(String pattern, String str) { + return matches(pattern, str, null); + } + + /** + * Expands the pattern in case it contains tokens that can be resolved with the given envVars. + * @param pattern the pattern to expand. + * @param envVars the envVars to use for expansion, if {@code null} then the unmodified pattern will be returned + * @return the expanded string, if no envVars were provided will return the initial unmodified pattern + */ + protected String expandWithEnvVarsIfPossible(String pattern, EnvVars envVars) { + if (envVars != null) { + return envVars.expand(pattern); + } else { + return pattern; + } + } + } + /** * Compares based on Ant-style paths. * like my/**/something*.git */ - static class AntCompareUtil implements CompareUtil { + class AntCompareUtil extends AbstractCompareUtil { @Override - public boolean matches(String pattern, String str) { + public boolean matches(String pattern, String str, EnvVars envVars) { // Replace the Git directory separator character (always '/') // with the platform specific directory separator before // invoking Ant's platform specific path matching. String safePattern = pattern.replace('/', File.separatorChar); + String expandedPattern = expandWithEnvVarsIfPossible(safePattern, envVars); String safeStr = str.replace('/', File.separatorChar); - return SelectorUtils.matchPath(safePattern, safeStr); + return SelectorUtils.matchPath(expandedPattern, safeStr); } @Override @@ -80,13 +115,15 @@ public char getOperator() { } /** - * Compares with pattern.equals(str). + * Compares with pattern.equalsIgnoreCase(str). */ - static class PlainCompareUtil implements CompareUtil { + class PlainCompareUtil extends AbstractCompareUtil { @Override - public boolean matches(String pattern, String str) { - return pattern.equalsIgnoreCase(str); + public boolean matches(String pattern, String str, EnvVars envVars) { + String expandedPattern = expandWithEnvVarsIfPossible(pattern, envVars); + + return expandedPattern.equalsIgnoreCase(str); } @Override @@ -105,11 +142,12 @@ public char getOperator() { * string.matches(pattern) * @see java.util.regex.Pattern */ - static class RegExpCompareUtil implements CompareUtil { + class RegExpCompareUtil extends AbstractCompareUtil { @Override - public boolean matches(String pattern, String str) { - return str.matches(pattern); + public boolean matches(String pattern, String str, EnvVars envVars) { + String expandedPattern = expandWithEnvVarsIfPossible(pattern, envVars); + return str.matches(expandedPattern); } @Override diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java index 1d332fe3e..3d2851ed4 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java @@ -23,6 +23,7 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; +import hudson.EnvVars; import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; @@ -92,11 +93,12 @@ public void setPattern(String pattern) { /** * Tells if the given files are matched by this rule. * @param files the files in the patch set. + * @param envVars the environment variables exisiting on the jenkins host. * @return true if the files match. */ - public boolean isInteresting(List files) { + public boolean isInteresting(List files, EnvVars envVars) { for (String file : files) { - if (compareType.matches(pattern, file)) { + if (compareType.matches(pattern, file, envVars)) { return true; } } diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java index 693e834e6..e45d140e4 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java @@ -25,6 +25,8 @@ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; import static com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer.ANY_SERVER; + +import hudson.EnvVars; import hudson.Extension; import hudson.RelativePath; import hudson.model.Describable; @@ -210,23 +212,24 @@ public void setForbiddenFilePaths(List forbiddenFilePaths) { * @param branch the branch. * @param topic the topic. * @param files the files. + * @param envVars the environment variables exisiting on the jenkins host. * @return true is the rules match. */ - public boolean isInteresting(String project, String branch, String topic, List files) { - if (compareType.matches(pattern, project)) { + public boolean isInteresting(String project, String branch, String topic, List files, EnvVars envVars) { + if (compareType.matches(pattern, project, envVars)) { for (Branch b : branches) { boolean foundInterestingForbidden = false; boolean foundInterestingTopicOrFile = false; - if (b.isInteresting(branch)) { + if (b.isInteresting(branch, envVars)) { if (forbiddenFilePaths != null) { for (FilePath ffp : forbiddenFilePaths) { - if (ffp.isInteresting(files)) { + if (ffp.isInteresting(files, envVars)) { foundInterestingForbidden = true; break; } } } - if (isInterestingTopic(topic) && isInterestingFile(files)) { + if (isInterestingTopic(topic, envVars) && isInterestingFile(files, envVars)) { foundInterestingTopicOrFile = true; } if (disableStrictForbiddenFileVerification) { @@ -256,10 +259,22 @@ public boolean isInteresting(String project, String branch, String topic, List 0) { for (Topic t : topics) { - if (t.isInteresting(topic)) { + if (t.isInteresting(topic, envVars)) { return true; } } @@ -288,12 +304,13 @@ private boolean isInterestingTopic(String topic) { * Compare files to see if the rules specified is a match. * * @param files the files. + * @param envVars the environment variables exisiting on the jenkins host. * @return true if the rules match or no rules. */ - private boolean isInterestingFile(List files) { + private boolean isInterestingFile(List files, EnvVars envVars) { if (filePaths != null && filePaths.size() > 0) { for (FilePath f : filePaths) { - if (f.isInteresting(files)) { + if (f.isInteresting(files, envVars)) { return true; } } diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Topic.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Topic.java index 4834d4d2b..416fafce1 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Topic.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/Topic.java @@ -23,6 +23,7 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; +import hudson.EnvVars; import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; @@ -89,13 +90,14 @@ public void setPattern(String pattern) { /** * Tells if the given topic are matched by this rule. * @param topic the topic in change. + * @param envVars the environment variables exisiting on the jenkins host. * @return true if the topic match. */ - public boolean isInteresting(String topic) { + public boolean isInteresting(String topic, EnvVars envVars) { if (topic == null) { topic = ""; } - if (compareType.matches(pattern, topic)) { + if (compareType.matches(pattern, topic, envVars)) { return true; } return false; diff --git a/src/main/webapp/trigger/help-GerritTriggerConfiguration.html b/src/main/webapp/trigger/help-GerritTriggerConfiguration.html index ef4bd54ff..b1257972b 100644 --- a/src/main/webapp/trigger/help-GerritTriggerConfiguration.html +++ b/src/main/webapp/trigger/help-GerritTriggerConfiguration.html @@ -8,7 +8,8 @@ You can specify the name pattern in three different ways, as provided by the "Type" drop-down menu.

    -
  • Plain: The exact name in Gerrit, case sensitive equality.
  • +
  • Plain: The exact name in Gerrit, case insensitive equality, allows usage of system environment + variables defined on the jenkins host (e.g. ${ENVIRONMENT}).
  • Path: ANT style pattern. Ex: "**/base/*"
  • RegExp: Regular expression.
diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java index 84177b39a..31c130f9b 100644 --- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java +++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerTest.java @@ -49,6 +49,7 @@ import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated; import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefUpdated; +import hudson.EnvVars; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.Action; @@ -758,7 +759,7 @@ public void testGerritEvent() { PowerMockito.when(ToGerritRunListener.getInstance()).thenReturn(listener); GerritProject gP = mock(GerritProject.class); - doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class)); + doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class), any(EnvVars.class)); when(gP.getFilePaths()).thenReturn(null); @@ -838,7 +839,7 @@ public void testGerritEventNotInteresting() { PowerMockito.when(ToGerritRunListener.getInstance()).thenReturn(listener); GerritProject gP = mock(GerritProject.class); - doReturn(false).when(gP).isInteresting(any(String.class), any(String.class), any(String.class)); + doReturn(false).when(gP).isInteresting(any(String.class), any(String.class), any(String.class), any(EnvVars.class)); when(gP.getFilePaths()).thenReturn(null); GerritTrigger trigger = Setup.createDefaultTrigger(project); @@ -882,7 +883,7 @@ public void testGerritEventManualEvent() { PowerMockito.when(ToGerritRunListener.getInstance()).thenReturn(listener); GerritProject gP = mock(GerritProject.class); - doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class)); + doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class), any(EnvVars.class)); when(gP.getFilePaths()).thenReturn(null); GerritTrigger trigger = Setup.createDefaultTrigger(project); @@ -1029,7 +1030,7 @@ public void testGerritEventSilentMode() { PowerMockito.when(ToGerritRunListener.getInstance()).thenReturn(listener); GerritProject gP = mock(GerritProject.class); - doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class)); + doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class), any(EnvVars.class)); when(gP.getFilePaths()).thenReturn(null); GerritTrigger trigger = Setup.createDefaultTrigger(null); @@ -1060,7 +1061,7 @@ public void testGerritEventManualEventSilentMode() { PowerMockito.when(ToGerritRunListener.getInstance()).thenReturn(listener); GerritProject gP = mock(GerritProject.class); - doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class)); + doReturn(true).when(gP).isInteresting(any(String.class), any(String.class), any(String.class), any(EnvVars.class)); when(gP.getFilePaths()).thenReturn(null); GerritTrigger trigger = Setup.createDefaultTrigger(project); diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java index 015916357..249c51516 100644 --- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java +++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectInterestingTest.java @@ -23,6 +23,7 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; +import hudson.EnvVars; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -42,6 +43,7 @@ public class GerritProjectInterestingTest { private final InterestingScenario scenario; + private EnvVars envVars; /** * Constructor. @@ -49,17 +51,18 @@ public class GerritProjectInterestingTest { */ public GerritProjectInterestingTest(InterestingScenario scenario) { this.scenario = scenario; + envVars = new EnvVars(); } /** - * Tests {@link GerritProject#isInteresting(String, String, String)}. + * Tests {@link GerritProject#isInteresting(String, String, String, hudson.EnvVars)}. */ @Test public void testInteresting() { assertEquals(scenario.expected, scenario.config.isInteresting( scenario.project, scenario.branch, - scenario.topic)); + scenario.topic, envVars)); } /** diff --git a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java index 1d93c3a98..ba7e438c6 100644 --- a/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java +++ b/src/test/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProjectWithFilesInterestingTest.java @@ -23,6 +23,8 @@ */ package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data; +import hudson.EnvVars; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -42,7 +44,7 @@ public class GerritProjectWithFilesInterestingTest { private final InterestingScenarioWithFiles scenarioWithFiles; - + private static EnvVars envVars = new EnvVars(); /** * Constructor. * @param scenarioWithFiles scenarioWithFiles @@ -52,12 +54,23 @@ public GerritProjectWithFilesInterestingTest(InterestingScenarioWithFiles scenar } /** - * Tests {@link GerritProject#isInteresting(String, String, String, java.util.List)}. + * Tests {@link GerritProject#isInteresting(String, String, String, List, hudson.EnvVars)}. */ @Test public void testInteresting() { assertEquals(scenarioWithFiles.expected, scenarioWithFiles.config.isInteresting( - scenarioWithFiles.project, scenarioWithFiles.branch, scenarioWithFiles.topic, scenarioWithFiles.files)); + scenarioWithFiles.project, scenarioWithFiles.branch, scenarioWithFiles.topic, scenarioWithFiles.files, envVars)); + } + + + /** + * Sets up the test environment. + */ + @BeforeClass + public static void setupEnv() { + // this is a somewhat hacky way to deal with the fact that the compare util was already set up before + // we get the chance to update the system environment + envVars.put("PROJECTNAME", "myproject"); } /** @@ -213,6 +226,14 @@ public static Collection getParameters() { parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles( config, "vendor/semc/master/project", "origin/master", null, files, false), }); + branches = new LinkedList(); + branch = new Branch(CompareType.PLAIN, "master"); + branches.add(branch); + config = new GerritProject(CompareType.PLAIN, "${PROJECTNAME}", branches, null, null, null, + false); + parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles( + config, "myproject", "master", null, null, true),}); + return parameters; }