ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1524378 - in /ace/trunk: org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/ org.apache.ace.agent/src/org/apache/ace/agent/ org.apache.ace.agent/src/org/apache/ace/agent/impl/ org.apache.ace.agent/test/org/apache/ace/agent/impl/ org...
Date Wed, 18 Sep 2013 12:03:41 GMT
Author: jawi
Date: Wed Sep 18 12:03:40 2013
New Revision: 1524378

URL: http://svn.apache.org/r1524378
Log:
ACE-387 - resume support for targets:

- added proper support for resuming downloads, if stopped prematurely;
- updated TestNG tests to use correct annotations.


Added:
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java   (with props)
Modified:
    ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/BaseAgentTest.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/DownloadHandler.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConnectionUtil.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ContentRangeInputStream.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadCallableImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandleImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandlerImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadResultImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java

Modified: ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/BaseAgentTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/BaseAgentTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/BaseAgentTest.java (original)
+++ ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/BaseAgentTest.java Wed Sep 18 12:03:40 2013
@@ -114,5 +114,6 @@ public abstract class BaseAgentTest exte
             }
             file.delete();
         }
+        dir.delete();
     }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/DownloadHandler.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/DownloadHandler.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/DownloadHandler.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/DownloadHandler.java Wed Sep 18 12:03:40 2013
@@ -21,19 +21,20 @@ package org.apache.ace.agent;
 import java.net.URL;
 
 /**
- * Service interface for a DownloadHandler component.
+ * Service interface for downloading content from a particular {@link URL}.
+ * <p>
+ * Download handles should be used for all download-related tasks, allowing one to download content asynchronously, and
+ * temporarily stop an ongoing download to resume it on a later moment.
+ * </p>
  */
 public interface DownloadHandler {
 
     /**
-     * Returns a {@link DownloadHandle} for a URL.
+     * Returns a {@link DownloadHandle} for a given URL.
      * 
-     * @param url The url
-     * @return The {@link DownloadHandle}
+     * @param url
+     *            The url to create a download handle for, cannot be <code>null</code>.
+     * @return a new {@link DownloadHandle} instance, never <code>null</code>.
      */
     DownloadHandle getHandle(URL url);
-
-    // TODO named handlers (resume over urls)
-    @Deprecated
-    DownloadHandle getHandle(URL url, int readBufferSize);
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConnectionUtil.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConnectionUtil.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConnectionUtil.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConnectionUtil.java Wed Sep 18 12:03:40 2013
@@ -111,6 +111,18 @@ class ConnectionUtil {
         }
     }
 
+    public static void skip(URLConnection connection, long count) {
+        try {
+            InputStream is = connection.getInputStream();
+            while (count-- > 0) {
+                is.read();
+            }
+        }
+        catch (IOException ignored) {
+            // Ignored...
+        }
+    }
+
     /**
      * Returns the response code for the given URL connection (assuming this connection represents a HTTP(S) URL).
      * 

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ContentRangeInputStream.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ContentRangeInputStream.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ContentRangeInputStream.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ContentRangeInputStream.java Wed Sep 18 12:03:40 2013
@@ -18,12 +18,14 @@
  */
 package org.apache.ace.agent.impl;
 
+import static org.apache.ace.agent.impl.ConnectionUtil.DEFAULT_RETRY_TIME;
 import static org.apache.ace.agent.impl.ConnectionUtil.*;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.RetryAfterException;
@@ -54,7 +56,7 @@ class ContentRangeInputStream extends In
 
     // see ST_* constants...
     private volatile int m_state;
-    private volatile HttpURLConnection m_conn;
+    private volatile URLConnection m_conn;
     // administration...
     private volatile long m_readTotal;
     private volatile long m_readChunk;
@@ -212,16 +214,24 @@ class ContentRangeInputStream extends In
      * @param conn
      *            the URL connection to add the HTTP-Range header to, cannot be <code>null</code>.
      */
-    private void applyRangeHeader(HttpURLConnection conn) {
+    private void applyRangeHeader(URLConnection conn) {
         if (m_readTotal > 0L || m_chunkSize > 0) {
-            String rangeHeader;
-            if (m_chunkSize > 0) {
-                rangeHeader = String.format("%s=%d-%d", BYTES, m_readTotal, (m_readTotal + m_chunkSize));
-            }
-            else {
-                rangeHeader = String.format("%s=%d-", BYTES, m_readTotal);
+            if (conn instanceof HttpURLConnection) {
+                String rangeHeader;
+                if (m_chunkSize > 0) {
+                    rangeHeader = String.format("%s=%d-%d", BYTES, m_readTotal, (m_readTotal + m_chunkSize));
+                }
+                else {
+                    rangeHeader = String.format("%s=%d-", BYTES, m_readTotal);
+                }
+                conn.setRequestProperty(HDR_RANGE, rangeHeader);
+            } else {
+                // Non-HTTP connection, skip the first few bytes when calling this method for the first time...
+                if (m_contentInfo == null) {
+                    long skip = m_readTotal;
+                    ConnectionUtil.skip(conn, skip);
+                }
             }
-            conn.setRequestProperty(HDR_RANGE, rangeHeader);
         }
     }
 
@@ -245,8 +255,7 @@ class ContentRangeInputStream extends In
      */
     private void closeChunk() {
         if (m_conn != null) {
-            ConnectionUtil.close(m_conn);
-            m_conn = null;
+            m_conn = ConnectionUtil.close(m_conn);
             m_readChunk = 0;
         }
     }
@@ -278,7 +287,16 @@ class ContentRangeInputStream extends In
      * @throws IOException
      *             in case of I/O problems or unexpected content.
      */
-    private long[] getContentRangeInfo(HttpURLConnection conn) throws IOException {
+    private long[] getContentRangeInfo(URLConnection conn) throws IOException {
+        if (conn instanceof HttpURLConnection) {
+            return getHttpContentRangeInfo((HttpURLConnection) conn);
+        }
+
+        long totalBytes = conn.getContentLength();
+        return new long[] { totalBytes, totalBytes };
+    }
+
+    private long[] getHttpContentRangeInfo(HttpURLConnection conn) throws IOException {
         int rc = conn.getResponseCode();
         if (rc == SC_OK) {
             // Non-chunked response...
@@ -338,7 +356,7 @@ class ContentRangeInputStream extends In
      */
     private boolean prepareNextChunk() throws IOException {
         if ((m_conn == null) && contentRemaining()) {
-            m_conn = (HttpURLConnection) m_handler.getConnection(m_url);
+            m_conn = m_handler.getConnection(m_url);
 
             applyRangeHeader(m_conn);
 

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadCallableImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadCallableImpl.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadCallableImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadCallableImpl.java Wed Sep 18 12:03:40 2013
@@ -22,7 +22,6 @@ import static org.apache.ace.agent.impl.
 
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
 import java.util.concurrent.Callable;
@@ -35,16 +34,19 @@ import org.apache.ace.agent.DownloadStat
  * Responsible for actually downloading content from a download handle.
  */
 final class DownloadCallableImpl implements Callable<DownloadResult> {
+    /**
+     * Size of the buffer used while downloading the content stream.
+     */
+    private static final int READBUFFER_SIZE = 4096;
+
     private final DownloadHandleImpl m_handle;
     private final DownloadProgressListener m_listener;
     private final File m_target;
-    private final int m_readBufferSize;
 
-    DownloadCallableImpl(DownloadHandleImpl handle, DownloadProgressListener listener, File target, int readBufferSize) {
+    DownloadCallableImpl(DownloadHandleImpl handle, DownloadProgressListener listener, File target) {
         m_handle = handle;
         m_listener = listener;
         m_target = target;
-        m_readBufferSize = readBufferSize;
     }
 
     @Override
@@ -59,7 +61,7 @@ final class DownloadCallableImpl impleme
             is = new ContentRangeInputStream(m_handle.getConnectionHandler(), m_handle.getURL(), targetLength);
             os = new BufferedOutputStream(new FileOutputStream(m_target, appendTarget));
 
-            byte buffer[] = new byte[m_readBufferSize];
+            byte buffer[] = new byte[READBUFFER_SIZE];
             long bytesRead = targetLength, totalBytes = -1L;
             int read;
 
@@ -83,7 +85,7 @@ final class DownloadCallableImpl impleme
 
             m_handle.logDebug("Download completed: %d bytes downloaded...", totalBytes);
 
-            return new DownloadResultImpl(DownloadState.SUCCESSFUL, new FileInputStream(m_target));
+            return new DownloadResultImpl(DownloadState.SUCCESSFUL, m_target);
         }
         finally {
             closeSilently(os);

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandleImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandleImpl.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandleImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandleImpl.java Wed Sep 18 12:03:40 2013
@@ -19,8 +19,9 @@
 package org.apache.ace.agent.impl;
 
 import java.io.File;
-import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 
@@ -33,26 +34,17 @@ import org.apache.ace.agent.DownloadResu
  * server supports this feature.
  */
 class DownloadHandleImpl implements DownloadHandle {
-    /**
-     * Size of the buffer used while downloading the content stream.
-     */
-    private static final int DEFAULT_READBUFFER_SIZE = 1024;
-
     private final DownloadHandlerImpl m_handler;
+    private final File m_file;
     private final URL m_url;
-    private final int m_readBufferSize;
 
     private volatile Future<DownloadResult> m_future;
-    private volatile File m_file;
 
     DownloadHandleImpl(DownloadHandlerImpl handler, URL url) {
-        this(handler, url, DEFAULT_READBUFFER_SIZE);
-    }
-
-    DownloadHandleImpl(DownloadHandlerImpl handler, URL url, int readBufferSize) {
         m_handler = handler;
         m_url = url;
-        m_readBufferSize = readBufferSize;
+        
+        m_file = new File(m_handler.getDataLocation(), getDownloadFileName());
     }
 
     @Override
@@ -61,9 +53,7 @@ class DownloadHandleImpl implements Down
             stop();
         }
         finally {
-            if (m_file != null) {
-                m_file.delete();
-            }
+            m_file.delete();
         }
     }
 
@@ -74,20 +64,12 @@ class DownloadHandleImpl implements Down
         }
         m_future = null;
 
-        if (m_file == null) {
-            try {
-                m_file = File.createTempFile("download", ".bin", m_handler.getDataLocation());
-            }
-            catch (IOException e) {
-                throw new RuntimeException("Failed to create temporary file!", e);
-            }
-        }
-
         ExecutorService executor = getExecutor();
         if (executor.isShutdown()) {
             m_handler.logWarning("Cannot start download, executor is shut down!");
-        } else {
-            m_future = executor.submit(new DownloadCallableImpl(this, listener, m_file, m_readBufferSize));
+        }
+        else {
+            m_future = executor.submit(new DownloadCallableImpl(this, listener, m_file));
         }
 
         return m_future;
@@ -104,6 +86,18 @@ class DownloadHandleImpl implements Down
         m_future = null;
     }
 
+    final ConnectionHandler getConnectionHandler() {
+        return m_handler.getConnectionHandler();
+    }
+    
+    final File getDownloadFile() {
+        return m_file;
+    }
+
+    final URL getURL() {
+        return m_url;
+    }
+
     final void logDebug(String message, Object... args) {
         m_handler.logDebug(message, args);
     }
@@ -112,15 +106,19 @@ class DownloadHandleImpl implements Down
         m_handler.logWarning(message, cause, args);
     }
 
-    final ConnectionHandler getConnectionHandler() {
-        return m_handler.getConnectionHandler();
-    }
-
-    final URL getURL() {
-        return m_url;
+    /**
+     * @return the filename for the (temporary) download location.
+     */
+    private String getDownloadFileName() {
+        try {
+            return URLEncoder.encode(m_url.toExternalForm(), "ASCII");
+        }
+        catch (UnsupportedEncodingException exception) {
+            throw new RuntimeException("ASCII encoding not supported?!");
+        }
     }
 
     private ExecutorService getExecutor() {
-        return m_handler.getExecutor();
+        return m_handler.getExecutorService();
     }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandlerImpl.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadHandlerImpl.java Wed Sep 18 12:03:40 2013
@@ -20,7 +20,6 @@ package org.apache.ace.agent.impl;
 
 import java.io.File;
 import java.net.URL;
-import java.util.concurrent.ExecutorService;
 
 import org.apache.ace.agent.DownloadHandle;
 import org.apache.ace.agent.DownloadHandler;
@@ -45,16 +44,4 @@ public class DownloadHandlerImpl extends
     public DownloadHandle getHandle(URL url) {
         return new DownloadHandleImpl(this, url);
     }
-
-    @Override
-    public DownloadHandle getHandle(URL url, int readBufferSize) {
-        return new DownloadHandleImpl(this, url, readBufferSize);
-    }
-
-    /*
-     * handle support methods
-     */
-    ExecutorService getExecutor() {
-        return getExecutorService();
-    }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadResultImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadResultImpl.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadResultImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DownloadResultImpl.java Wed Sep 18 12:03:40 2013
@@ -18,6 +18,8 @@
  */
 package org.apache.ace.agent.impl;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -29,16 +31,16 @@ import org.apache.ace.agent.DownloadStat
  */
 public class DownloadResultImpl implements DownloadResult {
     private final DownloadState m_state;
-    private final InputStream m_inputStream;
+    private final File m_result;
 
     DownloadResultImpl(DownloadState state) {
         m_state = state;
-        m_inputStream = null;
+        m_result = null;
     }
 
-    DownloadResultImpl(DownloadState state, InputStream is) {
+    DownloadResultImpl(DownloadState state, File result) {
         m_state = state;
-        m_inputStream = is;
+        m_result = result;
     }
 
     @Override
@@ -46,7 +48,7 @@ public class DownloadResultImpl implemen
         if (!isComplete()) {
             throw new IllegalStateException("Cannot access incomplete download result!");
         }
-        return m_inputStream;
+        return new FileInputStream(m_result);
     }
 
     @Override

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java Wed Sep 18 12:03:40 2013
@@ -31,16 +31,19 @@ import org.apache.ace.agent.LoggingHandl
  * Default thread-safe {@link LoggingHandler} implementation that logs messages to {@link System.out} .
  */
 public class LoggingHandlerImpl extends ComponentBase implements LoggingHandler, EventListener {
-    private static final Levels DEFAULT = Levels.WARNING;
-    
+    private static final Levels DEFAULT_LEVEL = Levels.WARNING;
+
     private volatile Levels m_logLevel;
 
     public LoggingHandlerImpl() {
+        this(DEFAULT_LEVEL);
+    }
+
+    public LoggingHandlerImpl(Levels defaultLevel) {
         super("logging");
-        
-        m_logLevel = DEFAULT;
+        m_logLevel = defaultLevel;
     }
-    
+
     @Override
     public void handle(String topic, Map<String, String> payload) {
         if (EVENT_AGENT_CONFIG_CHANGED.equals(topic)) {
@@ -69,12 +72,12 @@ public class LoggingHandlerImpl extends 
     public void logError(String component, String message, Throwable exception, Object... args) {
         log(Levels.ERROR, component, message, exception, args);
     }
-    
+
     @Override
     protected void onInit() throws Exception {
         getEventsHandler().addListener(this);
     }
-    
+
     @Override
     protected void onStop() throws Exception {
         getEventsHandler().removeListener(this);
@@ -97,13 +100,13 @@ public class LoggingHandlerImpl extends 
 
     private static Levels fromName(String name) {
         if (name == null) {
-            return DEFAULT;
+            return DEFAULT_LEVEL;
         }
         try {
             return Levels.valueOf(name.toUpperCase().trim());
         }
         catch (Exception e) {
-            return DEFAULT;
+            return DEFAULT_LEVEL;
         }
     }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java Wed Sep 18 12:03:40 2013
@@ -39,8 +39,8 @@ import org.apache.ace.agent.ConnectionHa
 import org.apache.ace.agent.EventsHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.apache.ace.agent.testutil.TestWebServer;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -76,7 +76,7 @@ public class ConnectionHandlerImplTest e
     private URL m_basicAuthURL;
     private AgentContextImpl m_agentContext;
 
-    @BeforeTest
+    @BeforeClass
     public void setUpOnceAgain() throws Exception {
         m_basicAuthURL = new URL("http://localhost:" + PORT + "/basicauth");
 
@@ -93,7 +93,7 @@ public class ConnectionHandlerImplTest e
         m_agentContext.start();
     }
 
-    @AfterTest
+    @AfterClass
     public void tearDownOnceAgain() throws Exception {
         m_agentContext.stop();
         m_webServer.stop();

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java Wed Sep 18 12:03:40 2013
@@ -56,8 +56,8 @@ import org.apache.ace.agent.testutil.Tes
 import org.osgi.framework.Version;
 import org.osgi.service.deploymentadmin.DeploymentAdmin;
 import org.osgi.service.deploymentadmin.DeploymentPackage;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -118,9 +118,8 @@ public class DeploymentHandlerImplTest e
 
     private AgentContextImpl m_agentContext;
 
-    @BeforeTest
+    @BeforeClass
     public void setUpOnceAgain() throws Exception {
-
         serverURL = new URL("http://localhost:" + port + "/");
         m_webserver = new TestWebServer(port, "/", "generated");
         m_webserver.start();
@@ -178,7 +177,7 @@ public class DeploymentHandlerImplTest e
         m_agentContext.start();
     }
 
-    @AfterTest
+    @AfterClass
     public void tearDownOnceAgain() throws Exception {
         m_webserver.stop();
         m_agentContext.stop();

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java Wed Sep 18 12:03:40 2013
@@ -31,8 +31,8 @@ import org.apache.ace.agent.DiscoveryHan
 import org.apache.ace.agent.EventsHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.apache.ace.agent.testutil.TestWebServer;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -49,7 +49,7 @@ public class DiscoveryHandlerImplTest ex
     private AgentContext m_agentContext;
     private AgentContextImpl m_agentContextImpl;
 
-    @BeforeTest
+    @BeforeClass
     public void setUpOnceAgain() throws Exception {
         m_webServer = new TestWebServer(PORT, "/", "generated");
         m_webServer.start();
@@ -66,7 +66,7 @@ public class DiscoveryHandlerImplTest ex
         m_agentContextImpl.start();
     }
 
-    @AfterTest
+    @AfterClass
     public void tearDownOnceAgain() throws Exception {
         m_webServer.stop();
 

Added: 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=1524378&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java (added)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java Wed Sep 18 12:03:40 2013
@@ -0,0 +1,254 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+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.net.URL;
+import java.security.DigestInputStream;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.ace.agent.ConnectionHandler;
+import org.apache.ace.agent.DownloadHandle;
+import org.apache.ace.agent.DownloadHandle.DownloadProgressListener;
+import org.apache.ace.agent.DownloadHandler;
+import org.apache.ace.agent.DownloadResult;
+import org.apache.ace.agent.testutil.BaseAgentTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Test cases for {@link DownloadHandleImpl}.
+ */
+public class DownloadHandleImplTest extends BaseAgentTest {
+    private AgentContextImpl m_agentContext;
+
+    private URL m_testContentURL;
+    private String m_digest;
+    private long m_contentLength;
+
+    @BeforeClass
+    public void setUp() throws Exception {
+        m_agentContext = mockAgentContext();
+        m_agentContext.setHandler(DownloadHandler.class, new DownloadHandlerImpl(m_agentContext.getWorkDir()));
+        m_agentContext.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
+        replayTestMocks();
+        m_agentContext.start();
+    }
+
+    @BeforeMethod
+    public void setUpTestCase() throws Exception {
+        File file = File.createTempFile("test", ".bin", new File("generated"));
+        file.deleteOnExit();
+
+        DigestOutputStream dos = null;
+        try {
+            dos = new DigestOutputStream(new FileOutputStream(file), MessageDigest.getInstance("MD5"));
+
+            for (int i = 0; i < 10000; i++) {
+                dos.write(String.valueOf(System.currentTimeMillis()).getBytes());
+                dos.write(" Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum\n".getBytes());
+            }
+            dos.flush();
+        }
+        finally {
+            if (dos != null) {
+                dos.close();
+            }
+        }
+
+        m_testContentURL = file.toURI().toURL();
+        m_contentLength = file.length();
+        m_digest = new String(dos.getMessageDigest().digest());
+    }
+
+    @AfterClass
+    public void tearDown() throws Exception {
+        m_agentContext.stop();
+        verifyTestMocks();
+        clearTestMocks();
+    }
+
+    @Test
+    public void testCreateDownloadHandleGeneratesSameTemporaryFilenameOk() throws Exception {
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+
+        DownloadHandleImpl handle;
+
+        handle = (DownloadHandleImpl) downloadHandler.getHandle(m_testContentURL);
+        String tempFilename = handle.getDownloadFile().getAbsolutePath();
+
+        handle = (DownloadHandleImpl) downloadHandler.getHandle(m_testContentURL);
+        assertEquals(handle.getDownloadFile().getAbsolutePath(), tempFilename);
+
+        handle = (DownloadHandleImpl) downloadHandler.getHandle(m_testContentURL);
+        assertEquals(handle.getDownloadFile().getAbsolutePath(), tempFilename);
+    }
+
+    @Test
+    public void testDownloadOk() throws Exception {
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+
+        DownloadResult downloadResult;
+        Future<DownloadResult> future;
+
+        final DownloadHandle handle = downloadHandler.getHandle(m_testContentURL);
+        future = handle.start(null);
+
+        downloadResult = future.get(5, TimeUnit.SECONDS);
+        assertNotNull(downloadResult, "Failed to finish download?!");
+        assertTrue(downloadResult.isComplete());
+
+        File file = ((DownloadHandleImpl) handle).getDownloadFile();
+        long fileLength = file.length();
+
+        assertTrue(file.exists(), file.getName() + " does not exist?!");
+        assertTrue(fileLength > 0 && fileLength == m_contentLength, "Nothing downloaded yet for " + file.getName() + "?");
+
+        // Verify the contents of the downloaded file is what we expect...
+        assertEquals(getDigest(file), m_digest);
+    }
+
+    @Test
+    public void testRestartDownloadOk() throws Exception {
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+
+        DownloadResult downloadResult;
+        Future<DownloadResult> future;
+
+        final DownloadHandle handle = downloadHandler.getHandle(m_testContentURL);
+        future = handle.start(new DownloadProgressListener() {
+            @Override
+            public void progress(long bytesRead, long totalBytes) {
+                handle.stop();
+            }
+        });
+
+        try {
+            future.get(5, TimeUnit.SECONDS);
+        }
+        catch (CancellationException exception) {
+            // Ok; expected...
+        }
+        assertTrue(future.isCancelled());
+
+        File file = ((DownloadHandleImpl) handle).getDownloadFile();
+        long fileLength = file.length();
+
+        // Discard the result...
+        handle.discard();
+
+        // Restart & finish the download...
+        DownloadHandle handle2 = downloadHandler.getHandle(m_testContentURL);
+        future = handle2.start(null);
+
+        downloadResult = future.get(5, TimeUnit.SECONDS);
+        assertNotNull(downloadResult, "Failed to finish download?!");
+        assertTrue(downloadResult.isComplete());
+
+        fileLength = file.length();
+
+        assertTrue(file.exists(), file.getName() + " does not exist?!");
+        assertTrue(fileLength == m_contentLength, "Nothing downloaded yet for " + file.getName() + "?");
+
+        // Verify the contents of the downloaded file is what we expect...
+        assertEquals(getDigest(file), m_digest);
+    }
+
+    @Test
+    public void testResumeDownloadOk() throws Exception {
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+
+        DownloadResult downloadResult;
+        Future<DownloadResult> future;
+
+        final DownloadHandle handle = downloadHandler.getHandle(m_testContentURL);
+        // Start the download, but interrupt it after reading the first chunk of data...
+        future = handle.start(new DownloadProgressListener() {
+            @Override
+            public void progress(long bytesRead, long totalBytes) {
+                Thread.currentThread().interrupt();
+            }
+        });
+
+        downloadResult = future.get(5, TimeUnit.SECONDS);
+        assertNotNull(downloadResult, "Failed to stop download?!");
+        assertFalse(downloadResult.isComplete());
+
+        File file = ((DownloadHandleImpl) handle).getDownloadFile();
+        long firstFileLength = file.length();
+
+        assertTrue(file.exists(), file.getName() + " does not exist?!");
+        assertTrue(firstFileLength > 0 && firstFileLength < m_contentLength, "Nothing downloaded yet for " + file.getName() + "?");
+
+        final DownloadHandle handle2 = downloadHandler.getHandle(m_testContentURL);
+        // Resume the download, but stop it after reading the first chunk of data...
+        future = handle2.start(new DownloadProgressListener() {
+            @Override
+            public void progress(long bytesRead, long totalBytes) {
+                handle2.stop();
+            }
+        });
+
+        try {
+            future.get(5, TimeUnit.SECONDS);
+        }
+        catch (CancellationException exception) {
+            // Ok; expected...
+        }
+        assertTrue(future.isCancelled());
+
+        long secondFileLength = file.length();
+
+        assertTrue(secondFileLength > firstFileLength && secondFileLength < m_contentLength, "Nothing downloaded yet for " + file.getName() + "?");
+
+        DownloadHandle handle3 = downloadHandler.getHandle(m_testContentURL);
+        // Resume the download, and finish it...
+        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() + "?");
+
+        // Verify the contents of the downloaded file is what we expect...
+        assertEquals(getDigest(file), m_digest);
+    }
+
+    private String getDigest(File file) throws Exception {
+        DigestInputStream dis = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance("MD5"));
+        while (dis.read() != -1) {
+        }
+        dis.close();
+        return new String(dis.getMessageDigest().digest());
+    }
+}

Propchange: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandleImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1524378&r1=1524377&r2=1524378&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 12:03:40 2013
@@ -33,11 +33,8 @@ 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.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
 import javax.servlet.ServletException;
@@ -54,11 +51,12 @@ import org.apache.ace.agent.DownloadHand
 import org.apache.ace.agent.DownloadResult;
 import org.apache.ace.agent.EventsHandler;
 import org.apache.ace.agent.LoggingHandler;
+import org.apache.ace.agent.LoggingHandler.Levels;
 import org.apache.ace.agent.RetryAfterException;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.apache.ace.agent.testutil.TestWebServer;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -94,18 +92,19 @@ public class DownloadHandlerTest extends
     private AgentContextImpl m_agentContextImpl;
     private AgentContext m_agentContext;
 
-    @BeforeTest
+    @BeforeClass
     public void setUpOnceAgain() throws Exception {
+        File dataLocation = new File("generated");
+
+        m_200file = File.createTempFile("download", ".bin", dataLocation);
+        m_200file.deleteOnExit();
+
         int port = 8883;
 
-        m_200url = new URL("http://localhost:" + port + "/testfile.txt");
+        m_200url = new URL("http://localhost:" + port + "/" + m_200file.getName());
         m_404url = new URL("http://localhost:" + port + "/error?status=404");
         m_503url = new URL("http://localhost:" + port + "/error?status=503&retry=500");
 
-        File dataLocation = new File("generated");
-
-        m_200file = new File(dataLocation, "testfile.txt");
-
         DigestOutputStream dos = new DigestOutputStream(new FileOutputStream(m_200file), MessageDigest.getInstance("MD5"));
         for (int i = 0; i < 10000; i++) {
             dos.write(String.valueOf(System.currentTimeMillis()).getBytes());
@@ -114,26 +113,23 @@ public class DownloadHandlerTest extends
         dos.close();
         m_200digest = new BigInteger(dos.getMessageDigest().digest()).toString();
 
-        m_webServer = new TestWebServer(port, "/", "generated");
+        m_webServer = new TestWebServer(port, "/", dataLocation.getName());
         m_webServer.addServlet(new TestErrorServlet(), "/error");
         m_webServer.start();
 
         m_agentContextImpl = mockAgentContext();
         m_agentContext = m_agentContextImpl;
 
-        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
-
-        m_agentContextImpl.setHandler(ScheduledExecutorService.class, executorService);
         m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(mockBundleContext()));
         m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
-        m_agentContextImpl.setHandler(LoggingHandler.class, new LoggingHandlerImpl());
+        m_agentContextImpl.setHandler(LoggingHandler.class, new LoggingHandlerImpl(Levels.DEBUG));
         m_agentContextImpl.setHandler(DownloadHandler.class, new DownloadHandlerImpl(dataLocation));
 
         m_agentContextImpl.start();
         replayTestMocks();
     }
 
-    @AfterTest
+    @AfterClass
     public void tearDownOnceAgain() throws Exception {
         m_agentContextImpl.stop();
         m_webServer.stop();
@@ -145,6 +141,8 @@ public class DownloadHandlerTest extends
         DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
 
         DownloadHandle handle = downloadHandler.getHandle(m_200url);
+        handle.discard();
+
         Future<DownloadResult> result = handle.start(null);
 
         assertSuccessful(result, 200, m_200digest);
@@ -155,10 +153,12 @@ public class DownloadHandlerTest extends
         DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
 
         final DownloadHandle handle = downloadHandler.getHandle(m_200url);
+        handle.discard();
+
         Future<DownloadResult> future = handle.start(new DownloadProgressListener() {
             @Override
             public void progress(long read, long total) {
-                handle.stop();
+                Thread.currentThread().interrupt();
             }
         });
 
@@ -227,16 +227,9 @@ public class DownloadHandlerTest extends
     }
 
     private static void assertStopped(Future<DownloadResult> future, int statusCode) throws Exception {
-        try {
-            future.get(5, TimeUnit.SECONDS);
-
-            fail("Expected CancellationException!");
-        }
-        catch (CancellationException exception) {
-            // Expected...
-        }
+        DownloadResult downloadResult = future.get(5, TimeUnit.SECONDS);
 
-        assertTrue(future.isCancelled());
+        assertFalse(downloadResult.isComplete());
     }
 
     private static String getDigest(InputStream is) throws Exception {

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java Wed Sep 18 12:03:40 2013
@@ -25,15 +25,14 @@ import static org.testng.Assert.assertNu
 import static org.testng.Assert.assertTrue;
 
 import java.io.IOException;
-import java.lang.reflect.Method;
 import java.util.Set;
 
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.EventsHandler;
 import org.apache.ace.agent.FeedbackHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -48,9 +47,9 @@ public class FeedbackHandlerImplTest ext
 
     private AgentContextImpl m_agentContextImpl;
 
-    @BeforeMethod
-    public void setUpAgain(Method method) throws Exception {
-        m_agentContextImpl = mockAgentContext(method.getName());
+    @BeforeClass
+    public void setUpAgain() throws Exception {
+        m_agentContextImpl = mockAgentContext();
         replayTestMocks();
 
         m_agentContextImpl.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl());
@@ -60,8 +59,8 @@ public class FeedbackHandlerImplTest ext
         m_agentContextImpl.start();
     }
 
-    @AfterMethod
-    public void tearDownAgain(Method method) throws Exception {
+    @AfterClass
+    public void tearDownAgain() throws Exception {
         m_agentContextImpl.stop();
         verifyTestMocks();
         clearTestMocks();

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java Wed Sep 18 12:03:40 2013
@@ -31,8 +31,8 @@ import org.apache.ace.agent.AgentContext
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.IdentificationHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /**
@@ -43,7 +43,7 @@ public class IdentificationHandlerImplTe
     private AgentContextImpl m_agentContextImpl;
     private AgentContext m_agentContext;
 
-    @BeforeTest
+    @BeforeClass
     public void setUpAgain() throws Exception {
         m_agentContextImpl = mockAgentContext();
         m_agentContext = m_agentContextImpl;
@@ -52,7 +52,7 @@ public class IdentificationHandlerImplTe
         replayTestMocks();
     }
 
-    @AfterTest
+    @AfterClass
     public void tearDownAgain() throws Exception {
         m_agentContextImpl.stop();
         verifyTestMocks();

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java Wed Sep 18 12:03:40 2013
@@ -18,48 +18,41 @@
  */
 package org.apache.ace.agent.testutil;
 
-import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.Stack;
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.AgentContextAware;
 import org.apache.ace.agent.impl.AgentContextImpl;
 import org.osgi.framework.BundleContext;
+import org.testng.annotations.AfterClass;
 
 /**
  * Simple base class.
  */
 public abstract class BaseAgentTest {
     private final Set<Object> m_mocks = new HashSet<Object>();
-
+    private File m_contextDir;
+    
     protected <T> T addTestMock(Class<T> clazz) {
         T mock = createNiceMock(clazz);
         m_mocks.add(mock);
         return mock;
     }
 
-    protected void cleanDir(File dir) {
-        if (!dir.isDirectory())
-            throw new IllegalStateException();
-        Stack<File> dirs = new Stack<File>();
-        dirs.push(dir);
-        while (!dirs.isEmpty()) {
-            File currentDir = dirs.pop();
-            File[] files = currentDir.listFiles();
-            for (File file : files) {
-                if (file.isDirectory()) {
-                    dirs.push(file);
-                }
-                else {
-                    file.delete();
-                }
-            }
-        }
+    @AfterClass
+    protected void cleanupMess() throws IOException {
+        cleanDir(m_contextDir);
+        m_contextDir.delete();
     }
 
     protected void clearTestMocks() {
@@ -75,11 +68,15 @@ public abstract class BaseAgentTest {
     }
 
     protected AgentContextImpl mockAgentContext(String subDir) throws Exception {
-        File contextDir = new File(getWorkDir(), subDir);
-        contextDir.mkdirs();
-        cleanDir(contextDir);
+        if (m_contextDir != null) {
+            cleanDir(m_contextDir);
+            m_contextDir.delete();
+        }
+        m_contextDir = new File(getWorkDir(), subDir);
+        m_contextDir.mkdirs();
+        cleanDir(m_contextDir);
 
-        AgentContextImpl context = new AgentContextImpl(contextDir);
+        AgentContextImpl context = new AgentContextImpl(m_contextDir);
         for (Class<?> handlerClass : AgentContextImpl.KNOWN_HANDLERS) {
             if (ScheduledExecutorService.class.equals(handlerClass)) {
                 // always inject a proper executor service that simply invokes synchronously...
@@ -93,7 +90,9 @@ public abstract class BaseAgentTest {
     }
 
     protected BundleContext mockBundleContext() throws Exception {
+        File dataFile = new File("generated");
         BundleContext result = createNiceMock(BundleContext.class);
+        expect(result.getDataFile(anyObject(String.class))).andReturn(dataFile).anyTimes();
         expect(result.createFilter(anyObject(String.class))).andReturn(null).anyTimes();
         replay(result);
         return result;
@@ -126,4 +125,17 @@ public abstract class BaseAgentTest {
             verify(mock);
         }
     }
+
+    private void cleanDir(File dir) {
+        if (!dir.isDirectory()) {
+            throw new IllegalStateException("Not a directory: " + dir);
+        }
+        File[] files = dir.listFiles();
+        for (File file : files) {
+            if (file.isDirectory()) {
+                cleanDir(file);
+            }
+            file.delete();
+        }
+    }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java?rev=1524378&r1=1524377&r2=1524378&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java Wed Sep 18 12:03:40 2013
@@ -23,6 +23,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
@@ -107,8 +108,8 @@ public class SynchronousExecutorService 
     }
 
     @Override
-    public <T> Future<T> submit(Callable<T> task) {
-        throw new UnsupportedOperationException();
+    public <T> Future<T> submit(final Callable<T> task) {
+        return Executors.newSingleThreadExecutor().submit(task);
     }
 
     @Override



Mime
View raw message