001 /**
002 * Copyright 2009-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.properties;
017
018 import java.io.File;
019 import java.util.ArrayList;
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.commons.io.FileUtils;
025 import org.junit.Test;
026
027 public class DirectoryComparer {
028
029 @Test
030 public void test() {
031 try {
032 File dir1 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/build/war");
033 // File dir1 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/target/foo");
034 File dir2 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/target/ole-fs-0.8.0-h-SNAPSHOT");
035 DD one = getDD(dir1);
036 DD two = getDD(dir2);
037 execute(one, two);
038 } catch (Throwable t) {
039 t.printStackTrace();
040 }
041
042 }
043
044 protected DD getDD(File dir) {
045 List<File> files = new ArrayList<File>(FileUtils.listFiles(dir, null, true));
046 Map<String, File> map = new HashMap<String, File>();
047 for (File file : files) {
048 String key = getRelativePath(dir, file);
049 map.put(key, file);
050 }
051 DD dd = new DD();
052 dd.setDir(dir);
053 dd.setFiles(files);
054 dd.setFileMap(map);
055 return dd;
056 }
057
058 protected void execute(DD one, DD two) {
059 List<File> u1 = getUnmatchedFiles(one, two);
060 List<File> u2 = getUnmatchedFiles(two, one);
061 System.out.println(u1.size() + " " + u2.size());
062 for (File u : u1) {
063 System.out.println(u);
064 }
065 System.out.println();
066 for (File u : u2) {
067 System.out.println(u);
068 }
069 }
070
071 protected List<File> getUnmatchedFiles(DD one, DD two) {
072 List<File> unmatched = new ArrayList<File>();
073 Map<String, File> fileMap = two.getFileMap();
074 for (File file : one.getFiles()) {
075 String relativePath = getRelativePath(one.getDir(), file);
076 File match = fileMap.get(relativePath);
077 if (match == null) {
078 unmatched.add(file);
079 }
080 }
081 return unmatched;
082 }
083
084 protected String getRelativePath(File dir, File file) {
085 String dirPath = dir.getAbsolutePath();
086 String path = file.getAbsolutePath();
087 return path.replace(dirPath, "");
088 }
089 }