Skip to content

Commit

Permalink
Merge 6fc60fb into 2bba98f
Browse files Browse the repository at this point in the history
  • Loading branch information
phillcunnington committed Sep 24, 2019
2 parents 2bba98f + 6fc60fb commit a7dd626
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 ForgeRock AS.
* Copyright 2015-2017 ForgeRock AS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.surefire.suite.RunResult;
import org.forgerock.cuppa.Runner;
import org.forgerock.cuppa.Test;
import org.forgerock.cuppa.model.Options;
import org.forgerock.cuppa.model.Tags;
import org.forgerock.cuppa.model.TestBlock;
import org.forgerock.cuppa.reporters.CompositeReporter;
Expand Down Expand Up @@ -109,7 +110,7 @@ private Set<String> split(String s) {
public RunResult invoke(Object forkTestSet) {
ReporterFactory reporterFactory = providerParameters.getReporterFactory();
RunListener listener = reporterFactory.createReporter();
Runner runner = new Runner(tags);
Runner runner = new Runner(Options.EMPTY.set(new Runner.TagsRunOption(tags)));
TestBlock rootBlock = runner.defineTests(getSuites());
List<Reporter> reporters = Arrays.asList(new DefaultReporter(), new CuppaSurefireReporter(listener));
runner.run(rootBlock, new CompositeReporter(reporters));
Expand Down
13 changes: 12 additions & 1 deletion cuppa/src/main/java/org/forgerock/cuppa/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Objects;
import java.util.function.Function;

import org.forgerock.cuppa.model.Options;
import org.forgerock.cuppa.model.TestBlock;
import org.forgerock.cuppa.reporters.Reporter;

Expand All @@ -31,8 +32,10 @@ public final class Configuration {
List<Function<TestBlock, TestBlock>> testTransforms = new ArrayList<>();
TestInstantiator testInstantiator = Class::newInstance;
Reporter additionalReporter;
private final Options runOptions;

Configuration() {
Configuration(Options runOptions) {
this.runOptions = runOptions;
}

/**
Expand Down Expand Up @@ -66,4 +69,12 @@ public void setAdditionalReporter(Reporter reporter) {
Objects.requireNonNull(reporter, "Reporter must not be null");
additionalReporter = reporter;
}

/**
* Get the set of options that can be used by test block transforms.
* @return The run state.
*/
public Options getRunOptions() {
return runOptions;
}
}
73 changes: 62 additions & 11 deletions cuppa/src/main/java/org/forgerock/cuppa/Runner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 ForgeRock AS.
* Copyright 2015-2017 ForgeRock AS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,10 @@

package org.forgerock.cuppa;

import static java.util.Collections.emptyList;
import static org.forgerock.cuppa.model.TestBlockType.ROOT;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
Expand All @@ -32,13 +32,15 @@
import org.forgerock.cuppa.internal.TestContainer;
import org.forgerock.cuppa.internal.filters.EmptyTestBlockFilter;
import org.forgerock.cuppa.internal.filters.OnlyTestBlockFilter;
import org.forgerock.cuppa.internal.filters.TagTestBlockFilter;
import org.forgerock.cuppa.internal.filters.expression.ExpressionTagTestBlockFilter;
import org.forgerock.cuppa.model.Option;
import org.forgerock.cuppa.model.Options;
import org.forgerock.cuppa.model.Tags;
import org.forgerock.cuppa.model.TestBlock;
import org.forgerock.cuppa.model.TestBlockBuilder;
import org.forgerock.cuppa.reporters.CompositeReporter;
import org.forgerock.cuppa.reporters.Reporter;
import org.forgerock.cuppa.transforms.ExpressionTagTestBlockFilter;
import org.forgerock.cuppa.transforms.TagTestBlockFilter;

/**
* Runs Cuppa tests.
Expand All @@ -51,6 +53,8 @@ public final class Runner {
.setTestClass(Cuppa.class)
.setDescription("")
.build();
private static final List<Function<TestBlock, TestBlock>> DEFAULT_CORE_TEST_TRANSFORMS =
Arrays.asList(new OnlyTestBlockFilter(), new EmptyTestBlockFilter());

private final List<Function<TestBlock, TestBlock>> coreTestTransforms;
private final Configuration configuration;
Expand All @@ -59,30 +63,63 @@ public final class Runner {
* Creates a new runner with no run tags and a configuration loaded from the classpath.
*/
public Runner() {
this(Tags.EMPTY_TAGS);
this(Options.EMPTY);
}

/**
* Creates a new runner with the given run tags and a configuration loaded from the classpath.
*
* @param runTags Tags to filter the tests on.
* @deprecated Use @{link {@link #Runner(Options)}} and provide {@literal runTags} as {@literal runState} instead
* and use state in {@link ConfigurationProvider} implementation to insert the {@link TagTestBlockFilter} in
* the appropriate order.
*/
@Deprecated
public Runner(Tags runTags) {
this(runTags, getConfiguration());
this(Options.EMPTY.set(new TagsRunOption(runTags)));
}

/**
* Creates a new runner with the given run tags and configuration.
*
* @param runTags Tags to filter the tests on.
* @param configuration Cuppa configuration to control the behaviour of the runner.
* @deprecated Use @{link {@link #Runner(Configuration)}} and provide {@literal runTags} as
* {@literal runState} instead and use state in {@link ConfigurationProvider} implementation to insert the
* {@link TagTestBlockFilter} in the appropriate order.
*/
@Deprecated
public Runner(Tags runTags, Configuration configuration) {
coreTestTransforms = Arrays.asList(new OnlyTestBlockFilter(), new ExpressionTagTestBlockFilter(runTags),
new TagTestBlockFilter(runTags), new EmptyTestBlockFilter());
this(Stream.concat(Stream.of(
new ExpressionTagTestBlockFilter(runTags),
new TagTestBlockFilter(runTags)),
DEFAULT_CORE_TEST_TRANSFORMS.stream()).collect(Collectors.toList()), configuration);
}

/**
* Creates a new runner with the given run state and a configuration loaded from the classpath.
*
* @param runOptions Any state information that should be used by test block transforms.
*/
public Runner(Options runOptions) {
this(getConfiguration(runOptions));
}

/**
* Creates a new runner with the given run state and configuration.
*
* @param configuration Cuppa configuration to control the behaviour of the runner.
*/
public Runner(Configuration configuration) {
this(DEFAULT_CORE_TEST_TRANSFORMS, configuration);
}

private Runner(List<Function<TestBlock, TestBlock>> coreTestTransforms, Configuration configuration) {
this.coreTestTransforms = coreTestTransforms;
this.configuration = configuration;
}


/**
* Instantiates the test classes, which define tests as side effects, and return the root test block.
*
Expand Down Expand Up @@ -130,8 +167,8 @@ private TestBlock mergeRootTestBlocks(TestBlock testBlock1, TestBlock testBlock2
testBlock2.testBlocks.stream()).collect(Collectors.toList())).build();
}

private static Configuration getConfiguration() {
Configuration configuration = new Configuration();
private static Configuration getConfiguration(Options runOptions) {
Configuration configuration = new Configuration(runOptions);
Iterator<ConfigurationProvider> iterator = CONFIGURATION_PROVIDER_LOADER.iterator();
if (iterator.hasNext()) {
ConfigurationProvider configurationProvider = iterator.next();
Expand All @@ -151,7 +188,7 @@ private TestBlock transformTests(TestBlock rootBlock, List<Function<TestBlock, T
}

private void runTests(TestBlock rootBlock, Reporter reporter) {
TestBlockRunner rootRunner = createRunner(rootBlock, Collections.emptyList(), reporter);
TestBlockRunner rootRunner = createRunner(rootBlock, emptyList(), reporter);
rootRunner.run();
}

Expand All @@ -165,4 +202,18 @@ private TestBlockRunner createRunner(TestBlock testBlock, List<TestBlockRunner>
}
return runner;
}

/**
* Tag run state to perform tag based filtering with.
*/
public static final class TagsRunOption extends Option<Tags> {
/**
* Create a new option.
*
* @param value The immutable value to store in this option.
*/
public TagsRunOption(Tags value) {
super(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.forgerock.cuppa.internal.TestContainer;
import org.forgerock.cuppa.model.Hook;
import org.forgerock.cuppa.model.Options;
import org.forgerock.cuppa.model.Tags;
import org.forgerock.cuppa.model.Test;
import org.forgerock.cuppa.model.TestBlock;
Expand Down Expand Up @@ -65,7 +66,8 @@ public static void runTests(TestBlock testBlock, Reporter reporter) {
* @param tags Tags to filter the tests on.
*/
public static void runTests(TestBlock testBlock, Reporter reporter, Tags tags) {
new Runner(tags, new Configuration()).run(testBlock, reporter);
new Runner(tags, new Configuration(Options.EMPTY))
.run(testBlock, reporter);
}

/**
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion cuppa/src/main/java/org/forgerock/cuppa/model/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Tags {
* @param excludedTags The set of excluded tags which tests must not be tagged with to be included
* in the test run.
* @param expressionTags An expression using condition to create complex tag filtering
* {@link org.forgerock.cuppa.internal.filters.expression.Condition}
* {@link org.forgerock.cuppa.transforms.expression.Condition}
*/
public Tags(Set<String> tags, Set<String> excludedTags, String expressionTags) {
this.tags = tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;
package org.forgerock.cuppa.transforms;

import java.util.Collections;
import java.util.HashSet;
Expand All @@ -28,6 +28,8 @@
import org.forgerock.cuppa.model.TagsOption;
import org.forgerock.cuppa.model.Test;
import org.forgerock.cuppa.model.TestBlock;
import org.forgerock.cuppa.transforms.expression.Condition;
import org.forgerock.cuppa.transforms.expression.ExpressionParser;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters;
package org.forgerock.cuppa.transforms;

import java.util.Collections;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;
package org.forgerock.cuppa.transforms.expression;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* A condition that composes other conditions with a logical AND.
*/
class AndCondition extends ConditionWrapper {
public class AndCondition extends ConditionWrapper {

/** An empty AND definition. */
public static final AndCondition EMPTY = new AndCondition(Collections.emptyList());
private final Collection<Condition> conditions;
final List<Condition> conditions;

/**
* Constructor.
*
* @param conditions a list of condition to compose.
*/
AndCondition(Collection<Condition> conditions) {
this.conditions = Collections.unmodifiableCollection(conditions);
AndCondition(List<Condition> conditions) {
this.conditions = Collections.unmodifiableList(conditions);
}

@Override
public boolean shouldRun(Collection<String> tags) {
public final boolean shouldRun(Collection<String> tags) {
return conditions.stream().allMatch(c -> c.shouldRun(tags));
}

@Override
public ConditionWrapper setConditions(Collection<Condition> conditions) {
final ConditionWrapper setConditions(List<Condition> conditions) {
return new AndCondition(conditions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;
package org.forgerock.cuppa.transforms.expression;

import java.util.Collection;

import org.forgerock.cuppa.transforms.ExpressionTagTestBlockFilter;

/**
* A condition used by {@link ExpressionTagTestBlockFilter}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.forgerock.cuppa.internal.filters.expression;
package org.forgerock.cuppa.transforms.expression;

import java.util.Arrays;
import java.util.List;

/**
* A factory to creates {@link ConditionWrapper}.
*/
final class ConditionFactory {
public final class ConditionFactory {

/**
* link the operator to an instance of {@link Condition}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.forgerock.cuppa.internal.filters.expression;
package org.forgerock.cuppa.transforms.expression;

import java.util.Collection;
import java.util.List;

/**
* A condition wrapper for Condition that needs to wrap other conditions.
Expand All @@ -9,8 +9,8 @@ abstract class ConditionWrapper implements Condition {

/**
* Create a new Condition with the given conditions.
* @param conditions the collection of conditions
* @param conditions the list of conditions
* @return a new condition
*/
abstract ConditionWrapper setConditions(Collection<Condition> conditions);
abstract ConditionWrapper setConditions(List<Condition> conditions);
}
Loading

0 comments on commit a7dd626

Please sign in to comment.