View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.comparator;
18  
19  import org.apache.commons.io.IOUtils;
20  import org.apache.commons.io.testtools.TestUtils;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import java.io.BufferedOutputStream;
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Comparator;
30  import java.util.List;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   * Test case for {@link CompositeFileComparator}.
37   */
38  public class CompositeFileComparatorTest extends ComparatorAbstractTestCase {
39  
40      @Before
41      public void setUp() throws Exception {
42          comparator = new CompositeFileComparator(
43                  new AbstractFileComparator[] {
44                      (AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR,
45                      (AbstractFileComparator) ExtensionFileComparator.EXTENSION_COMPARATOR});
46          reverse = new ReverseComparator(comparator);
47          final File dir = getTestDirectory();
48          lessFile   = new File(dir, "xyz.txt");
49          equalFile1 = new File(dir, "foo.txt");
50          equalFile2 = new File(dir, "bar.txt");
51          moreFile   = new File(dir, "foo.xyz");
52          if (!lessFile.getParentFile().exists()) {
53              throw new IOException("Cannot create file " + lessFile
54                      + " as the parent directory does not exist");
55          }
56          final BufferedOutputStream output3 =
57                  new BufferedOutputStream(new FileOutputStream(lessFile));
58          try {
59              TestUtils.generateTestData(output3, (long) 32);
60          } finally {
61              IOUtils.closeQuietly(output3);
62          }
63          if (!equalFile1.getParentFile().exists()) {
64              throw new IOException("Cannot create file " + equalFile1
65                      + " as the parent directory does not exist");
66          }
67          final BufferedOutputStream output2 =
68                  new BufferedOutputStream(new FileOutputStream(equalFile1));
69          try {
70              TestUtils.generateTestData(output2, (long) 48);
71          } finally {
72              IOUtils.closeQuietly(output2);
73          }
74          if (!equalFile2.getParentFile().exists()) {
75              throw new IOException("Cannot create file " + equalFile2
76                      + " as the parent directory does not exist");
77          }
78          final BufferedOutputStream output1 =
79                  new BufferedOutputStream(new FileOutputStream(equalFile2));
80          try {
81              TestUtils.generateTestData(output1, (long) 48);
82          } finally {
83              IOUtils.closeQuietly(output1);
84          }
85          if (!moreFile.getParentFile().exists()) {
86              throw new IOException("Cannot create file " + moreFile
87                      + " as the parent directory does not exist");
88          }
89          final BufferedOutputStream output =
90                  new BufferedOutputStream(new FileOutputStream(moreFile));
91          try {
92              TestUtils.generateTestData(output, (long) 48);
93          } finally {
94              IOUtils.closeQuietly(output);
95          }
96      }
97  
98      /**
99       * Test Constructor with null Iterable
100      */
101     @Test
102     public void constructorIterable_order() {
103         final List<Comparator<File>> list = new ArrayList<Comparator<File>>();
104         list.add(SizeFileComparator.SIZE_COMPARATOR);
105         list.add(ExtensionFileComparator.EXTENSION_COMPARATOR);
106         final Comparator<File> c = new CompositeFileComparator(list);
107 
108         assertEquals("equal", 0, c.compare(equalFile1, equalFile2));
109         assertTrue("less",  c.compare(lessFile, moreFile) < 0);
110         assertTrue("more",  c.compare(moreFile, lessFile) > 0);
111     }
112 
113     /**
114      * Test Constructor with null Iterable
115      */
116     @Test
117     public void constructorIterable_Null() {
118         final Comparator<File> c = new CompositeFileComparator((Iterable<Comparator<File>>)null);
119         assertEquals("less,more", 0, c.compare(lessFile, moreFile));
120         assertEquals("more,less", 0, c.compare(moreFile, lessFile));
121         assertEquals("toString", "CompositeFileComparator{}", c.toString());
122     }
123 
124     /**
125      * Test Constructor with null array
126      */
127     @Test
128     public void constructorArray_Null() {
129         final Comparator<File> c = new CompositeFileComparator((Comparator<File>[])null);
130         assertEquals("less,more", 0, c.compare(lessFile, moreFile));
131         assertEquals("more,less", 0, c.compare(moreFile, lessFile));
132         assertEquals("toString", "CompositeFileComparator{}", c.toString());
133     }
134 }