From 03d8ad8d96243ec9499f0b974750b6ea64f7b41f Mon Sep 17 00:00:00 2001
From: Adam Domurad <adomurad@redhat.com>
Date: Thu, 25 Apr 2013 10:28:08 -0400
Subject: Unit tests for AsyncCall test extension

---
 .../unit/net/sourceforge/jnlp/AsyncCallTest.java   | 93 ++++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java

(limited to 'tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java')

diff --git a/tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java b/tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java
new file mode 100644
index 0000000..eb6903b
--- /dev/null
+++ b/tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java
@@ -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
-- 
cgit v1.2.3