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