-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api,bundle,core): add an interface for BoxBootstrapContext to ac…
…cess it from api module
- Loading branch information
1 parent
076bb3a
commit fee9369
Showing
7 changed files
with
132 additions
and
50 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
api/src/main/java/net/okocraft/box/api/bootstrap/BootstrapContext.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,38 @@ | ||
package net.okocraft.box.api.bootstrap; | ||
|
||
import net.okocraft.box.api.feature.BoxFeature; | ||
import net.okocraft.box.api.feature.FeatureContext; | ||
import net.okocraft.box.api.message.DefaultMessageCollector; | ||
import net.okocraft.box.api.model.manager.EventManager; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
|
||
public interface BootstrapContext { | ||
|
||
static @NotNull BootstrapContext get() { | ||
try { | ||
var clazz = Class.forName("net.okocraft.box.bootstrap.BoxBootstrap"); | ||
var bootstrap = clazz.getMethod("get").invoke(null); | ||
var getContext = clazz.getMethod("getContext"); | ||
return (BootstrapContext) getContext.invoke(bootstrap); | ||
} catch (ClassNotFoundException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { | ||
throw new RuntimeException("Could not get BootstrapContext", e); | ||
} | ||
} | ||
|
||
@NotNull Path getDataDirectory(); | ||
|
||
@NotNull String getVersion(); | ||
|
||
@NotNull EventManager getEventManager(); | ||
|
||
@NotNull DefaultMessageCollector getDefaultMessageCollector(); | ||
|
||
@Contract("_ -> this") | ||
@NotNull BootstrapContext addFeature(@NotNull Function<FeatureContext.Registration, ? extends BoxFeature> featureFactory); | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
bundle/src/test/java/net/okocraft/box/bootstrap/BootstrapContextTest.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,19 @@ | ||
package net.okocraft.box.bootstrap; | ||
|
||
import net.okocraft.box.api.bootstrap.BootstrapContext; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
class BootstrapContextTest { | ||
@Test | ||
void testGet() { | ||
try (var mock = Mockito.mockStatic(BoxBootstrap.class)) { | ||
var bootstrap = Mockito.mock(BoxBootstrap.class); | ||
var context = Mockito.mock(BoxBootstrapContext.class); | ||
Mockito.when(bootstrap.getContext()).thenReturn(context); | ||
mock.when(BoxBootstrap::get).thenReturn(bootstrap); | ||
Assertions.assertSame(context, BootstrapContext.get()); | ||
} | ||
} | ||
} |
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