Search in sources :

Example 1 with DescriptorParserException

use of org.infinispan.protostream.DescriptorParserException in project kogito-apps by kiegroup.

the class ProtoIndexParserTest method testConfigureBuilderWithInvalidFile.

@Test
void testConfigureBuilderWithInvalidFile() {
    SerializationContext ctx = new SerializationContextImpl(configureBuilder().build());
    FileDescriptorSource invalidFileDescriptorSource = FileDescriptorSource.fromString("invalid", "invalid");
    try {
        ctx.registerProtoFiles(invalidFileDescriptorSource);
        fail("Failed to process invalid proto file");
    } catch (DescriptorParserException ex) {
    // Successfully throw exception
    }
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.jupiter.api.Test)

Example 2 with DescriptorParserException

use of org.infinispan.protostream.DescriptorParserException in project protostream by infinispan.

the class SerializationContextImplTest method testTwoFilesWithErrorsSeparately.

@Test
public void testTwoFilesWithErrorsSeparately() {
    SerializationContext ctx = createContext();
    Map<String, Throwable> errors1 = new HashMap<>();
    List<String> successful1 = new ArrayList<>();
    FileDescriptorSource source1 = FileDescriptorSource.fromString("test1.proto", "kabooom1").withProgressCallback(new FileDescriptorSource.ProgressCallback() {

        @Override
        public void handleError(String fileName, DescriptorParserException ex) {
            errors1.put(fileName, ex);
        }

        @Override
        public void handleSuccess(String fileName) {
            successful1.add(fileName);
        }
    });
    Map<String, Throwable> errors2 = new HashMap<>();
    List<String> successful2 = new ArrayList<>();
    FileDescriptorSource source2 = FileDescriptorSource.fromString("test2.proto", "kabooom2").withProgressCallback(new FileDescriptorSource.ProgressCallback() {

        @Override
        public void handleError(String fileName, DescriptorParserException ex) {
            errors2.put(fileName, ex);
        }

        @Override
        public void handleSuccess(String fileName) {
            successful2.add(fileName);
        }
    });
    ctx.registerProtoFiles(source1);
    ctx.registerProtoFiles(source2);
    assertTrue(successful1.isEmpty());
    assertTrue(successful2.isEmpty());
    assertEquals(1, errors1.size());
    assertEquals(2, errors2.size());
    assertTrue(errors1.containsKey("test1.proto"));
    assertTrue(errors2.containsKey("test1.proto"));
    assertTrue(errors2.containsKey("test2.proto"));
    assertEquals("java.lang.IllegalStateException: Syntax error in test1.proto at 1:9: unexpected label: kabooom1", errors1.get("test1.proto").getMessage());
    assertEquals("java.lang.IllegalStateException: Syntax error in test1.proto at 1:9: unexpected label: kabooom1", errors2.get("test1.proto").getMessage());
    assertEquals("java.lang.IllegalStateException: Syntax error in test2.proto at 1:9: unexpected label: kabooom2", errors2.get("test2.proto").getMessage());
    assertTrue(ctx.getFileDescriptors().containsKey("test1.proto"));
    assertTrue(ctx.getFileDescriptors().containsKey("test2.proto"));
    assertFalse(ctx.getFileDescriptors().get("test1.proto").isResolved());
    assertFalse(ctx.getFileDescriptors().get("test2.proto").isResolved());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 3 with DescriptorParserException

use of org.infinispan.protostream.DescriptorParserException in project protostream by infinispan.

the class SerializationContextImplTest method testFileCannotExistWithParsingErrors.

/**
 * Test that files with syntax errors DO NOT get registered if there is no progress callback present.
 */
@Test
public void testFileCannotExistWithParsingErrors() {
    SerializationContext ctx = createContext();
    FileDescriptorSource source = FileDescriptorSource.fromString("file1.proto", "this is bogus");
    try {
        ctx.registerProtoFiles(source);
        fail("DescriptorParserException expected");
    } catch (DescriptorParserException e) {
        assertEquals("java.lang.IllegalStateException: Syntax error in file1.proto at 1:5: unexpected label: this", e.getMessage());
    }
    FileDescriptor fileDescriptor = ctx.getFileDescriptors().get("file1.proto");
    assertNull(fileDescriptor);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 4 with DescriptorParserException

use of org.infinispan.protostream.DescriptorParserException in project protostream by infinispan.

the class SerializationContextImplTest method testTwoFilesWithErrorsAtOnce.

@Test
public void testTwoFilesWithErrorsAtOnce() {
    SerializationContext ctx = createContext();
    Map<String, Throwable> errors = new HashMap<>();
    List<String> successful = new ArrayList<>();
    FileDescriptorSource source = FileDescriptorSource.fromString("test1.proto", "kabooom1").addProtoFile("test2.proto", "kabooom2").withProgressCallback(new FileDescriptorSource.ProgressCallback() {

        @Override
        public void handleError(String fileName, DescriptorParserException ex) {
            errors.put(fileName, ex);
        }

        @Override
        public void handleSuccess(String fileName) {
            successful.add(fileName);
        }
    });
    ctx.registerProtoFiles(source);
    assertTrue(successful.isEmpty());
    assertEquals(2, errors.size());
    assertTrue(errors.containsKey("test1.proto"));
    assertTrue(errors.containsKey("test2.proto"));
    assertEquals("java.lang.IllegalStateException: Syntax error in test1.proto at 1:9: unexpected label: kabooom1", errors.get("test1.proto").getMessage());
    assertEquals("java.lang.IllegalStateException: Syntax error in test2.proto at 1:9: unexpected label: kabooom2", errors.get("test2.proto").getMessage());
    assertTrue(ctx.getFileDescriptors().containsKey("test1.proto"));
    assertTrue(ctx.getFileDescriptors().containsKey("test2.proto"));
    assertFalse(ctx.getFileDescriptors().get("test1.proto").isResolved());
    assertFalse(ctx.getFileDescriptors().get("test2.proto").isResolved());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 5 with DescriptorParserException

use of org.infinispan.protostream.DescriptorParserException in project protostream by infinispan.

the class SerializationContextImplTest method testUnregisterFileWithErrors.

@Test
public void testUnregisterFileWithErrors() {
    SerializationContext ctx = createContext();
    Map<String, Throwable> errors = new HashMap<>();
    List<String> successful = new ArrayList<>();
    FileDescriptorSource source = FileDescriptorSource.fromString("test.proto", "kabooom").withProgressCallback(new FileDescriptorSource.ProgressCallback() {

        @Override
        public void handleError(String fileName, DescriptorParserException ex) {
            errors.put(fileName, ex);
        }

        @Override
        public void handleSuccess(String fileName) {
            successful.add(fileName);
        }
    });
    ctx.registerProtoFiles(source);
    assertTrue(successful.isEmpty());
    assertEquals(1, errors.size());
    assertTrue(errors.containsKey("test.proto"));
    assertEquals("java.lang.IllegalStateException: Syntax error in test.proto at 1:8: unexpected label: kabooom", errors.get("test.proto").getMessage());
    assertTrue(ctx.getFileDescriptors().containsKey("test.proto"));
    assertFalse(ctx.getFileDescriptors().get("test.proto").isResolved());
    ctx.unregisterProtoFile("test.proto");
    assertFalse(ctx.getFileDescriptors().containsKey("test.proto"));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Aggregations

DescriptorParserException (org.infinispan.protostream.DescriptorParserException)11 SerializationContext (org.infinispan.protostream.SerializationContext)8 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)4 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 AutoService (com.google.auto.service.AutoService)1 ProtoFile (com.squareup.protoparser.ProtoFile)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Optional (java.util.Optional)1