-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92bffc2
commit e09c27f
Showing
10 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/de/florianmichael/dietrichevents2/BreakableEventTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/de/florianmichael/dietrichevents2/BreakableTestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/de/florianmichael/dietrichevents2/CancellableEventTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/de/florianmichael/dietrichevents2/CancellableTestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/test/java/de/florianmichael/dietrichevents2/ExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
src/test/java/de/florianmichael/dietrichevents2/GlobalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
src/test/java/de/florianmichael/dietrichevents2/PriorityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/test/java/de/florianmichael/dietrichevents2/TestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.