PathResolver { @SuppressWarnings("IndexOfReplaceableByContains") public static List<File> resolvePath(File incPath, File cwd) { if(cwd == null) cwd = new File(Config.getHomeDir()); if(incPath.toString().startsWith(CURRENT_WITH_SEPARATOR)) { incPath = new File(incPath.toString().substring(CURRENT_WITH_SEPARATOR.length())); } if(incPath.toString().startsWith(TILDE_WITH_SEPARATOR)) { if(Config.getHomeDir().endsWith(Config.getPathSeparator())) incPath = new File(Config.getHomeDir()+incPath.toString().substring(2)); else incPath = new File(Config.getHomeDir()+incPath.toString().substring(1)); } if(incPath.toString().indexOf(TILDE) == 0) { if(incPath.toString().length() > 1) { incPath = new File(cwd.toString() + Config.getPathSeparator() + incPath.toString()); } else { incPath = new File(Config.getHomeDir()); } } if(incPath.toString().indexOf(SEPARATOR_CURRENT_SEPARATOR) > -1) { int index = incPath.toString().indexOf(SEPARATOR_CURRENT_SEPARATOR); if(index == 0) { incPath = new File(incPath.toString().substring(SEPARATOR_CURRENT_SEPARATOR.length()-1)); } else { incPath = new File(incPath.toString().substring(0, index) + incPath.toString().substring(index+2, incPath.toString().length())); } } if( incPath.toString().indexOf(ROOT) != 0 && incPath.toString().indexOf(DRIVER_SEPARATOR) == -1 && !cwd.toString().equals(ROOT)) { if(cwd.toString().endsWith(Config.getPathSeparator())) incPath = new File(cwd.toString() + incPath.toString()); else incPath = new File(cwd.toString() + Config.getPathSeparator() + incPath.toString()); } if(incPath.toString().indexOf(PARENT_WITH_SEPARATOR) > -1) { String tmp = incPath.toString(); while(tmp.indexOf(PARENT_WITH_SEPARATOR) > -1) { int index = tmp.indexOf(PARENT_WITH_SEPARATOR); if(index == 0) { tmp = tmp.substring(PARENT_WITH_SEPARATOR.length()); } else { File tmpFile = new File(tmp.substring(0, index)); tmpFile = tmpFile.getParentFile(); if(tmpFile == null) tmpFile = new File(Config.getPathSeparator()); tmpFile = new File(tmpFile.toString() + tmp.substring(index+ PARENT_WITH_SEPARATOR.length()-1)); tmp = tmpFile.toString(); } } incPath = new File(tmp); } if(incPath.toString().endsWith(PARENT)) { incPath = new File(incPath.toString().substring(0, incPath.toString().length()-PARENT.length())); incPath = incPath.getParentFile(); if(incPath == null) incPath = new File(Config.getPathSeparator()); } if( incPath.toString().indexOf(STAR) > -1 || incPath.toString().indexOf(WILDCARD) > -1) { PathCriteria pathCriteria = parsePath(incPath); if(incPath.toString().indexOf(SEPARATOR) > -1) { List<File> foundFiles = null; if(pathCriteria.getCriteria().equals(String.valueOf(STAR))) { foundFiles = new ArrayList<>(); foundFiles.add(new File(pathCriteria.getParentPath())); } else foundFiles = findFiles(new File(pathCriteria.parentPath), pathCriteria.getCriteria(), false); if(pathCriteria.childPath.length() == 0) return foundFiles; else { List<File> outFiles = new ArrayList<>(); for(File f : foundFiles) if(new File(f+Config.getPathSeparator()+pathCriteria.childPath).exists()) outFiles.add(new File(f+Config.getPathSeparator()+pathCriteria.childPath)); return outFiles; } } else { if(incPath.toString().length() == 1) { List<File> foundFiles = findFiles(new File(pathCriteria.parentPath), pathCriteria.getCriteria(), false); if(pathCriteria.childPath.length() == 0) return foundFiles; } return new ArrayList<File>(); } } else { ArrayList<File> fileList = new ArrayList<>(1); fileList.add(incPath); return fileList; } }  @SuppressWarnings("IndexOfReplaceableByContains") static List<File> resolvePath(File incPath, File cwd);  }
@Test public void testListFiles() { File tmp = tempDir.toFile(); File child1 = new File(tmp + Config.getPathSeparator()+"child1"); File child2 = new File(tmp + Config.getPathSeparator()+"child2"); File child11 = new File(child1 + Config.getPathSeparator()+"child11"); File child12 = new File(child1 + Config.getPathSeparator()+"child12"); File child21 = new File(child1 + Config.getPathSeparator()+"child21"); File child22 = new File(child1 + Config.getPathSeparator()+"child22"); assertEquals(child1, PathResolver.resolvePath(new File(""), child1).get(0)); assertEquals(child1, PathResolver.resolvePath(new File(".."), child11).get(0)); assertEquals(child1, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()), child11).get(0)); assertEquals(tmp, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+".."), child11).get(0)); assertEquals(tmp, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+ ".."+Config.getPathSeparator()), child11).get(0)); if(Config.isOSPOSIXCompatible()) { assertEquals(tmp.getParentFile(), PathResolver.resolvePath(new File("../../../"), child11).get(0)); assertEquals(tmp.getParentFile().getParentFile(), PathResolver.resolvePath(new File("../../../../"), child11).get(0)); } assertEquals(child11, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+"child11"), child11).get(0)); assertEquals(child1, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+".."+Config.getPathSeparator()+"child1"), child11).get(0)); assertEquals(child11, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+".."+ Config.getPathSeparator()+"child1"+Config.getPathSeparator()+"child11"), child11).get(0)); assertEquals(tmp, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+".."+ Config.getPathSeparator()+".."+Config.getPathSeparator()+tmp.getName()), child11).get(0)); assertEquals(child11, PathResolver.resolvePath(new File("child11"), child1).get(0)); assertEquals(child11, PathResolver.resolvePath(new File("child1"+Config.getPathSeparator()+"child11"), tmp).get(0)); assertEquals(child11, PathResolver.resolvePath(new File("."+Config.getPathSeparator()+"child11"), child1).get(0)); assertEquals(child1, PathResolver.resolvePath( new File(".."+Config.getPathSeparator()+"child1"+Config.getPathSeparator()), child1).get(0)); assertEquals(child1, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+"child1"+ Config.getPathSeparator()+".."+Config.getPathSeparator()+"child1"), child1).get(0)); assertEquals(child11, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+"child1"+ Config.getPathSeparator()+".."+Config.getPathSeparator()+"child1"+Config.getPathSeparator()+"child11"), child1).get(0)); assertEquals(child11, PathResolver.resolvePath(new File(".."+Config.getPathSeparator()+ "child1"+Config.getPathSeparator()+"."+Config.getPathSeparator()+"child11"), child1).get(0)); assertEquals(child11, PathResolver.resolvePath(new File("."+Config.getPathSeparator()+".."+ Config.getPathSeparator()+"child1"+Config.getPathSeparator()+"."+Config.getPathSeparator()+"child11"), child1).get(0)); System.setProperty("user.home", tempDir.toString()+Config.getPathSeparator()+"home"); assertEquals(new File(Config.getHomeDir()), PathResolver.resolvePath(new File("~/../home"), child1).get(0)); assertEquals(new File(Config.getHomeDir()), PathResolver.resolvePath(new File("~"), child1).get(0)); assertEquals(new File(child1, "~a"), PathResolver.resolvePath(new File("~a"), child1).get(0)); }
@Test public void testWildcards() throws IOException { File tmp = tempDir.toFile(); File child1 = new File(tempDir + Config.getPathSeparator()+"child1"); File child2 = new File(tempDir + Config.getPathSeparator()+"child2"); File child3 = new File(tempDir + Config.getPathSeparator()+"child3"); if(Config.isOSPOSIXCompatible()) { Files.createDirectory(child1.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child2.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child3.toPath(), fileAttribute).toFile().deleteOnExit(); } else { Files.createDirectory(child1.toPath()).toFile().deleteOnExit(); Files.createDirectory(child2.toPath()).toFile().deleteOnExit(); Files.createDirectory(child3.toPath()).toFile().deleteOnExit(); } List<File> files = PathResolver.resolvePath(new File("*"), tmp); assertEquals(1, files.size()); }
@Test public void testSearchFiles() throws IOException { File child1 = new File(tempDir + Config.getPathSeparator()+"child1"); File child2 = new File(tempDir + Config.getPathSeparator()+"child2"); File child11 = new File(child1 + Config.getPathSeparator()+"child11"); File child111 = new File(child11 + Config.getPathSeparator()+"child111"); File child12 = new File(child1 + Config.getPathSeparator()+"child12"); File child21 = new File(child2 + Config.getPathSeparator()+"child21"); File child22 = new File(child2 + Config.getPathSeparator()+"child22"); if(Config.isOSPOSIXCompatible()) { Files.createDirectory(child1.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child2.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child11.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child12.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child21.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child22.toPath(), fileAttribute).toFile().deleteOnExit(); Files.createDirectory(child111.toPath(), fileAttribute).toFile().deleteOnExit(); } else { Files.createDirectory(child1.toPath()).toFile().deleteOnExit(); Files.createDirectory(child2.toPath()).toFile().deleteOnExit(); Files.createDirectory(child11.toPath()).toFile().deleteOnExit(); Files.createDirectory(child12.toPath()).toFile().deleteOnExit(); Files.createDirectory(child21.toPath()).toFile().deleteOnExit(); Files.createDirectory(child22.toPath()).toFile().deleteOnExit(); Files.createDirectory(child111.toPath()).toFile().deleteOnExit(); } ArrayList<File> expected = new ArrayList<>(); expected.add(child11); expected.add(child12); List<File> actual = PathResolver.resolvePath( new File(child1.getAbsolutePath() + Config.getPathSeparator() + "*"), new File(Config.getPathSeparator())); assertEquals(1, actual.size()); actual = PathResolver.resolvePath( new File(child1.getAbsolutePath()+Config.getPathSeparator()+"child*"), new File(Config.getPathSeparator())); assertEquals(expected.size(), actual.size()); for(File f : expected) assertTrue( actual.contains(f)); actual = PathResolver.resolvePath( new File(tempDir.toFile().getName()+Config.getPathSeparator()+"child*"), new File(Config.getTmpDir())); assertEquals(expected.size(), actual.size()); assertTrue( actual.contains(child1)); assertTrue( actual.contains(child2)); actual = PathResolver.resolvePath( new File(child1.getAbsolutePath()+Config.getPathSeparator()+"child*"+Config.getPathSeparator()+"child111"), new File(Config.getPathSeparator())); assertEquals(1, actual.size()); assertTrue(actual.contains(child111)); }