Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed May 20, 2024
1 parent 92bffc2 commit e09c27f
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 0 deletions.
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ repositories {
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.9.3")
testImplementation "org.junit.jupiter:junit-jupiter"

jmh "org.openjdk.jmh:jmh-core:1.36"
jmh "org.openjdk.jmh:jmh-generator-annprocess:1.36"
jmhAnnotationProcessor "org.openjdk.jmh:jmh-generator-annprocess:1.36"
Expand All @@ -42,6 +45,14 @@ jar {
}
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
maxParallelForks Runtime.runtime.availableProcessors()
}

publishing {
repositories {
maven {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class BreakableEventTest {

private static int executions;

@Test
void fire() {
final BreakableTestListener.BreakableTestEvent event = new BreakableTestListener.BreakableTestEvent();

final DietrichEvents2 d = DietrichEvents2.global();
d.subscribe(BreakableTestListener.BreakableTestEvent.ID, (BreakableTestListener) e -> {
executions++;
e.stopHandling();
});
for (int i = 0; i < 10; i++) {
d.postInternal(BreakableTestListener.BreakableTestEvent.ID, event);
}

Assertions.assertEquals(1, executions);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

public interface BreakableTestListener {

void onTest(final BreakableTestEvent event);

class BreakableTestEvent extends BreakableEvent<BreakableTestListener> {

public static final int ID = 1;

@Override
public void call0(BreakableTestListener listener) {
listener.onTest(this);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CancellableEventTest {

@Test
void fire() {
final DietrichEvents2 d = DietrichEvents2.global();
d.subscribe(CancellableTestListener.CancellableTestEvent.ID, (CancellableTestListener) CancellableEvent::cancel);

CancellableTestListener.CancellableTestEvent event = new CancellableTestListener.CancellableTestEvent();
d.post(CancellableTestListener.CancellableTestEvent.ID, event);
Assertions.assertTrue(event.isCancelled());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

public interface CancellableTestListener {

void onTest(final CancellableTestEvent event);

class CancellableTestEvent extends CancellableEvent<CancellableTestListener> {

public static final int ID = 2;

@Override
public void call(CancellableTestListener listener) {
listener.onTest(this);
}

}

}
44 changes: 44 additions & 0 deletions src/test/java/de/florianmichael/dietrichevents2/ExceptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ExceptionTest {

private static final TestListener event = event -> System.out.println("TestEvent: " + event.something.getClass());

private static DietrichEvents2 instance;

private static String message;

@BeforeAll
static void setUp() {
instance = new DietrichEvents2(1, throwable -> message = "Custom message: " + throwable.getMessage());
instance.subscribe(TestListener.TestEvent.ID, event);
}

@Test
void fireException() {
instance.post(TestListener.TestEvent.ID, new TestListener.TestEvent(null));
Assertions.assertTrue(message.contains("Custom message"));
}

}
68 changes: 68 additions & 0 deletions src/test/java/de/florianmichael/dietrichevents2/GlobalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class GlobalTest {

private static final List<Object> VALUES = new ArrayList<>();

private static final TestListener ADDING_EVENT = event -> {
System.out.println("TestEvent: " + event.something);
VALUES.add(event.something);
};

@BeforeAll
static void setUp() {
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, ADDING_EVENT);
}

@Test
void hasFunctions() {
final DietrichEvents2 d = DietrichEvents2.global();

Assertions.assertTrue(d.hasSubscriber(TestListener.TestEvent.ID));
Assertions.assertNotEquals(0, d.getSubscribers(TestListener.TestEvent.ID).length);
Assertions.assertTrue(d.isSubscriber(TestListener.TestEvent.ID, ADDING_EVENT));
}

@Test
void fire() {
DietrichEvents2.global().post(TestListener.TestEvent.ID, new TestListener.TestEvent("Hello World"));
DietrichEvents2.global().post(TestListener.TestEvent.ID, new TestListener.TestEvent(10));

Assertions.assertTrue(VALUES.contains("Hello World"));
Assertions.assertTrue(VALUES.contains(10));
Assertions.assertFalse(VALUES.contains(20));
}

@Test
void unsubscribe() {
final DietrichEvents2 d = DietrichEvents2.global();

d.unsubscribe(TestListener.TestEvent.ID, ADDING_EVENT);
Assertions.assertFalse(d.isSubscriber(TestListener.TestEvent.ID, ADDING_EVENT));
}

}
54 changes: 54 additions & 0 deletions src/test/java/de/florianmichael/dietrichevents2/PriorityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;
import java.util.List;

public class PriorityTest {

private static final List<Object> VALUES = new LinkedList<>();

@Test
void fire() {
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test3"), Priorities.NORMAL);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test2"), Priorities.HIGH);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test1"), Priorities.LOWEST);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test5"), Priorities.HIGHEST);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test4"), Priorities.LOW);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test6"), Priorities.FALLBACK);
DietrichEvents2.global().subscribe(TestListener.TestEvent.ID, createListener("Test7"), Priorities.MONITOR);
DietrichEvents2.global().post(TestListener.TestEvent.ID, new TestListener.TestEvent(null));

Assertions.assertEquals(VALUES.get(0), "Test7");
Assertions.assertEquals(VALUES.get(1), "Test5");
Assertions.assertEquals(VALUES.get(2), "Test2");
Assertions.assertEquals(VALUES.get(3), "Test3");
Assertions.assertEquals(VALUES.get(4), "Test4");
Assertions.assertEquals(VALUES.get(5), "Test1");
Assertions.assertEquals(VALUES.get(6), "Test6");
}

private static TestListener createListener(final String name) {
return event -> VALUES.add(name);
}

}
41 changes: 41 additions & 0 deletions src/test/java/de/florianmichael/dietrichevents2/TestListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of DietrichEvents2 - https://github.com/FlorianMichael/DietrichEvents2
* Copyright (C) 2023-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.florianmichael.dietrichevents2;

public interface TestListener {

void onTest(final TestEvent event);

class TestEvent extends AbstractEvent<TestListener> {

public static final int ID = 0;

public final Object something;

public TestEvent(Object something) {
this.something = something;
}

@Override
public void call(TestListener listener) {
listener.onTest(this);
}

}

}
Loading

0 comments on commit e09c27f

Please sign in to comment.