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 java.io.File;
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  /**
25   * Test case for {@link SizeFileComparator}.
26   */
27  public class SizeFileComparatorTest extends ComparatorAbstractTestCase {
28  
29      private File smallerDir;
30      private File largerDir;
31      private File smallerFile;
32      private File largerFile;
33  
34      /**
35       * Run the test.
36       *
37       * @param args arguments
38       */
39      public static void main(String[] args) {
40          TestRunner.run(suite());
41      }
42  
43      /**
44       * Create a test suite.
45       *
46       * @return The test suite
47       */
48      public static Test suite() {
49          return new TestSuite(SizeFileComparatorTest.class);
50      }
51  
52      /**
53       * Construct a new test case with the specified name.
54       *
55       * @param name Name of the test
56       */
57      public SizeFileComparatorTest(String name) {
58          super(name);
59      }
60  
61      /** @see junit.framework.TestCase#setUp() */
62      protected void setUp() throws Exception {
63          super.setUp();
64          comparator = SizeFileComparator.SIZE_COMPARATOR;
65          reverse = SizeFileComparator.SIZE_REVERSE;
66          File dir = getTestDirectory();
67          smallerDir = new File(dir, "smallerdir");
68          largerDir = new File(dir, "largerdir");
69          smallerFile = new File(smallerDir, "smaller.txt");
70          largerFile = new File(largerDir, "larger.txt");
71          smallerDir.mkdir();
72          largerDir.mkdir();
73          createFile(smallerFile, 32);
74          createFile(largerFile, 64);
75          equalFile1 = smallerFile;
76          equalFile2 = smallerFile;
77          lessFile   = smallerFile;
78          moreFile   = largerFile;
79      }
80  
81      /**
82       * Test a file which doesn't exist.
83       */
84      public void testNonexistantFile() {
85          File nonexistantFile = new File(new File("."), "nonexistant.txt");
86          assertFalse(nonexistantFile.exists());
87          assertTrue("less",  comparator.compare(nonexistantFile, moreFile) < 0);
88      }
89  
90      /**
91       * Test a file which doesn't exist.
92       */
93      public void testCompareDirectorySizes() {
94          assertEquals("sumDirectoryContents=false", 0, comparator.compare(smallerDir, largerDir));
95          assertEquals("less", -1, SizeFileComparator.SIZE_SUMDIR_COMPARATOR.compare(smallerDir, largerDir));
96          assertEquals("less", 1,  SizeFileComparator.SIZE_SUMDIR_REVERSE.compare(smallerDir, largerDir));
97      }
98  }