-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add unit tests for EventType and EventManager
The @contract of the EventManager interface have also been revised.
- Loading branch information
Showing
9 changed files
with
325 additions
and
10 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
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
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
48 changes: 48 additions & 0 deletions
48
api/src/test/java/be/darkkraft/transferproxy/api/event/EventTypeTest.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,48 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Darkkraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package be.darkkraft.transferproxy.api.event; | ||
|
||
import be.darkkraft.transferproxy.api.util.test.MockedTransferProxy; | ||
import be.darkkraft.transferproxy.api.util.test.TestUtil; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class EventTypeTest { | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
MockedTransferProxy.mock(); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(EventType.class) | ||
void testClassOfDefaultListener(final EventType type) throws ClassNotFoundException { | ||
assertTrue(TestUtil.getGenericType(type.buildDefaultListener().getClass()).isAssignableFrom(type.getEventClass())); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
api/src/test/java/be/darkkraft/transferproxy/api/util/test/MockedTransferProxy.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,77 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Darkkraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package be.darkkraft.transferproxy.api.util.test; | ||
|
||
import be.darkkraft.transferproxy.api.TransferProxy; | ||
import be.darkkraft.transferproxy.api.configuration.ProxyConfiguration; | ||
import be.darkkraft.transferproxy.api.configuration.yaml.YamlProxyConfiguration; | ||
import be.darkkraft.transferproxy.api.module.ModuleManager; | ||
import be.darkkraft.transferproxy.api.network.NetworkServer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mockito.Mockito; | ||
|
||
public class MockedTransferProxy extends TransferProxy { | ||
|
||
public static void mock() { | ||
new MockedTransferProxy().start(); | ||
} | ||
|
||
// I use the yaml configuration because it has the default values | ||
private final ProxyConfiguration configuration = new YamlProxyConfiguration(); | ||
private final ModuleManager moduleManager = Mockito.mock(ModuleManager.class); | ||
private final NetworkServer networkServer = Mockito.mock(NetworkServer.class); | ||
private final long startedTime = System.currentTimeMillis(); | ||
|
||
@Override | ||
public void start() { | ||
this.setInstance(this); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
|
||
} | ||
|
||
@Override | ||
public @NotNull ProxyConfiguration getConfiguration() { | ||
return this.configuration; | ||
} | ||
|
||
@Override | ||
public @NotNull ModuleManager getModuleManager() { | ||
return this.moduleManager; | ||
} | ||
|
||
@Override | ||
public NetworkServer getNetworkServer() { | ||
return this.networkServer; | ||
} | ||
|
||
@Override | ||
public long getStartedTime() { | ||
return this.startedTime; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
api/src/test/java/be/darkkraft/transferproxy/api/util/test/TestUtil.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,52 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Darkkraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package be.darkkraft.transferproxy.api.util.test; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
public class TestUtil { | ||
|
||
private TestUtil() throws IllegalAccessException { | ||
throw new IllegalAccessException("You cannot instantiate a utility class"); | ||
} | ||
|
||
public static @NotNull Class<?> getGenericType(final Class<?> clazz) throws ClassNotFoundException { | ||
for (final Type type : clazz.getGenericInterfaces()) { | ||
if (type instanceof final ParameterizedType paramType) { | ||
final Type[] arguments = paramType.getActualTypeArguments(); | ||
if (arguments.length >= 1) { | ||
return Class.forName(arguments[0].getTypeName()); | ||
} | ||
} else { | ||
return getGenericType(Class.forName(type.getTypeName())); | ||
} | ||
} | ||
throw new IllegalArgumentException("No generic type found"); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.