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  
18  package org.apache.commons.io.file;
19  
20  import org.apache.commons.io.file.Counters.Counter;
21  import org.apache.commons.io.file.Counters.PathCounters;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  public class CountersEqualsAndHashCodeTest {
26  
27      @Test
28      public void testBigIntegerCounterEquals() {
29          testEquals(Counters.bigIntegerCounter(), Counters.bigIntegerCounter());
30      }
31  
32      @Test
33      public void testBigIntegerHashCode() {
34          testHashCodes(Counters.bigIntegerCounter(), Counters.bigIntegerCounter());
35      }
36  
37      private void testEquals(final Counter counter1, final Counter counter2) {
38          Assertions.assertEquals(counter1, counter2);
39          counter1.increment();
40          Assertions.assertNotEquals(counter1, counter2);
41          counter2.increment();
42          Assertions.assertEquals(counter1, counter2);
43      }
44  
45      private void testEqualsByteCounters(final PathCounters counter1, final PathCounters counter2) {
46          Assertions.assertEquals(counter1, counter2);
47          counter1.getByteCounter().increment();
48          Assertions.assertNotEquals(counter1, counter2);
49          counter2.getByteCounter().increment();
50          Assertions.assertEquals(counter1, counter2);
51      }
52  
53      private void testEqualsDirectoryCounters(final PathCounters counter1, final PathCounters counter2) {
54          Assertions.assertEquals(counter1, counter2);
55          counter1.getDirectoryCounter().increment();
56          Assertions.assertNotEquals(counter1, counter2);
57          counter2.getDirectoryCounter().increment();
58          Assertions.assertEquals(counter1, counter2);
59      }
60  
61      private void testEqualsFileCounters(final PathCounters counter1, final PathCounters counter2) {
62          Assertions.assertEquals(counter1, counter2);
63          counter1.getFileCounter().increment();
64          Assertions.assertNotEquals(counter1, counter2);
65          counter2.getFileCounter().increment();
66          Assertions.assertEquals(counter1, counter2);
67      }
68  
69      private void testHashCodeFileCounters(final PathCounters counter1, final PathCounters counter2) {
70          Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
71          counter1.getFileCounter().increment();
72          Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
73          counter2.getFileCounter().increment();
74          Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
75      }
76  
77      private void testHashCodes(final Counter counter1, final Counter counter2) {
78          Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
79          counter1.increment();
80          Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
81          counter2.increment();
82          Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
83      }
84  
85      @Test
86      public void testLongCounterEquals() {
87          testEquals(Counters.longCounter(), Counters.longCounter());
88      }
89  
90      @Test
91      public void testLongCounterHashCodes() {
92          testHashCodes(Counters.longCounter(), Counters.longCounter());
93      }
94  
95      @Test
96      public void testLongCounterMixEquals() {
97          testEquals(Counters.longCounter(), Counters.bigIntegerCounter());
98          testEquals(Counters.bigIntegerCounter(), Counters.longCounter());
99      }
100 
101     @Test
102     public void testLongPathCountersEqualsByteCounters() {
103         testEqualsByteCounters(Counters.longPathCounters(), Counters.longPathCounters());
104     }
105 
106     @Test
107     public void testLongPathCountersEqualsDirectoryCounters() {
108         testEqualsDirectoryCounters(Counters.longPathCounters(), Counters.longPathCounters());
109     }
110 
111     @Test
112     public void testLongPathCountersEqualsFileCounters() {
113         testEqualsFileCounters(Counters.longPathCounters(), Counters.longPathCounters());
114     }
115 
116     @Test
117     public void testLongPathCountersHashCodeFileCounters() {
118         testHashCodeFileCounters(Counters.longPathCounters(), Counters.longPathCounters());
119     }
120 
121     @Test
122     public void testMix() {
123         testHashCodeFileCounters(Counters.longPathCounters(), Counters.bigIntegerPathCounters());
124     }
125 }