changeset 693:b0bef68756a6

Unit tests for AsyncCall test extension
author Adam Domurad <adomurad@redhat.com>
date Thu, 25 Apr 2013 10:28:08 -0400
parents 1da533f89607
children 142217481a51
files ChangeLog tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java
diffstat 2 files changed, 98 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Apr 25 10:33:21 2013 -0400
+++ b/ChangeLog	Thu Apr 25 10:28:08 2013 -0400
@@ -1,3 +1,8 @@
+2013-04-25  Adam Domurad  <adomurad@redhat.com>
+
+	* tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java: Unit tests for
+	AsyncCall test extension.
+
 2013-04-25  Adam Domurad  <adomurad@redhat.com>
 
 	Tests & test extensions for mocking the plugin input & output pipes.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java	Thu Apr 25 10:28:08 2013 -0400
@@ -0,0 +1,93 @@
+package net.sourceforge.jnlp;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.Callable;
+
+import org.junit.Test;
+
+public class AsyncCallTest {
+
+    @Test
+    public void timeOutTest() {
+        final boolean[] wasInterrupted = { false };
+
+        AsyncCall<Void> call = AsyncCall.startWithTimeOut(new Callable<Void>() {
+            @Override
+            public synchronized Void call() {
+                try {
+                    wait();
+                } catch (InterruptedException ie) {
+                    // Received on time-out
+                    wasInterrupted[0] = true;
+                }
+                return null;
+            }
+        }, 100 /* 100 millisecond time-out */);
+
+        boolean completedNormally = false;
+
+        try {
+            call.join();
+            completedNormally = true;
+        } catch (Exception e) {
+            ServerAccess.logErrorReprint(e.toString());
+            assertTrue(e instanceof AsyncCall.TimeOutException);
+        }
+
+        assertFalse(completedNormally);
+        assertTrue(wasInterrupted[0]);
+    }
+
+    @Test
+    public void normalReturnTest() {
+        AsyncCall<Integer> call = AsyncCall.startWithTimeOut(new Callable<Integer>() {
+            @Override
+            public Integer call() {
+                return 1;
+            }
+        });
+
+        Integer result = null;
+        boolean completedNormally = false;
+
+        try {
+            result = call.join();
+            completedNormally = true;
+        } catch (Exception e) {
+            ServerAccess.logErrorReprint(e.toString());
+        }
+
+        assertTrue(completedNormally);
+        assertEquals(Integer.valueOf(1), result);
+    }
+
+    @Test
+    public void thrownExceptionTest() {
+
+        @SuppressWarnings("serial")
+        class TestException extends RuntimeException {
+        }
+
+        AsyncCall<Void> call = AsyncCall.startWithTimeOut(new Callable<Void>() {
+            @Override
+            public Void call() {
+                throw new TestException();
+            }
+        });
+
+        boolean completedNormally = false;
+
+        try {
+            call.join();
+            completedNormally = true;
+        } catch (Exception e) {
+            ServerAccess.logErrorReprint(e.toString());
+            assertTrue(e instanceof TestException);
+        }
+
+        assertFalse(completedNormally);
+    }
+}
\ No newline at end of file