/** * Stores an Object marshalled value as a VFSRegistryEntry in the target path. * @param path a path for storing the generated VFSRegistryEntry. * @param value an object value to marshall and store. * @throws Exception exceptions might be thrown in cases of filesystem or marshalling errors. */ public void storeEntry(final Path path, final Object value) throws Exception { final Marshaller marshaller = marshallerRegistry.get(value.getClass()); if (marshaller == null) { throw new Exception("No marshaller was found for class: " + value.getClass()); } @SuppressWarnings("unchecked") final String marshalledValue = marshaller.marshal(value); final VFSRegistryEntry entry = new VFSRegistryEntry(value.getClass().getName(), marshalledValue); final String content = entryMarshaller.marshal(entry); writeBatch(path, content); }
@Override public Marshaller<VFSRegistryEntry> createMarshaller() { return new VFSRegistryEntryMarshaller(); }
/** * Reads an Object previously marshalled and stored as a VFSRegistryEntry in a given path. * @param path the path where the VFSRegistryEntry is stored. * @return the unmarshalled object backed by the VFSRegistryEntry. * @throws Exception exceptions might be thrown in cases of filesystem or marshalling errors. */ public Object readEntry(final Path path) throws Exception { final String entryContent = ioService.readAllString(path); final VFSRegistryEntry entry = entryMarshaller.unmarshal(entryContent); final Marshaller marshaller = marshallerRegistry.get(Class.forName(entry.getContentType())); if (marshaller == null) { throw new Exception("No marshaller was found for class: " + entry.getContentType()); } return marshaller.unmarshal(entry.getContent()); }
@Test public void testReadEntry() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); Object unmarshalledValue = mock(Object.class); when(marshaller.unmarshal(MARSHALLED_VALUE)).thenReturn(unmarshalledValue); Object result = registryHelper.readEntry(path); assertEquals(unmarshalledValue, result); }
@Test public void testReadEntry() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); Object unmarshalledValue = mock(Object.class); when(marshaller.unmarshal(MARSHALLED_VALUE)).thenReturn(unmarshalledValue); Object result = registryHelper.readEntry(path); assertEquals(unmarshalledValue, result); }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
@Override public Marshaller<VFSRegistryEntry> createMarshaller() { return new VFSRegistryEntryMarshaller(); }
@Test public void testReadEntryWhenMarshallerNotExists() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(marshallerRegistry.get(Object.class.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + entry.getContentType()); registryHelper.readEntry(path); }
@Test public void testStoreEntryWhenMarshallerExists() throws Exception { when(marshallerRegistry.get(value.getClass())).thenReturn(marshaller); when(marshaller.marshal(value)).thenReturn(MARSHALLED_VALUE); VFSRegistryEntry expectedEntry = new VFSRegistryEntry(value.getClass().getName(), MARSHALLED_VALUE); when(entryMarshaller.marshal(expectedEntry)).thenReturn(MARSHALLED_ENTRY); registryHelper.storeEntry(path, value); verify(marshallerRegistry, times(1)).get(value.getClass()); verify(registryHelper, times(1)).writeBatch(path, MARSHALLED_ENTRY); }
@Test public void testReadEntryWhenMarshallerNotExists() throws Exception { when(ioService.readAllString(path)).thenReturn(MARSHALLED_ENTRY); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(marshallerRegistry.get(Object.class.getClass())).thenReturn(null); expectedException.expectMessage("No marshaller was found for class: " + entry.getContentType()); registryHelper.readEntry(path); }
private void prepareReadEntries() throws Exception { entryPaths = mockList(Path.class, ENTRY_COUNT); Iterator<Path> pathIterator = entryPaths.iterator(); expectedObjects = mockList(Object.class, ENTRY_COUNT); entries = mockList(VFSRegistryEntry.class, ENTRY_COUNT); DirectoryStream<Path> directoryStream = mock(DirectoryStream.class); when(directoryStream.iterator()).thenReturn(pathIterator); when(ioService.newDirectoryStream(rootPath, filter)).thenReturn(directoryStream); for (int i = 0; i < ENTRY_COUNT; i++) { VFSRegistryEntry entry = entries.get(i); when(ioService.readAllString(entryPaths.get(i))).thenReturn(MARSHALLED_ENTRY + i); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY + i)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE + i); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); when(marshaller.unmarshal(MARSHALLED_VALUE + i)).thenReturn(expectedObjects.get(i)); } } }
private void prepareReadEntries() throws Exception { entryPaths = mockList(Path.class, ENTRY_COUNT); Iterator<Path> pathIterator = entryPaths.iterator(); expectedObjects = mockList(Object.class, ENTRY_COUNT); entries = mockList(VFSRegistryEntry.class, ENTRY_COUNT); DirectoryStream<Path> directoryStream = mock(DirectoryStream.class); when(directoryStream.iterator()).thenReturn(pathIterator); when(ioService.newDirectoryStream(rootPath, filter)).thenReturn(directoryStream); for (int i = 0; i < ENTRY_COUNT; i++) { VFSRegistryEntry entry = entries.get(i); when(ioService.readAllString(entryPaths.get(i))).thenReturn(MARSHALLED_ENTRY + i); when(entryMarshaller.unmarshal(MARSHALLED_ENTRY + i)).thenReturn(entry); when(entry.getContentType()).thenReturn(Object.class.getName()); when(entry.getContent()).thenReturn(MARSHALLED_VALUE + i); when(marshallerRegistry.get(any(Class.class))).thenReturn(marshaller); when(marshaller.unmarshal(MARSHALLED_VALUE + i)).thenReturn(expectedObjects.get(i)); } } }