Search in sources :

Example 6 with ProvidenceConfigException

use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.

the class ProvidenceConfigParserTest method assertConfigFails.

private void assertConfigFails(String cfg, String message) throws IOException {
    try {
        File a = writeContentTo(cfg, temp.newFile());
        ProvidenceConfigParser config = new ProvidenceConfigParser(registry, false);
        config.parseConfig(a.toPath(), null);
        fail("No exception on fail: " + message);
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is(message));
    }
}
Also used : File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 7 with ProvidenceConfigException

use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.

the class ProvidenceConfigParserTest method testInternalReference.

@Test
public void testInternalReference() throws IOException {
    File a = writeContentTo("config.RefMerge {\n" + "  ref1 & first = {\n" + "    bool_value & boo = false\n" + "    msg_value & db {\n" + "      driver = \"Driver\"\n" + "    }\n" + "  }\n" + "  ref1_1 = first {\n" + "    i16_value = 12345\n" + "    msg_value & db2 = db {\n" + "      uri = \"someuri\"\n" + "    }\n" + "  }\n" + "  ref2 {\n" + "    bool_value = boo" + "    msg_value = db2\n" + "  }\n" + "}\n", temp.newFile("a.cfg"));
    try {
        ProvidenceConfigParser config = new ProvidenceConfigParser(registry, false);
        RefMerge merged = config.parseConfig(a.toPath(), (RefMerge) null).first;
        assertThat(debugString(merged), is("{\n" + "  ref1 = {\n" + "    bool_value = false\n" + "    msg_value = {\n" + "      driver = \"Driver\"\n" + "    }\n" + "  }\n" + "  ref1_1 = {\n" + "    bool_value = false\n" + "    i16_value = 12345\n" + "    msg_value = {\n" + "      uri = \"someuri\"\n" + "      driver = \"Driver\"\n" + "    }\n" + "  }\n" + "  ref2 = {\n" + "    bool_value = false\n" + "    msg_value = {\n" + "      uri = \"someuri\"\n" + "      driver = \"Driver\"\n" + "    }\n" + "  }\n" + "}"));
    } catch (ProvidenceConfigException e) {
        System.err.println(e.asString());
        throw e;
    }
}
Also used : RefMerge(net.morimekta.test.providence.config.RefMerge) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Test(org.junit.Test)

Example 8 with ProvidenceConfigException

use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.

the class ProvidenceConfigParserTest method assertParseFailure.

private void assertParseFailure(String message, String pretty, boolean strict) throws IOException {
    File a = temp.newFile("test.cfg");
    writeContentTo(pretty, a);
    ProvidenceConfigParser config = new ProvidenceConfigParser(registry, strict);
    try {
        config.parseConfig(a.toPath(), null);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        String actual = e.asString().replaceAll("\\r", "");
        if (!actual.equals(message)) {
            e.printStackTrace();
        }
        assertThat(actual, is(message));
    }
    a.delete();
}
Also used : ProvidenceHelper.debugString(net.morimekta.providence.util.ProvidenceHelper.debugString) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 9 with ProvidenceConfigException

use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.

the class ProvidenceConfigParserTest method testParseWithUnknown_strict.

@Test
public void testParseWithUnknown_strict() throws IOException {
    copyResourceTo("/net/morimekta/providence/config/files/unknown.cfg", temp.getRoot());
    File file = copyResourceTo("/net/morimekta/providence/config/files/unknown_include.cfg", temp.getRoot());
    ProvidenceConfigParser config = new ProvidenceConfigParser(registry, true);
    try {
        config.parseConfig(file.toPath(), null);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertEquals("Unknown declared type: unknown.OtherConfig", e.getMessage());
    }
    file = copyResourceTo("/net/morimekta/providence/config/files/unknown_field.cfg", temp.getRoot());
    try {
        config.parseConfig(file.toPath(), null);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertEquals("No such field unknown_field in config.Database", e.getMessage());
    }
    file = copyResourceTo("/net/morimekta/providence/config/files/unknown_enum_value.cfg", temp.getRoot());
    try {
        config.parseConfig(file.toPath(), null);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertEquals("No such enum value 'LAST' for config.Value.", e.getMessage());
    }
    file = copyResourceTo("/net/morimekta/providence/config/files/unknown_enum_value2.cfg", temp.getRoot());
    try {
        config.parseConfig(file.toPath(), null);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertEquals("No such enum value 'second' for config.Value, did you mean 'SECOND'?", e.getMessage());
    }
}
Also used : File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Test(org.junit.Test)

Example 10 with ProvidenceConfigException

use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.

the class ProvidenceConfigSupplierTest method testSupplierKeepInstanceOnFailedReload.

@Test
public void testSupplierKeepInstanceOnFailedReload() throws IOException {
    Database first = Database.builder().build();
    File file = tmp.newFile().getAbsoluteFile().getCanonicalFile();
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(first, ImmutableSet.of(file.toString())));
    ArgumentCaptor<FileWatcher.Watcher> watcherCapture = ArgumentCaptor.forClass(FileWatcher.Watcher.class);
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    ProvidenceConfigSupplier<Database, Database._Field> supplier = new ProvidenceConfigSupplier<>(file, null, watcher, parser, clock);
    assertThat(supplier.get(), is(first));
    verify(watcher).weakAddWatcher(any(FileWatcher.Watcher.class));
    reset(parser, watcher);
    when(parser.parseConfig(file.toPath(), null)).thenThrow(new ProvidenceConfigException("test"));
    watcherCapture.getValue().onFileUpdate(file);
    verify(parser).parseConfig(file.toPath(), null);
    verifyNoMoreInteractions(parser);
    assertThat(supplier.get(), is(first));
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) Database(net.morimekta.test.providence.config.Database) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Pair(net.morimekta.util.Pair) Test(org.junit.Test)

Aggregations

ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)13 File (java.io.File)8 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)4 Test (org.junit.Test)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 PMap (net.morimekta.providence.descriptor.PMap)3 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 PMessage (net.morimekta.providence.PMessage)2 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)2 ProvidenceTools (net.morimekta.providence.tools.common.ProvidenceTools)2 Database (net.morimekta.test.providence.config.Database)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 ArgumentException (net.morimekta.console.args.ArgumentException)1