MetadataCreator { public Metadata create() { return MetadataBuilder.newMetadata() .withPath(Paths.convert(path)) .withRealPath(Paths.convert(path.toRealPath())) .withCheckinComment(getCheckinComment()) .withLastContributor(getLastContributor()) .withCreator(getCreator()) .withLastModified(getLastModified()) .withDateCreated(getDateCreated()) .withSubject(getSubject()) .withType(getType()) .withExternalRelation(getExternalRelation()) .withExternalSource(getExternalSource()) .withDescription(getDescription()) .withTags(getTags()) .withDiscussion(getDiscussion()) .withLockInfo(retrieveLockInfo(Paths.convert(path))) .withVersion(getVersion()) .withGenerated(getGenerated()) .build(); } MetadataCreator(Path path,
                           IOService configIOService,
                           SessionInfo sessionInfo,
                           DublinCoreView dublinCoreView,
                           DiscussionView discussionView,
                           OtherMetaView otherMetaView,
                           VersionAttributeView versionAttributeView,
                           GeneratedAttributesView generatedAttributesView); Metadata create();  }
@Test public void testGeneratedAttributes() { when(generatedFileAttributes.isGenerated()).thenReturn(true); when(generatedAttributesView.readAttributes()).thenReturn(generatedFileAttributes); Metadata metadata = service.create(); assertTrue(metadata.isGenerated()); }
@Test public void testConcurrency() throws Throwable { FileSystemProviders.getDefaultProvider(); final AtomicBoolean exists = new AtomicBoolean(false); when(configIOService.exists(any(Path.class))).<Boolean>thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return exists.get(); } }); when(configIOService.write(any(Path.class), any(String.class))).<Path>thenAnswer(new Answer<Path>() { @Override public Path answer(final InvocationOnMock invocation) throws Throwable { exists.set(true); return mainFilePath; } }); when(configIOService.readAllString(any(Path.class))).<String>thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { if (!exists.get()) { throw new NoSuchFileException(); } return "content"; } }); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { exists.set(false); return null; } }).when(configIOService).delete(any(Path.class)); final int THREADS = 100; final Result result = new Result(); final ExecutorService es = Executors.newCachedThreadPool(); for (int i = 0; i < THREADS; i++) { final int threadCount = i; final Operation op = i % 2 == 1 ? Operation.WRITE : Operation.CHECK; es.execute(new Runnable() { @Override public void run() { try { System.out.println("[Thread : " + threadCount + "] Running..."); switch (op) { case WRITE: System.out.println("[Thread : " + threadCount + "] Writing..." + output()); configIOService.write(mainFilePath, "content"); configIOService.delete(mainFilePath); break; case CHECK: System.out.println("[Thread : " + threadCount + "] Checking..." + output()); service.create(); } } catch (Throwable e) { result.setFailed(true); result.setException(e); } finally { System.out.println("[Thread : " + threadCount + "] Completed."); } } private String output() { if (exists.get()) { return "Exists"; } else { return "Not exists"; } } }); } try { es.shutdown(); es.awaitTermination(1000 * 5, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } if (result.isFailed()) { throw result.getException(); } }