diff --git a/src/fitnesse/junit/CustomVariableProvider.java b/src/fitnesse/junit/CustomVariableProvider.java new file mode 100644 index 0000000000..a5074d0b9c --- /dev/null +++ b/src/fitnesse/junit/CustomVariableProvider.java @@ -0,0 +1,12 @@ +package fitnesse.junit; + +import java.util.Map; + +/** + * Provide custom variables to override page variables in a similar way as done with URL-Parameters + * when using the fitnesse-stabdalone runner + * @author mwyraz + */ +public interface CustomVariableProvider { + public Map getCustomVariables(); +} diff --git a/src/fitnesse/junit/FitNesseRunner.java b/src/fitnesse/junit/FitNesseRunner.java index 90904746c6..25670d4405 100644 --- a/src/fitnesse/junit/FitNesseRunner.java +++ b/src/fitnesse/junit/FitNesseRunner.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import fitnesse.ConfigurationParameter; import fitnesse.ContextConfigurator; @@ -68,6 +69,16 @@ public class FitNesseRunner extends ParentRunner { boolean value() default true; } + /** + * The CustomVariableProviderClass annotation specifies an optional {@link CustomVariableProvider} to overwrite page variables + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface CustomVariableProviderClass { + + Class value(); + } + /** * The SuiteFilter annotation specifies the suite filter of the Fitnesse suite * to be run, e.g.: fasttests @@ -165,6 +176,7 @@ public class FitNesseRunner extends ParentRunner { private FitNesseContext context; private DescriptionFactory descriptionFactory; private List children; + private Map customVariables; public FitNesseRunner(Class suiteClass) throws InitializationError { super(suiteClass); @@ -219,6 +231,13 @@ protected void collectInitializationErrors(List errors) { } catch (Exception e) { errors.add(e); } + + try { + this.customVariables = getCustomVariables(suiteClass); + } catch (Exception e) { + errors.add(e); + } + try { this.context = createContext(suiteClass); } catch (Exception e) { @@ -320,6 +339,15 @@ protected boolean shouldPreventSystemExit(Class klass) throws Exception { return preventSystemExitAnnotation.value(); } + + protected Map getCustomVariables(Class klass) throws Exception { + CustomVariableProviderClass customVariableProviderClass = klass.getAnnotation(CustomVariableProviderClass.class); + if (null == customVariableProviderClass) { + return null; + } + return customVariableProviderClass.value().newInstance().getCustomVariables(); + } + protected String getFitNesseDir(Class klass) throws InitializationError { FitnesseDir fitnesseDirAnnotation = klass.getAnnotation(FitnesseDir.class); @@ -416,6 +444,13 @@ protected void addExecutionLogListener(RunNotifier notifier, MultipleTestsRunner testRunner.addExecutionLogListener(new ConsoleExecutionLogListener()); } + protected WikiPage getRootPage() { + if (this.customVariables==null) { + return context.getRootPage(); + } + return context.getRootPage(customVariables); + } + protected List initChildren() { WikiPage suiteRoot = getSuiteRootPage(); if (suiteRoot == null) { @@ -423,7 +458,7 @@ protected List initChildren() { } List children; if (suiteRoot.getData().hasAttribute("Suite")) { - children = new SuiteContentsFinder(suiteRoot, getSuiteFilter(), context.getRootPage()).getAllPagesToRunForThisSuite(); + children = new SuiteContentsFinder(suiteRoot, getSuiteFilter(), getRootPage()).getAllPagesToRunForThisSuite(); } else { children = Collections.singletonList(suiteRoot); } @@ -460,12 +495,12 @@ protected ContextConfigurator initContextConfigurator() throws InitializationErr private WikiPage getSuiteRootPage() { WikiPagePath path = PathParser.parse(this.suiteName); - PageCrawler crawler = context.getRootPage().getPageCrawler(); + PageCrawler crawler = getRootPage().getPageCrawler(); return crawler.getPage(path); } private MultipleTestsRunner createTestRunner(List pages) { - final PagesByTestSystem pagesByTestSystem = new PagesByTestSystem(pages, context.getRootPage()); + final PagesByTestSystem pagesByTestSystem = new PagesByTestSystem(pages, getRootPage()); MultipleTestsRunner runner = new MultipleTestsRunner(pagesByTestSystem, context.testSystemFactory);