View Javadoc

1   /**
2    * Copyright 2009-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.properties;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.io.FileUtils;
25  import org.junit.Test;
26  
27  public class DirectoryComparer {
28  
29      @Test
30      public void test() {
31          try {
32              File dir1 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/build/war");
33              // File dir1 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/target/foo");
34              File dir2 = new File("/Users/jeffcaddel/ws/OLE-2987/ole/target/ole-fs-0.8.0-h-SNAPSHOT");
35              DD one = getDD(dir1);
36              DD two = getDD(dir2);
37              execute(one, two);
38          } catch (Throwable t) {
39              t.printStackTrace();
40          }
41  
42      }
43  
44      protected DD getDD(File dir) {
45          List<File> files = new ArrayList<File>(FileUtils.listFiles(dir, null, true));
46          Map<String, File> map = new HashMap<String, File>();
47          for (File file : files) {
48              String key = getRelativePath(dir, file);
49              map.put(key, file);
50          }
51          DD dd = new DD();
52          dd.setDir(dir);
53          dd.setFiles(files);
54          dd.setFileMap(map);
55          return dd;
56      }
57  
58      protected void execute(DD one, DD two) {
59          List<File> u1 = getUnmatchedFiles(one, two);
60          List<File> u2 = getUnmatchedFiles(two, one);
61          System.out.println(u1.size() + " " + u2.size());
62          for (File u : u1) {
63              System.out.println(u);
64          }
65          System.out.println();
66          for (File u : u2) {
67              System.out.println(u);
68          }
69      }
70  
71      protected List<File> getUnmatchedFiles(DD one, DD two) {
72          List<File> unmatched = new ArrayList<File>();
73          Map<String, File> fileMap = two.getFileMap();
74          for (File file : one.getFiles()) {
75              String relativePath = getRelativePath(one.getDir(), file);
76              File match = fileMap.get(relativePath);
77              if (match == null) {
78                  unmatched.add(file);
79              }
80          }
81          return unmatched;
82      }
83  
84      protected String getRelativePath(File dir, File file) {
85          String dirPath = dir.getAbsolutePath();
86          String path = file.getAbsolutePath();
87          return path.replace(dirPath, "");
88      }
89  }