### Question:
FileUtils { public static FileOutputStream openOutputStream(File file, boolean append) throws IOException { if (file.exists()) { if (file.isDirectory()) { throw new IOException("File '" + file + "' exists but is a directory"); } if (!file.canWrite()) { throw new IOException("File '" + file + "' cannot be written to"); } } else { File parent = file.getParentFile(); if (parent != null) { if (!parent.mkdirs() && !parent.isDirectory()) { throw new IOException("Directory '" + parent + "' could not be created"); } } } return new FileOutputStream(file, append); }  static void writeByteArrayToFile(File file, byte[] data); static void writeByteArrayToFile(File file, byte[] data, boolean append); static FileOutputStream openOutputStream(File file, boolean append); static void saveCommandHistory(List<int[]> history, File file); static List<int[]> loadCommandHistory(File file); static void saveCommandHistoryString(List<String> history, File file); static List<String> loadCommandHistoryString(File file); static String readFileToString(File file, Charset encoding); static Properties readProperties(String file);  }### Answer:
@Test public void testOpenOutputStreamIsDirectory() throws IOException { thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' exists but is a directory"))); FileUtils.openOutputStream(getTestDirectory(), true); thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' exists but is a directory"))); FileUtils.openOutputStream(getTestDirectory(), false); }
@Test public void testOpenOutputStreamCannotWrite() throws IOException { thrown.expectMessage(allOf(startsWith("File '") ,endsWith("' cannot be written to"))); File targetFile = temporaryFolder.newFile("cannotWrite.txt"); targetFile.setWritable(false); FileUtils.openOutputStream(targetFile, true); }
@Test public void testOpenOutputStream() throws IOException { File targetFile = temporaryFolder.newFile("targetFile.txt"); FileOutputStream outputStream = FileUtils.openOutputStream(targetFile, true); Assert.assertNotNull(outputStream); outputStream.close(); }