ServiceProviderExtensionStorage extends ExtensionStorage { @Override public Map<String, Set<String>> read() { Map<String, Set<String>> extensions = new HashMap<>(); for (String extensionPoint : processor.getExtensions().keySet()) { try { FileObject file = getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE + "/" + extensionPoint); Set<String> entries = new HashSet<>(); ExtensionStorage.read(file.openReader(true), entries); extensions.put(extensionPoint, entries); } catch (FileNotFoundException | NoSuchFileException e) { } catch (FilerException e) { } catch (IOException e) { error(e.getMessage()); } } return extensions; } ServiceProviderExtensionStorage(ExtensionAnnotationProcessor processor); @Override Map<String, Set<String>> read(); @Override void write(Map<String, Set<String>> extensions); static final String EXTENSIONS_RESOURCE; }
@Test public void ensureServiceProviderExtensionStorageReadWorks() throws IOException { final StringReader file = new StringReader("#hello\n World"); final Set<String> entries = new HashSet<>(); ServiceProviderExtensionStorage.read(file, entries); assertThat(entries.size(), is(1)); assertThat(entries.contains("World"), is(true)); }
@Test public void ensureReadingExtensionsProducesCorrectListOfExtensions() { final StringReader file = new StringReader("#hello\n World"); final ExtensionAnnotationProcessor processor = mock(ExtensionAnnotationProcessor.class); final Map<String, Set<String>> extensions = new HashMap<>(); extensions.put("hello", Collections.singleton("world")); given(processor.getExtensions()).willReturn(extensions); ServiceProviderExtensionStorage extensionStorage = new ServiceProviderExtensionStorage(processor) { @Override protected Filer getFiler() { try { Filer filer = mock(Filer.class); FileObject fileObject = mock(FileObject.class); given(fileObject.openReader(true)).willReturn(file); given(filer.getResource( any(StandardLocation.class), any(String.class), any(String.class) )).willReturn(fileObject); return filer; } catch(IOException ex) { throw new IllegalStateException("Shouldn't have gotten here"); } } }; Map<String, Set<String>> read = extensionStorage.read(); assertThat(read.containsKey("hello"), is(true)); assertThat(read.get("hello"), is(Collections.singleton("World"))); }