### Question:
BuilderContext { public final boolean checkAndRecordWrite(String file) { String normalized = normalizer.normalize(file); if (writeExceptionsMatcher.matches(normalized)) { return true; } synchronized (writeLock) { if (writes.contains(normalized) || tempWrites.contains(normalized)) { return true; } if (workspace.isRegularFile(toPath(normalized)) && !readAndTrackExceptionsMatcher.includes(normalized)) { return false; } if (writeMatcher.includes(normalized)) { writes.add(normalized); recordInprogressWrite(normalized); workspace.processOutput(toPath(normalized)); return true; } if (tempMatcher.includes(normalized)) { tempWrites.add(normalized); recordInprogressWrite(normalized); return true; } } return false; } private  BuilderContext(Logger log, PathNormalizer normalizer, String id, PathMatcher readMatcher,
      PathMatcher writeMatcher, PathMatcher tempMatcher, MessageCollector messages,
      Collection<String> execExceptions, boolean networkAccessAllowed,
      Collection<String> readExceptions, Collection<String> writeExceptions,
      PathMatcher readAndTrackExceptionsMatcher, InprogressStateWriter inprogressWriter,
      BuilderWorkspace workspace); void enter(); void leave(); static Builder builder(Logger log, String id, Path sessionBasedir,
      MessageCollector messages, BuilderWorkspace workspace); @Override String toString(); final boolean checkRead(String file); final boolean checkAndRecordWrite(String file); final boolean checkExec(String command); final boolean checkSockets(); boolean checkAndRecordProperty(String action, String name); boolean addViolation(EnforcerViolation violation); Set<EnforcerViolation> getViolations(); Collection<String> getWrittenFiles(); Collection<String> getTemporaryFiles(); Collection<String> getReadProperties(); String getId(); MessageCollector getMessages(); boolean wasWhitelistedException(String file); static Messages MESSAGES; }### Answer:
@Test public void testWriteExistingFiles() throws Exception { File output = temp.newFolder(); BuilderContext testee = newBuilder() .addOutputDirectory(output.toPath()) .build(); create(output, "a"); assertFalse(testee.checkAndRecordWrite(new File(output, "a").getAbsolutePath())); }
@Test public void testWriteMultipleTimes() throws Exception { File output = temp.newFolder(); BuilderContext testee = newBuilder() .addOutputDirectory(output.toPath()) .build(); File file = new File(output, "a").getAbsoluteFile(); assertTrue(testee.checkAndRecordWrite(file.getAbsolutePath())); assertTrue(file.createNewFile()); assertTrue(testee.checkAndRecordWrite(file.getAbsolutePath())); }
@Test public void testCreateExistingOutputDirectory() throws Exception { File dir = temp.newFolder().getCanonicalFile(); BuilderContext testee = newBuilder() .addOutputDirectory(dir.toPath()) .build(); assertTrue(testee.checkAndRecordWrite(dir.getCanonicalPath())); }