Search in sources :

Example 6 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser in project webpieces by deanhiller.

the class PrivateWebserverForTest method init.

private void init(PrivateTestConfig testConfig, String... args) {
    // read here and checked for correctness on last line of server construction
    Arguments arguments = new CommandLineParser().parse(args);
    String filePath = System.getProperty("user.dir");
    log.info("property user.dir=" + filePath);
    File baseWorkingDir = FileFactory.getBaseWorkingDir();
    // 3 pieces to the webserver so a configuration for each piece
    WebServerConfig config = new WebServerConfig().setPlatformOverrides(testConfig.getPlatformOverrides());
    RouterConfig routerConfig = new RouterConfig(baseWorkingDir, "webpiecesTestSuite").setMetaFile(testConfig.getMetaFile()).setWebappOverrides(testConfig.getAppOverrides()).setFileEncoding(CHAR_SET_TO_USE).setDefaultResponseBodyEncoding(CHAR_SET_TO_USE).setCachedCompressedDirectory(cacheDir).setSecretKey(SecretKeyInfo.generateForTest()).setTokenCheckOn(testConfig.isUseTokenCheck());
    TemplateConfig templateConfig = new TemplateConfig();
    webServer = WebServerFactory.create(config, routerConfig, templateConfig, arguments);
    arguments.checkConsumedCorrectly();
}
Also used : WebServerConfig(org.webpieces.webserver.api.WebServerConfig) Arguments(org.webpieces.util.cmdline2.Arguments) TemplateConfig(org.webpieces.templating.api.TemplateConfig) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) RouterConfig(org.webpieces.router.api.RouterConfig)

Example 7 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser in project webpieces by deanhiller.

the class CmdLineParser2Test method testKeyIsOptionalAndRequiredNotPassedInShouldFail.

@Test
public void testKeyIsOptionalAndRequiredNotPassedInShouldFail() {
    String[] args = new String[] {};
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    parse.createRequiredArg("key1", "key1 help from different plugin, and reasons are different", (s) -> convertInt(s));
    try {
        parse.checkConsumedCorrectly();
    } catch (CommandLineException e) {
        Assert.assertEquals("Errors converting command line arguments:\n" + "(Call CommandLineException.getErrors to get the stack trace of each failure)\n" + "java.lang.IllegalArgumentException: Argument -key1 is required but was not supplied.  help='key1 help from different plugin, and reasons are different'\n" + "\n" + "Dynamically generated help(depends on which plugins you pull in):\n" + "	-key1 following usages:\n" + "		(optional, default: 123)key1 help\n" + "				Value Parsed:null foundKey:false foundValue:false\n" + "		key1 help from different plugin, and reasons are different\n" + "				Value Parsed:null foundKey:false foundValue:false\n", e.getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 8 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser in project webpieces by deanhiller.

the class CmdLineParser2Test method testDeveloperHasInvalidDefaultValueAndRequiredMissing.

@Test
public void testDeveloperHasInvalidDefaultValueAndRequiredMissing() {
    String[] args = new String[] { "-key1=something" };
    Arguments parse = new CommandLineParser().parse(args);
    parse.createOptionalArg("key1", "invalid", "key1 help", (s) -> convertInt(s));
    parse.createRequiredArg("key3", "Some key3", (s) -> s);
    try {
        parse.checkConsumedCorrectly();
        Assert.fail("Should have thrown telling developer all errors");
    } catch (CommandLineException e) {
        List<Throwable> errors = e.getErrors();
        Assert.assertEquals(2, errors.size());
        Assert.assertEquals("Bug, The defaultValue conversion test failed.  key=key1 value=invalid", errors.get(0).getMessage());
        Assert.assertEquals("Argument -key3 is required but was not supplied.  help='Some key3'", errors.get(1).getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) List(java.util.List) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 9 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser in project webpieces by deanhiller.

the class CmdLineParser2Test method testKeyIsOptionalAndRequiredPassedInShouldSucceed.

@Test
public void testKeyIsOptionalAndRequiredPassedInShouldSucceed() {
    String[] args = new String[] { "-key1=5" };
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    Supplier<Integer> val1 = parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    Supplier<Integer> val2 = parse.createRequiredArg("key1", "key1 help", (s) -> convertInt(s));
    parse.checkConsumedCorrectly();
    Assert.assertEquals(Integer.valueOf(5), val1.get());
    Assert.assertEquals(Integer.valueOf(5), val2.get());
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) Test(org.junit.Test)

Example 10 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser in project webpieces by deanhiller.

the class TestProdRouter method bothServers.

@SuppressWarnings("rawtypes")
@Parameterized.Parameters
public static Collection bothServers() {
    String moduleFileContents = AppModules.class.getName();
    VirtualFile f = new VirtualFileInputStream(moduleFileContents.getBytes(), "testAppModules");
    File baseWorkingDir = FileFactory.getBaseWorkingDir();
    TestModule module = new TestModule();
    Arguments args = new CommandLineParser().parse();
    RouterConfig config = new RouterConfig(baseWorkingDir, "TestProdRouter").setMetaFile(f).setWebappOverrides(module).setSecretKey(SecretKeyInfo.generateForTest());
    SimpleMeterRegistry metrics = new SimpleMeterRegistry();
    TemplateApi nullApi = new NullTemplateApi();
    RouterService prodSvc = RouterServiceFactory.create(metrics, config, nullApi);
    prodSvc.configure(args);
    args.checkConsumedCorrectly();
    return Arrays.asList(new Object[][] { { prodSvc, module } });
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) VirtualFileInputStream(org.webpieces.router.api.mocks.VirtualFileInputStream) RouterService(org.webpieces.router.api.RouterService) TemplateApi(org.webpieces.router.api.TemplateApi) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) RouterConfig(org.webpieces.router.api.RouterConfig)

Aggregations

Arguments (org.webpieces.util.cmdline2.Arguments)11 CommandLineParser (org.webpieces.util.cmdline2.CommandLineParser)11 Test (org.junit.Test)6 File (java.io.File)5 CommandLineException (org.webpieces.util.cmdline2.CommandLineException)5 VirtualFile (org.webpieces.util.file.VirtualFile)5 List (java.util.List)4 RouterConfig (org.webpieces.router.api.RouterConfig)4 RouterService (org.webpieces.router.api.RouterService)3 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 TemplateApi (org.webpieces.router.api.TemplateApi)2 VirtualFileInputStream (org.webpieces.router.api.mocks.VirtualFileInputStream)2 CompileConfig (org.webpieces.compiler.api.CompileConfig)1 TemplateConfig (org.webpieces.templating.api.TemplateConfig)1 VirtualFileImpl (org.webpieces.util.file.VirtualFileImpl)1 WebServerConfig (org.webpieces.webserver.api.WebServerConfig)1