ResourceVersionedSchemaSetProvider implements VersionedSchemaSetProvider { @Override public VersionedSchemaSet loadSchemas() { VersionedSchemaSet result = new VersionedSchemaSet(); InputStream indexIns = null; indexIns = _classLoader.getResourceAsStream(INDEX_RESOURCE_NAME); if (null == indexIns) { LOG.info("resource not found: " + INDEX_RESOURCE_NAME + "; no schemas will be loaded"); } else { BufferedReader indexReader = null; try { indexReader = new BufferedReader(new InputStreamReader(indexIns, "UTF-8")); List<String> resourceNames = readIndex(indexReader); LOG.info("schema resources found: " + resourceNames); for (String resource: resourceNames) { VersionedSchema schema = readSchemaFromResource(resource); if (null != schema) result.add(schema); } } catch (IOException ioe) { LOG.error("i/o error: " + ioe.getMessage(), ioe); } finally { try { indexIns.close(); if (null != indexReader) indexReader.close(); } catch (IOException e1) { LOG.error("cleanup failed: " + e1.getMessage(), e1); } } } return result; } ResourceVersionedSchemaSetProvider(ClassLoader classLoader); @Override VersionedSchemaSet loadSchemas(); static final String MODULE; static final Logger LOG; static final String INDEX_RESOURCE_NAME; }
@Test public void testStandardSchema() throws Exception { ResourceVersionedSchemaSetProvider resourceProvider = new ResourceVersionedSchemaSetProvider(this.getClass().getClassLoader()); VersionedSchemaSet schemaSet = resourceProvider.loadSchemas(); VersionedSchema fakeExistingSchema = schemaSet.getLatestVersionByName("com.linkedin.events.example.fake.FakeSchema"); Assert.assertNotNull(fakeExistingSchema); Assert.assertTrue(fakeExistingSchema.getVersion() >= 2, "version >= 2: " + fakeExistingSchema.getVersion()); VersionedSchema personSchema = schemaSet.getLatestVersionByName("com.linkedin.events.example.person.Person"); Assert.assertNotNull(personSchema); VersionedSchema fakeNonExistingSchema = schemaSet.getLatestVersionByName("fake.schema"); Assert.assertTrue(null == fakeNonExistingSchema); }