• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xmlunit / xmlunit / cd160610-9b67-4752-a2c4-e3a0d82d991e

21 Apr 2025 11:55AM UTC coverage: 91.756% (-0.02%) from 91.78%
cd160610-9b67-4752-a2c4-e3a0d82d991e

push

circleci

web-flow
Merge pull request #289 from xmlunit/circleci-project-setup

CircleCI project setup

3996 of 4698 branches covered (85.06%)

11754 of 12810 relevant lines covered (91.76%)

2.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.06
/xmlunit-core/src/main/java/org/xmlunit/diff/Diff.java
1
/*
2
  This file is licensed to You under the Apache License, Version 2.0
3
  (the "License"); you may not use this file except in compliance with
4
  the License.  You may obtain a copy of the License at
5

6
  http://www.apache.org/licenses/LICENSE-2.0
7

8
  Unless required by applicable law or agreed to in writing, software
9
  distributed under the License is distributed on an "AS IS" BASIS,
10
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
  See the License for the specific language governing permissions and
12
  limitations under the License.
13
*/
14

15
package org.xmlunit.diff;
16

17
import javax.xml.transform.Source;
18
import java.util.Iterator;
19

20
/**
21
 * The Diff-Object is the result of two comparisons.
22
 * @see org.xmlunit.builder.DiffBuilder
23
 */
24
public class Diff {
25

26
    private static final ComparisonFormatter DEFAULT_FORMATTER =
1✔
27
        new DefaultComparisonFormatter();
28

29
    private final Iterable<Difference> differences;
30

31
    private final Source controlSource;
32

33
    private final Source testSource;
34

35
    private final ComparisonFormatter formatter;
36

37
    /**
38
     * Encapsulates the compared sources and the differences found.
39
     *
40
     * @param controlSource the control XML source
41
     * @param testSource the test XML source
42
     * @param differences the differences found
43
     */
44
    public Diff(Source controlSource, Source testSource, Iterable<Difference> differences) {
45
        this(controlSource, testSource, DEFAULT_FORMATTER, differences);
1✔
46
    }
1✔
47

48
    /**
49
     * Encapsulates the compared sources and the differences found.
50
     *
51
     * @param controlSource the control XML source
52
     * @param testSource the test XML source
53
     * @param differences the differences found
54
     * @param formatter formatter to use when displaying the differences
55
     */
56
    public Diff(Source controlSource, Source testSource,
57
                ComparisonFormatter formatter, Iterable<Difference> differences) {
1✔
58
        this.controlSource = controlSource;
1✔
59
        this.testSource = testSource;
1✔
60
        this.formatter = formatter;
1✔
61
        this.differences = differences;
1✔
62
        for (Difference d : differences) {
1✔
63
            d.setComparisonFormatter(formatter);
1✔
64
        }
1✔
65
    }
1✔
66

67
    /**
68
     * Returns a string representation of this diff
69
     * using internal {@link ComparisonFormatter} or
70
     * {@link DefaultComparisonFormatter} if formatter wasn't set.
71
     *
72
     * <p>Each comparison result separated by the end of the line.</p>
73
     *
74
     * @return a string representation of this diff
75
     * @since 2.8.3
76
     */
77
    public String fullDescription() {
78
        return fullDescription(formatter);
×
79
    }
80

81
    /**
82
     * Returns a string representation of this diff
83
     * using the given {@link ComparisonFormatter}
84
     *
85
     * <p>Each comparison result separated by the end of the line.</p>
86
     *
87
     * @param formatter the {@link ComparisonFormatter} to use
88
     * @return a string representation of this diff
89
     * @since 2.8.3
90
     */
91
    public String fullDescription(ComparisonFormatter formatter) {
92
        if (!hasDifferences()) {
×
93
            return "[identical]";
×
94
        }
95
        Iterator<Difference> diffIterator = getDifferences().iterator();
×
96
        StringBuilder result = new StringBuilder()
×
97
            .append(diffIterator.next().getComparison().toString(formatter));
×
98
        String lineSeparator = System.lineSeparator();
×
99
        while (diffIterator.hasNext()) {
×
100
            result.append(lineSeparator)
×
101
                .append(diffIterator.next().getComparison().toString(formatter));
×
102
        }
103
        return result.toString();
×
104
    }
105

106
    /**
107
     * @return true if there was at least one difference.
108
     */
109
    public boolean hasDifferences() {
110
        return differences.iterator().hasNext();
1✔
111
    }
112

113
    /**
114
     * @return all differences found before the comparison process stopped.
115
     */
116
    public Iterable<Difference> getDifferences() {
117
        return differences;
1✔
118
    }
119

120
    /**
121
     * @return the control XML source
122
     */
123
    public Source getControlSource() {
124
        return controlSource;
×
125
    }
126

127
    /**
128
     * @return the test XML source
129
     */
130
    public Source getTestSource() {
131
        return testSource;
×
132
    }
133

134
    /**
135
     * Returns a string representation of first found difference in this diff
136
     * using internal {@link ComparisonFormatter} or
137
     * {@link DefaultComparisonFormatter} if formatter wasn't set
138
     * @return a string representation of first found difference in this diff
139
     * @see #fullDescription()
140
     */
141
    @Override
142
    public String toString() {
143
        return toString(formatter);
1✔
144
    }
145

146
    /**
147
     * Returns a string representation of first found difference in this diff
148
     * using the given {@link ComparisonFormatter}
149
     * @param formatter the {@link ComparisonFormatter} to use
150
     * @return a string representation of first found difference in this diff
151
     * @see #fullDescription(ComparisonFormatter)
152
     */
153
    public String toString(ComparisonFormatter formatter) {
154
        if (!hasDifferences()) {
1✔
155
            return "[identical]";
1✔
156
        }
157
        return getDifferences().iterator().next().getComparison().toString(formatter);
1✔
158
    }
159

160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc