ClassFileNameHandler { public File getUniqueFilenameForClass(String className) { if (className.charAt(0) != 'L' || className.charAt(className.length()-1) != ';') { throw new RuntimeException("Not a valid dalvik class name"); } int packageElementCount = 1; for (int i=1; i<className.length()-1; i++) { if (className.charAt(i) == '/') { packageElementCount++; } } String[] packageElements = new String[packageElementCount]; int elementIndex = 0; int elementStart = 1; for (int i=1; i<className.length()-1; i++) { if (className.charAt(i) == '/') { if (i-elementStart==0) { throw new RuntimeException("Not a valid dalvik class name"); } packageElements[elementIndex++] = className.substring(elementStart, i); elementStart = ++i; } } if (elementStart >= className.length()-1) { throw new RuntimeException("Not a valid dalvik class name"); } packageElements[elementIndex] = className.substring(elementStart, className.length()-1); return addUniqueChild(top, packageElements, 0); } ClassFileNameHandler(File path, String fileExtension);  ClassFileNameHandler(File path, String fileExtension, boolean caseSensitive,
                                boolean modifyWindowsReservedFilenames); File getUniqueFilenameForClass(String className);  }
@Test public void testMultipleLongNames() { String filenameFragment = Strings.repeat("a", 512); File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali"); File file1 = handler.getUniqueFilenameForClass("La/a/" + filenameFragment + "1" + filenameFragment + ";"); checkFilename(tempDir, file1, "a", "a", Strings.repeat("a", 124) + "#" + Strings.repeat("a", 118) + ".smali"); File file2 = handler.getUniqueFilenameForClass("La/a/" + filenameFragment + "2" + filenameFragment + ";"); checkFilename(tempDir, file2, "a", "a", Strings.repeat("a", 124) + "#" + Strings.repeat("a", 118) + ".1.smali"); Assert.assertFalse(file1.getAbsolutePath().equals(file2.getAbsolutePath())); }
@Test public void testBasicFunctionality() { File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali"); File file = handler.getUniqueFilenameForClass("La/b/c/d;"); checkFilename(tempDir, file, "a", "b", "c", "d.smali"); file = handler.getUniqueFilenameForClass("La/b/c/e;"); checkFilename(tempDir, file, "a", "b", "c", "e.smali"); file = handler.getUniqueFilenameForClass("La/b/d/d;"); checkFilename(tempDir, file, "a", "b", "d", "d.smali"); file = handler.getUniqueFilenameForClass("La/b;"); checkFilename(tempDir, file, "a", "b.smali"); file = handler.getUniqueFilenameForClass("Lb;"); checkFilename(tempDir, file, "b.smali"); }
@Test public void testCaseInsensitiveFilesystem() { File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali", false, false); File file = handler.getUniqueFilenameForClass("La/b/c;"); checkFilename(tempDir, file, "a", "b", "c.smali"); file = handler.getUniqueFilenameForClass("La/b/C;"); checkFilename(tempDir, file, "a", "b", "C.1.smali"); file = handler.getUniqueFilenameForClass("La/B/c;"); checkFilename(tempDir, file, "a", "B.1", "c.smali"); }
@Test public void testCaseSensitiveFilesystem() { File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali", true, false); File file = handler.getUniqueFilenameForClass("La/b/c;"); checkFilename(tempDir, file, "a", "b", "c.smali"); file = handler.getUniqueFilenameForClass("La/b/C;"); checkFilename(tempDir, file, "a", "b", "C.smali"); file = handler.getUniqueFilenameForClass("La/B/c;"); checkFilename(tempDir, file, "a", "B", "c.smali"); }
@Test public void testWindowsReservedFilenames() { File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali", false, true); File file = handler.getUniqueFilenameForClass("La/con/c;"); checkFilename(tempDir, file, "a", "con#", "c.smali"); file = handler.getUniqueFilenameForClass("La/Con/c;"); checkFilename(tempDir, file, "a", "Con#.1", "c.smali"); file = handler.getUniqueFilenameForClass("La/b/PRN;"); checkFilename(tempDir, file, "a", "b", "PRN#.smali"); file = handler.getUniqueFilenameForClass("La/b/prN;"); checkFilename(tempDir, file, "a", "b", "prN#.1.smali"); file = handler.getUniqueFilenameForClass("La/b/com0;"); checkFilename(tempDir, file, "a", "b", "com0.smali"); for (String reservedName: new String[] {"con", "prn", "aux", "nul", "com1", "com9", "lpt1", "lpt9"}) { file = handler.getUniqueFilenameForClass("L" + reservedName + ";"); checkFilename(tempDir, file, reservedName +"#.smali"); } }
@Test public void testIgnoringWindowsReservedFilenames() { File tempDir = Files.createTempDir(); ClassFileNameHandler handler = new ClassFileNameHandler(tempDir, ".smali", true, false); File file = handler.getUniqueFilenameForClass("La/con/c;"); checkFilename(tempDir, file, "a", "con", "c.smali"); file = handler.getUniqueFilenameForClass("La/Con/c;"); checkFilename(tempDir, file, "a", "Con", "c.smali"); file = handler.getUniqueFilenameForClass("La/b/PRN;"); checkFilename(tempDir, file, "a", "b", "PRN.smali"); file = handler.getUniqueFilenameForClass("La/b/prN;"); checkFilename(tempDir, file, "a", "b", "prN.smali"); file = handler.getUniqueFilenameForClass("La/b/com0;"); checkFilename(tempDir, file, "a", "b", "com0.smali"); for (String reservedName: new String[] {"con", "prn", "aux", "nul", "com1", "com9", "lpt1", "lpt9"}) { file = handler.getUniqueFilenameForClass("L" + reservedName + ";"); checkFilename(tempDir, file, reservedName +".smali"); } }