Bundles { protected static Path withExtension(Path path, String extension) { if (!extension.isEmpty() && !extension.startsWith(".")) throw new IllegalArgumentException( "Extension must be empty or start with ."); String p = path.getFileName().toString(); if (!extension.isEmpty() && p.toLowerCase().endsWith(extension.toLowerCase())) return path; String newP = p.replaceFirst("(\\.[^.]*)?$", extension); return path.resolveSibling(newP); }  static void closeAndSaveBundle(Bundle bundle, Path destination); static Path closeBundle(Bundle bundle); static void copyRecursively(final Path source,
			final Path destination, final CopyOption... copyOptions); static Bundle createBundle(); static Bundle createBundle(Path path); static void deleteRecursively(Path p); static Path getAnnotations(Bundle bundle); static Path getManifestPath(Bundle bundle); static String getMimeType(Bundle bundle); static URI getReference(Path path); static String getStringValue(Path path); static boolean isMissing(Path item); static boolean isReference(Path path); static boolean isValue(Path path); static Bundle openBundle(InputStream in); static Bundle openBundle(Path zip); static Bundle openBundle(URL url); static Bundle openBundleReadOnly(Path zip); static void safeCopy(Path source, Path destination); static void safeMove(Path source, Path destination); static void setMimeType(Bundle bundle, String mimetype); static Path setReference(Path path, URI ref); static void setStringValue(Path path, String string); static Path uriToBundlePath(Bundle bundle, URI uri);  }
@Test public void withExtension() throws Exception { Path testDir = Files.createTempDirectory("test"); testDir.toFile().deleteOnExit(); Path fileTxt = testDir.resolve("file.txt"); fileTxt.toFile().deleteOnExit(); assertEquals("file.txt", fileTxt.getFileName().toString()); Path fileHtml = Bundles.withExtension(fileTxt, ".html"); assertEquals(fileTxt.getParent(), fileHtml.getParent()); assertEquals("file.html", fileHtml.getFileName().toString()); Path fileDot = Bundles.withExtension(fileTxt, "."); assertEquals("file.", fileDot.getFileName().toString()); Path fileEmpty = Bundles.withExtension(fileTxt, ""); assertEquals("file", fileEmpty.getFileName().toString()); Path fileDoc = Bundles.withExtension(fileEmpty, ".doc"); assertEquals("file.doc", fileDoc.getFileName().toString()); Path fileManyPdf = Bundles.withExtension(fileTxt, ".test.many.pdf"); assertEquals("file.test.many.pdf", fileManyPdf.getFileName().toString()); Path fileManyTxt = Bundles.withExtension(fileManyPdf, ".txt"); assertEquals("file.test.many.txt", fileManyTxt.getFileName().toString()); }