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

xmlunit / xmlunit / 667

pending completion
667

push

travis-ci-com

bodewig
add Cyclone DX SBOM generation to build

5824 of 6326 relevant lines covered (92.06%)

3.68 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 =
4✔
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
    public Diff(Source controlSource, Source testSource, Iterable<Difference> differences) {
38
        this(controlSource, testSource, DEFAULT_FORMATTER, differences);
4✔
39
    }
4✔
40

41
    public Diff(Source controlSource, Source testSource,
42
                ComparisonFormatter formatter, Iterable<Difference> differences) {
4✔
43
        this.controlSource = controlSource;
4✔
44
        this.testSource = testSource;
4✔
45
        this.formatter = formatter;
4✔
46
        this.differences = differences;
4✔
47
        for (Difference d : differences) {
4✔
48
            d.setComparisonFormatter(formatter);
4✔
49
        }
4✔
50
    }
4✔
51

52
    /**
53
     * Returns a string representation of this diff
54
     * using internal {@link ComparisonFormatter} or
55
     * {@link DefaultComparisonFormatter} if formatter wasn't set.
56
     *
57
     * <p>Each comparison result separated by the end of the line.</p>
58
     *
59
     * @return a string representation of this diff
60
     * @since 2.8.3
61
     */
62
    public String fullDescription() {
63
        return fullDescription(formatter);
×
64
    }
65

66
    /**
67
     * Returns a string representation of this diff
68
     * using the given {@link ComparisonFormatter}
69
     *
70
     * <p>Each comparison result separated by the end of the line.</p>
71
     *
72
     * @param formatter the {@link ComparisonFormatter} to use
73
     * @return a string representation of this diff
74
     * @since 2.8.3
75
     */
76
    public String fullDescription(ComparisonFormatter formatter) {
77
        if (!hasDifferences()) {
×
78
            return "[identical]";
×
79
        }
80
        Iterator<Difference> diffIterator = getDifferences().iterator();
×
81
        StringBuilder result = new StringBuilder()
×
82
            .append(diffIterator.next().getComparison().toString(formatter));
×
83
        String lineSeparator = System.lineSeparator();
×
84
        while (diffIterator.hasNext()) {
×
85
            result.append(lineSeparator)
×
86
                .append(diffIterator.next().getComparison().toString(formatter));
×
87
        }
88
        return result.toString();
×
89
    }
90

91
    /**
92
     * @return true if there was at least one difference.
93
     */
94
    public boolean hasDifferences() {
95
        return differences.iterator().hasNext();
4✔
96
    }
97

98
    /**
99
     * @return all differences found before the comparison process stopped.
100
     */
101
    public Iterable<Difference> getDifferences() {
102
        return differences;
4✔
103
    }
104

105

106
    public Source getControlSource() {
107
        return controlSource;
×
108
    }
109

110

111
    public Source getTestSource() {
112
        return testSource;
×
113
    }
114

115
    /**
116
     * Returns a string representation of first found difference in this diff
117
     * using internal {@link ComparisonFormatter} or
118
     * {@link DefaultComparisonFormatter} if formatter wasn't set
119
     * @return a string representation of first found difference in this diff
120
     * @see #fullDescription()
121
     */
122
    @Override
123
    public String toString() {
124
        return toString(formatter);
4✔
125
    }
126

127
    /**
128
     * Returns a string representation of first found difference in this diff
129
     * using the given {@link ComparisonFormatter}
130
     * @param formatter the {@link ComparisonFormatter} to use
131
     * @return a string representation of first found difference in this diff
132
     * @see #fullDescription(ComparisonFormatter)
133
     */
134
    public String toString(ComparisonFormatter formatter) {
135
        if (!hasDifferences()) {
4✔
136
            return "[identical]";
4✔
137
        }
138
        return getDifferences().iterator().next().getComparison().toString(formatter);
4✔
139
    }
140

141
}
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