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 * http://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.juneau.utils;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.internal.ArrayUtils.*;
21 import static org.junit.jupiter.api.Assertions.*;
22
23 import java.util.*;
24
25 import org.apache.juneau.*;
26 import org.junit.jupiter.api.*;
27
28 class ArrayUtilsTest extends TestBase {
29
30 //====================================================================================================
31 // append(T[], T...)
32 //====================================================================================================
33 @Test void a01_appendArrayToArray() {
34 String[] s = {};
35
36 s = append(s, "a", "b");
37 assertList(s, "a", "b");
38
39 s = append(s, "c");
40 assertList(s, "a", "b", "c");
41
42 s = append(s);
43 assertList(s, "a", "b", "c");
44
45 var o = append((Object[])null);
46 assertEmpty(o);
47
48 s = append((String[])null, "a", "b");
49 assertList(s, "a", "b");
50 }
51
52 //====================================================================================================
53 // asSet(T[])
54 //====================================================================================================
55 @Test void a02_asSet() {
56 assertThrows(IllegalArgumentException.class, ()->asSet((String[])null));
57
58 var s = a("a");
59 var i = asSet(s).iterator();
60 assertEquals("a", i.next());
61
62 assertThrows(UnsupportedOperationException.class, i::remove);
63 assertThrows(NoSuchElementException.class, i::next);
64 }
65
66 //====================================================================================================
67 // combine(T[]...)
68 //====================================================================================================
69 @Test void a03_combine() {
70 var s1 = a("a");
71 var s2 = a("b");
72
73 assertList(combine(s1, s2), "a", "b");
74 assertList(combine(s1), "a");
75 assertList(combine(s2), "b");
76 assertList(combine(s1,null), "a");
77 assertList(combine(null,s2), "b");
78 assertNull(combine(null,null));
79 assertNull(combine());
80 }
81 }