Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
| 18 | import org.junit.Test; |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 19 | import org.onlab.junit.TestTools; |
| 20 | |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import java.io.ByteArrayInputStream; |
| 25 | import java.io.InputStream; |
| 26 | import java.util.Collections; |
| 27 | import java.util.Dictionary; |
| 28 | import java.util.Hashtable; |
| 29 | import java.util.Map; |
Sho SHIMIZU | b5638b8 | 2016-02-11 14:55:05 -0800 | [diff] [blame] | 30 | import java.util.Optional; |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 32 | import java.util.concurrent.ThreadFactory; |
Sho SHIMIZU | b5638b8 | 2016-02-11 14:55:05 -0800 | [diff] [blame] | 33 | import java.util.stream.Stream; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 34 | |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 35 | import static org.hamcrest.Matchers.equalTo; |
Sho SHIMIZU | b5638b8 | 2016-02-11 14:55:05 -0800 | [diff] [blame] | 36 | import static org.hamcrest.Matchers.is; |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 37 | import static org.hamcrest.Matchers.nullValue; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 38 | import static org.junit.Assert.*; |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 39 | import static org.onlab.junit.TestTools.assertAfter; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Test of the miscellaneous tools. |
| 43 | */ |
| 44 | public 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 HIGUCHI | ae51470 | 2018-05-04 16:13:45 -0700 | [diff] [blame] | 62 | assertEquals("0xffffffffffffffff", Tools.toHexWithPrefix(0xffffffffffffffffL)); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 63 | |
| 64 | } |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 65 | |
| 66 | @Test |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 67 | 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 Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 86 | public void namedThreads() { |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 87 | 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 Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 93 | public void groupedThreads() { |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 94 | ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d"); |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 95 | Thread t = f.newThread(() -> TestTools.print("yo")); |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 96 | assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-")); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 97 | assertTrue("wrong group", "foo/bar-me".equals(t.getThreadGroup().getName())); |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 100 | @Test |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 101 | 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 Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 115 | 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 SHIMIZU | b5638b8 | 2016-02-11 14:55:05 -0800 | [diff] [blame] | 125 | @Test |
Yuta HIGUCHI | 06fde30 | 2018-06-08 22:50:22 -0700 | [diff] [blame] | 126 | 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 SHIMIZU | b5638b8 | 2016-02-11 14:55:05 -0800 | [diff] [blame] | 226 | 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 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 235 | } |