### Question:
SourceProjectImpl implements SourceProject { @Override public void addCompilationUnit(String packageName, String name, CompilationUnit compilationUnit) { Path path = sourceRoot.getRoot().resolve(packageToPath(packageName)); compilationUnit.setStorage(path.resolve(name + ".java")); String compilationCacheKey = packageName + "." + name; if (compilationUnitCache.containsKey(compilationCacheKey)) { CompilationUnit alreadyAdded = compilationUnitCache.get(compilationCacheKey); if (!alreadyAdded.equals(compilationUnit)) { throw new RuntimeException( "Duplicate interface/class/enum found on " + compilationCacheKey); } } compilationUnitCache.put(compilationCacheKey, compilationUnit); sourceRoot.add(compilationUnit); } SourceProjectImpl(Path outputLocation);  SourceProjectImpl(SourceRoot sourceRoot); @Override void addCompilationUnit(String packageName, String name, CompilationUnit compilationUnit); @Override void saveAll();  }### Answer:
@Test public void testAddCompilationUnit() { CompilationUnit compilationUnit1 = new CompilationUnit(); expect(path.resolve("com/github/kklisura")) .andReturn(Paths.get("source-project/main/com/github/kklisura")); expect(sourceRoot.getRoot()).andReturn(path); expect(sourceRoot.add(compilationUnit1)).andReturn(sourceRoot); replayAll(); sourceProject.addCompilationUnit("com.github.kklisura", "name", compilationUnit1); verifyAll(); assertTrue( compilationUnit1 .getStorage() .get() .getPath() .endsWith("source-project/main/com/github/kklisura/name.java")); }
@Test(expected = RuntimeException.class) public void testAddCompilationUnitThrowsExceptionWhenAddingDuplicateCompilationUnit() { CompilationUnit compilationUnit1 = new CompilationUnit(); CompilationUnit compilationUnit2 = new CompilationUnit(); compilationUnit2.addClass("TestClass"); expect(path.resolve("com/github/kklisura")) .andReturn(Paths.get("source-project/main/com/github/kklisura")) .times(2); expect(sourceRoot.getRoot()).andReturn(path).times(2); expect(sourceRoot.add(compilationUnit1)).andReturn(sourceRoot); expect(sourceRoot.add(compilationUnit2)).andReturn(sourceRoot); replayAll(); sourceProject.addCompilationUnit("com.github.kklisura", "name", compilationUnit1); sourceProject.addCompilationUnit("com.github.kklisura", "name", compilationUnit2); }
@Test public void testAddCompilationUnitAddingDuplicateCompilationUnit() { CompilationUnit compilationUnit1 = new CompilationUnit(); expect(path.resolve("com/github/kklisura")) .andReturn(Paths.get("source-project/main/com/github/kklisura")) .times(2); expect(sourceRoot.getRoot()).andReturn(path).times(2); expect(sourceRoot.add(compilationUnit1)).andReturn(sourceRoot).times(2); replayAll(); sourceProject.addCompilationUnit("com.github.kklisura", "name", compilationUnit1); sourceProject.addCompilationUnit("com.github.kklisura", "name", compilationUnit1); verifyAll(); assertTrue( compilationUnit1 .getStorage() .get() .getPath() .endsWith("source-project/main/com/github/kklisura/name.java")); }