├── .gitignore
├── .travis.yml
├── oleaster-runner
├── src
│ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── mscharhag
│ │ │ └── oleaster
│ │ │ └── runner
│ │ │ ├── PendingInvokable.java
│ │ │ ├── OleasterTest.java
│ │ │ ├── StaticSupportingSuiteBuilder.java
│ │ │ ├── Invokable.java
│ │ │ ├── suite
│ │ │ ├── SuiteDefinition.java
│ │ │ ├── Spec.java
│ │ │ ├── SuiteDefinitionEvaluator.java
│ │ │ ├── Suite.java
│ │ │ └── SuiteBuilder.java
│ │ │ ├── OleasterRunner.java
│ │ │ └── StaticRunnerSupport.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mscharhag
│ │ └── oleaster
│ │ └── runner
│ │ ├── FocusedSpecTest.java
│ │ ├── AssertUtil.java
│ │ ├── PendingSpecTest.java
│ │ ├── SpecTest.java
│ │ ├── OleasterRunnerTest.java
│ │ ├── SuiteBuilderTest.java
│ │ ├── HandlerTest.java
│ │ └── SuiteDefinitionEvaluatorTest.java
├── pom.xml
└── README.md
├── oleaster-examples
├── src
│ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── mscharhag
│ │ │ └── oleaster
│ │ │ └── examples
│ │ │ ├── Track.java
│ │ │ ├── AudioPlayer.java
│ │ │ └── Person.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mscharhag
│ │ └── oleaster
│ │ └── examples
│ │ ├── OleasterIntroductionTest.java
│ │ ├── RunnerExamplesTest.java
│ │ ├── AudioPlayerExampleTest.java
│ │ └── MatcherExamplesTest.java
└── pom.xml
├── oleaster-matcher
├── pom.xml
├── src
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── mscharhag
│ │ │ └── oleaster
│ │ │ └── matcher
│ │ │ ├── TestUtil.java
│ │ │ ├── matchers
│ │ │ ├── BooleanMatcherTest.java
│ │ │ ├── ExceptionMatcherTest.java
│ │ │ ├── StringMatcherTest.java
│ │ │ ├── ObjectMatcherTest.java
│ │ │ ├── IntegerNumberMatcherTest.java
│ │ │ ├── datetime
│ │ │ │ └── DateMatcherTest.java
│ │ │ ├── FloatingPointNumberMatcherTest.java
│ │ │ ├── CollectionMatcherTest.java
│ │ │ └── MapMatcherTest.java
│ │ │ └── MatchersTest.java
│ └── main
│ │ └── java
│ │ └── com
│ │ └── mscharhag
│ │ └── oleaster
│ │ └── matcher
│ │ ├── util
│ │ ├── Arguments.java
│ │ └── Expectations.java
│ │ ├── matchers
│ │ ├── BooleanMatcher.java
│ │ ├── ObjectMatcher.java
│ │ ├── datetime
│ │ │ └── DateMatcher.java
│ │ ├── IntegerNumberMatcher.java
│ │ ├── CollectionMatcher.java
│ │ ├── ExceptionMatcher.java
│ │ ├── StringMatcher.java
│ │ ├── MapMatcher.java
│ │ └── FloatingPointNumberMatcher.java
│ │ └── Matchers.java
└── README.md
├── pom.xml
├── README.md
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .classpath/
3 | .project/
4 | .settings/
5 | .idea/
6 | *.iml
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | script: mvn clean test
3 | after_failure:
4 | - "cat ./target/surefire-reports/*.xml"
5 | jdk:
6 | - oraclejdk8
7 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/PendingInvokable.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | public interface PendingInvokable extends Invokable {}
4 |
--------------------------------------------------------------------------------
/oleaster-examples/src/main/java/com/mscharhag/oleaster/examples/Track.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | public class Track {
4 |
5 | private String title;
6 |
7 | public Track(String title) {
8 | this.title = title;
9 | }
10 |
11 | @Override
12 | public boolean equals(Object o) {
13 | if (this == o) return true;
14 | if (o == null || getClass() != o.getClass()) return false;
15 | return title.equals(((Track)o).title);
16 | }
17 |
18 | @Override
19 | public int hashCode() {
20 | return title.hashCode();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/oleaster-examples/src/main/java/com/mscharhag/oleaster/examples/AudioPlayer.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | import com.mscharhag.oleaster.examples.Track;
4 |
5 | public class AudioPlayer {
6 |
7 | private PlayerState state;
8 | private Track currentlyPlayedTrack;
9 |
10 | public AudioPlayer() {
11 | this.state = PlayerState.stopped;
12 | this.currentlyPlayedTrack = null;
13 | }
14 |
15 | public void play(Track track) {
16 | this.state = PlayerState.playing;
17 | this.currentlyPlayedTrack = track;
18 | }
19 |
20 | public PlayerState getState() {
21 | return this.state;
22 | }
23 |
24 | public Track getCurrentlyPlayedTrack() {
25 | return this.currentlyPlayedTrack;
26 | }
27 |
28 | public static enum PlayerState {
29 | stopped, playing
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/oleaster-examples/src/test/java/com/mscharhag/oleaster/examples/OleasterIntroductionTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | import org.junit.runner.RunWith;
4 | import com.mscharhag.oleaster.runner.OleasterRunner;
5 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
6 | import static com.mscharhag.oleaster.matcher.Matchers.*;
7 |
8 | /**
9 | * This test shows a minimal Oleaster test containing a suite and a spec.
10 | * It is shown in the introduction of the Oleaster documentation.
11 | * If changes in this file are made, the documentation needs be updated.
12 | */
13 | @RunWith(OleasterRunner.class)
14 | public class OleasterIntroductionTest {{
15 | describe("A suite", () -> {
16 | it("contains a spec with an expectation", () -> {
17 | expect(40 + 2).toEqual(42);
18 | });
19 | });
20 | }}
21 |
--------------------------------------------------------------------------------
/oleaster-examples/src/main/java/com/mscharhag/oleaster/examples/Person.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | public class Person {
4 |
5 | private String firstName;
6 | private String lastName;
7 |
8 | public Person(String firstName, String lastName) {
9 | this.firstName = firstName;
10 | this.lastName = lastName;
11 | }
12 |
13 | @Override
14 | public boolean equals(Object o) {
15 | if (this == o) return true;
16 | if (o == null || getClass() != o.getClass()) return false;
17 |
18 | Person person = (Person) o;
19 |
20 | if (!firstName.equals(person.firstName)) return false;
21 | if (!lastName.equals(person.lastName)) return false;
22 |
23 | return true;
24 | }
25 |
26 | @Override
27 | public int hashCode() {
28 | int result = firstName.hashCode();
29 | result = 31 * result + lastName.hashCode();
30 | return result;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/OleasterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner;
17 |
18 | import com.mscharhag.oleaster.runner.suite.SuiteBuilder;
19 |
20 |
21 | public interface OleasterTest {
22 |
23 | public void buildTestSuite(SuiteBuilder sb);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/oleaster-runner/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | com.mscharhag.oleaster
9 | oleaster
10 | 0.2.0
11 |
12 |
13 | com.mscharhag.oleaster
14 | oleaster-runner
15 | jar
16 | 0.2.0
17 | Oleaster Runner
18 | A JUnit runner to write Jasmine like JUnit tests
19 |
20 |
21 |
22 | junit
23 | junit
24 | 4.11
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/oleaster-examples/src/test/java/com/mscharhag/oleaster/examples/RunnerExamplesTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | import com.mscharhag.oleaster.runner.OleasterRunner;
4 | import org.junit.runner.RunWith;
5 |
6 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
7 |
8 | @RunWith(OleasterRunner.class)
9 | public class RunnerExamplesTest {{
10 |
11 | it("describes your test", () -> {
12 | // use assertions to check the expected result
13 | });
14 |
15 | describe("describes your suite", () -> {
16 | // suite implementation
17 | });
18 |
19 | describe("a suite", () -> {
20 | describe("a nested suite", () -> {
21 | // suite implementation
22 | });
23 | });
24 |
25 | describe("beforeEach/afterEach example", () -> {
26 |
27 | beforeEach(() -> {
28 | // runs before every spec in this suite
29 | });
30 |
31 | afterEach(() -> {
32 | // runs after every spec in this suite
33 | });
34 | });
35 | }}
36 |
--------------------------------------------------------------------------------
/oleaster-matcher/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | com.mscharhag.oleaster
8 | oleaster
9 | 0.2.0
10 |
11 |
12 | 4.0.0
13 |
14 | com.mscharhag.oleaster
15 | oleaster-matcher
16 | 0.2.0
17 |
18 | Oleaster Matcher
19 | jar
20 | Jasmine like matchers for Oleaster
21 |
22 |
23 |
24 | com.mscharhag.oleaster
25 | oleaster-runner
26 | 0.1.1
27 | test
28 |
29 |
30 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/FocusedSpecTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | import org.junit.runner.RunWith;
4 |
5 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
6 | import static org.junit.Assert.*;
7 |
8 | @RunWith(OleasterRunner.class)
9 | public class FocusedSpecTest {{
10 | describe("The focused spec", () -> {
11 |
12 | fit("is focused and will run", () -> {
13 | assertTrue(true);
14 | });
15 |
16 | it("is not focused and will not run" , () -> {
17 | fail("fail");
18 | });
19 |
20 | fdescribe("focused describe", () -> {
21 | it("will run", () -> {
22 | assertTrue(true);
23 | });
24 | it("will also run", () -> {
25 | assertTrue(true);
26 | });
27 | });
28 |
29 | fdescribe("another focused describe", () -> {
30 | fit("is focused and will run", () -> {
31 | assertTrue(true);
32 | });
33 |
34 | it("is not focused and will not run", () -> {
35 | fail("fail");
36 | });
37 | });
38 | });
39 | }}
40 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/TestUtil.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher;
2 |
3 | public class TestUtil {
4 |
5 | public static void expectAssertionError(Runnable runnable, String expectedMessage) {
6 | AssertionError assertionError = null;
7 | try {
8 | runnable.run();
9 | } catch (AssertionError ae) {
10 | assertionError = ae;
11 | }
12 | if (assertionError == null) {
13 | throw new AssertionError("passed code block did not throw an AssertionError");
14 | }
15 | if (expectedMessage != null) {
16 | if (!assertionError.getMessage().equals(expectedMessage)) {
17 | throw new AssertionError("Expected message '" + expectedMessage + "' but was '" + assertionError.getMessage() + "'");
18 | }
19 | }
20 | }
21 |
22 | public static void expectAssertionError(Runnable runnable) {
23 | expectAssertionError(runnable, null);
24 | }
25 |
26 | public static Exception catchException(Runnable runnable) {
27 | Exception ex = null;
28 | try {
29 | runnable.run();
30 | } catch (Exception e) {
31 | ex = e;
32 | }
33 | return ex;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/StaticSupportingSuiteBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner;
17 |
18 | import com.mscharhag.oleaster.runner.suite.SuiteBuilder;
19 |
20 | public class StaticSupportingSuiteBuilder extends SuiteBuilder {
21 |
22 | @Override
23 | public void beforeEvaluation() {
24 | super.beforeEvaluation();
25 | StaticRunnerSupport.setSuiteBuilder(this);
26 | }
27 |
28 | @Override
29 | public void afterEvaluation() {
30 | super.afterEvaluation();
31 | StaticRunnerSupport.setSuiteBuilder(null);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/Invokable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner;
17 |
18 | /**
19 | * An Invokable represents a block of code in Oleaster.
20 | * Common Oleaster functions like {@code describe()} or {@code beforeEach()}
21 | * take an Invokable as parameter. These Invokable instances are then evaluated by Oleaster
22 | * to build a set of test suites.
23 | *
24 | *
Invokable is typically implemented using a Lambda expression.
25 | *
26 | * Invokable is similar to {@code Runnable}. The only difference is that Invokable allows you to
27 | * throw checked exceptions.
28 | */
29 | public interface Invokable {
30 |
31 | void invoke() throws Exception;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/util/Arguments.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.util;
17 |
18 | /**
19 | * Internal Utility class for validating arguments
20 | */
21 | public class Arguments {
22 |
23 | public static void ensureNotNull(Object value, String format, Object... args) {
24 | ensureNotNull(value, String.format(format, args));
25 | }
26 |
27 | public static void ensureNotNull(Object value, String message) {
28 | if (value == null) {
29 | throw new IllegalArgumentException(message);
30 | }
31 | }
32 |
33 | public static void ensureTrue(boolean condition, String format, Object... args) {
34 | if (!condition) {
35 | throw new IllegalArgumentException(String.format(format, args));
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/util/Expectations.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.util;
17 |
18 | public class Expectations {
19 |
20 | public static void expectNotNull(Object value, String message) {
21 | if (value == null) {
22 | fail(message);
23 | }
24 | }
25 |
26 |
27 | public static void expectNotNull(Object value, String format, Object... args) {
28 | expectNotNull(value, String.format(format, args));
29 | }
30 |
31 |
32 | public static void expectTrue(boolean condition, String message) {
33 | if (!condition) {
34 | fail(message);
35 | }
36 | }
37 |
38 |
39 | public static void expectTrue(boolean condition, String format, Object... args) {
40 | expectTrue(condition, String.format(format, args));
41 | }
42 |
43 |
44 | public static void fail(String message) {
45 | if (message == null) {
46 | throw new AssertionError();
47 | }
48 | throw new AssertionError(message);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/oleaster-examples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | oleaster
8 | com.mscharhag.oleaster
9 | 0.2.0
10 |
11 |
12 | 4.0.0
13 |
14 | com.mscharhag.oleaster
15 | oleaster-examples
16 | 0.2.0
17 |
18 |
19 |
20 | com.mscharhag.oleaster
21 | oleaster-matcher
22 | 0.1.0
23 | test
24 |
25 |
26 | com.mscharhag.oleaster
27 | oleaster-runner
28 | 0.1.0
29 | test
30 |
31 |
32 | junit
33 | junit
34 | 4.11
35 | test
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-deploy-plugin
44 | 2.8.2
45 |
46 |
47 | true
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/BooleanMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import com.mscharhag.oleaster.matcher.TestUtil;
4 | import com.mscharhag.oleaster.runner.OleasterRunner;
5 | import org.junit.runner.RunWith;
6 |
7 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
8 |
9 | @RunWith(OleasterRunner.class)
10 | public class BooleanMatcherTest {
11 | private BooleanMatcher matcher;
12 | {
13 | describe("BooleanMatcher tests", () -> {
14 |
15 | describe("when the stored value is true", () -> {
16 | beforeEach(() -> {
17 | matcher = new BooleanMatcher(true);
18 | });
19 |
20 | it("is ok if it is checked for 'true' value",() -> {
21 | matcher.toBeTrue();
22 | matcher.toEqual(true);
23 | });
24 |
25 | it("fails if it is checked for 'false' value", () -> {
26 | TestUtil.expectAssertionError(matcher::toBeFalse, "Expected true to be false");
27 | TestUtil.expectAssertionError(() -> matcher.toEqual(false), "Expected true to equal false");
28 | });
29 | });
30 |
31 | describe("when the stored value is false", () -> {
32 | beforeEach(() -> {
33 | matcher = new BooleanMatcher(false);
34 | });
35 |
36 | it("fails if it is checked for 'true' value", () -> {
37 | TestUtil.expectAssertionError(matcher::toBeTrue, "Expected false to be true");
38 | TestUtil.expectAssertionError(() -> matcher.toEqual(true), "Expected false to equal true");
39 | });
40 |
41 | it("is ok if it is checked for 'false' value", () -> {
42 | matcher.toBeFalse();
43 | matcher.toEqual(false);
44 | });
45 | });
46 | });
47 | }}
48 |
--------------------------------------------------------------------------------
/oleaster-examples/src/test/java/com/mscharhag/oleaster/examples/AudioPlayerExampleTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | import com.mscharhag.oleaster.examples.AudioPlayer.PlayerState;
4 | import com.mscharhag.oleaster.runner.OleasterRunner;
5 | import org.junit.runner.RunWith;
6 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
7 | import static com.mscharhag.oleaster.matcher.Matchers.*;
8 |
9 | /**
10 | * This is an Oleaster example test.
11 | * These test is shown in the Oleaster Runner documentation page.
12 | * If this test is updated, the documentation should be updated too.
13 | */
14 | @RunWith(OleasterRunner.class)
15 | public class AudioPlayerExampleTest {
16 | private AudioPlayer player;
17 | {
18 | beforeEach(() -> {
19 | // create a new AudioPlayer instance before each test
20 | player = new AudioPlayer();
21 | });
22 |
23 | it("sets the initial player state to stopped", () -> {
24 | expect(player.getState()).toEqual(PlayerState.stopped);
25 | });
26 |
27 | it("sets the currently played track to null", () -> {
28 | expect(player.getCurrentlyPlayedTrack()).toBeNull();
29 | });
30 |
31 | describe("when a track is played", () -> {
32 | beforeEach(() -> {
33 | // play a track; this is the common precondition for all specs in this suite
34 | player.play(new Track("Run to the hills"));
35 | });
36 |
37 | it("updates the player state to playing", () -> {
38 | expect(player.getState()).toEqual(PlayerState.playing);
39 | });
40 |
41 | it("updates the currently played track", () -> {
42 | Track expectedTrack = new Track("Run to the hills");
43 | expect(player.getCurrentlyPlayedTrack()).toEqual(expectedTrack);
44 | });
45 | });
46 | }}
47 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/suite/SuiteDefinition.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner.suite;
17 |
18 | import com.mscharhag.oleaster.runner.Invokable;
19 | import com.mscharhag.oleaster.runner.PendingInvokable;
20 |
21 | public class SuiteDefinition {
22 |
23 | private Suite parent;
24 | private String description;
25 | private Invokable block;
26 | private boolean parentIsPending;
27 |
28 |
29 | public SuiteDefinition(Suite parent, String description, Invokable block) {
30 | this(parent, description, block, false);
31 | }
32 | public SuiteDefinition(Suite parent, String description, Invokable block, boolean parentIsPending) {
33 | this.parent = parent;
34 | this.description = description;
35 | this.block = block;
36 | this.parentIsPending = parentIsPending;
37 | }
38 |
39 | public Suite getParent() {
40 | return parent;
41 | }
42 |
43 | public String getDescription() {
44 | return description;
45 | }
46 |
47 | public Invokable getBlock() {
48 | return block;
49 | }
50 |
51 | public boolean isPending() {
52 | return block instanceof PendingInvokable || parentIsPending;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/AssertUtil.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | import com.mscharhag.oleaster.runner.suite.Suite;
4 | import com.mscharhag.oleaster.runner.suite.SuiteBuilder;
5 |
6 | import static org.junit.Assert.assertEquals;
7 | import static org.junit.Assert.fail;
8 |
9 | public class AssertUtil {
10 |
11 | public static void assertEmptySuiteBuilderCollections(SuiteBuilder sb) {
12 | assertEquals(0, sb.getSuiteDefinitions().size());
13 | assertEquals(0, sb.getAfterEachHandlers().size());
14 | assertEquals(0, sb.getBeforeEachHandlers().size());
15 | assertEquals(0, sb.getSpecDefinitions().size());
16 | }
17 |
18 |
19 | public static void assertEmptySuiteCollections(Suite suite) {
20 | assertEquals(0, suite.getSpecs().size());
21 | assertEquals(0, suite.getBeforeEachHandlers().size());
22 | assertEquals(0, suite.getAfterEachHandlers().size());
23 | assertEquals(0, suite.getSuites().size());
24 | }
25 |
26 |
27 | public static ExceptionMatcher expect(Invokable block) {
28 | return new ExceptionMatcher(block);
29 | }
30 |
31 |
32 | public static class ExceptionMatcher {
33 | private Invokable block;
34 |
35 | private ExceptionMatcher(Invokable block) {
36 | this.block = block;
37 | }
38 |
39 | public void toFailWith(Class extends Exception> exceptionClass) {
40 | Exception ex = null;
41 | try {
42 | block.invoke();
43 | } catch (Exception e) {
44 | ex = e;
45 | }
46 | if (ex == null) {
47 | fail("Expected exception of type " + exceptionClass.getName() + " but no exception was thrown");
48 | }
49 | if (!ex.getClass().isAssignableFrom(exceptionClass) || !exceptionClass.isAssignableFrom(ex.getClass())) {
50 | fail("Expected exception of type " + exceptionClass.getName() + " but got " + ex.getClass().getName());
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/PendingSpecTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 |
4 | import org.junit.runner.RunWith;
5 |
6 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
7 | import static junit.framework.TestCase.*;
8 |
9 | @RunWith(OleasterRunner.class)
10 | public class PendingSpecTest {{
11 | describe("PendingSpecs", () -> {
12 | describe("with a nested describe", () -> {
13 | it("should be executed", () -> {
14 | assertTrue(true);
15 | });
16 |
17 | xit("should not be executed", () -> {
18 | fail("Pending it (xit) should not be executed!");
19 | });
20 |
21 | describe("a nested describe should be executed", () -> {
22 | it("should be fine", () -> {
23 | assertTrue(true);
24 | });
25 | });
26 |
27 | xdescribe("a pending describe should not be executed", () -> {
28 | it("should not be executed", () -> {
29 | fail("'it' in 'xdescribe' should not be executed!");
30 | });
31 |
32 | describe("a describe with a xdescribe parent", () -> {
33 | it("should not be executed", () -> {
34 | fail("'it' in 'describe' with 'xdescribe' parent should not be executed!");
35 | });
36 |
37 | describe("a describe with a xdescribe grandparent", () -> {
38 | it("should not be executed", () -> {
39 | fail("'it' in 'describe' with 'xdescribe' grandparent should not be executed!");
40 | });
41 | });
42 | });
43 | });
44 | });
45 |
46 | it("should be executed", () -> {
47 | assertTrue(true);
48 | });
49 |
50 | xit("should not be executed", () -> {
51 | fail("Pending it (xit) should not be executed!");
52 | });
53 |
54 | it("another pending style of test");
55 | });
56 | }}
57 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/suite/Spec.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner.suite;
17 |
18 | import java.util.Optional;
19 |
20 | import com.mscharhag.oleaster.runner.Invokable;
21 |
22 | import org.junit.runners.model.Statement;
23 |
24 | public class Spec extends Statement {
25 |
26 | private Suite suite;
27 | private String description;
28 | private Optional block;
29 |
30 | public Spec(Suite suite, String description, Optional block) {
31 | this.suite = suite;
32 | this.description = description;
33 | this.block = block;
34 | }
35 |
36 | public String getDescription() {
37 | return this.description;
38 | }
39 |
40 | public Suite getSuite() {
41 | return suite;
42 | }
43 |
44 | public String getFullDescription() {
45 | String suiteDescription = suite.getFullDescription();
46 | if (suiteDescription != null) {
47 | return String.format("%s, it %s", suiteDescription, this.description);
48 | }
49 | return String.format("it %s", this.description);
50 | }
51 |
52 | @Override
53 | public void evaluate() throws Throwable {
54 | if (block.isPresent()) {
55 | block.get().invoke();
56 | }
57 | }
58 |
59 | public Optional getBlock() {
60 | return block;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/MatchersTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher;
2 |
3 | import com.mscharhag.oleaster.runner.OleasterRunner;
4 | import org.junit.runner.RunWith;
5 |
6 | import java.util.Date;
7 | import java.util.HashMap;
8 | import java.util.LinkedList;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
13 | import static com.mscharhag.oleaster.matcher.Matchers.*;
14 |
15 | @RunWith(OleasterRunner.class)
16 | public class MatchersTest {{
17 |
18 | // Just a few tests to make sure matchers can be accessed via expect() as intended.
19 | // More detailed tests are located in matcher specific test files
20 |
21 | describe("Matchers test", () -> {
22 |
23 | it("IntegerNumberMatcher", () -> {
24 | expect(40 + 2).toEqual(42);
25 | });
26 |
27 | it("FloatingPointNumberMatcher", () -> {
28 | expect(39.666666 + 2.333333).toBeCloseTo(42.0);
29 | });
30 |
31 | it("ExceptionMatcher", () -> {
32 | expect(() -> {
33 | throw new NullPointerException("test");
34 | }).toThrow(NullPointerException.class);
35 | });
36 |
37 | it("BooleanMatcher", () -> {
38 | expect(true).toBeTrue();
39 | });
40 |
41 | it("StringMatcher", () -> {
42 | expect("foo").toEndWith("oo");
43 | });
44 |
45 | it("Date", () -> {
46 | Long millis = System.currentTimeMillis();
47 | Integer delta = 5;
48 | Date date = new Date(millis);
49 | Date dateWithinDelta = new Date(millis+delta-1);
50 | expect(date).toBeCloseTo(dateWithinDelta, delta);
51 | });
52 |
53 | it("CollectionMatcher", () -> {
54 | final List list = new LinkedList<>();
55 | list.add("one");
56 | list.add("two");
57 | expect(list).toContain("one");
58 | });
59 |
60 | it("MapMatcher", () -> {
61 | final Map map = new HashMap<>();
62 | map.put("one", false);
63 | map.put("two", true);
64 | expect(map).toContainKey("one");
65 | expect(map).toContainValue(true);
66 | });
67 | });
68 | }}
69 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/SpecTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | import java.util.Optional;
4 |
5 | import com.mscharhag.oleaster.runner.suite.Spec;
6 | import com.mscharhag.oleaster.runner.suite.Suite;
7 |
8 | import org.junit.runner.RunWith;
9 |
10 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
11 | import static org.junit.Assert.*;
12 |
13 | @RunWith(OleasterRunner.class)
14 | public class SpecTest {
15 |
16 | private Suite suite;
17 | private Spec spec;
18 | private Optional emptyInvokable = Optional.of(() -> {});
19 |
20 | {
21 | describe("Spec", () -> {
22 |
23 | beforeEach(() -> {
24 | suite = new Suite(null, "suite");
25 | });
26 |
27 | describe("when a simple spec is created", () -> {
28 | beforeEach(() -> {
29 | spec = new Spec(suite, "test spec", emptyInvokable);
30 | });
31 |
32 | it("returns the suite", () -> {
33 | assertEquals(suite, spec.getSuite());
34 | });
35 |
36 | it("returns the description", () -> {
37 | assertEquals("test spec", spec.getDescription());
38 | });
39 |
40 | it("returns the suite description followed by the spec description as full description", () -> {
41 | assertEquals("suite, it test spec", spec.getFullDescription());
42 | });
43 | });
44 |
45 | describe("when the suite has a parent suite", () -> {
46 | beforeEach(() -> {
47 | Suite childSuite = new Suite(suite, "child suite");
48 | spec = new Spec(childSuite, "test spec", emptyInvokable);
49 | });
50 |
51 | it("returns the description of all parent suites followed by the spec description as full description", () -> {
52 | assertEquals("suite, child suite, it test spec", spec.getFullDescription());
53 | });
54 | });
55 |
56 | describe("when the suite does not return a description", () -> {
57 | beforeEach(() -> {
58 | suite = new Suite(null, null);
59 | spec = new Spec(suite, "test spec", emptyInvokable);
60 | });
61 |
62 | it("returns only the spec description as full description", () -> {
63 | assertEquals("it test spec", spec.getFullDescription());
64 | });
65 | });
66 | });
67 |
68 | }}
69 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/BooleanMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.matchers;
17 |
18 | import com.mscharhag.oleaster.matcher.util.Expectations;
19 |
20 | /**
21 | * Matcher class to validate boolean values
22 | */
23 | public class BooleanMatcher {
24 |
25 | private boolean value;
26 |
27 | /**
28 | * Creates a new BooleanMatcher that can be used to validate the passed {@code value}.
29 | * @param value the value that should be validated
30 | */
31 | public BooleanMatcher(boolean value) {
32 | this.value = value;
33 | }
34 |
35 | /**
36 | * Checks if the stored value is equal to another value.
37 | *
This method throws an {@code AssertionError} if the stored value is not equal to {@code other}.
38 | * @param other the value to compare with
39 | */
40 | public void toEqual(boolean other) {
41 | Expectations.expectTrue(this.value == other, "Expected %s to equal %s", this.value, other);
42 | }
43 |
44 | /**
45 | * Checks if the stored value is {@code true}.
46 | *
This method throws an {@code AssertionError} if the stored value is {@code false}.
47 | */
48 | public void toBeTrue() {
49 | Expectations.expectTrue(this.value, "Expected %s to be true", this.value);
50 | }
51 |
52 | /**
53 | * Checks if the stored value is {@code false}.
54 | *
This method throws an {@code AssertionError} if the stored value is {@code true}.
55 | */
56 | public void toBeFalse() {
57 | Expectations.expectTrue(!this.value, "Expected %s to be false", this.value);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/ExceptionMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import com.mscharhag.oleaster.matcher.TestUtil;
4 | import com.mscharhag.oleaster.runner.OleasterRunner;
5 | import org.junit.runner.RunWith;
6 |
7 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
8 |
9 | @RunWith(OleasterRunner.class)
10 | public class ExceptionMatcherTest {
11 | private ExceptionMatcher matcher;
12 | {
13 | describe("ExceptionMatcher tests", () -> {
14 |
15 | describe("when the passed code block throws an exception", () -> {
16 | beforeEach(() -> {
17 | matcher = new ExceptionMatcher(() -> {
18 | throw new IllegalArgumentException("test exception");
19 | });
20 | });
21 |
22 | it("fails if it is checked for a different exception class", () -> {
23 | TestUtil.expectAssertionError(() -> matcher.toThrow(NullPointerException.class));
24 | });
25 |
26 | it("is ok if it is checked for the correct exception class", () -> {
27 | matcher.toThrow(IllegalArgumentException.class);
28 | });
29 |
30 | it("is ok if it is checked with the correct exception class and the correct message", () -> {
31 | matcher.toThrow(IllegalArgumentException.class, "test exception");
32 | });
33 |
34 | it("fails if the exception message is not equal to the expected message", () -> {
35 | String expectedMessage = "Expected exception message 'foo' but was 'test exception'";
36 | TestUtil.expectAssertionError(() -> matcher.toThrow(IllegalArgumentException.class, "foo"), expectedMessage);
37 | });
38 |
39 | it("is ok if it is checked for a super class of the thrown exception", () -> {
40 | matcher.toThrow(RuntimeException.class);
41 | });
42 | });
43 |
44 | describe("when the passed code block throws no exception", () -> {
45 | beforeEach(() -> {
46 | matcher = new ExceptionMatcher(() -> {});
47 | });
48 |
49 | it("fails if it is checked for an exception", () -> {
50 | TestUtil.expectAssertionError(() -> matcher.toThrow(IllegalArgumentException.class));
51 | });
52 |
53 | it("fails if it is checked for an exception and a message", () -> {
54 | TestUtil.expectAssertionError(() -> matcher.toThrow(IllegalArgumentException.class, "foo"));
55 | });
56 | });
57 |
58 | });
59 | }}
60 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/ObjectMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.matchers;
17 |
18 |
19 | import static com.mscharhag.oleaster.matcher.util.Expectations.*;
20 |
21 | /**
22 | * Matcher class to validate Objects of type {@code T}.
23 | */
24 | public class ObjectMatcher {
25 |
26 | private T value;
27 |
28 | public ObjectMatcher(T value) {
29 | this.value = value;
30 | }
31 |
32 | /**
33 | * Checks if the stored value is equal to {@code other}.
34 | *
This method throws an {@code AssertionError} if the stored value is not equal to {@code other}.
35 | * @param other the value to compare with
36 | */
37 | public void toEqual(T other) {
38 | if (this.value == null && other == null) {
39 | return;
40 | }
41 | expectNotNull(this.value, "Expected null to be equal '%s'", other);
42 | expectNotNull(other, "Expected '%s' to be equal null", this.value);
43 | expectTrue(this.value.equals(other), "Expected '%s' to be equal '%s'", this.value, other);
44 | }
45 |
46 | /**
47 | * Checks if the stored value is {@code null}.
48 | *
This method throws an {@code AssertionError} if the stored value is {@code null}.
49 | */
50 | public void toBeNull() {
51 | expectTrue(this.value == null, "Expected '%s' to be null", this.value);
52 | }
53 |
54 | /**
55 | * Checks if the stored value is not {@code null}.
56 | *
This method throws an {@code AssertionError} if the stored value is not {@code null}.
57 | */
58 | public void toBeNotNull() {
59 | expectTrue(this.value != null, "Expected null to be not null");
60 | }
61 |
62 | /**
63 | * Checks if the stored value is an instance of the {@code expectedClass}
64 | *
This method throws an {@code AssertionError} if the stored value is not of instance of {@code expectedClass}.
65 | * @param expectedClass The expected class
66 | */
67 | public void toBeInstanceOf(Class> expectedClass) {
68 | expectTrue(expectedClass.isInstance(value), "Expected '%s' to be instance of '%s'", this.value, expectedClass.getName());
69 | }
70 |
71 | protected T getValue() {
72 | return this.value;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/OleasterRunnerTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
4 |
5 | import com.mscharhag.oleaster.runner.suite.Spec;
6 | import com.mscharhag.oleaster.runner.suite.Suite;
7 | import com.mscharhag.oleaster.runner.suite.SuiteBuilder;
8 | import org.junit.runner.RunWith;
9 | import org.junit.runner.notification.RunNotifier;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | import java.util.*;
14 | import java.util.function.Function;
15 | import java.util.stream.Collectors;
16 |
17 | @RunWith(OleasterRunner.class)
18 | public class OleasterRunnerTest {
19 |
20 | private static List calls;
21 | private static Function block = (String name) -> () -> { calls.add(name); };
22 |
23 | private OleasterRunner runner;
24 | private Suite suite;
25 | private List specs;
26 |
27 | public static class TestClass {{
28 | describe("outer describe", () -> {
29 | describe("inner describe", () -> {
30 | it("inner it", block.apply("inner it"));
31 | });
32 | it("outer it", block.apply("outer it"));
33 | });
34 | }}
35 |
36 |
37 | public static class OleasterTestImplementingTestClass implements OleasterTest {
38 |
39 | public static SuiteBuilder suiteBuilder;
40 |
41 | @Override
42 | public void buildTestSuite(SuiteBuilder sb) {
43 | suiteBuilder = sb;
44 | }
45 | }
46 |
47 | {
48 | describe("OleasterRunner", () -> {
49 |
50 | beforeEach(() -> {
51 | calls = new ArrayList<>();
52 | runner = new OleasterRunner(TestClass.class);
53 | });
54 |
55 | it("adds the filtered children to the suite", () -> {
56 | int numberOfChildren = runner.getDescription().getChildren().size();
57 | assertEquals(2, numberOfChildren);
58 | });
59 |
60 | describe("when specs are obtained from the test class using getChildren()", () -> {
61 |
62 | beforeEach(() -> {
63 | specs = runner.getChildren();
64 | });
65 |
66 | it("returns a list that contains all specs", () -> {
67 | List specNames = specs.stream().map(Spec::getDescription).collect(Collectors.toList());
68 | assertEquals(Arrays.asList("outer it", "inner it"), specNames);
69 | });
70 |
71 | describe("when the test class implements OleasterTest", () -> {
72 | beforeEach(() -> {
73 | OleasterTestImplementingTestClass.suiteBuilder = null;
74 | runner = new OleasterRunner(OleasterTestImplementingTestClass.class);
75 | runner.getChildren();
76 | });
77 |
78 | it("calls buildTestSuite() and passes a SuiteBuilder instance", () -> {
79 | assertNotNull(OleasterTestImplementingTestClass.suiteBuilder);
80 | });
81 | });
82 | });
83 | });
84 |
85 | }}
86 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/StringMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import static com.mscharhag.oleaster.matcher.TestUtil.*;
4 | import com.mscharhag.oleaster.runner.OleasterRunner;
5 | import org.junit.runner.RunWith;
6 |
7 | import java.util.regex.Pattern;
8 |
9 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
10 |
11 | @RunWith(OleasterRunner.class)
12 | public class StringMatcherTest {{
13 | describe("StringMatcher test", () -> {
14 |
15 | describe("when toMatch() is called", () -> {
16 | it("fails if the stored value does not match the expected pattern", () -> {
17 | expectAssertionError(() -> new StringMatcher("foo").toMatch(Pattern.compile("bar")),
18 | "Expected 'foo' to match 'bar'");
19 | });
20 |
21 | it("fails if the stored value is null", () -> {
22 | expectAssertionError(() -> new StringMatcher(null).toMatch(Pattern.compile("bar")),
23 | "Expected null to match 'bar'");
24 | });
25 |
26 | it("is ok if the stored value matches the expected pattern", () -> {
27 | new StringMatcher("foo").toMatch(Pattern.compile("fo{2}"));
28 | });
29 |
30 | it("creates a pattern instance if the pattern is passed as string", () -> {
31 | new StringMatcher("foo").toMatch("fo{2}");
32 | });
33 | });
34 |
35 |
36 | describe("when toStartWith() is called", () -> {
37 | it("fails if the stored value is null", () -> {
38 | expectAssertionError(() -> new StringMatcher(null).toStartWith("bar"), "Expected null to start with 'bar'");
39 | });
40 |
41 | it("fails if the stored value does not start with the expected sub string", () -> {
42 | expectAssertionError(() -> new StringMatcher("foo").toStartWith("fee"), "Expected 'foo' to start with 'fee'");
43 | });
44 |
45 | it("is ok if the stored value starts with the expected sub string", () -> {
46 | new StringMatcher("foo").toStartWith("fo");
47 | });
48 | });
49 |
50 |
51 | describe("when toEndWith() is called", () -> {
52 | it("fails if the stored value is null", () -> {
53 | expectAssertionError(() -> new StringMatcher(null).toEndWith("bar"), "Expected null to end with 'bar'");
54 | });
55 |
56 | it("fails if the stored value does not end with the expected sub string", () -> {
57 | expectAssertionError(() -> new StringMatcher("foo").toEndWith("fee"), "Expected 'foo' to end with 'fee'");
58 | });
59 |
60 | it("is ok if the stored value ends with the expected sub string", () -> {
61 | new StringMatcher("foo").toEndWith("oo");
62 | });
63 | });
64 |
65 |
66 | describe("when toContain() is called", () -> {
67 | it("fails if the stored value is null", () -> {
68 | expectAssertionError(() -> new StringMatcher(null).toContain("bar"), "Expected null to contain 'bar'");
69 | });
70 |
71 | it("fails if the stored value does not contain the passed value", () -> {
72 | expectAssertionError(() -> new StringMatcher("foo").toContain("a"), "Expected 'foo' to contain 'a'");
73 | });
74 |
75 | it("is ok if the stored value contains the passed value", () -> {
76 | new StringMatcher("foo").toContain("o");
77 | });
78 | });
79 | });
80 | }}
81 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/suite/SuiteDefinitionEvaluator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner.suite;
17 |
18 | import java.util.Map;
19 | import java.util.Optional;
20 |
21 | import com.mscharhag.oleaster.runner.Invokable;
22 |
23 | public class SuiteDefinitionEvaluator {
24 |
25 |
26 | public Suite evaluate(SuiteDefinition suiteDefinition, SuiteBuilder suiteBuilder) {
27 | this.invokeSuiteDefinition(suiteDefinition, suiteBuilder);
28 | return this.createSuite(suiteDefinition, suiteBuilder);
29 | }
30 |
31 |
32 | private void invokeSuiteDefinition(SuiteDefinition suiteDefinition, SuiteBuilder suiteBuilder) {
33 | suiteBuilder.beforeEvaluation();
34 | try {
35 | suiteDefinition.getBlock().invoke();
36 | } catch (Exception e) {
37 | String message = String.format("An error occurred while evaluating suite '%s'", suiteDefinition.getDescription());
38 | throw new RuntimeException(message, e);
39 | } finally {
40 | suiteBuilder.afterEvaluation();
41 | }
42 | }
43 |
44 |
45 | private Suite createSuite(SuiteDefinition suiteDefinition, SuiteBuilder suiteBuilder) {
46 | Suite suite = new Suite(suiteDefinition.getParent(), suiteDefinition.getDescription());
47 |
48 | suite.addBeforeHandlers(suiteBuilder.getBeforeHandlers());
49 | suite.addBeforeEachHandlers(suiteBuilder.getBeforeEachHandlers());
50 | suite.addAfterEachHandlers(suiteBuilder.getAfterEachHandlers());
51 | suite.addAfterHandlers(suiteBuilder.getAfterHandlers());
52 |
53 | Map> specDefinitions = suiteBuilder.getFocusedSpecDefinitions().size() > 0
54 | ? suiteBuilder.getFocusedSpecDefinitions()
55 | : suiteBuilder.getSpecDefinitions();
56 |
57 | specDefinitions.forEach((description, block) ->
58 | suite.addSpec(new Spec(suite, description, suiteDefinition.isPending() ? Optional.empty() : block)));
59 |
60 | if (suiteBuilder.getFocusedSuiteDefinitions().size() > 0) {
61 | suiteBuilder.getFocusedSuiteDefinitions().forEach((description, block) -> {
62 | SuiteDefinition childSuiteDefinition = new SuiteDefinition(suite, description, block, suiteDefinition.isPending());
63 | suite.addChildSuite(this.evaluate(childSuiteDefinition, suiteBuilder));
64 | });
65 | } else {
66 | suiteBuilder.getSuiteDefinitions().forEach((description, block) -> {
67 | SuiteDefinition childSuiteDefinition = new SuiteDefinition(suite, description, block, suiteDefinition.isPending());
68 | suite.addChildSuite(this.evaluate(childSuiteDefinition, suiteBuilder));
69 | });
70 | }
71 |
72 | return suite;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | org.sonatype.oss
9 | oss-parent
10 | 7
11 |
12 |
13 | com.mscharhag.oleaster
14 | oleaster
15 | pom
16 | 0.2.0
17 | Oleaster
18 |
19 |
20 | oleaster-runner
21 | oleaster-matcher
22 | oleaster-examples
23 |
24 |
25 | https://github.com/mscharhag/oleaster
26 |
27 |
28 | https://github.com/mscharhag/oleaster
29 | scm:git:git://github.com/mscharhag/oleaster.git
30 |
31 |
32 |
33 |
34 | The Apache Software License, Version 2.0
35 | http://www.apache.org/licenses/LICENSE-2.0.txt
36 |
37 |
38 |
39 |
40 |
41 | mscharhag
42 | Michael Scharhag
43 |
44 |
45 |
46 |
47 |
48 | release-sign-artifacts
49 |
50 |
51 | performRelease
52 | true
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-gpg-plugin
60 |
61 |
62 | ${gpg.key}
63 |
64 |
65 |
66 | sign-artifacts
67 | verify
68 |
69 | sign
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | org.apache.maven.plugins
83 | maven-compiler-plugin
84 | 3.1
85 |
86 | 1.8
87 | 1.8
88 |
89 |
90 |
91 | org.apache.maven.plugins
92 | maven-source-plugin
93 |
94 |
95 | attach-sources
96 |
97 | jar
98 |
99 |
100 |
101 |
102 |
103 | org.apache.maven.plugins
104 | maven-javadoc-plugin
105 |
106 |
107 | attach-javadocs
108 |
109 | jar
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/datetime/DateMatcher.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers.datetime;
2 |
3 | import com.mscharhag.oleaster.matcher.matchers.ObjectMatcher;
4 | import com.mscharhag.oleaster.matcher.util.Arguments;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 |
9 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectNotNull;
10 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue;
11 |
12 | public class DateMatcher extends ObjectMatcher {
13 |
14 | protected static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
15 |
16 |
17 | public DateMatcher(Date date) {
18 | super(date);
19 | }
20 |
21 | private String formatDate(Date date) {
22 | return dateFormat.format(date);
23 | }
24 |
25 |
26 | public void toBeBefore(Date otherDate) {
27 | Arguments.ensureNotNull(otherDate, "Cannot test if a date is before null, otherDate cannot be null");
28 | expectNotNull(this.getValue(), "Expected null to be before '%s'", this.formatDate(otherDate));
29 | expectTrue(this.getValue().before(otherDate), "Expected '%s' to be before '%s'",
30 | this.formatDate(this.getValue()), this.formatDate(otherDate));
31 | }
32 |
33 |
34 | public void toBeAfter(Date otherDate) {
35 | Arguments.ensureNotNull(otherDate, "Cannot test if a date is after null, otherDate cannot be null");
36 | expectNotNull(this.getValue(), "Expected null to be after '%s'", this.formatDate(otherDate));
37 | expectTrue(this.getValue().after(otherDate), "Expected '%s' to be after '%s'",
38 | this.formatDate(this.getValue()), this.formatDate(otherDate));
39 | }
40 |
41 |
42 | public void toBeBetween(Date first, Date second) {
43 | Arguments.ensureNotNull(first, "first cannot be null");
44 | Arguments.ensureNotNull(second, "second cannot be null");
45 | Arguments.ensureTrue(first.before(second), "first date needs to be before second date");
46 | expectNotNull(this.getValue(), "Expected null to be between '%s' and '%s'",
47 | this.formatDate(first), this.formatDate(second));
48 | expectTrue((this.getValue().equals(first) || this.getValue().after(first)) && this.getValue().before(second),
49 | "Expected '%s' to be between '%s' and '%s'", this.formatDate(this.getValue()), this.formatDate(first), this.formatDate(second));
50 | }
51 |
52 |
53 | public void toBeCloseTo(Date otherDate, long deltaMillis) {
54 | Arguments.ensureNotNull(otherDate, "otherDate cannot be null");
55 | Arguments.ensureTrue(deltaMillis > 0, "deltaMillis need to be greater than zero");
56 | expectNotNull(this.getValue(), "Expected null to be close to '%s', delta: %sms",
57 | this.formatDate(otherDate), deltaMillis);
58 |
59 | long millis = otherDate.getTime();
60 | Date min = new Date(millis - deltaMillis);
61 | Date max = new Date(millis + deltaMillis);
62 |
63 | expectTrue(this.getValue().after(min) && this.getValue().before(max),
64 | "Expected '%s' to be close to '%s', delta: %sms", this.formatDate(this.getValue()),
65 | this.formatDate(otherDate), deltaMillis);
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Oleaster
2 | =====
3 |
4 | Oleaster allows you to write JUnit tests like you would write [Jasmine](http://jasmine.github.io/) tests.
5 |
6 | An Oleaster JUnit test looks like this:
7 |
8 | ```java
9 | @RunWith(OleasterRunner.class)
10 | public class OleasterIntroductionTest {{
11 | describe("A suite", () -> {
12 | it("contains a spec with an expectation", () -> {
13 | expect(40 + 2).toEqual(42);
14 | });
15 | });
16 | }}
17 | ```
18 |
19 | Oleaster consists out of two independent libraries:
20 |
21 | The [Oleaster JUnit Runner](https://github.com/mscharhag/oleaster/tree/master/oleaster-runner) gives you the option
22 | to write JUnit tests in the format shown above. Java 8 Lambda expressions are used to structure a test in suites
23 | and specifications.
24 |
25 | [Oleaster-Matcher](https://github.com/mscharhag/oleaster/tree/master/oleaster-matcher)
26 | provides Jasmine-like Matchers (`expect(..).toEqual(..)`) to validate test results. These Matchers can be used
27 | as a replacement (or extension) for standard JUnit assertions.
28 |
29 | ## Documentation and examples
30 |
31 | [Oleaster JUnit Runner Documentation](https://github.com/mscharhag/oleaster/blob/master/oleaster-runner/README.md)
32 |
33 | [Oleaster Matcher Documentation](https://github.com/mscharhag/oleaster/blob/master/oleaster-matcher/README.md)
34 |
35 | [Source of the AudioPlayer example](https://github.com/mscharhag/oleaster/blob/master/oleaster-examples/src/test/java/com/mscharhag/oleaster/examples/AudioPlayerExampleTest.java) from the Oleaster Runner documentation.
36 |
37 | Oleaster tests are (mostly) written with Oleaster (see: [Oleaster JUnit Runner Tests](https://github.com/mscharhag/oleaster/tree/master/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner) and [Oleaster Matcher Tests](https://github.com/mscharhag/oleaster/tree/master/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers)).
38 |
39 | Travis CI builds can be found [here](https://travis-ci.org/mscharhag/oleaster)
40 |
41 | ## Maven dependencies
42 | ```xml
43 |
44 |
45 |
46 |
47 | com.mscharhag.oleaster
48 | oleaster-matcher
49 | 0.2.0
50 |
51 |
52 |
53 |
54 | com.mscharhag.oleaster
55 | oleaster-runner
56 | 0.2.0
57 |
58 |
59 |
60 |
61 | junit
62 | junit
63 | 4.11
64 |
65 |
66 |
67 | ```
68 |
69 | ## License
70 |
71 | This software is licensed under the Apache 2 license, quoted below.
72 |
73 | Copyright 2017 Michael Scharhag
74 |
75 | Licensed under the Apache License, Version 2.0 (the "License");
76 | you may not use this file except in compliance with the License.
77 | You may obtain a copy of the License at
78 |
79 | http://www.apache.org/licenses/LICENSE-2.0
80 |
81 | Unless required by applicable law or agreed to in writing, software
82 | distributed under the License is distributed on an "AS IS" BASIS,
83 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
84 | See the License for the specific language governing permissions and
85 | limitations under the License.
86 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/ObjectMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import com.mscharhag.oleaster.runner.OleasterRunner;
4 |
5 | import org.junit.runner.RunWith;
6 |
7 | import static com.mscharhag.oleaster.matcher.TestUtil.*;
8 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
9 |
10 | @RunWith(OleasterRunner.class)
11 | public class ObjectMatcherTest {{
12 | describe("ObjectMatcher tests", () -> {
13 | describe("when toEqual() is called", () -> {
14 | it("fails if both values are not equal", () -> {
15 | expectAssertionError(() -> new ObjectMatcher<>("foo").toEqual("bar"),
16 | "Expected 'foo' to be equal 'bar'");
17 | });
18 |
19 | it("fails if the matcher value is null", () -> {
20 | expectAssertionError(() -> new ObjectMatcher(null).toEqual("bar"),
21 | "Expected null to be equal 'bar'");
22 | });
23 |
24 | it("fails if the passed value is null", () -> {
25 | expectAssertionError(() -> new ObjectMatcher<>("foo").toEqual(null),
26 | "Expected 'foo' to be equal null");
27 | });
28 |
29 | it("is ok if both values are equal", () -> {
30 | new ObjectMatcher<>("foo").toEqual("foo");
31 | });
32 |
33 | it("is ok if the stored value and the expected value are null", () -> {
34 | new ObjectMatcher<>(null).toEqual(null);
35 | });
36 | });
37 |
38 | describe("when toBeNull() is called", () -> {
39 | it("fails if the value is not null", () -> {
40 | expectAssertionError(() -> new ObjectMatcher<>("foo").toBeNull(), "Expected 'foo' to be null");
41 | });
42 |
43 | it("is ok if the value is null", () -> {
44 | new ObjectMatcher<>(null).toBeNull();
45 | });
46 | });
47 |
48 | describe("when toBeNotNull() is called", () -> {
49 | it("fails if the value is null", () -> {
50 | expectAssertionError(() -> new ObjectMatcher<>(null).toBeNotNull(), "Expected null to be not null");
51 | });
52 |
53 | it("is ok if the value is not null", () -> {
54 | new ObjectMatcher<>("foo").toBeNotNull();
55 | });
56 | });
57 |
58 | describe("when toBeInstanceOf() is called", () -> {
59 | it("fails if the value is not of instance of input", () -> {
60 | expectAssertionError(() -> new ObjectMatcher<>("foo").toBeInstanceOf(Integer.class), "Expected 'foo' to be instance of 'java.lang.Integer'");
61 | });
62 |
63 | it("is ok if the value is instance of the expected class", () -> {
64 | new ObjectMatcher<>("foo").toBeInstanceOf(String.class);
65 |
66 | });
67 |
68 | it("is ok if the value is of the same class", () -> {
69 | final Animal john = new Animal("John");
70 | new ObjectMatcher<>(john).toBeInstanceOf(Animal.class);
71 | });
72 |
73 | it("is ok if the value is a child of the parent class", () -> {
74 | final Dog marie = new Dog("Marie");
75 | new ObjectMatcher<>(marie).toBeInstanceOf(Animal.class);
76 | });
77 |
78 | it("is not ok if the value is a parent of the child class", () -> {
79 | final Animal bo = new Animal("Bo");
80 | expectAssertionError(() -> new ObjectMatcher<>(bo).toBeInstanceOf(Dog.class), "Expected '" + bo + "' to be instance of '" + Dog.class.getName() + "'");
81 | });
82 |
83 | });
84 | });
85 | }
86 | public static class Animal {
87 | public final String name;
88 | public Animal(final String name) {
89 | this.name = name;
90 | }
91 | }
92 | public static class Dog extends Animal {
93 | public Dog(final String name) {
94 | super(name);
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/IntegerNumberMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.matchers;
17 |
18 | import com.mscharhag.oleaster.matcher.util.Arguments;
19 | import com.mscharhag.oleaster.matcher.util.Expectations;
20 |
21 | /**
22 | * Matcher class to validate integer numbers
23 | */
24 | public class IntegerNumberMatcher {
25 |
26 | private long value;
27 |
28 | /**
29 | * Creates a new matcher object that stores the passed value.
30 | * @param value the value to store
31 | */
32 | public IntegerNumberMatcher(long value) {
33 | this.value = value;
34 | }
35 |
36 | /**
37 | * Checks if the stored value is equal to another value.
38 | *
This method throws an {@code AssertionError} if the stored value is not equal to {@code other}.
39 | *
40 | * @param other the value to compare with
41 | */
42 | public void toEqual(long other) {
43 | Expectations.expectTrue(this.value == other, "Expected %s to equal %s", this.value, other);
44 | }
45 |
46 | /**
47 | * Checks if the stored value is greater than another value.
48 | *
This method throws an {@code AssertionError} if:
49 | *
50 | *
the stored value is smaller than {@code other}
51 | *
the stored value is equal to {@code other}
52 | *
53 | * @param other the value to compare with
54 | */
55 | public void toBeGreaterThan(long other) {
56 | Expectations.expectTrue(this.value > other, "Expected %s to be greater than %s", this.value, other);
57 | }
58 |
59 | /**
60 | * Checks if the stored value is smaller than another value.
61 | *
This method throws an {@code AssertionError} if:
62 | *
63 | *
the stored value is greater than {@code other}
64 | *
the stored value is equal to {@code other}
65 | *
66 | * @param other the value to compare with
67 | */
68 | public void toBeSmallerThan(long other) {
69 | Expectations.expectTrue(this.value < other, "Expected %s to be smaller than %s", this.value, other);
70 | }
71 |
72 | /**
73 | * Checks if the stored value is between a lower and an upper bound.
74 | *
This method throws an {@code AssertionError} if:
75 | *
76 | *
the stored value is smaller than the lower bound
77 | *
the stored value is greater than the upper bound
78 | *
79 | *
It is ok if the stored value is equal to the lower or the upper bound.
80 | * @param lower the lower bound
81 | * @param upper the upper bound
82 | * @throws java.lang.IllegalArgumentException if {@code lower} is not smaller than {@code upper}
83 | */
84 | public void toBeBetween(long lower, long upper) {
85 | Arguments.ensureTrue(lower < upper, "upper has to be greater than lower");
86 | boolean isBetween = this.value >= lower && this.value <= upper;
87 | Expectations.expectTrue(isBetween, "Expected %s to be between %s and %s", this.value, lower, upper);
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/IntegerNumberMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import com.mscharhag.oleaster.matcher.TestUtil;
4 | import com.mscharhag.oleaster.matcher.matchers.IntegerNumberMatcher;
5 | import com.mscharhag.oleaster.runner.OleasterRunner;
6 | import static org.junit.Assert.*;
7 | import org.junit.runner.RunWith;
8 |
9 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
10 |
11 | @RunWith(OleasterRunner.class)
12 | public class IntegerNumberMatcherTest {{
13 | describe("IntegerNumberMatcher tests", () -> {
14 |
15 | describe("when toEqual() is called", () -> {
16 | it("fails if both values are not equal", () -> {
17 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toEqual(2), "Expected 1 to equal 2");
18 | });
19 |
20 | it("is ok if both values are equal", () -> {
21 | new IntegerNumberMatcher(1).toEqual(1);
22 | });
23 | });
24 |
25 | describe("when toBeGreaterThan() is called", () -> {
26 | it("fails if the passed value greater", () -> {
27 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toBeGreaterThan(2),
28 | "Expected 1 to be greater than 2");
29 | });
30 |
31 | it("fails if both values are equal", () -> {
32 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toBeGreaterThan(1),
33 | "Expected 1 to be greater than 1");
34 | });
35 |
36 | it("is ok if the passed value is smaller", () -> {
37 | new IntegerNumberMatcher(1).toBeGreaterThan(0);
38 | });
39 | });
40 |
41 | describe("when toBeSmallerThan() is called", () -> {
42 | it("fails if the passed value smaller", () -> {
43 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toBeSmallerThan(0),
44 | "Expected 1 to be smaller than 0");
45 | });
46 |
47 | it("fails if both values are equal", () -> {
48 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toBeSmallerThan(1),
49 | "Expected 1 to be smaller than 1");
50 | });
51 |
52 | it("is ok if the passed value is greater", () -> {
53 | new IntegerNumberMatcher(1).toBeSmallerThan(2);
54 | });
55 | });
56 |
57 | describe("when toBeBetween() is called", () -> {
58 | it("throws an exception if the lower bound is greater than the upper bound", () -> {
59 | Exception ex = TestUtil.catchException(() -> new IntegerNumberMatcher(1).toBeBetween(2, 1));
60 | assertTrue(ex instanceof IllegalArgumentException);
61 | });
62 |
63 | it("throws an exception if the lower bound is equal to the upper bound", () -> {
64 | Exception ex = TestUtil.catchException(() -> new IntegerNumberMatcher(1).toBeBetween(2, 2));
65 | assertTrue(ex instanceof IllegalArgumentException);
66 | });
67 |
68 | it("fails if the value smaller than the lower bound", () -> {
69 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(1).toBeBetween(2, 3),
70 | "Expected 1 to be between 2 and 3");
71 | });
72 |
73 | it("is ok if the value is equal to the lower bound", () -> {
74 | new IntegerNumberMatcher(1).toBeBetween(1, 2);
75 | });
76 |
77 | it("is ok if the value is greater than the lower bound and lower than the upper bound", () -> {
78 | new IntegerNumberMatcher(1).toBeBetween(0, 2);
79 | });
80 |
81 | it("is ok if the value is equal to the upper bound", () -> {
82 | new IntegerNumberMatcher(1).toBeBetween(0, 1);
83 | });
84 |
85 | it("fails if the value is greater than the upper bound", () -> {
86 | TestUtil.expectAssertionError(() -> new IntegerNumberMatcher(2).toBeBetween(0, 1),
87 | "Expected 2 to be between 0 and 1");
88 | });
89 | });
90 |
91 | });
92 | }}
93 |
--------------------------------------------------------------------------------
/oleaster-examples/src/test/java/com/mscharhag/oleaster/examples/MatcherExamplesTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.examples;
2 |
3 | import com.mscharhag.oleaster.runner.OleasterRunner;
4 | import org.junit.runner.RunWith;
5 |
6 | import java.util.regex.Pattern;
7 |
8 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
9 | import static com.mscharhag.oleaster.matcher.Matchers.*;
10 |
11 | /**
12 | * This test shows how Oleaster Matchers can be used.
13 | * These examples are also shown on the Oleaster Matcher documentation page.
14 | * If changes in this file are made, the documentation needs be updated.
15 | */
16 | @RunWith(OleasterRunner.class)
17 | public class MatcherExamplesTest {{
18 |
19 | it("gives a quick overview", () -> {
20 |
21 | // same as JUnit's assertEquals(40 + 2, 42)
22 | expect(40 + 2).toEqual(42);
23 |
24 | // see if a String matches a regular expression
25 | expect("foobar").toMatch("fo{2}\\w+");
26 |
27 | // test exceptions with Java 8 Lambdas
28 | expect(() -> {
29 | // code that throws IllegalArgumentException
30 | throw new IllegalArgumentException();
31 | }).toThrow(IllegalArgumentException.class);
32 | });
33 |
34 | it("shows how numbers can be validated", () -> {
35 | int value = 42;
36 |
37 | // check for exact value
38 | expect(value).toEqual(42);
39 |
40 | // check for greater/smaller values
41 | expect(value).toBeGreaterThan(41);
42 | expect(value).toBeSmallerThan(43);
43 |
44 | // check for range
45 | expect(value).toBeBetween(40, 45);
46 |
47 | // floating point number can often not be precisely represented by float/double values.
48 | // So make sure to compare them with an absolute error (delta)
49 | expect(42.0000001).toBeCloseTo(42); // uses default delta of 0.00001
50 | expect(42.0000001).toBeCloseTo(42, 0.000001);
51 | });
52 |
53 | it("shows how boolean values can be validated", () -> {
54 | boolean value = true;
55 |
56 | // check for a given parameter
57 | expect(value).toEqual(true);
58 |
59 | // check if true
60 | expect(value).toBeTrue();
61 |
62 | // check if false
63 | value = false;
64 | expect(value).toBeFalse();
65 | });
66 |
67 | it("shows how Objects can be validated", () -> {
68 | Person person = new Person("John", "Smith");
69 |
70 | // check for equality, delegates to Person.equals()
71 | expect(person).toEqual(new Person("John", "Smith"));
72 |
73 | // check if not null
74 | expect(person).toBeNotNull();
75 |
76 | // check if null
77 | person = null;
78 | expect(person).toBeNull();
79 | });
80 |
81 | it("shows how Strings can be validated", () -> {
82 | // check for exact value
83 | expect("foo").toEqual("foo");
84 |
85 | // check string starting/ending
86 | expect("foobar").toStartWith("foo");
87 | expect("foobar").toEndWith("bar");
88 |
89 | // check if a String contains a given substring
90 | expect("foobar").toContain("oba");
91 |
92 | // check if a String matches a regular expression
93 | expect("foobar").toMatch(Pattern.compile("fo+\\w*"));
94 | expect("foobar").toMatch("fo+\\w*");
95 | });
96 |
97 | it("shows how Exceptions can be tested", () -> {
98 | // check if an exception is thrown
99 | expect(() -> {
100 | // code that throws IllegalArgumentException
101 | throw new IllegalArgumentException();
102 | }).toThrow(IllegalArgumentException.class);
103 |
104 | // with exception message
105 | expect(() -> {
106 | // code that throws IllegalArgumentException
107 | throw new IllegalArgumentException("An argument is invalid");
108 | }).toThrow(IllegalArgumentException.class, "An argument is invalid");
109 | });
110 |
111 | }}
112 |
--------------------------------------------------------------------------------
/oleaster-runner/src/main/java/com/mscharhag/oleaster/runner/suite/Suite.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.runner.suite;
17 |
18 | import com.mscharhag.oleaster.runner.Invokable;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | public class Suite {
24 |
25 | private Suite parent;
26 | private String description;
27 |
28 | private List suites = new ArrayList<>();
29 | private List specs = new ArrayList<>();
30 | private List beforeEachHandlers = new ArrayList<>();
31 | private List beforeHandlers = new ArrayList<>();
32 | private List afterEachHandlers = new ArrayList<>();
33 | private List afterHandlers = new ArrayList<>();
34 |
35 |
36 | public Suite(Suite parent, String description) {
37 | this.parent = parent;
38 | this.description = description;
39 | }
40 |
41 | public void addBeforeEachHandler(Invokable block) {
42 | beforeEachHandlers.add(block);
43 | }
44 |
45 | public void addAfterEachHandler(Invokable block) {
46 | afterEachHandlers.add(block);
47 | }
48 |
49 | public void addBeforeEachHandlers(List calls) {
50 | this.beforeEachHandlers.addAll(calls);
51 | }
52 |
53 | public void addBeforeHandler(Invokable block) {
54 | this.beforeHandlers.add(block);
55 | }
56 |
57 | public void addBeforeHandlers(List calls) {
58 | this.beforeHandlers.addAll(calls);
59 | }
60 |
61 | public void addAfterHandler(Invokable block) {
62 | this.afterHandlers.add(block);
63 | }
64 |
65 | public void addAfterHandlers(List calls) {
66 | this.afterHandlers.addAll(calls);
67 | }
68 |
69 | public void addAfterEachHandlers(List calls) {
70 | this.afterEachHandlers.addAll(calls);
71 | }
72 |
73 | public void addSpec(Spec spec) {
74 | this.specs.add(spec);
75 | }
76 |
77 | public Suite getParent() {
78 | return parent;
79 | }
80 |
81 | public void addChildSuite(Suite child) {
82 | this.suites.add(child);
83 | }
84 |
85 | public List getBeforeEachHandlers() {
86 | return this.beforeEachHandlers;
87 | }
88 |
89 | public List getBeforeHandlers() {
90 | return this.beforeHandlers;
91 | }
92 |
93 | public List getAfterEachHandlers() {
94 | return this.afterEachHandlers;
95 | }
96 |
97 | public List getAfterHandlers() {
98 | return this.afterHandlers;
99 | }
100 |
101 |
102 | public List collectSpecs() {
103 | List allSpecs = new ArrayList<>(this.specs);
104 | this.suites.forEach(suite -> allSpecs.addAll(suite.collectSpecs()));
105 | return allSpecs;
106 | }
107 |
108 | public String getDescription() {
109 | return description;
110 | }
111 |
112 | public String getFullDescription() {
113 | if (this.parent != null) {
114 | String parentDescription = this.parent.getFullDescription();
115 | if (parentDescription != null) {
116 | return String.format("%s, %s", parentDescription, this.description);
117 | }
118 | }
119 | return this.description;
120 | }
121 |
122 | public List getSuites() {
123 | return suites;
124 | }
125 |
126 | public List getSpecs() {
127 | return specs;
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import com.mscharhag.oleaster.matcher.util.Arguments;
4 |
5 | import java.util.Collection;
6 |
7 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectNotNull;
8 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue;
9 |
10 | /**
11 | * Matcher class to validate Collections
12 | */
13 | public class CollectionMatcher extends ObjectMatcher {
14 |
15 | public CollectionMatcher(final Collection value) {
16 | super(value);
17 | }
18 |
19 | /**
20 | * Checks if the stored {@code Collection} contains the provided item.
21 | *
This method throws an {@code AssertionError} if
22 | *
23 | *
the stored {@code Collection} does not contain the passed {@code item}
24 | *
the given {@code Collection} is {@code null}
25 | *
26 | * @param item the {@code Object} which the {@code Collection} should contain.
27 | */
28 | public void toContain(final Object item) {
29 | expectNotNull(this.getValue(), "Expected null to contain '%s'", item);
30 | expectTrue(this.getValue().contains(item), "Expected '%s' to contain '%s'", this.getValue(), item);
31 | }
32 |
33 | /**
34 | * Checks if the stored {@code Collection} does not contain the provided item.
35 | *
This method throws an {@code AssertionError} if
36 | *
37 | *
the stored {@code Collection} does contain the passed {@code item}
38 | *
the given {@code Collection} is {@code null}
39 | *
40 | * @param item the {@code Object} which the {@code Collection} should not contain.
41 | */
42 | public void toNotContain(final Object item) {
43 | expectNotNull(this.getValue(), "Expected null to not contain '%s'", item);
44 | expectTrue(!this.getValue().contains(item), "Expected '%s' to not contain '%s'", this.getValue(), item);
45 | }
46 |
47 | /**
48 | * Checks if the given {@code Collection} is empty
49 | *
This method throws an {@code AssertionError} if
50 | *
51 | *
the stored {@code Collection} is not empty
52 | *
the given {@code Collection} is {@code null}
53 | *
54 | */
55 | public void toBeEmpty() {
56 | expectNotNull(this.getValue(), "Expected null to be empty");
57 | expectTrue(this.getValue().isEmpty(), "Expected '%s' to be empty", this.getValue());
58 | }
59 |
60 | /**
61 | * Checks if the given {@code Collection} is not empty
62 | *
This method throws an {@code AssertionError} if
63 | *
64 | *
the stored {@code Collection} is empty
65 | *
the given {@code Collection} is {@code null}
66 | *
67 | */
68 | public void toNotBeEmpty() {
69 | expectNotNull(this.getValue(), "Expected null to not be empty");
70 | expectTrue(!this.getValue().isEmpty(), "Expected '%s' to not be empty", this.getValue());
71 | }
72 |
73 | /**
74 | * Checks if the given {@code Collection} has the expected size
75 | *
This method throws an {@code AssertionError} if
76 | *
77 | *
the stored {@code Collection} has a different size than the passed value
78 | *
the stored {@code Collection} is {@code null}
79 | *
80 | * @param size The expected size of the collection
81 | */
82 | public void toHaveSize(final int size) {
83 | expectNotNull(this.getValue(), "Expected null to have size '%d'", size);
84 | expectTrue(this.getValue().size() == size, "Expected '%s' to have a size of %d, " +
85 | "instead has a size of %d", this.getValue(), size, this.getValue().size());
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/oleaster-runner/src/test/java/com/mscharhag/oleaster/runner/SuiteBuilderTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.runner;
2 |
3 | import java.util.Arrays;
4 | import java.util.Optional;
5 |
6 | import com.mscharhag.oleaster.runner.suite.SuiteBuilder;
7 |
8 | import org.junit.runner.RunWith;
9 |
10 | import static com.mscharhag.oleaster.runner.AssertUtil.*;
11 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
12 | import static org.junit.Assert.*;
13 |
14 | @RunWith(OleasterRunner.class)
15 | public class SuiteBuilderTest {
16 |
17 | private Invokable invokable = () -> {};
18 | private SuiteBuilder suiteBuilder;
19 | {
20 | describe("SuiteBuilder", () -> {
21 |
22 | beforeEach(() -> {
23 | suiteBuilder = new SuiteBuilder();
24 | });
25 |
26 | it("initialize new SuiteBuilders with empty collections", () -> {
27 | assertEmptySuiteBuilderCollections(suiteBuilder);
28 | });
29 |
30 | it("empties existing collections before an evaluation", () -> {
31 | // fill collections
32 | suiteBuilder.describe("test suite", invokable);
33 | suiteBuilder.before(invokable);
34 | suiteBuilder.beforeEach(invokable);
35 | suiteBuilder.afterEach(invokable);
36 | suiteBuilder.after(invokable);
37 | suiteBuilder.it("test spec", invokable);
38 |
39 | suiteBuilder.beforeEvaluation();
40 | assertEmptySuiteBuilderCollections(suiteBuilder);
41 | });
42 |
43 | describe("when a suite is created", () -> {
44 | beforeEach(() -> {
45 | suiteBuilder.describe("new suite", invokable);
46 | });
47 |
48 | it("adds a single suite definition", () -> {
49 | assertEquals(1, suiteBuilder.getSuiteDefinitions().size());
50 | });
51 |
52 | it("returns the new suite definition", () -> {
53 | assertEquals(invokable, suiteBuilder.getSuiteDefinitions().get("new suite"));
54 | });
55 |
56 | it("is not possible to add another suite with the same name", () -> {
57 | expect(() -> {
58 | suiteBuilder.describe("new suite", invokable);
59 | }).toFailWith(IllegalArgumentException.class);
60 | });
61 | });
62 |
63 | describe("when a spec is created", () -> {
64 | beforeEach(() -> {
65 | suiteBuilder.it("new spec", invokable);
66 | });
67 |
68 | it("returns the new spec", () -> {
69 | assertEquals(Optional.of(invokable), suiteBuilder.getSpecDefinitions().get("new spec"));
70 | });
71 |
72 | it("is not possible to add another spec with the same name", () -> {
73 | expect(() -> {
74 | suiteBuilder.it("new spec", invokable);
75 | }).toFailWith(IllegalArgumentException.class);
76 | });
77 | });
78 |
79 | describe("when a beforeEach handler is added", () -> {
80 | beforeEach(() -> {
81 | suiteBuilder.beforeEach(invokable);
82 | });
83 |
84 | it("returns the newly added handler", () -> {
85 | assertEquals(Arrays.asList(invokable), suiteBuilder.getBeforeEachHandlers());
86 | });
87 | });
88 |
89 | describe("when a before handler is added", () -> {
90 | beforeEach(() -> {
91 | suiteBuilder.before(invokable);
92 | });
93 |
94 | it("returns the newly added handler", () -> {
95 | assertEquals(Arrays.asList(invokable), suiteBuilder.getBeforeHandlers());
96 | });
97 | });
98 |
99 | describe("when a afterEach handler is added", () -> {
100 | beforeEach(() -> {
101 | suiteBuilder.afterEach(invokable);
102 | });
103 |
104 | it("returns the newly added handler", () -> {
105 | assertEquals(Arrays.asList(invokable), suiteBuilder.getAfterEachHandlers());
106 | });
107 | });
108 |
109 | describe("when a after handler is added", () -> {
110 | beforeEach(() -> {
111 | suiteBuilder.after(invokable);
112 | });
113 |
114 | it("returns the newly added handler", () -> {
115 | assertEquals(Arrays.asList(invokable), suiteBuilder.getAfterHandlers());
116 | });
117 | });
118 | });
119 | }}
120 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/ExceptionMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.matchers;
17 |
18 | import com.mscharhag.oleaster.matcher.util.Arguments;
19 | import com.mscharhag.oleaster.matcher.util.Expectations;
20 |
21 | /**
22 | * Matcher class to validate if a code block throws an exception.
23 | */
24 | public class ExceptionMatcher {
25 |
26 | private Exception exception;
27 |
28 | /**
29 | * Creates a new {@code ExceptionMatcher} that runs the passed {@code CodeBlock}.
30 | * @param block the code block that should throw an exception
31 | */
32 | public ExceptionMatcher(CodeBlock block) {
33 | this.exception = null;
34 | this.runCodeBlock(block);
35 | }
36 |
37 | /**
38 | * Checks if an exception of type {@code expectedExceptionClass} was thrown.
39 | *
This method throws an {@code AssertionError} if:
40 | *
41 | *
no exception was thrown.
42 | *
the thrown exception is not an instance of {@code expectedExceptionClass}
43 | *
44 | * @param expectedExceptionClass the expected exception
45 | */
46 | public void toThrow(Class expectedExceptionClass) {
47 | Arguments.ensureNotNull(expectedExceptionClass, "expectedExceptionClass cannot be null");
48 | String expectedExceptionName = expectedExceptionClass.getName();
49 | Expectations.expectNotNull(this.exception, "Expected code block to throw %s but it did not throw an exception",
50 | expectedExceptionName);
51 | String exceptionName = this.exception.getClass().getName();
52 | Expectations.expectTrue(expectedExceptionClass.isInstance(this.exception),
53 | "Expected code block to throw %s but it did throw %s", expectedExceptionName, exceptionName);
54 | }
55 |
56 | /**
57 | * Checks if an exception of type {@code expectedExceptionClass} with message {@code expectedMessage} was thrown.
58 | *
This method throws an {@code AssertionError} if:
59 | *
60 | *
no exception was thrown.
61 | *
the thrown exception is not an instance of {@code expectedExceptionClass}
62 | *
the message of the thrown exception is not equal {@code expectedMessage}
{@code CodeBlock} is a functional interface that is typically implemented using
86 | * Java 8 Lambda expressions. Its single method {@code run()} can throw checked exceptions.
87 | */
88 | public static interface CodeBlock {
89 | public void run() throws Exception;
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/datetime/DateMatcherTest.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers.datetime;
2 |
3 | import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
4 |
5 | import com.mscharhag.oleaster.matcher.TestUtil;
6 | import com.mscharhag.oleaster.runner.OleasterRunner;
7 | import org.junit.runner.RunWith;
8 |
9 | import java.text.ParseException;
10 | import java.text.SimpleDateFormat;
11 | import java.util.Date;
12 |
13 | @RunWith(OleasterRunner.class)
14 | public class DateMatcherTest {
15 | private DateMatcher matcher;
16 |
17 | private Date date(String text) {
18 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
19 | try {
20 | return format.parse(text);
21 | } catch (ParseException e) {
22 | throw new RuntimeException(e);
23 | }
24 | }
25 | {
26 | describe("DateMatcher tests", () -> {
27 | beforeEach(() -> {
28 | matcher = new DateMatcher(date("2015-06-05 12:30:00"));
29 | });
30 |
31 |
32 | describe("when toBeBefore() is called", () -> {
33 | it("is ok if the stored date is before the passed date", () -> {
34 | matcher.toBeBefore(date("2015-06-05 12:31:00"));
35 | });
36 |
37 | it("fails if the stored date is equal to the passed date", () -> {
38 | TestUtil.expectAssertionError(() -> matcher.toBeBefore(date("2015-06-05 12:30:00")),
39 | "Expected '2015-06-05 12:30:00.000' to be before '2015-06-05 12:30:00.000'");
40 | });
41 |
42 | it("fails if the stored date is after the passed date", () -> {
43 | TestUtil.expectAssertionError(() -> matcher.toBeBefore(date("2015-06-05 12:29:00")),
44 | "Expected '2015-06-05 12:30:00.000' to be before '2015-06-05 12:29:00.000'");
45 | });
46 | });
47 |
48 | describe("when toBeAfter() is called", () -> {
49 | it("is ok if the stored date is after the passed date", () -> {
50 | matcher.toBeAfter(date("2015-06-05 12:29:00"));
51 | });
52 |
53 | it("fails if the stored date is equal to the passed date", () -> {
54 | TestUtil.expectAssertionError(() -> matcher.toBeAfter(date("2015-06-05 12:30:00")),
55 | "Expected '2015-06-05 12:30:00.000' to be after '2015-06-05 12:30:00.000'");
56 | });
57 |
58 | it("fails if the stored date is before the passed date", () -> {
59 | TestUtil.expectAssertionError(() -> matcher.toBeAfter(date("2015-06-05 12:31:00")),
60 | "Expected '2015-06-05 12:30:00.000' to be after '2015-06-05 12:31:00.000'");
61 | });
62 | });
63 |
64 | describe("when toBeBetween() is called", () -> {
65 | it("is ok if the stored date is between the passed values", () -> {
66 | matcher.toBeBetween(date("2015-06-05 12:29:00"), date("2015-06-05 12:31:00"));
67 | });
68 |
69 | it("is ok if the stored date is equal to the lower value", () -> {
70 | matcher.toBeBetween(date("2015-06-05 12:30:00"), date("2015-06-05 12:31:00"));
71 | });
72 |
73 | it("fails if the stored date is before the lower value", () -> {
74 | TestUtil.expectAssertionError(() -> matcher.toBeBetween(date("2015-06-05 12:31:00"), date("2015-06-05 12:32:00")),
75 | "Expected '2015-06-05 12:30:00.000' to be between '2015-06-05 12:31:00.000' and '2015-06-05 12:32:00.000'");
76 | });
77 |
78 | it("fails if the stored date is after the upper value", () -> {
79 | TestUtil.expectAssertionError(() -> matcher.toBeBetween(date("2015-06-05 12:28:00"), date("2015-06-05 12:29:00")),
80 | "Expected '2015-06-05 12:30:00.000' to be between '2015-06-05 12:28:00.000' and '2015-06-05 12:29:00.000'");
81 | });
82 | });
83 |
84 | describe("when toBeCloseTo() is called", () -> {
85 | it("is ok if the stored date is closed to the passed value", () -> {
86 | matcher.toBeCloseTo(date("2015-06-05 12:29:59"), 1001);
87 | });
88 |
89 | it("fails if the stored date is not close to the passed value", () -> {
90 | TestUtil.expectAssertionError(() -> matcher.toBeCloseTo(date("2015-06-05 12:29:59"), 999),
91 | "Expected '2015-06-05 12:30:00.000' to be close to '2015-06-05 12:29:59.000', delta: 999ms");
92 | });
93 | });
94 | });
95 |
96 | }}
97 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/StringMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Michael Scharhag
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.mscharhag.oleaster.matcher.matchers;
17 |
18 | import com.mscharhag.oleaster.matcher.util.Arguments;
19 | import static com.mscharhag.oleaster.matcher.util.Expectations.*;
20 |
21 | import java.util.regex.Pattern;
22 |
23 | /**
24 | * Matcher class to validate Strings
25 | */
26 | public class StringMatcher extends ObjectMatcher {
27 |
28 |
29 | public StringMatcher(String value) {
30 | super(value);
31 | }
32 |
33 | /**
34 | * Checks if the stored {@code String} matches a regular expression.
35 | *
This method throws an {@code AssertionError} if
36 | *
37 | *
the stored {@code String} does not match the passed {@code pattern}
38 | *
the stored {@code String} is {@code null}
39 | *
40 | * @param pattern the {@code Pattern} representing the regular expression
41 | * @throws java.lang.IllegalArgumentException if {@code pattern} is {@code null}.
42 | */
43 | public void toMatch(Pattern pattern) {
44 | Arguments.ensureNotNull(pattern, "pattern cannot be null");
45 | expectNotNull(this.getValue(), "Expected null to match '%s'", pattern.pattern());
46 | expectTrue(pattern.matcher(this.getValue()).matches(),
47 | "Expected '%s' to match '%s'", this.getValue(), pattern.pattern());
48 | }
49 |
50 | /**
51 | * Checks if the stored {@code String} matches a regular expression.
52 | *
This method is similar to {@link #toMatch(java.util.regex.Pattern)} except it takes the regular expression as
53 | * String. The passed String {@code pattern} will be compiled to a {@link java.util.regex.Pattern} using
54 | * {@code Pattern.compile()}.
55 | * @param pattern a {@code String} containing the regular expression.
56 | */
57 | public void toMatch(String pattern) {
58 | this.toMatch(Pattern.compile(pattern));
59 | }
60 |
61 | /**
62 | * Checks if the stored {@code String} starts with a given sub string.
63 | *
This method throws an {@code AssertionError} if
64 | *
65 | *
the stored {@code String} does not start with {@code start}
66 | *
the stored {@code String} is {@code null}
67 | *
68 | * @throws java.lang.IllegalArgumentException if {@code start} is {@code null}.
69 | * @param start the sub string
70 | */
71 | public void toStartWith(String start) {
72 | Arguments.ensureNotNull(start, "start cannot be null");
73 | expectNotNull(this.getValue(), "Expected null to start with '%s'", start);
74 | expectTrue(this.getValue().startsWith(start), "Expected '%s' to start with '%s'", this.getValue(), start);
75 | }
76 |
77 | /**
78 | * Checks if the stored {@code String} ends with a given sub string.
79 | *
This method throws an {@code AssertionError} if
80 | *
81 | *
the stored {@code String} does not end with {@code end}
82 | *
the stored {@code String} is {@code null}
83 | *
84 | * @throws java.lang.IllegalArgumentException if {@code end} is {@code null}.
85 | * @param end the sub string
86 | */
87 | public void toEndWith(String end) {
88 | Arguments.ensureNotNull(end, "end cannot be null");
89 | expectNotNull(this.getValue(), "Expected null to end with '%s'", end);
90 | expectTrue(this.getValue().endsWith(end), "Expected '%s' to end with '%s'", this.getValue(), end);
91 | }
92 |
93 |
94 | /**
95 | * Checks if the stored {@code String} contains a given sub string.
96 | *
This method throws an {@code AssertionError} if
97 | *
98 | *
the stored {@code String} does not contain {@code substr}
99 | *
the stored {@code String} is {@code null}
100 | *
101 | * @throws java.lang.IllegalArgumentException if {@code substr} is {@code null}.
102 | * @param substr the sub string
103 | */
104 | public void toContain(String substr) {
105 | Arguments.ensureNotNull(substr, "substr cannot be null");
106 | expectNotNull(this.getValue(), "Expected null to contain '%s'", substr);
107 | expectTrue(this.getValue().contains(substr), "Expected '%s' to contain '%s'", this.getValue(), substr);
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.java:
--------------------------------------------------------------------------------
1 | package com.mscharhag.oleaster.matcher.matchers;
2 |
3 | import java.util.Map;
4 |
5 | import com.mscharhag.oleaster.matcher.util.Arguments;
6 |
7 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectNotNull;
8 | import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue;
9 |
10 | /**
11 | * Matcher class to validate {@link Map}s
12 | */
13 | public class MapMatcher extends ObjectMatcher