From a69667499b483374d2c6edb8670a44883346fbb6 Mon Sep 17 00:00:00 2001 From: Mikael Auno Date: Wed, 6 Apr 2016 15:56:10 +0200 Subject: [PATCH] Use Integer.compare() in HookComparator in order to guard against possible underflow --- .../main/java/cucumber/runtime/HookComparator.java | 2 +- .../test/java/cucumber/runtime/HookOrderTest.java | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/cucumber/runtime/HookComparator.java b/core/src/main/java/cucumber/runtime/HookComparator.java index 2b0f4a04d2..51657208a1 100644 --- a/core/src/main/java/cucumber/runtime/HookComparator.java +++ b/core/src/main/java/cucumber/runtime/HookComparator.java @@ -11,7 +11,7 @@ public HookComparator(boolean ascending) { @Override public int compare(HookDefinition hook1, HookDefinition hook2) { - int comparison = hook1.getOrder() - hook2.getOrder(); + int comparison = Integer.compare(hook1.getOrder(), hook2.getOrder()); return ascending ? comparison : -comparison; } } diff --git a/core/src/test/java/cucumber/runtime/HookOrderTest.java b/core/src/test/java/cucumber/runtime/HookOrderTest.java index 19f5cacfa5..5bbd27e275 100644 --- a/core/src/test/java/cucumber/runtime/HookOrderTest.java +++ b/core/src/test/java/cucumber/runtime/HookOrderTest.java @@ -37,7 +37,7 @@ public void buildMockWorld() { @Test public void before_hooks_execute_in_order() throws Throwable { - List hooks = mockHooks(3, Integer.MAX_VALUE, 1); + List hooks = mockHooks(3, Integer.MAX_VALUE, 1, -1, 0, 10000, Integer.MIN_VALUE); for (HookDefinition hook : hooks) { glue.addBeforeHook(hook); } @@ -45,14 +45,18 @@ public void before_hooks_execute_in_order() throws Throwable { runtime.runBeforeHooks(mock(Reporter.class), new HashSet()); InOrder inOrder = inOrder(hooks.toArray()); + inOrder.verify(hooks.get(6)).execute(Matchers.any()); + inOrder.verify(hooks.get(3)).execute(Matchers.any()); + inOrder.verify(hooks.get(4)).execute(Matchers.any()); inOrder.verify(hooks.get(2)).execute(Matchers.any()); inOrder.verify(hooks.get(0)).execute(Matchers.any()); + inOrder.verify(hooks.get(5)).execute(Matchers.any()); inOrder.verify(hooks.get(1)).execute(Matchers.any()); } @Test public void after_hooks_execute_in_reverse_order() throws Throwable { - List hooks = mockHooks(2, Integer.MAX_VALUE, 4); + List hooks = mockHooks(Integer.MIN_VALUE, 2, Integer.MAX_VALUE, 4, -1, 0, 10000); for (HookDefinition hook : hooks) { glue.addAfterHook(hook); } @@ -60,8 +64,12 @@ public void after_hooks_execute_in_reverse_order() throws Throwable { runtime.runAfterHooks(mock(Reporter.class), new HashSet()); InOrder inOrder = inOrder(hooks.toArray()); - inOrder.verify(hooks.get(1)).execute(Matchers.any()); inOrder.verify(hooks.get(2)).execute(Matchers.any()); + inOrder.verify(hooks.get(6)).execute(Matchers.any()); + inOrder.verify(hooks.get(3)).execute(Matchers.any()); + inOrder.verify(hooks.get(1)).execute(Matchers.any()); + inOrder.verify(hooks.get(5)).execute(Matchers.any()); + inOrder.verify(hooks.get(4)).execute(Matchers.any()); inOrder.verify(hooks.get(0)).execute(Matchers.any()); }