/* * FileUtilAcceptance.java September 13, 2002 * * Hirornori Washizaki (washi@fuka.info.waseda.ac.jp) */ import joint.FileUtil; import java.io.File; import java.util.ArrayList; public class FileUtilAcceptance { private static final String SEP = File.separator; private static File[] candidates = { new File("child" + SEP + "child1.xxx"), new File("child" + SEP + "child2.xxx"), new File("child" + SEP + "child3.yyy"), new File("child" + SEP + "grand" + SEP + "grand1.xxx"), new File("child" + SEP + "grand" + SEP + "grand2.yyy"), new File("child" + SEP + "grand" + SEP + "grand3.yyy"), }; public static void main(String[] args) throws Exception { FileUtil fileUtil = new FileUtil(); init(); /** * scenario_1 * * (expected results) * ---- scenario_1: child/* (start) ----- * /home/washi/work/java/myprojects/joint/child/child1.xxx * /home/washi/work/java/myprojects/joint/child/child2.xxx * /home/washi/work/java/myprojects/joint/child/child3.yyy * ---- scenario_1: child/* (end) ----- */ executeScenario(fileUtil, false, "child" + SEP + "*", "scenario_1: child/*", new File[] { candidates[0], candidates[1], candidates[2]}); /** * scenario_2 * * (expected results) * ---- scenario_2: child/../child/grand/* (start) ----- * /home/washi/work/java/myprojects/joint/child/grand/grand1.xxx * /home/washi/work/java/myprojects/joint/child/grand/grand2.yyy * /home/washi/work/java/myprojects/joint/child/grand/grand3.yyy * ---- scenario_2: child/../child/grand/* (end) ----- */ executeScenario(fileUtil, false, "child" + SEP + ".." + SEP + "child" + SEP + "grand" + SEP + "*", "scenario_2: child/../child/grand/*", new File[] { candidates[3], candidates[4], candidates[5]}); /** * scenario_3 * * (expected results) * ---- scenario_3: child/*.yyy (start) ----- * /home/washi/work/java/myprojects/joint/child/child3.yyy * ---- scenario_3: child/*.yyy (end) ----- */ executeScenario(fileUtil, false, "child" + SEP + "*.yyy", "scenario_3: child/*.yyy", new File[] { candidates[2] }); /** * scenario_4 * * (expected results) * ---- scenario_4 child/*.yyy expanded (start) ----- * /home/washi/work/java/myprojects/joint/child/grand/grand2.yyy * /home/washi/work/java/myprojects/joint/child/grand/grand3.yyy * /home/washi/work/java/myprojects/joint/child/child3.yyy * ---- scenario_4 child/*.yyy expanded (end) ----- */ executeScenario(fileUtil, true, "child" + SEP + "*.yyy", "scenario_4: child/*.yyy expanded", new File[] { candidates[2], candidates[4], candidates[5]}); return; } private static void executeScenario(FileUtil fileUtil, boolean expand, String statement, String title, File[] expects) throws Exception { fileUtil.setExpand(expand); fileUtil.setStatement(statement); File[] results = fileUtil.getFiles(); if(!compareFiles(expects, results)) { throw new Exception("Failed " + title); } printFiles(title, results); } private static boolean compareFiles(File[] first, File[] second) throws Exception { ArrayList diffList = new ArrayList(); if(first == second) return true; if(first == null || second == null || first.length != second.length) { return false; } for(int i = 0; i < first.length; i++) { diffList.add(first[i].getCanonicalPath()); } for(int i = 0; i < second.length; i++) { diffList.remove(second[i].getCanonicalPath()); } return (diffList.size() == 0) ? true : false; } private static void init() throws Exception { File childDirectory = new File("child"); File grandDirectory = new File("child" + SEP + "grand"); childDirectory.mkdir(); grandDirectory.mkdir(); for(int i = 0; i < candidates.length; i++) { candidates[i].createNewFile(); } } private static void printFiles(String title, File[] files) throws Exception { p("---- " + title + " (start) -----"); for(int i = 0; i < files.length; i++) { p(files[i].getCanonicalPath()); } p("---- " + title + " (end) -----"); } private static final void p(String text) { System.out.println(text); } }