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.lang3.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Unit tests {@link DiffResult}.
31   */
32  public class DiffResultTest extends AbstractLangTest {
33  
34      private static final class EmptyClass {
35          // empty
36      }
37      private static final class SimpleClass implements Diffable<SimpleClass> {
38          static String getFieldName() {
39              return "booleanField";
40          }
41  
42          private final boolean booleanField;
43  
44          SimpleClass(final boolean booleanField) {
45              this.booleanField = booleanField;
46          }
47  
48          @Override
49          public DiffResult<SimpleClass> diff(final SimpleClass obj) {
50              return new DiffBuilder<>(this, obj, ToStringStyle.SHORT_PREFIX_STYLE)
51                      .append(getFieldName(), booleanField, obj.booleanField)
52                      .build();
53          }
54      }
55      private static final SimpleClass SIMPLE_FALSE = new SimpleClass(false);
56  
57      private static final SimpleClass SIMPLE_TRUE = new SimpleClass(true);
58  
59      private static final ToStringStyle SHORT_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;
60  
61      @Test
62      public void testIterator() {
63          final SimpleClass lhs = new SimpleClass(true);
64          final SimpleClass rhs = new SimpleClass(false);
65  
66          final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();
67          final Iterator<Diff<?>> expectedIterator = diffs.iterator();
68  
69          final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, SHORT_STYLE);
70          final Iterator<Diff<?>> iterator = list.iterator();
71  
72          while (iterator.hasNext()) {
73              assertTrue(expectedIterator.hasNext());
74              assertEquals(expectedIterator.next(), iterator.next());
75          }
76      }
77  
78      @Test
79      public void testLeftAndRightGetters() {
80          final SimpleClass left = new SimpleClass(true);
81          final SimpleClass right = new SimpleClass(false);
82  
83          final List<Diff<?>> diffs = left.diff(right).getDiffs();
84          final DiffResult diffResult = new DiffResult(left, right, diffs, SHORT_STYLE);
85  
86          assertEquals(left, diffResult.getLeft());
87          assertEquals(right, diffResult.getRight());
88      }
89  
90      @Test
91      public void testListIsNonModifiable() {
92          final SimpleClass lhs = new SimpleClass(true);
93          final SimpleClass rhs = new SimpleClass(false);
94  
95          final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();
96  
97          final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, SHORT_STYLE);
98          assertEquals(diffs, list.getDiffs());
99          assertEquals(1, list.getNumberOfDiffs());
100         assertThrows(UnsupportedOperationException.class, () -> list.getDiffs().remove(0));
101     }
102 
103     @Test
104     public void testNoDifferencesString() {
105         final DiffResult<SimpleClass> diffResult = new DiffBuilder<>(SIMPLE_TRUE, SIMPLE_TRUE,
106                 SHORT_STYLE).build();
107         assertEquals(DiffResult.OBJECTS_SAME_STRING, diffResult.toString());
108     }
109 
110     @Test
111     public void testNullLhs() {
112         assertThrows(NullPointerException.class,
113             () -> new DiffResult<>(null, SIMPLE_FALSE, SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
114     }
115 
116     @Test
117     public void testNullList() {
118         assertThrows(NullPointerException.class,
119             () -> new DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, null, SHORT_STYLE));
120     }
121 
122     @Test
123     public void testNullRhs() {
124         assertThrows(NullPointerException.class,
125             () -> new DiffResult<>(SIMPLE_TRUE, null, SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
126     }
127 
128     @Test
129     public void testNullStyle() {
130         final DiffResult<SimpleClass> diffResult = new DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, SIMPLE_TRUE
131                 .diff(SIMPLE_FALSE).getDiffs(), null);
132         assertEquals(ToStringStyle.DEFAULT_STYLE, diffResult.getToStringStyle());
133     }
134 
135     @Test
136     public void testToStringOutput() {
137         final DiffResult<EmptyClass> list = new DiffBuilder<>(new EmptyClass(), new EmptyClass(),
138                 ToStringStyle.SHORT_PREFIX_STYLE).append("test", false, true)
139                 .build();
140         assertEquals(
141                 "DiffResultTest.EmptyClass[test=false] differs from DiffResultTest.EmptyClass[test=true]",
142                 list.toString());
143     }
144 
145     @Test
146     public void testToStringSpecifyStyleOutput() {
147         final DiffResult<SimpleClass> list = SIMPLE_FALSE.diff(SIMPLE_TRUE);
148         assertEquals(list.getToStringStyle(), SHORT_STYLE);
149 
150         final String lhsString = new ToStringBuilder(SIMPLE_FALSE,
151                 ToStringStyle.MULTI_LINE_STYLE).append(
152                 SimpleClass.getFieldName(), SIMPLE_FALSE.booleanField).build();
153 
154         final String rhsString = new ToStringBuilder(SIMPLE_TRUE,
155                 ToStringStyle.MULTI_LINE_STYLE).append(
156                 SimpleClass.getFieldName(), SIMPLE_TRUE.booleanField).build();
157 
158         final String expectedOutput = String.format("%s differs from %s", lhsString,
159                 rhsString);
160         assertEquals(expectedOutput,
161                 list.toString(ToStringStyle.MULTI_LINE_STYLE));
162     }
163 }