Author: jawi
Date: Wed Sep 18 15:19:24 2013
New Revision: 1524449
URL: http://svn.apache.org/r1524449
Log:
Fixed unit tests that failed on Solaris.
Modified:
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
Modified:
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java
URL:
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java?rev=1524449&r1=1524448&r2=1524449&view=diff
==============================================================================
---
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java
(original)
+++
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java
Wed Sep 18 15:19:24 2013
@@ -20,17 +20,18 @@ package org.apache.ace.agent.impl;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.InterruptedIOException;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -124,7 +125,6 @@ public class DownloadHandleImplTest exte
future = handle.start(null);
downloadResult = future.get(5, TimeUnit.SECONDS);
- assertNotNull(downloadResult, "Failed to finish download?!");
assertTrue(downloadResult.isComplete());
File file = ((DownloadHandleImpl) handle).getDownloadFile();
@@ -152,13 +152,7 @@ public class DownloadHandleImplTest exte
}
});
- try {
- future.get(5, TimeUnit.SECONDS);
- }
- catch (CancellationException exception) {
- // Ok; expected...
- }
- assertTrue(future.isCancelled());
+ assertDownloadStopped(future);
File file = ((DownloadHandleImpl) handle).getDownloadFile();
long fileLength = file.length();
@@ -171,7 +165,6 @@ public class DownloadHandleImplTest exte
future = handle2.start(null);
downloadResult = future.get(5, TimeUnit.SECONDS);
- assertNotNull(downloadResult, "Failed to finish download?!");
assertTrue(downloadResult.isComplete());
fileLength = file.length();
@@ -199,9 +192,7 @@ public class DownloadHandleImplTest exte
}
});
- downloadResult = future.get(5, TimeUnit.SECONDS);
- assertNotNull(downloadResult, "Failed to stop download?!");
- assertFalse(downloadResult.isComplete());
+ assertDownloadStopped(future);
File file = ((DownloadHandleImpl) handle).getDownloadFile();
long firstFileLength = file.length();
@@ -219,13 +210,7 @@ public class DownloadHandleImplTest exte
}
});
- try {
- future.get(5, TimeUnit.SECONDS);
- }
- catch (CancellationException exception) {
- // Ok; expected...
- }
- assertTrue(future.isCancelled());
+ assertDownloadStopped(future);
long secondFileLength = file.length();
@@ -237,7 +222,6 @@ public class DownloadHandleImplTest exte
future = handle3.start(null);
downloadResult = future.get(5, TimeUnit.SECONDS);
- assertNotNull(downloadResult, "Failed to complete download?!");
assertTrue(downloadResult.isComplete());
assertEquals(file.length(), m_contentLength, "Not all content
downloaded for " + file.getName() + "?");
@@ -246,6 +230,22 @@ public class DownloadHandleImplTest exte
assertEquals(getDigest(file), m_digest);
}
+ private void assertDownloadStopped(Future<DownloadResult> future) throws
Exception {
+ try {
+ DownloadResult result = future.get(5, TimeUnit.SECONDS);
+ assertFalse(result.isComplete());
+ }
+ catch (CancellationException exception) {
+ // Ok; also fine...
+ assertTrue(future.isCancelled());
+ }
+ catch (ExecutionException exception) {
+ Throwable cause = exception.getCause();
+ // On Solaris, interrupting an I/O operation yields an
InterruptedIOException...
+ assertTrue(cause instanceof InterruptedIOException, "Expected
InterruptedIOException, but got: " + cause);
+ }
+ }
+
private String getDigest(File file) throws Exception {
DigestInputStream dis = new DigestInputStream(new
FileInputStream(file), MessageDigest.getInstance("MD5"));
while (dis.read() != -1) {
Modified:
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
URL:
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java?rev=1524449&r1=1524448&r2=1524449&view=diff
==============================================================================
---
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
(original)
+++
ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
Wed Sep 18 15:19:24 2013
@@ -28,11 +28,12 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.math.BigInteger;
+import java.io.InterruptedIOException;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
+import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -83,6 +84,7 @@ public class DownloadHandlerTest extends
}
private TestWebServer m_webServer;
+
private URL m_200url;
private File m_200file;
private String m_200digest;
@@ -111,7 +113,7 @@ public class DownloadHandlerTest extends
dos.write(" Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum
Ipsum\n".getBytes());
}
dos.close();
- m_200digest = new
BigInteger(dos.getMessageDigest().digest()).toString();
+ m_200digest = new String(dos.getMessageDigest().digest());
m_webServer = new TestWebServer(port, "/", dataLocation.getName());
m_webServer.addServlet(new TestErrorServlet(), "/error");
@@ -137,6 +139,27 @@ public class DownloadHandlerTest extends
}
@Test
+ public void testFailed404_noresume_result() throws Exception {
+ DownloadHandler downloadHandler =
m_agentContext.getHandler(DownloadHandler.class);
+
+ DownloadHandle handle = downloadHandler.getHandle(m_404url);
+ Future<DownloadResult> future = handle.start(null);
+
+ assertIOException(future);
+ }
+
+ @Test
+ public void testFailed503_noresume_result() throws Exception {
+ DownloadHandler downloadHandler =
m_agentContext.getHandler(DownloadHandler.class);
+
+ DownloadHandle handle = downloadHandler.getHandle(m_503url);
+
+ assertRetryException(handle.start(null));
+
+ assertRetryException(handle.start(null));
+ }
+
+ @Test
public void testSuccessful_noresume_result() throws Exception {
DownloadHandler downloadHandler =
m_agentContext.getHandler(DownloadHandler.class);
@@ -145,7 +168,7 @@ public class DownloadHandlerTest extends
Future<DownloadResult> result = handle.start(null);
- assertSuccessful(result, 200, m_200digest);
+ assertSuccessful(result, m_200digest);
}
@Test
@@ -162,41 +185,11 @@ public class DownloadHandlerTest extends
}
});
- assertStopped(future, 200);
- assertSuccessful(handle.start(null), 206, m_200digest);
+ assertStopped(future);
+ assertSuccessful(handle.start(null), m_200digest);
}
- @Test
- public void testFailed404_noresume_result() throws Exception {
- DownloadHandler downloadHandler =
m_agentContext.getHandler(DownloadHandler.class);
-
- DownloadHandle handle = downloadHandler.getHandle(m_404url);
- Future<DownloadResult> future = handle.start(null);
-
- assertIOException(future);
- }
-
- @Test
- public void testFailed503_noresume_result() throws Exception {
- DownloadHandler downloadHandler =
m_agentContext.getHandler(DownloadHandler.class);
-
- DownloadHandle handle = downloadHandler.getHandle(m_503url);
-
- assertRetryException(handle.start(null));
-
- assertRetryException(handle.start(null));
- }
-
- private static void assertSuccessful(Future<DownloadResult> future, int
statusCode, String digest) throws Exception {
- DownloadResult result = future.get(5, TimeUnit.SECONDS);
-
- assertTrue(result.isComplete(), "Expected state SUCCESSFUL after
succesful completion");
- assertNotNull(result.getInputStream(), "Expected non null file after
successful completion");
-
- assertEquals(getDigest(result.getInputStream()), digest, "Expected
same digest after successful completion");
- }
-
- private static void assertRetryException(Future<DownloadResult> future)
throws Exception {
+ private void assertIOException(Future<DownloadResult> future) throws
Exception {
try {
future.get(5, TimeUnit.SECONDS);
@@ -204,14 +197,14 @@ public class DownloadHandlerTest extends
}
catch (ExecutionException exception) {
// Expected...
- assertTrue(exception.getCause() instanceof RetryAfterException,
"Expected RetryAfterException, got " + exception.getCause());
+ assertTrue(exception.getCause() instanceof IOException, "Expected
IOException, got " + exception.getCause());
}
assertFalse(future.isCancelled());
assertTrue(future.isDone());
}
- private static void assertIOException(Future<DownloadResult> future)
throws Exception {
+ private void assertRetryException(Future<DownloadResult> future) throws
Exception {
try {
future.get(5, TimeUnit.SECONDS);
@@ -219,24 +212,43 @@ public class DownloadHandlerTest extends
}
catch (ExecutionException exception) {
// Expected...
- assertTrue(exception.getCause() instanceof IOException, "Expected
IOException, got " + exception.getCause());
+ assertTrue(exception.getCause() instanceof RetryAfterException,
"Expected RetryAfterException, got " + exception.getCause());
}
assertFalse(future.isCancelled());
assertTrue(future.isDone());
}
- private static void assertStopped(Future<DownloadResult> future, int
statusCode) throws Exception {
- DownloadResult downloadResult = future.get(5, TimeUnit.SECONDS);
+ private void assertStopped(Future<DownloadResult> future) throws Exception
{
+ try {
+ DownloadResult result = future.get(5, TimeUnit.SECONDS);
+ assertFalse(result.isComplete());
+ }
+ catch (CancellationException exception) {
+ // Ok; also fine...
+ assertTrue(future.isCancelled());
+ }
+ catch (ExecutionException exception) {
+ Throwable cause = exception.getCause();
+ // On Solaris, interrupting an I/O operation yields an
InterruptedIOException...
+ assertTrue(cause instanceof InterruptedIOException, "Expected
InterruptedIOException, but got: " + cause);
+ }
+ }
+
+ private void assertSuccessful(Future<DownloadResult> future, String
digest) throws Exception {
+ DownloadResult result = future.get(5, TimeUnit.SECONDS);
+
+ assertTrue(result.isComplete(), "Expected state SUCCESSFUL after
succesful completion");
+ assertNotNull(result.getInputStream(), "Expected non null file after
successful completion");
- assertFalse(downloadResult.isComplete());
+ assertEquals(getDigest(result.getInputStream()), digest, "Expected
same digest after successful completion");
}
- private static String getDigest(InputStream is) throws Exception {
+ private String getDigest(InputStream is) throws Exception {
DigestInputStream dis = new DigestInputStream(is,
MessageDigest.getInstance("MD5"));
while (dis.read() != -1) {
}
dis.close();
- return new BigInteger(dis.getMessageDigest().digest()).toString();
+ return new String(dis.getMessageDigest().digest());
}
}