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