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

pgpainless / sop-java / #80

01 Feb 2026 09:23PM UTC coverage: 57.685% (+0.4%) from 57.236%
#80

push

other

vanitasvitae
SOP-Java 15.0.2-SNAPSHOT

2113 of 3663 relevant lines covered (57.68%)

0.58 hits per line

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

56.67
/sop-java-testfixtures/src/main/java/sop/testsuite/JUtils.java
1
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package sop.testsuite;
6

7
import sop.util.UTCUtil;
8

9
import java.nio.charset.StandardCharsets;
10
import java.util.Arrays;
11
import java.util.Date;
12

13
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
14
import static org.junit.jupiter.api.Assertions.assertEquals;
15
import static org.junit.jupiter.api.Assertions.fail;
16

17
/**
18
 * Contains utility functions for JUnit tests.
19
 */
20
public class JUtils {
×
21

22
    /**
23
     * Return true, if the given <pre>array</pre> starts with <pre>start</pre>.
24
     *
25
     * @param array array
26
     * @param start start
27
     * @return true if array starts with start, false otherwise
28
     */
29
    public static boolean arrayStartsWith(byte[] array, byte[] start) {
30
        return arrayStartsWith(array, start, 0);
1✔
31
    }
32

33
    /**
34
     * Return true, if the given <pre>array</pre> contains the given <pre>start</pre> at offset <pre>offset</pre>.
35
     *
36
     * @param array array
37
     * @param start start
38
     * @param offset offset
39
     * @return true, if array contains start at offset, false otherwise
40
     */
41
    public static boolean arrayStartsWith(byte[] array, byte[] start, int offset) {
42
        if (offset < 0) {
1✔
43
            throw new IllegalArgumentException("Offset cannot be negative");
×
44
        }
45

46
        if (start.length + offset > array.length) {
1✔
47
            return false;
×
48
        }
49

50
        for (int i = 0; i < start.length; i++) {
1✔
51
            if (array[offset + i] != start[i]) {
1✔
52
                return false;
1✔
53
            }
54
        }
55
        return true;
1✔
56
    }
57

58
    /**
59
     * Assert that the given <pre>array</pre> starts with <pre>start</pre>.
60
     *
61
     * @param array array
62
     * @param start start
63
     */
64
    public static void assertArrayStartsWith(byte[] array, byte[] start) {
65
        if (!arrayStartsWith(array, start)) {
1✔
66
            byte[] actual = new byte[Math.min(start.length, array.length)];
×
67
            System.arraycopy(array, 0, actual, 0, actual.length);
×
68
            fail("Array does not start with expected bytes.\n" +
×
69
                    "Expected: <" + Arrays.toString(start) + ">\n" +
×
70
                    "Actual: <" + Arrays.toString(actual) + ">");
×
71
        }
72
    }
1✔
73

74
    /**
75
     * Assert that the given <pre>array</pre> contains <pre>start</pre> at <pre>offset</pre>.
76
     *
77
     * @param array array
78
     * @param start start
79
     * @param offset offset
80
     */
81
    public static void assertArrayStartsWith(byte[] array, byte[] start, int offset) {
82
        if (!arrayStartsWith(array, start, offset)) {
×
83
            byte[] actual = new byte[Math.min(start.length, array.length - offset)];
×
84
            System.arraycopy(array, offset, actual, 0, actual.length);
×
85
            fail("Array does not start with expected bytes at offset " + offset + ".\n" +
×
86
                    "Expected: <" + Arrays.toString(start) + ">\n" +
×
87
                    "Actual: <" + Arrays.toString(actual) + ">");
×
88
        }
89
    }
×
90

91
    /**
92
     * Returns true if the given <pre>array</pre> ends with the given <pre>end</pre> bytes.
93
     *
94
     * @param array array to examine
95
     * @param end expected ending bytes
96
     * @return true if array ends with end
97
     */
98
    public static boolean arrayEndsWith(byte[] array, byte[] end) {
99
        return arrayEndsWith(array, end, 0);
×
100
    }
101

102
    /**
103
     * Returns true if the given <pre>array</pre> ends with the given <pre>end</pre> bytes.
104
     *
105
     * @param array array to examine
106
     * @param end expected ending bytes
107
     * @param offset from the end
108
     * @return true if array ends with end
109
     */
110
    public static boolean arrayEndsWith(byte[] array, byte[] end, int offset) {
111
        if (end.length + offset > array.length) {
1✔
112
            return false;
×
113
        }
114

115
        int arrOff = array.length - end.length - offset;
1✔
116
        for (int i = 0; i < end.length; i++) {
1✔
117
            if (end[i] != array[arrOff + i]) {
1✔
118
                return false;
×
119
            }
120
        }
121
        return true;
1✔
122
    }
123

124
    /**
125
     * Assert hat the given array ends with the given end bytes.
126
     *
127
     * @param array array
128
     * @param end ending bytes
129
     */
130
    public static void assertArrayEndsWith(byte[] array, byte[] end) {
131
        assertArrayEndsWith(array, end, 0);
×
132
    }
×
133

134
    /**
135
     * Assert hat the given array contains the given end bytes at the end, shifted by offset.
136
     *
137
     * @param array array
138
     * @param end ending bytes
139
     * @param offset offset from the end
140
     */
141
    public static void assertArrayEndsWith(byte[] array, byte[] end, int offset) {
142
        if (!arrayEndsWith(array, end, offset)) {
1✔
143
            byte[] actual = new byte[Math.min(end.length, array.length - offset)];
×
144
            System.arraycopy(array, array.length - actual.length, actual, 0, actual.length);
×
145
            fail("Array does not end with the expected bytes.\n" +
×
146
                    "Expected: <" + Arrays.toString(end) + ">\n" +
×
147
                    "Actual: <" + Arrays.toString(actual) + ">");
×
148
        }
149
    }
1✔
150

151
    /**
152
     * Assert hat the given array contains the given end bytes at the end, ignoring new lines.
153
     *
154
     * @param array array
155
     * @param end ending bytes
156
     */
157
    public static void assertArrayEndsWithIgnoreNewlines(byte[] array, byte[] end) {
158
        int offset = 0;
1✔
159
        while (offset < array.length && array[array.length - 1 - offset] == (byte) 10) {
1✔
160
            offset++;
1✔
161
        }
162

163
        assertArrayEndsWith(array, end, offset);
1✔
164
    }
1✔
165

166
    /**
167
     * Assert equality of the given two ascii armored byte arrays, ignoring armor header lines.
168
     *
169
     * @param first first ascii armored bytes
170
     * @param second second ascii armored bytes
171
     */
172
    public static void assertAsciiArmorEquals(byte[] first, byte[] second) {
173
        byte[] firstCleaned = removeArmorHeaders(first);
1✔
174
        byte[] secondCleaned = removeArmorHeaders(second);
1✔
175

176
        assertArrayEquals(firstCleaned, secondCleaned);
1✔
177
    }
1✔
178

179
    /**
180
     * Remove armor headers "Comment:", "Version:", "MessageID:", "Hash:" and "Charset:" along with their values
181
     * from the given ascii armored byte array.
182
     *
183
     * @param armor ascii armored byte array
184
     * @return ascii armored byte array with header lines removed
185
     */
186
    public static byte[] removeArmorHeaders(byte[] armor) {
187
        String string = new String(armor, StandardCharsets.UTF_8);
1✔
188
        string = string.replaceAll("Comment: .+\\R", "")
1✔
189
                .replaceAll("Version: .+\\R", "")
1✔
190
                .replaceAll("MessageID: .+\\R", "")
1✔
191
                .replaceAll("Hash: .+\\R", "")
1✔
192
                .replaceAll("Charset: .+\\R", "");
1✔
193
        return string.getBytes(StandardCharsets.UTF_8);
1✔
194
    }
195

196
    /**
197
     * Assert that the expected date equals the actual instance.
198
     *
199
     * @param expected expected date, non-null
200
     * @param actual actual date, non-null
201
     */
202
    public static void assertDateEquals(Date expected, Date actual) {
203
        assertEquals(UTCUtil.formatUTCDate(expected), UTCUtil.formatUTCDate(actual));
1✔
204
    }
1✔
205

206
    /**
207
     * Returns true if the actual date equals the expected date.
208
     *
209
     * @param expected expected data, non-null
210
     * @param actual actual date, non-null
211
     * @return true if expected equals actual
212
     */
213
    public static boolean dateEquals(Date expected, Date actual) {
214
        return UTCUtil.formatUTCDate(expected).equals(UTCUtil.formatUTCDate(actual));
×
215
    }
216

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

© 2026 Coveralls, Inc