1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.commons.csv;
21
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import java.util.List;
28
29 /**
30 * Utility methods for test cases
31 */
32 final class Utils {
33
34 /**
35 * Checks if the 2d array has the same contents as the list of records.
36 *
37 * @param message the message to be displayed
38 * @param expected the 2d array of expected results
39 * @param actual the List of {@link CSVRecord} entries, each containing an array of values
40 * @param maxRows the maximum number of rows expected, less than or equal to zero means no limit.
41 */
42 public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual, final long maxRows) {
43 final long expectedLength = maxRows > 0 ? Math.min(maxRows, expected.length) : expected.length;
44 assertEquals(expectedLength, actual.size(), message + " - outer array size");
45 for (int i = 0; i < expectedLength; i++) {
46 assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")");
47 }
48 }
49
50 /**
51 * Creates an input stream, with or without a BOM.
52 */
53 static InputStream createUtf8Input(final byte[] baseData, final boolean addBom) {
54 byte[] data = baseData;
55 if (addBom) {
56 data = new byte[baseData.length + 3];
57 data[0] = (byte) 0xEF;
58 data[1] = (byte) 0xBB;
59 data[2] = (byte) 0xBF;
60 System.arraycopy(baseData, 0, data, 3, baseData.length);
61 }
62 return new ByteArrayInputStream(data);
63 }
64
65 private Utils() {
66 }
67 }