Search in sources :

Example 1 with OrchidModule

use of com.eden.orchid.api.registration.OrchidModule in project Orchid by JavaEden.

the class TestFlags method testParsingFlags.

// Run Actual Tests
// ----------------------------------------------------------------------------------------------------------------------
@Test
public void testParsingFlags() throws Throwable {
    OrchidFlags.instance = new OrchidFlags(flagParsers);
    OrchidModule module = OrchidFlags.getInstance().parseFlags(OrchidUtils.parseCommandLineArgs(args));
    // verify that the options get parsed correctly
    assertThat(OrchidFlags.getInstance().getFlagValue("one"), is(equalTo("valueOne")));
    assertThat(OrchidFlags.getInstance().getFlagValue("two"), is(equalTo("valueTwo")));
    assertThat(OrchidFlags.getInstance().getFlagValue("three"), is(equalTo("valueThree")));
    // "four" has no parser for it
    assertThat(OrchidFlags.getInstance().getFlagValue("four"), emptyOrNullString());
    // "five"'s parser returned an int
    assertThat(OrchidFlags.getInstance().getFlagValue("five"), is(equalTo(17)));
    assertThat(OrchidFlags.getInstance().getFlagValue("six"), is(equalTo("valueSix")));
    assertThat(OrchidFlags.getInstance().getFlagValue("seven"), is(equalTo(14)));
    // Test that the parser correctly creates a module that is capable of injecting the parsed flags
    Injector injector = Guice.createInjector(module);
    TestInjectionOneClass oneClass = injector.getInstance(TestInjectionOneClass.class);
    assertThat(oneClass.value, is(equalTo("valueOne")));
    TestInjectionTwoClass twoClass = injector.getInstance(TestInjectionTwoClass.class);
    assertThat(twoClass.value, is(equalTo(17)));
    TestInjectionSixClass sixClass = injector.getInstance(TestInjectionSixClass.class);
    assertThat(sixClass.value, is(equalTo("valueSix")));
    TestInjectionSevenClass sevenClass = injector.getInstance(TestInjectionSevenClass.class);
    assertThat(sevenClass.value, is(equalTo(14)));
    boolean threwError = false;
    try {
        injector.getInstance(TestInjectionThreeClass.class);
    } catch (ConfigurationException e) {
        threwError = true;
    }
    if (!threwError) {
        throw new Exception("No error was thrown when injecting the wrong type");
    }
    threwError = false;
    try {
        List<String> args = new ArrayList<>();
        args.addAll(Arrays.asList(this.args));
        args.addAll(Arrays.asList("--four", "valueFour"));
        this.args = new String[args.size()];
        args.toArray(this.args);
        OrchidFlags.getInstance().parseFlags(OrchidUtils.parseCommandLineArgs(this.args));
    } catch (IllegalArgumentException e) {
        threwError = true;
    }
    if (!threwError) {
        throw new Exception("No error was thrown when passing a flag that is not registered.");
    }
}
Also used : ArrayList(java.util.ArrayList) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) ConfigurationException(com.google.inject.ConfigurationException) ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) OrchidModule(com.eden.orchid.api.registration.OrchidModule) OrchidUnitTest(com.eden.orchid.testhelpers.OrchidUnitTest) Test(org.junit.jupiter.api.Test)

Example 2 with OrchidModule

use of com.eden.orchid.api.registration.OrchidModule in project Orchid by JavaEden.

the class OrchidFlags method parseFlags.

public OrchidModule parseFlags(Map<String, Object> flagsMap) {
    parsedFlagsData = new HashMap<>();
    for (OrchidFlag flag : flags) {
        DefaultExtractor.getInstance().extractOptions(flag, flagsMap);
    }
    return new OrchidModule() {

        @Override
        protected void configure() {
            for (OrchidFlag flag : flags) {
                for (OrchidFlag.Value entry : flag.getParsedFlags().values()) {
                    parsedFlagsData.put(entry.getKey(), entry);
                    AnnotatedBindingBuilder binder = bind(entry.getType());
                    binder.annotatedWith(Names.named(entry.getKey())).toInstance(entry.getValue());
                }
            }
        }
    };
}
Also used : AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) OrchidModule(com.eden.orchid.api.registration.OrchidModule)

Aggregations

OrchidModule (com.eden.orchid.api.registration.OrchidModule)2 OrchidUnitTest (com.eden.orchid.testhelpers.OrchidUnitTest)1 ConfigurationException (com.google.inject.ConfigurationException)1 Injector (com.google.inject.Injector)1 AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)1 ArrayList (java.util.ArrayList)1 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)1 Test (org.junit.jupiter.api.Test)1