/** * 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 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 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)); } } }