Java Code Examples for io.bootique.BQRuntime#getInstance()

The following examples show how to use io.bootique.BQRuntime#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BQRuntimeChecker.java    From bootique with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies that runtime contains expected modules.
 *
 * @param runtime         a Bootique runtime whose contents we are testing.
 * @param expectedModules a vararg array of expected module types.
 */
@SafeVarargs
public static void testModulesLoaded(BQRuntime runtime, Class<? extends BQModule>... expectedModules) {

    final ModulesMetadata modulesMetadata = runtime.getInstance(ModulesMetadata.class);

    final List<String> actualModules = modulesMetadata
            .getModules()
            .stream()
            .map(ModuleMetadata::getName)
            .collect(toList());

    final String[] expectedModuleNames = Stream.of(expectedModules)
            .map(Class::getSimpleName)
            .toArray(String[]::new);

    // Using class names for checking module existing - weak.
    assertThat(actualModules, hasItems(expectedModuleNames));
}
 
Example 2
Source File: ModuleMetadataIT.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomNamedModule() {
    BQRuntime runtime = runtimeFactory.app().moduleProvider(new BQModuleProvider() {
        @Override
        public BQModule module() {
            return b -> {
            };
        }

        @Override
        public BQModuleMetadata.Builder moduleBuilder() {
            return BQModuleProvider.super
                    .moduleBuilder()
                    .name("mymodule");
        }
    }).createRuntime();

    ModulesMetadata md = runtime.getInstance(ModulesMetadata.class);
    assertEquals(4, md.getModules().size(), "Expected BQCoreModule + 2 test modules + custom module");

    Optional<ModuleMetadata> myMd = md.getModules()
            .stream()
            .filter(m -> "mymodule".equals(m.getName()))
            .findFirst();
    assertTrue(myMd.isPresent());
}
 
Example 3
Source File: CommandsIT.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Test
public void testModule_ExtraCommandAsType() {
    BQModuleProvider provider = Commands.builder(C1.class).build();
    BQRuntime runtime = testFactory.app(args).moduleProvider(provider).createRuntime();
    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    Map<String, ManagedCommand> commands = commandManager.getAllCommands();
    assertCommandKeys(commands, "c1", "help", "help-config");

    assertFalse(commands.get("help").isHidden());
    assertFalse(commands.get("help").isDefault());
    assertTrue(commands.get("help").isHelp());

    assertFalse(commands.get("help-config").isHidden());
    assertFalse(commands.get("help-config").isDefault());

    assertTrue(commands.containsKey("c1"));
    assertFalse(commands.get("c1").isDefault());
}
 
Example 4
Source File: CommandManagerIT.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultAndHelpAndModuleCommands() {

    Command defaultCommand = cli -> CommandOutcome.succeeded();
    BQModule defaultCommandModule = binder -> BQCoreModule.extend(binder).setDefaultCommand(defaultCommand);

    BQRuntime runtime = runtimeFactory.app().modules(M0.class, M1.class).module(defaultCommandModule).createRuntime();

    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    assertEquals(5, commandManager.getAllCommands().size());
    assertSame(M0.mockCommand, commandManager.lookupByName("m0command").getCommand());
    assertSame(M1.mockCommand, commandManager.lookupByName("m1command").getCommand());
    assertSame(defaultCommand, commandManager.getPublicDefaultCommand().get());
    assertSame(runtime.getInstance(HelpCommand.class), commandManager.getPublicHelpCommand().get());
}
 
Example 5
Source File: CommandsIT.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoModuleCommands() {
    BQModuleProvider provider = Commands.builder().noModuleCommands().build();
    BQRuntime runtime = testFactory.app(args).moduleProvider(provider).createRuntime();
    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    Map<String, ManagedCommand> commands = commandManager.getAllCommands();
    assertCommandKeys(commands, "help", "help-config");

    assertTrue(commands.get("help").isHidden());
    assertFalse(commands.get("help").isDefault());
    assertTrue(commands.get("help").isHelp());

    assertTrue(commands.get("help-config").isHidden());
    assertFalse(commands.get("help-config").isDefault());
}
 
Example 6
Source File: CommandsIT.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Test
public void testModuleCommands() {
    BQModuleProvider provider = Commands.builder().build();
    BQRuntime runtime = testFactory.app(args).moduleProvider(provider).createRuntime();
    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    Map<String, ManagedCommand> commands = commandManager.getAllCommands();
    assertCommandKeys(commands, "help", "help-config");

    assertFalse(commands.get("help").isHidden());
    assertFalse(commands.get("help").isDefault());
    assertTrue(commands.get("help").isHelp());

    assertFalse(commands.get("help-config").isHidden());
    assertFalse(commands.get("help-config").isDefault());
}
 
Example 7
Source File: InjectableTest.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testService() {

    BQRuntime runtime = testFactory.app("--config=src/test/resources/my.yml")
            // end::Testing[]
            .module(binder -> binder.bind(MyService.class).to(MyServiceImpl.class))
            // tag::Testing[]
            .createRuntime();

    MyService service = runtime.getInstance(MyService.class);
    assertEquals("xyz", service.someMethod());
}
 
Example 8
Source File: BootiqueMain.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the options of the program.
 *
 * @return the options of the program.
 */
public List<HelpOption> getOptions() {
	final BQRuntime runtime = createRuntime();
	final ApplicationMetadata application = runtime.getInstance(ApplicationMetadata.class);
	final HelpOptions helpOptions = new HelpOptions();

	/*application.getCommands().forEach(c -> {
		helpOptions.add(c.asOption());
		c.getOptions().forEach(o -> helpOptions.add(o));
	});*/

	application.getOptions().forEach(o -> helpOptions.add(o));

	return helpOptions.getOptions();
}
 
Example 9
Source File: CommandsIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testModule_ExtraCommandOverride() {
    BQModuleProvider provider = Commands.builder().add(C2_Help.class).build();
    BQRuntime runtime = testFactory.app(args).moduleProvider(provider).createRuntime();
    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    Map<String, ManagedCommand> commands = commandManager.getAllCommands();
    assertCommandKeys(commands, "help", "help-config");
}
 
Example 10
Source File: CommandsIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testModule_ExtraCommandAsInstance() {
    BQModuleProvider provider = Commands.builder().add(new C1()).build();
    BQRuntime runtime = testFactory.app(args).moduleProvider(provider).createRuntime();
    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    Map<String, ManagedCommand> commands = commandManager.getAllCommands();
    assertCommandKeys(commands, "c1", "help", "help-config");
}
 
Example 11
Source File: CommandManagerIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultCommandWithMetadata() {

    Command defaultCommand = new Command() {
        @Override
        public CommandOutcome run(Cli cli) {
            return CommandOutcome.succeeded();
        }

        @Override
        public CommandMetadata getMetadata() {
            // note how this name intentionally matches one of the existing commands
            return CommandMetadata.builder("m0command").build();
        }
    };

    BQModule defaultCommandModule = binder -> BQCoreModule.extend(binder).setDefaultCommand(defaultCommand);

    BQRuntime runtime = runtimeFactory.app()
            .modules(M0.class, M1.class)
            .module(defaultCommandModule)
            .createRuntime();

    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    // the main assertion we care about...
    assertSame(defaultCommand,
            commandManager.lookupByName("m0command").getCommand(),
            "Default command did not override another command with same name");

    // sanity check
    assertEquals(4, commandManager.getAllCommands().size());
    assertSame(M1.mockCommand, commandManager.lookupByName("m1command").getCommand());
    assertSame(runtime.getInstance(HelpCommand.class), commandManager.lookupByName("help").getCommand());
    assertSame(defaultCommand, commandManager.getPublicDefaultCommand().get());
}
 
Example 12
Source File: CommandManagerIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiddenCommands() {

    Command hiddenCommand = new Command() {
        @Override
        public CommandOutcome run(Cli cli) {
            return CommandOutcome.succeeded();
        }

        @Override
        public CommandMetadata getMetadata() {
            return CommandMetadata.builder("xyz").hidden().build();
        }
    };

    BQRuntime runtime = runtimeFactory.app()
            .module(binder -> BQCoreModule.extend(binder).addCommand(hiddenCommand))
            .createRuntime();

    CommandManager commandManager = runtime.getInstance(CommandManager.class);

    assertEquals(3, commandManager.getAllCommands().size());

    ManagedCommand hiddenManaged = commandManager.getAllCommands().get("xyz");
    assertSame(hiddenCommand, hiddenManaged.getCommand());
    assertTrue(hiddenManaged.isHidden());
}
 
Example 13
Source File: CommandManagerIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testHelpAndModuleCommands() {
    BQRuntime runtime = runtimeFactory.app().modules(M0.class, M1.class).createRuntime();

    CommandManager commandManager = runtime.getInstance(CommandManager.class);
    assertEquals(4, commandManager.getAllCommands().size(), "help, helpconfig and module commands must be present");

    assertSame(M0.mockCommand, commandManager.lookupByName("m0command").getCommand());
    assertSame(M1.mockCommand, commandManager.lookupByName("m1command").getCommand());

    assertFalse(commandManager.getPublicDefaultCommand().isPresent());
    assertSame(runtime.getInstance(HelpCommand.class), commandManager.getPublicHelpCommand().get());
}
 
Example 14
Source File: ApplicationMetadataIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomDescription() {
    BQRuntime runtime = runtimeFactory.app()
            .module(b -> BQCoreModule.extend(b).setApplicationDescription("app desc"))
            .createRuntime();

    ApplicationMetadata md = runtime.getInstance(ApplicationMetadata.class);

    // we really don't know what the generated name is. It varies depending on the unit test execution environment
    assertNotNull(md.getName());
    assertEquals("app desc", md.getDescription());
    assertEquals(2, md.getCommands().size());
    assertEquals(1, md.getOptions().size());
}
 
Example 15
Source File: ApplicationMetadataIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefault() {
    BQRuntime runtime = runtimeFactory.app().createRuntime();

    ApplicationMetadata md = runtime.getInstance(ApplicationMetadata.class);

    // we really don't know what the generated name is. It varies depending on the unit test execution environment
    assertNotNull(md.getName());
    assertNull(md.getDescription());
    assertEquals(2, md.getCommands().size());
    assertEquals(1, md.getOptions().size());
}
 
Example 16
Source File: BQRuntimeChecker.java    From bootique with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that runtime contains expected modules.
 *
 * @param runtime         a Bootique runtime whose contents we are testing.
 * @param expectedModules a vararg array of expected module types.
 */
@SafeVarargs
public static void testModulesLoaded(BQRuntime runtime, Class<? extends BQModule>... expectedModules) {

    ModulesMetadata modulesMetadata = runtime.getInstance(ModulesMetadata.class);

    List<String> actualModules = modulesMetadata
            .getModules()
            .stream()
            .map(ModuleMetadata::getName)
            .collect(toList());

    List<String> missingModules = new ArrayList<>();
    Stream.of(expectedModules)
            .map(Class::getSimpleName)
            .forEach(n -> {

                // Using class names for checking module existing - weak.
                if (!actualModules.contains(n)) {
                    missingModules.add(n);
                }
            });


    if(!missingModules.isEmpty()) {
        fail("The following expected modules are missing: " + missingModules);
    }
}
 
Example 17
Source File: AppUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenService_expectBoolen() {
    BQRuntime runtime = bqTestFactory.app("--server").autoLoadModules().createRuntime();
    HelloService service = runtime.getInstance(HelloService.class);
    assertEquals(true, service.save());
}