diff options
author | Roberto E. Escobar | 2013-03-11 22:25:27 +0000 |
---|---|---|
committer | Roberto E. Escobar | 2013-03-11 22:25:27 +0000 |
commit | a8b9f85df335f54ee722c6ced99a5f47b0f2a7bb (patch) | |
tree | 4fbe1bb5ea0c75094cc4645f7ac72023f30de2be /plugins/org.eclipse.osee.executor.admin.test | |
parent | 2d4ff83855af37eb7506c41da0777245d85f8b50 (diff) | |
download | org.eclipse.osee-a8b9f85df335f54ee722c6ced99a5f47b0f2a7bb.tar.gz org.eclipse.osee-a8b9f85df335f54ee722c6ced99a5f47b0f2a7bb.tar.xz org.eclipse.osee-a8b9f85df335f54ee722c6ced99a5f47b0f2a7bb.zip |
refinement: Change executor admin tests to use mockito
Change-Id: I0203f3fbc67eef154db5d1cbb34e9ecc397ea7a5
Diffstat (limited to 'plugins/org.eclipse.osee.executor.admin.test')
6 files changed, 97 insertions, 348 deletions
diff --git a/plugins/org.eclipse.osee.executor.admin.test/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.executor.admin.test/META-INF/MANIFEST.MF index cb6d81106bf..aa2cad647c1 100644 --- a/plugins/org.eclipse.osee.executor.admin.test/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.executor.admin.test/META-INF/MANIFEST.MF @@ -6,7 +6,7 @@ Bundle-Version: 0.11.0.qualifier Bundle-Vendor: Eclipse Open System Engineering Environment Fragment-Host: org.eclipse.osee.executor.admin Bundle-RequiredExecutionEnvironment: JavaSE-1.6 -Import-Package: org.eclipse.osee.framework.jdk.core.type, - org.junit;version="4.8.2", - org.junit.runner;version="4.8.2", - org.junit.runners;version="4.8.2" +Require-Bundle: org.junit, + org.mockito;bundle-version="1.9.0", + org.hamcrest.core +Import-Package: org.eclipse.osee.framework.jdk.core.type diff --git a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutionCallbackTest.java b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutionCallbackTest.java index e8517480947..994a6ec44ef 100644 --- a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutionCallbackTest.java +++ b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutionCallbackTest.java @@ -10,120 +10,110 @@ *******************************************************************************/ package org.eclipse.osee.executor.admin.internal; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.util.HashMap; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.eclipse.osee.event.EventService; import org.eclipse.osee.executor.admin.CancellableCallable; -import org.eclipse.osee.executor.admin.mock.MockEventService; -import org.eclipse.osee.executor.admin.mock.MockExecutionCallback; -import org.eclipse.osee.executor.admin.mock.MockLog; -import org.junit.Assert; +import org.eclipse.osee.executor.admin.ExecutionCallback; +import org.eclipse.osee.logger.Log; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; /** * @author Roberto E. Escobar */ public class ExecutionCallbackTest { - @Test - public void testCallbackOnSuccess() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.setLogger(new MockLog()); - admin.setEventService(new MockEventService()); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + //@formatter:off + @Mock private Log logger; + @Mock private EventService eventService; + @Mock private Callable<String> callable; + @Mock private ExecutionCallback<String> callback; + //@formatter:on + + private ExecutorAdminImpl admin; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + admin = new ExecutorAdminImpl(); + admin.setLogger(logger); + admin.setEventService(eventService); + admin.start(new HashMap<String, Object>()); + } - final String expected = "Was Called"; + @After + public void tearDown() { + if (admin != null) { + admin.stop(new HashMap<String, Object>()); + } + } - MockExecutionCallback<String> callback = new MockExecutionCallback<String>(500); - Callable<String> callable = new Callable<String>() { + @Test + public void testCallbackOnSuccess() throws Exception { + String expected = "Was Called"; - @Override - public String call() throws Exception { - return expected; - } + when(callable.call()).thenReturn(expected); - }; Future<String> future = admin.schedule(callable, callback); String actual = future.get(); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); - Assert.assertTrue(callback.wasOnSuccess()); - Assert.assertFalse(callback.wasOnCancelled()); - Assert.assertFalse(callback.wasOnFailure()); - - Assert.assertEquals(expected, callback.getResult()); - Assert.assertNull(callback.getThrowable()); + verify(callback).onSuccess(expected); + verify(callback, times(0)).onCancelled(); + verify(callback, times(0)).onFailure(Matchers.<Throwable> any()); } @Test public void testCallbackOnFailure() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.setLogger(new MockLog()); - admin.setEventService(new MockEventService()); - admin.start(new HashMap<String, Object>()); - - final Exception expectedException = new IllegalStateException(); - MockExecutionCallback<String> callback = new MockExecutionCallback<String>(500); - Callable<String> callable = new Callable<String>() { + Exception expectedException = new IllegalStateException(); - @Override - public String call() throws Exception { - throw expectedException; - } + when(callable.call()).thenThrow(expectedException); - }; Future<String> future = admin.schedule(callable, callback); - try { - future.get(); - Assert.assertTrue("An exception should have been thrown", false); - } catch (Exception ex) { - Assert.assertEquals(ExecutionException.class, ex.getClass()); - Assert.assertEquals(expectedException, ex.getCause()); - } - - Assert.assertFalse(callback.wasOnSuccess()); - Assert.assertFalse(callback.wasOnCancelled()); - Assert.assertTrue(callback.wasOnFailure()); + thrown.expect(ExecutionException.class); + future.get(); - Assert.assertNull(callback.getResult()); - Assert.assertEquals(IllegalStateException.class, callback.getThrowable().getClass()); + verify(callback, times(0)).onSuccess(Matchers.anyString()); + verify(callback, times(0)).onCancelled(); + verify(callback).onFailure(expectedException); } @Test public void testCallbackOnCancel() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.setLogger(new MockLog()); - admin.setEventService(new MockEventService()); - admin.start(new HashMap<String, Object>()); - - final String results = "results"; - - MockExecutionCallback<String> callback = new MockExecutionCallback<String>(500); - - TestCancellableCallable callable = new TestCancellableCallable(results); + TestCancellableCallable callable = new TestCancellableCallable("results"); Future<String> future = admin.schedule(callable, callback); future.cancel(true); - Assert.assertFalse(callback.wasOnSuccess()); - Assert.assertTrue(callback.wasOnCancelled()); - Assert.assertFalse(callback.wasOnFailure()); - - Assert.assertNull(callback.getResult()); - Assert.assertNull(callback.getThrowable()); + verify(callback, times(0)).onSuccess(Matchers.anyString()); + verify(callback).onCancelled(); + verify(callback, times(0)).onFailure(Matchers.<Throwable> any()); - Assert.assertEquals(true, callable.isCancelled()); - Assert.assertEquals(true, future.isCancelled()); + assertEquals(true, callable.isCancelled()); + assertEquals(true, future.isCancelled()); - try { - future.get(); - Assert.assertTrue("An exception should have been thrown", false); - } catch (Exception ex) { - Assert.assertEquals(CancellationException.class, ex.getClass()); - } + thrown.expect(CancellationException.class); + future.get(); } private class TestCancellableCallable extends CancellableCallable<String> { @@ -138,7 +128,6 @@ public class ExecutionCallbackTest { public String call() throws Exception { while (!isCancelled()) { checkForCancelled(); - // System.out.println("working..."); } return results; } diff --git a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutorAdminTest.java b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutorAdminTest.java index 65011395d53..28120350da0 100644 --- a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutorAdminTest.java +++ b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/internal/ExecutorAdminTest.java @@ -12,29 +12,47 @@ package org.eclipse.osee.executor.admin.internal; import java.util.HashMap; import java.util.concurrent.ExecutorService; -import org.eclipse.osee.executor.admin.mock.MockEventService; -import org.eclipse.osee.executor.admin.mock.MockLog; +import org.eclipse.osee.event.EventService; +import org.eclipse.osee.logger.Log; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; /** * @author Roberto E. Escobar */ public class ExecutorAdminTest { - @Test(expected = IllegalStateException.class) - public void testInitializationGuard() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.getDefaultExecutor(); + //@formatter:off + @Mock private Log logger; + @Mock private EventService eventService; + //@formatter:on + + private ExecutorAdminImpl admin; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + admin = new ExecutorAdminImpl(); + admin.setLogger(logger); + admin.setEventService(eventService); + + admin.start(new HashMap<String, Object>()); + } + + @After + public void tearDown() { + if (admin != null) { + admin.stop(new HashMap<String, Object>()); + } } @Test public void testGetService() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.setLogger(new MockLog()); - admin.setEventService(new MockEventService()); - admin.start(new HashMap<String, Object>()); - ExecutorService defaultService = admin.getDefaultExecutor(); Assert.assertNotNull(defaultService); @@ -49,11 +67,6 @@ public class ExecutorAdminTest { @Test public void testTerminateExecutorService() throws Exception { - ExecutorAdminImpl admin = new ExecutorAdminImpl(); - admin.setLogger(new MockLog()); - admin.setEventService(new MockEventService()); - admin.start(new HashMap<String, Object>()); - ExecutorService anotherExecutor = admin.getExecutor("hello"); Assert.assertNotNull(anotherExecutor); diff --git a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockEventService.java b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockEventService.java deleted file mode 100644 index edb8fa8b69b..00000000000 --- a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockEventService.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.executor.admin.mock; - -import java.util.Map; -import org.eclipse.osee.event.EventService; - -/** - * @author Roberto E. Escobar - */ -public class MockEventService implements EventService { - - private String topic; - private Map<String, ?> data; - private int postEvent; - private int sendEvent; - - @Override - public void postEvent(String topic, Map<String, ?> data) { - this.topic = topic; - this.data = data; - postEvent++; - } - - @Override - public void sendEvent(String topic, Map<String, ?> data) { - this.topic = topic; - this.data = data; - sendEvent++; - } - - public void reset() { - this.topic = null; - this.data = null; - postEvent = 0; - sendEvent = 0; - } - - public String getTopic() { - return topic; - } - - public Map<String, ?> getData() { - return data; - } - - public int getPostEventCount() { - return postEvent; - } - - public int getSendEventCount() { - return sendEvent; - } -} diff --git a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockExecutionCallback.java b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockExecutionCallback.java deleted file mode 100644 index 10eb250392a..00000000000 --- a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockExecutionCallback.java +++ /dev/null @@ -1,86 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.executor.admin.mock; - -import org.eclipse.osee.executor.admin.ExecutionCallback; - -/** - * @author Roberto E. Escobar - */ -public class MockExecutionCallback<T> implements ExecutionCallback<T> { - - private boolean wasOnCancelled; - private boolean wasOnSuccess; - private boolean wasOnFailure; - private Throwable throwable; - private T result; - private final long waitTime; - - public MockExecutionCallback(long waitTime) { - this.waitTime = waitTime; - wasOnCancelled = false; - wasOnSuccess = false; - wasOnFailure = false; - throwable = null; - result = null; - } - - private void waitIfNeeded() { - if (waitTime > 0) { - try { - Thread.sleep(waitTime); - } catch (InterruptedException ex) { - // Do nothing; - } - } - } - - @Override - public void onCancelled() { - waitIfNeeded(); - this.wasOnCancelled = true; - } - - @Override - public void onSuccess(T result) { - waitIfNeeded(); - this.result = result; - this.wasOnSuccess = true; - } - - @Override - public void onFailure(Throwable throwable) { - waitIfNeeded(); - this.wasOnFailure = true; - this.throwable = throwable; - } - - public boolean wasOnCancelled() { - return wasOnCancelled; - } - - public boolean wasOnSuccess() { - return wasOnSuccess; - } - - public boolean wasOnFailure() { - return wasOnFailure; - } - - public Throwable getThrowable() { - return throwable; - } - - public T getResult() { - return result; - } - -}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockLog.java b/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockLog.java deleted file mode 100644 index 7588b181b57..00000000000 --- a/plugins/org.eclipse.osee.executor.admin.test/src/org/eclipse/osee/executor/admin/mock/MockLog.java +++ /dev/null @@ -1,105 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.executor.admin.mock; - -import org.eclipse.osee.logger.Log; - -/** - * @author Andrew M. Finkbeiner - */ -public class MockLog implements Log { - - @Override - public boolean isTraceEnabled() { - return true; - } - - @Override - public void trace(String format, Object... args) { - commonOut(format, args); - } - - @Override - public void trace(Throwable th, String format, Object... args) { - commonOut(format, args); - } - - @Override - public boolean isDebugEnabled() { - return true; - } - - @Override - public void debug(String format, Object... args) { - commonOut(format, args); - } - - @Override - public void debug(Throwable th, String format, Object... args) { - commonOut(th, format, args); - } - - @Override - public boolean isInfoEnabled() { - return true; - } - - @Override - public void info(String format, Object... args) { - commonOut(format, args); - } - - @Override - public void info(Throwable th, String format, Object... args) { - commonOut(th, format, args); - } - - @Override - public boolean isWarnEnabled() { - return true; - } - - @Override - public void warn(String format, Object... args) { - commonOut(format, args); - } - - @Override - public void warn(Throwable th, String format, Object... args) { - commonOut(th, format, args); - } - - @Override - public boolean isErrorEnabled() { - return true; - } - - @Override - public void error(String format, Object... args) { - commonOut(format, args); - } - - @Override - public void error(Throwable th, String format, Object... args) { - commonOut(th, format, args); - } - - private void commonOut(String format, Object... args) { - System.out.printf(format, args); - System.out.println(); - } - - private void commonOut(Throwable th, String format, Object... args) { - commonOut(format, args); - th.printStackTrace(); - } - -} |