blob: abef47ede44efd46cd5c1f6c7020d46fdc19b79a [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom782a7cf2014-09-11 23:58:38 -070016package org.onlab.util;
17
18import org.junit.Test;
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080019import org.onlab.junit.TestTools;
20
Yuta HIGUCHI06fde302018-06-08 22:50:22 -070021import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import com.google.common.collect.ImmutableSet;
24import java.io.ByteArrayInputStream;
25import java.io.InputStream;
26import java.util.Collections;
27import java.util.Dictionary;
28import java.util.Hashtable;
29import java.util.Map;
Sho SHIMIZUb5638b82016-02-11 14:55:05 -080030import java.util.Optional;
Yuta HIGUCHI06fde302018-06-08 22:50:22 -070031import java.util.Set;
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080032import java.util.concurrent.ThreadFactory;
Sho SHIMIZUb5638b82016-02-11 14:55:05 -080033import java.util.stream.Stream;
tom782a7cf2014-09-11 23:58:38 -070034
Yuta HIGUCHI06fde302018-06-08 22:50:22 -070035import static org.hamcrest.Matchers.equalTo;
Sho SHIMIZUb5638b82016-02-11 14:55:05 -080036import static org.hamcrest.Matchers.is;
Yuta HIGUCHI06fde302018-06-08 22:50:22 -070037import static org.hamcrest.Matchers.nullValue;
tom782a7cf2014-09-11 23:58:38 -070038import static org.junit.Assert.*;
Thomas Vachuska480adad2015-03-06 10:27:09 -080039import static org.onlab.junit.TestTools.assertAfter;
tom782a7cf2014-09-11 23:58:38 -070040
41/**
42 * Test of the miscellaneous tools.
43 */
44public class ToolsTest {
45
46 @Test
47 public void fromHex() throws Exception {
48 assertEquals(15, Tools.fromHex("0f"));
49 assertEquals(16, Tools.fromHex("10"));
50 assertEquals(65535, Tools.fromHex("ffff"));
51 assertEquals(4096, Tools.fromHex("1000"));
52 assertEquals(0xffffffffffffffffL, Tools.fromHex("ffffffffffffffff"));
53 }
54
55 @Test
56 public void toHex() throws Exception {
57 assertEquals("0f", Tools.toHex(15, 2));
58 assertEquals("ffff", Tools.toHex(65535, 4));
59 assertEquals("1000", Tools.toHex(4096, 4));
60 assertEquals("000000000000000f", Tools.toHex(15));
61 assertEquals("ffffffffffffffff", Tools.toHex(0xffffffffffffffffL));
Yuta HIGUCHIae514702018-05-04 16:13:45 -070062 assertEquals("0xffffffffffffffff", Tools.toHexWithPrefix(0xffffffffffffffffL));
tom782a7cf2014-09-11 23:58:38 -070063
64 }
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080065
66 @Test
Yuta HIGUCHI06fde302018-06-08 22:50:22 -070067 public void getBytesUtf8() {
68 assertThat(Tools.getBytesUtf8("Hi!"),
69 is(equalTo(new byte[] {0x48, 0x69, 0x21})));
70 }
71
72 @Test
73 public void toStringUtf8() {
74 assertThat(Tools.toStringUtf8(new byte[] {0x48, 0x69, 0x21}),
75 is(equalTo("Hi!")));
76 }
77
78 @Test
79 public void copyOf() {
80 byte[] input = new byte[] {1, 2, 3};
81 assertThat(Tools.copyOf(input), is(equalTo(input)));
82 assertNotSame(input, Tools.copyOf(input));
83 }
84
85 @Test
Thomas Vachuska480adad2015-03-06 10:27:09 -080086 public void namedThreads() {
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080087 ThreadFactory f = Tools.namedThreads("foo-%d");
88 Thread t = f.newThread(() -> TestTools.print("yo"));
89 assertTrue("wrong pattern", t.getName().startsWith("foo-"));
90 }
91
92 @Test
Thomas Vachuska480adad2015-03-06 10:27:09 -080093 public void groupedThreads() {
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080094 ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d");
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080095 Thread t = f.newThread(() -> TestTools.print("yo"));
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080096 assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-"));
Jon Halla3fcf672017-03-28 16:53:22 -070097 assertTrue("wrong group", "foo/bar-me".equals(t.getThreadGroup().getName()));
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080098 }
99
Thomas Vachuska480adad2015-03-06 10:27:09 -0800100 @Test
Yuta HIGUCHI06fde302018-06-08 22:50:22 -0700101 public void minPriority() {
102 ThreadFactory f = Tools.minPriority(Tools.namedThreads("foo-%d"));
103 Thread t = f.newThread(() -> TestTools.print("yo"));
104 assertThat(t.getPriority(), is(equalTo(Thread.MIN_PRIORITY)));
105 }
106
107 @Test
108 public void maxPriority() {
109 ThreadFactory f = Tools.maxPriority(Tools.namedThreads("foo-%d"));
110 Thread t = f.newThread(() -> TestTools.print("yo"));
111 assertThat(t.getPriority(), is(equalTo(Thread.MAX_PRIORITY)));
112 }
113
114 @Test
Thomas Vachuska480adad2015-03-06 10:27:09 -0800115 public void exceptionHandler() throws InterruptedException {
116 ThreadFactory f = Tools.namedThreads("foo");
117 Thread t = f.newThread(() -> {
118 throw new IllegalStateException("BOOM!");
119 });
120 assertNotNull("thread should have exception handler", t.getUncaughtExceptionHandler());
121 t.start();
122 assertAfter(100, () -> assertEquals("incorrect thread state", Thread.State.TERMINATED, t.getState()));
123 }
124
Sho SHIMIZUb5638b82016-02-11 14:55:05 -0800125 @Test
Yuta HIGUCHI06fde302018-06-08 22:50:22 -0700126 public void testIsNullOrEmpty() {
127 assertTrue(Tools.isNullOrEmpty(null));
128 assertTrue(Tools.isNullOrEmpty(Collections.emptyList()));
129 }
130
131 @Test(expected = ItemNotFoundException.class)
132 public void testNullIsNotFoundThrow() {
133 Tools.nullIsNotFound(null, "Not found!");
134 fail("Should've thrown some thing");
135 }
136
137 @Test
138 public void testNullIsNotFound() {
139 String input = "Foo";
140 String output = Tools.nullIsNotFound(input, "Not found!");
141 assertEquals(input, output);
142 assertSame(input, output);
143 }
144
145 @Test(expected = ItemNotFoundException.class)
146 public void testEmptyIsNotFoundNullThrow() {
147 Tools.emptyIsNotFound(null, "Not found!");
148 fail("Should've thrown some thing");
149 }
150
151 @Test(expected = ItemNotFoundException.class)
152 public void testEmptyIsNotFoundEmptyThrow() {
153 Tools.emptyIsNotFound(Collections.emptySet(), "Not found!");
154 fail("Should've thrown some thing");
155 }
156
157 @Test
158 public void testEmptyIsNotFound() {
159 Set<String> input = ImmutableSet.of("Foo", "Bar");
160 Set<String> output = Tools.emptyIsNotFound(input, "Not found!");
161 assertEquals(input, output);
162 assertSame(input, output);
163 }
164
165 @Test(expected = IllegalArgumentException.class)
166 public void testNullIsIllegalThrow() {
167 Tools.nullIsIllegal(null, "Not found!");
168 fail("Should've thrown some thing");
169 }
170
171 @Test
172 public void testNullIsIllegal() {
173 String input = "Foo";
174 String output = Tools.nullIsIllegal(input, "Not found!");
175 assertEquals(input, output);
176 assertSame(input, output);
177 }
178
179 @Test
180 public void testReadTreeFromStream() throws Exception {
181 InputStream stream = new ByteArrayInputStream(Tools.getBytesUtf8("{\"foo\" : \"bar\"}"));
182 ObjectNode obj = Tools.readTreeFromStream(new ObjectMapper(), stream);
183 assertTrue(obj.has("foo"));
184 assertThat(obj.get("foo").asText(), is("bar"));
185 }
186
187 @Test
188 public void testDictonary() {
189 Map<String, Object> map = new Hashtable<>();
190 map.put("foo", "bar");
191 map.put("one", Integer.valueOf(1));
192 map.put("empty", "");
193 map.put("enabled", "true");
194 map.put("Enabled", true);
195 map.put("disabled", "disabled");
196 map.put("Disabled", false);
197 Dictionary<?, ?> dict = (Dictionary<?, ?>) map;
198
199 assertThat(Tools.get(dict, "foo"), is(equalTo("bar")));
200 assertThat(Tools.get(dict, "don't exist"), is(nullValue()));
201 assertThat(Tools.get(dict, "one"), is(equalTo("1")));
202 assertThat(Tools.get(dict, "empty"), is(nullValue()));
203
204 assertThat(Tools.getIntegerProperty(dict, "one"), is(equalTo(1)));
205 assertThat(Tools.getIntegerProperty(dict, "empty"), is(nullValue()));
206 assertThat(Tools.getIntegerProperty(dict, "foo"), is(nullValue()));
207
208 assertThat(Tools.getIntegerProperty(dict, "one", 2), is(equalTo(1)));
209 assertThat(Tools.getIntegerProperty(dict, "empty", 2), is(equalTo(2)));
210
211 assertThat(Tools.isPropertyEnabled(dict, "enabled"), is(equalTo(Boolean.TRUE)));
212 assertThat(Tools.isPropertyEnabled(dict, "Enabled"), is(equalTo(Boolean.TRUE)));
213 assertThat(Tools.isPropertyEnabled(dict, "disabled"), is(equalTo(Boolean.FALSE)));
214 assertThat(Tools.isPropertyEnabled(dict, "Disabled"), is(equalTo(Boolean.FALSE)));
215 assertThat(Tools.isPropertyEnabled(dict, "empty"), is(nullValue()));
216
217 assertThat(Tools.isPropertyEnabled(dict, "enabled", false), is(true));
218 assertThat(Tools.isPropertyEnabled(dict, "disabled", true), is(false));
219 assertThat(Tools.isPropertyEnabled(dict, "empty", false), is(false));
220
221 assertThat(Tools.getLongProperty(dict, "one"), is(1L));
222 assertThat(Tools.getLongProperty(dict, "foo"), is(nullValue()));
223}
224
225 @Test
Sho SHIMIZUb5638b82016-02-11 14:55:05 -0800226 public void testOptionalStream() {
227 Stream<Object> empty = Tools.stream(Optional.empty());
228 assertThat(empty.count(), is(0L));
229
230 String value = "value";
231 Stream<String> stream = Tools.stream(Optional.of(value));
232 assertThat(stream.allMatch(value::equals), is(true));
233 }
234
tom782a7cf2014-09-11 23:58:38 -0700235}