/** * Parse a collection of {@link HollowSchema}s from the provided String. * * @param schemas the schemas as a string * @return the list of schema * @throws IOException if the schema cannot be parsed */ public static List<HollowSchema> parseCollectionOfSchemas(String schemas) throws IOException { return parseCollectionOfSchemas(new StringReader(schemas)); }
/** * Reads a schema file into the provided HollowWriteStateEngine. The schema file must be on the classpath. * * @param schemaFilePath the path to the schema * @param engine the write state engine * @throws IOException if the schema could not be read */ public static void readSchemaFileIntoWriteState(String schemaFilePath, HollowWriteStateEngine engine) throws IOException { InputStream input = null; try { input = HollowWriteStateCreator.class.getClassLoader().getResourceAsStream(schemaFilePath); Collection<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(new BufferedReader(new InputStreamReader(input))); populateStateEngineWithTypeWriteStates(engine, schemas); } finally { if (input != null) { input.close(); } } }
@Test public void parsesManySchemas() throws IOException { String manySchemas = "/* This is a comment\n" + " consisting of multiple lines */\n" + " TypeA {\n" + " int a1;\n" + " \tstring a2; //This is a comment\n" + " String a3;\n" + "}\n\n"+ "MapOfStringToTypeA Map<String, TypeA>;\n"+ "ListOfTypeA List<TypeA>;\n"+ "TypeB { float b1; double b2; boolean b3; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(manySchemas); Assert.assertEquals(4, schemas.size()); }
@Test public void testParseCollectionOfSchemas_reader() throws Exception { InputStream input = null; try { input = getClass().getResourceAsStream("/schema1.txt"); List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(new BufferedReader(new InputStreamReader(input))); Assert.assertEquals("Should have two schemas", 2, schemas.size()); Assert.assertEquals("Should have Minion schema", "Minion", schemas.get(0).getName()); Assert.assertEquals("Should have String schema", "String", schemas.get(1).getName()); } finally { if (input != null) { input.close(); } } } }
@Test public void sortsSchemasEvenIfDependencyTypesNotPresent() throws IOException { String schemasText = "TypeA { TypeB b; }" + "TypeB { TypeC c; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(schemasText); List<HollowSchema> sortedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(schemas); Assert.assertEquals(2, sortedSchemas.size()); Assert.assertEquals("TypeB", sortedSchemas.get(0).getName()); Assert.assertEquals("TypeA", sortedSchemas.get(1).getName()); }
@Test public void schemasAreSortedBasedOnDependencies() throws IOException { String schemasText = "TypeB {" + " ListOfString str;" + "}" + "" + "String {" + " string value;" + "}" + "" + "ListOfString List<String>;" + "" + "TypeA {" + " TypeB b;" + " String str;" + "}"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(schemasText); List<HollowSchema> sortedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(schemas); Assert.assertEquals(4, sortedSchemas.size()); Assert.assertEquals("String", sortedSchemas.get(0).getName()); Assert.assertEquals("ListOfString", sortedSchemas.get(1).getName()); Assert.assertEquals("TypeB", sortedSchemas.get(2).getName()); Assert.assertEquals("TypeA", sortedSchemas.get(3).getName()); }
@Test public void determinesIfSchemasAreTransitivelyDependent() throws IOException { String schemasText = "TypeA { TypeB b; }" + "TypeB { TypeC c; }" + "TypeC { TypeD d; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(schemasText); HollowWriteStateEngine stateEngine = new HollowWriteStateEngine(); HollowWriteStateCreator.populateStateEngineWithTypeWriteStates(stateEngine, schemas); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeA", "TypeB")); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeA", "TypeC")); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeB", "TypeC")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeC", "TypeB")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeB", "TypeA")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeC", "TypeA")); }