import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
import java.util.NoSuchElementException;
import org.junit.Test;
assertFalse(deque.removeFirstOccurrence(null));
assertFalse(deque.removeLastOccurrence(null));
- try {
- deque.push(null);
- fail();
- } catch (IllegalStateException e) {
- // expeced
- }
- try {
- deque.addFirst(null);
- fail();
- } catch (IllegalStateException e) {
- // expeced
- }
- try {
- deque.addLast(null);
- fail();
- } catch (IllegalStateException e) {
- // expeced
- }
-
- try {
- deque.getFirst();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
- try {
- deque.getLast();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
- try {
- deque.pop();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
- try {
- deque.remove();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
- try {
- deque.removeFirst();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
- try {
- deque.removeLast();
- fail();
- } catch (NoSuchElementException e) {
- // expeced
- }
+ assertThrows(IllegalStateException.class, () -> deque.push(null));
+ assertThrows(IllegalStateException.class, () -> deque.addFirst(null));
+ assertThrows(IllegalStateException.class, () -> deque.addLast(null));
+ assertThrows(NoSuchElementException.class, () -> deque.getFirst());
+ assertThrows(NoSuchElementException.class, () -> deque.getLast());
+ assertThrows(NoSuchElementException.class, () -> deque.pop());
+ assertThrows(NoSuchElementException.class, () -> deque.remove());
+ assertThrows(NoSuchElementException.class, () -> deque.removeFirst());
+ assertThrows(NoSuchElementException.class, () -> deque.removeLast());
}
}
package org.opendaylight.yangtools.util;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@Test
public void testEmpty() {
- ImmutableMapTemplate<?> template;
- try {
- template = ImmutableMapTemplate.ordered(ImmutableList.of());
- fail("Returned template " + template);
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- try {
- template = ImmutableMapTemplate.unordered(ImmutableList.of());
- fail("Returned template " + template);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.ordered(ImmutableList.of()));
+ assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.unordered(ImmutableList.of()));
}
@Test
assertEquals("{foo=foo}", map.toString());
// Null transformation
- try {
- map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null));
// Empty input
- try {
- map = template.instantiateWithValues();
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
// Two-item input
- try {
- map = template.instantiateWithValues(FOO, BAR);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- map = template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO, BAR));
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key));
// Mismatched input
- try {
- map = template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key));
}
private static void assertTwo(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
assertEquals("{foo=foo, bar=bar}", map.toString());
// Empty input
- try {
- map = template.instantiateWithValues();
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
// One-item input
- try {
- map = template.instantiateWithValues(FOO);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO));
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key));
// Mismatched input
- try {
- map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key);
- fail("Returned map " + map);
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key));
}
}
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.AbstractMap.SimpleEntry;
+import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
OffsetMapCache.invalidateCache();
}
- @Test(expected = IllegalArgumentException.class)
public void testWrongImmutableConstruction() {
- new ImmutableOffsetMap.Ordered<>(ImmutableMap.of(), new String[1]);
+ assertThrows(IllegalArgumentException.class,
+ () -> new ImmutableOffsetMap.Ordered<>(ImmutableMap.of(), new String[1]));
}
@Test
public void testImmutableGuards() {
final Map<String, String> map = createMap();
- try {
- map.values().add("v1");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.values().remove("v1");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.values().clear();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- final Iterator<String> it = map.values().iterator();
- it.next();
- it.remove();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.keySet().add("k1");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.keySet().clear();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.keySet().remove("k1");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- final Iterator<String> it = map.keySet().iterator();
- it.next();
- it.remove();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.entrySet().clear();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
-
- try {
- map.entrySet().add(new SimpleEntry<>("k1", "v1"));
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Collection<String> values = map.values();
+ assertThrows(UnsupportedOperationException.class, () -> values.add("v1"));
+ assertThrows(UnsupportedOperationException.class, () -> values.remove("v1"));
+ assertThrows(UnsupportedOperationException.class, () -> values.clear());
- try {
- map.entrySet().remove(new SimpleEntry<>("k1", "v1"));
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Iterator<String> vit = values.iterator();
+ vit.next();
+ assertThrows(UnsupportedOperationException.class, () -> vit.remove());
- try {
- final Iterator<Entry<String, String>> it = map.entrySet().iterator();
- it.next();
- it.remove();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Set<String> keySet = map.keySet();
+ assertThrows(UnsupportedOperationException.class, () -> keySet.add("k1"));
+ assertThrows(UnsupportedOperationException.class, () -> keySet.clear());
+ assertThrows(UnsupportedOperationException.class, () -> keySet.remove("k1"));
- try {
- map.clear();
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Iterator<String> kit = keySet.iterator();
+ kit.next();
+ assertThrows(UnsupportedOperationException.class, () -> kit.remove());
- try {
- map.put("k1", "fail");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Set<Entry<String, String>> entrySet = map.entrySet();
+ assertThrows(UnsupportedOperationException.class, () -> entrySet.clear());
+ assertThrows(UnsupportedOperationException.class, () -> entrySet.add(new SimpleEntry<>("k1", "v1")));
+ assertThrows(UnsupportedOperationException.class, () -> entrySet.remove(new SimpleEntry<>("k1", "v1")));
- try {
- map.putAll(ImmutableMap.of("k1", "fail"));
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ final Iterator<Entry<String, String>> eit = entrySet.iterator();
+ eit.next();
+ assertThrows(UnsupportedOperationException.class, () -> eit.remove());
- try {
- map.remove("k1");
- fail();
- } catch (UnsupportedOperationException e) {
- // OK
- }
+ assertThrows(UnsupportedOperationException.class, () -> map.clear());
+ assertThrows(UnsupportedOperationException.class, () -> map.put("k1", "fail"));
+ assertThrows(UnsupportedOperationException.class, () -> map.putAll(ImmutableMap.of("k1", "fail")));
+ assertThrows(UnsupportedOperationException.class, () -> map.remove("k1"));
}
@Test
assertTrue(result.needClone());
// Creates a immutable view, which shares the array
- final ImmutableOffsetMap<String, String> immutable = (ImmutableOffsetMap<String, String>) source
- .toUnmodifiableMap();
+ final ImmutableOffsetMap<String, String> immutable =
+ (ImmutableOffsetMap<String, String>) source.toUnmodifiableMap();
assertTrue(source.needClone());
assertSame(source.array(), immutable.objects());
}
}
private static void assertIteratorBroken(final Iterator<?> it) {
- try {
- it.hasNext();
- fail();
- } catch (ConcurrentModificationException e) {
- // OK
- }
- try {
- it.next();
- fail();
- } catch (ConcurrentModificationException e) {
- // OK
- }
- try {
- it.remove();
- fail();
- } catch (ConcurrentModificationException e) {
- // OK
- }
+ assertThrows(ConcurrentModificationException.class, () -> it.hasNext());
+ assertThrows(ConcurrentModificationException.class, () -> it.next());
+ assertThrows(ConcurrentModificationException.class, () -> it.remove());
}
@Test
final Iterator<Entry<String, String>> it = map.entrySet().iterator();
// Not advanced, remove should fail
- try {
- it.remove();
- fail();
- } catch (IllegalStateException e) {
- // OK
- }
+ assertThrows(IllegalStateException.class, () -> it.remove());
assertTrue(it.hasNext());
assertEquals("k1", it.next().getKey());
assertFalse(it.hasNext());
// Check end-of-iteration throw
- try {
- it.next();
- fail();
- } catch (NoSuchElementException e) {
- // OK
- }
+ assertThrows(NoSuchElementException.class, () -> it.next());
}
@Test
*/
package org.opendaylight.yangtools.util;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableMap;
assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
final Set<String> set = m.keySet();
- assertTrue(set instanceof SingletonSet);
+ assertThat(set, instanceOf(SingletonSet.class));
assertTrue(set.contains("k1"));
}
assertTrue(m.equals(t));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testEmptyOrderedCopyOf() {
- SharedSingletonMap.orderedCopyOf(ImmutableMap.of());
+ assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testEmptyUnorderedCopyOf() {
- SharedSingletonMap.unorderedCopyOf(ImmutableMap.of());
+ assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testClear() {
- create().clear();
+ assertThrows(UnsupportedOperationException.class, () -> create().clear());
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testPut() {
- create().put(null, null);
+ assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testPutAll() {
- create().putAll(Collections.singletonMap("", ""));
+ assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRemove() {
- create().remove(null);
+ assertThrows(UnsupportedOperationException.class, () -> create().remove(null));
}
}
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
assertFalse(it.hasNext());
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedAdd() {
final SingletonSet<?> s = nullSet();
- s.add(null);
+ assertThrows(UnsupportedOperationException.class, () -> s.add(null));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedAddAll() {
final SingletonSet<?> s = nullSet();
- s.addAll(null);
+ assertThrows(UnsupportedOperationException.class, () -> s.addAll(null));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedClear() {
final SingletonSet<?> s = nullSet();
- s.clear();
+ assertThrows(UnsupportedOperationException.class, () -> s.clear());
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedRemove() {
final SingletonSet<?> s = nullSet();
- s.remove(null);
+ assertThrows(UnsupportedOperationException.class, () -> s.remove(null));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedRemoveAll() {
final SingletonSet<?> s = nullSet();
- s.removeAll(null);
+ assertThrows(UnsupportedOperationException.class, () -> s.removeAll(null));
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void testRejectedRetainAll() {
final SingletonSet<?> s = nullSet();
- s.retainAll(null);
+ assertThrows(UnsupportedOperationException.class, () -> s.retainAll(null));
}
}
package org.opendaylight.yangtools.util;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import java.util.HashSet;
import java.util.List;
public class TopologicalSortTest {
- @Test(expected = IllegalStateException.class)
+ @Test
public void test() {
Set<Node> nodes = new HashSet<>();
node2.addEdge(node3);
node3.addEdge(node1);
- // We expect an IllegalStateException here
- TopologicalSort.sort(nodes);
+ assertThrows(IllegalStateException.class, () -> TopologicalSort.sort(nodes));
}
@Test
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.yangtools.util.concurrent;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
int tasks = 5;
for (int i = 0; i < tasks - 1; i++) {
- try {
- executor.execute(new Task(null, null, null, null, 0));
- fail("Expected RejectedExecutionException");
- } catch (RejectedExecutionException e) {
- // Expected
- }
+ assertThrows(RejectedExecutionException.class, () -> executor.execute(new Task(null, null, null, null, 0)));
}
assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
package org.opendaylight.yangtools.util.concurrent;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testNoArgumentsContructor() {
- ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class);
+ assertThrows(IllegalArgumentException.class,
+ () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testPrivateContructor() {
- ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class);
+ assertThrows(IllegalArgumentException.class,
+ () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testFailingContructor() {
- ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class);
+ assertThrows(IllegalArgumentException.class,
+ () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
}
@Test
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.yangtools.util.concurrent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
assertEquals("size", 2, queue.size());
- try {
- queue.add("3");
- fail("Expected IllegalStateException");
- } catch (IllegalStateException e) {
- // Expected
- assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
- assertEquals("size", 2, queue.size());
- }
+ assertThrows(IllegalStateException.class, () -> queue.add("3"));
+ assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
+ assertEquals("size", 2, queue.size());
}
@Test
assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
assertEquals("size", 2, queue.size());
- try {
- queue.addAll(Arrays.asList("3", "4"));
- fail("Expected IllegalStateException");
- } catch (IllegalStateException e) {
- // Expected
- assertEquals("getLargestQueueSize", 3, queue.getLargestQueueSize());
- assertEquals("size", 3, queue.size());
- }
+ assertThrows(IllegalStateException.class, () -> queue.addAll(Arrays.asList("3", "4")));
+ assertEquals("getLargestQueueSize", 3, queue.getLargestQueueSize());
+ assertEquals("size", 3, queue.size());
}
}