ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1731916 - in /ace/trunk: org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/ org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/ org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/ org.apache.ace.auth...
Date Tue, 23 Feb 2016 18:01:51 GMT
Author: jawi
Date: Tue Feb 23 18:01:50 2016
New Revision: 1731916

URL: http://svn.apache.org/viewvc?rev=1731916&view=rev
Log:
ACE-532 - some cleanups and minor improvements:

- make sure that all URLConnections are properly closed in the integration
  tests;
- use a default wait time for URLs to be available (or not);
- this closes #9.


Modified:
    ace/trunk/org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/BaseAgentControllerTest.java
    ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/AgentExtensionTest.java
    ace/trunk/org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/AgentUpdateTest.java
    ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/AuthenticationTestBase.java
    ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/LogAuthenticationTest.java
    ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/ObrAuthenticationTest.java
    ace/trunk/org.apache.ace.client.repository.itest/bnd.bnd
    ace/trunk/org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/BaseRepositoryAdminTest.java
    ace/trunk/org.apache.ace.client.rest.itest/src/org/apache/ace/client/rest/itest/RESTClientTest.java
    ace/trunk/org.apache.ace.deployment.itest/src/org/apache/ace/it/deployment/Ace330Test.java
    ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
    ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
    ace/trunk/org.apache.ace.test/bnd.bnd
    ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java
    ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/NetUtils.java
    ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/packageinfo
    ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/UserAdminRepositoryTest.java
    ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/Utils.java

Modified: ace/trunk/org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/BaseAgentControllerTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/BaseAgentControllerTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/BaseAgentControllerTest.java
(original)
+++ ace/trunk/org.apache.ace.agent.controller.itest/src/org/apache/ace/agent/itest/BaseAgentControllerTest.java
Tue Feb 23 18:01:50 2016
@@ -296,11 +296,11 @@ public abstract class BaseAgentControlle
         StubDeploymentServlet servlet = new StubDeploymentServlet(AGENT_ID, package1);
 
         String url = String.format("http://localhost:%d/", TestConstants.PORT);
-        NetUtils.waitForURL(url, 404, 10000);
+        NetUtils.waitForURL_NotFound(url);
 
         m_http.registerServlet("/", servlet, null, null);
 
-        NetUtils.waitForURL(url, 200, 10000);
+        NetUtils.waitForURL(url);
 
         // Tell our agent what controller to use, in this case, we simply disable the controller
as we want to invoke
         // everything externally from the AgentControl service...

Modified: ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/AgentExtensionTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/AgentExtensionTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/AgentExtensionTest.java
(original)
+++ ace/trunk/org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/AgentExtensionTest.java
Tue Feb 23 18:01:50 2016
@@ -114,7 +114,6 @@ public class AgentExtensionTest extends
     private ServiceRegistration<ConnectionHandler> registerConnectionHandler() {
         return m_bundleContext
             .registerService(ConnectionHandler.class, new ConnectionHandler() {
-
                 @Override
                 public URLConnection getConnection(URL url) throws IOException {
                     return url.openConnection();

Modified: ace/trunk/org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/AgentUpdateTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/AgentUpdateTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/AgentUpdateTest.java
(original)
+++ ace/trunk/org.apache.ace.agent.update.itest/src/org/apache/ace/agent/itest/AgentUpdateTest.java
Tue Feb 23 18:01:50 2016
@@ -137,6 +137,10 @@ public class AgentUpdateTest extends Int
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
             String path = req.getPathInfo();
+            if (path == null) {
+                path = "/";
+            }
+            
             if ("/repository.xml".equals(path)) {
                 PrintWriter w = resp.getWriter();
                 w.println("<?xml version='1.0' encoding='utf-8'?><repository>");
@@ -264,14 +268,14 @@ public class AgentUpdateTest extends Int
         m_servlet = new AgentUpdateOBRServlet(currentAgentVersion);
 
         String url = String.format("http://localhost:%d/obr", TestConstants.PORT);
-        NetUtils.waitForURL(url, 404, 10000);
+        NetUtils.waitForURL_NotFound(url);
 
         m_http.registerServlet("/obr", m_servlet, null, null);
         m_http.registerServlet("/auditlog", new DummyAuditLogServlet(), null, null);
         m_http.registerServlet("/deployment", new DeploymentServlet(), null, null);
         m_http.registerServlet("/agent", new DummyAgentVersionServlet(), null, null);
 
-        NetUtils.waitForURL(url, 200, 10000);
+        NetUtils.waitForURL(url);
     }
 
     @Override

Modified: ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/AuthenticationTestBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/AuthenticationTestBase.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/AuthenticationTestBase.java
(original)
+++ ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/AuthenticationTestBase.java
Tue Feb 23 18:01:50 2016
@@ -23,18 +23,21 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.Date;
 import java.util.Enumeration;
-
-import junit.framework.Assert;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.ace.connectionfactory.ConnectionFactory;
 import org.apache.ace.it.IntegrationTestBase;
 import org.apache.ace.repository.Repository;
+import org.apache.ace.test.utils.NetUtils;
 import org.osgi.service.log.LogEntry;
 import org.osgi.service.log.LogReaderService;
 import org.osgi.service.useradmin.UserAdmin;
 
+import junit.framework.Assert;
+
 /**
  * Provides a common base class for all authentication integration tests.
  */
@@ -63,11 +66,10 @@ public class AuthenticationTestBase exte
      *             in case of exceptions during the import.
      */
     protected final void importSingleUser(Repository userRepository, String userName, String
password) throws Exception {
-        ByteArrayInputStream bis = new ByteArrayInputStream((
-            "<roles>" +
-                "<user name=\"" + userName + "\">" +
-                "<credentials><password>" + password + "</password></credentials>"
+
-                "</user>" +
+        ByteArrayInputStream bis = new ByteArrayInputStream(("<roles>" +
+            "<user name=\"" + userName + "\">" +
+            "<credentials><password>" + password + "</password></credentials>"
+
+            "</user>" +
             "</roles>").getBytes());
 
         Assert.assertTrue("Committing test user data failed!", userRepository.commit(bis,
userRepository.getRange().getHigh()));
@@ -85,34 +87,30 @@ public class AuthenticationTestBase exte
      *            Amount of milliseconds to keep trying to access the URL.
      * @return True if the response of the URL has the specified status code within the specified
timeout delay, false
      *         otherwise.
+     * @throws IOException
      * @throws IllegalArgumentException
      *             If the specified URL does not use the HTTP protocol.
      */
-    protected final boolean waitForURL(ConnectionFactory connectionFactory, URL url, int
responseCode, int timeout) {
-        long deadline = System.currentTimeMillis() + timeout;
-        while (System.currentTimeMillis() < deadline) {
-            HttpURLConnection connection = null;
-            try {
-                connection = (HttpURLConnection) connectionFactory.createConnection(url);
+    protected final boolean waitForURL(ConnectionFactory connectionFactory, URL url, int
responseCode) throws IOException {
+        URLConnection conn = null;
 
-                int respCode = ((HttpURLConnection) connection).getResponseCode();
-                if (respCode == responseCode) {
+        int tries = 4;
+        while (tries-- > 0) {
+            conn = connectionFactory.createConnection(url);
+            try {
+                boolean result = ((HttpURLConnection) conn).getResponseCode() == responseCode;
+                if (result) {
                     return true;
                 }
-                else {
-                    System.err.println("Got response code " + respCode + " for " + url);
+                try {
+                    TimeUnit.MILLISECONDS.sleep(250);
                 }
-            }
-            catch (IOException ioe) {
-                if (connection != null) {
-                    connection.disconnect();
+                catch (InterruptedException exception) {
+                    return false;
                 }
             }
-            try {
-                Thread.sleep(100);
-            }
-            catch (InterruptedException ie) {
-                return false;
+            finally {
+                NetUtils.closeConnection(conn);
             }
         }
         return false;

Modified: ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/LogAuthenticationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/LogAuthenticationTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/LogAuthenticationTest.java
(original)
+++ ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/LogAuthenticationTest.java
Tue Feb 23 18:01:50 2016
@@ -18,9 +18,11 @@
  */
 package org.apache.ace.it.authentication;
 
+import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +40,7 @@ import org.apache.ace.log.server.store.L
 import org.apache.ace.repository.Repository;
 import org.apache.ace.repository.RepositoryConstants;
 import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
 import org.apache.felix.dm.Component;
 import org.osgi.framework.Constants;
 import org.osgi.service.http.HttpService;
@@ -87,7 +90,7 @@ public class LogAuthenticationTest exten
         try {
             String baseURL = "http://" + HOST + ":" + TestConstants.PORT;
             URL testURL = new URL(baseURL.concat(AUDITLOG_ENDPOINT));
-            assertTrue("Failed to access auditlog in time!", waitForURL(m_connectionFactory,
testURL, 403, 15000));
+            assertTrue("Failed to access auditlog in time!", waitForURL(m_connectionFactory,
testURL, 403));
 
             String userName = "d";
             String password = "f";
@@ -101,7 +104,7 @@ public class LogAuthenticationTest exten
                 "authentication.user.name", userName,
                 "authentication.user.password", password);
 
-            assertTrue("Failed to access auditlog in time!", waitForURL(m_connectionFactory,
testURL, 200, 15000));
+            assertTrue("Failed to access auditlog in time!", waitForURL(m_connectionFactory,
testURL, 200));
         }
         catch (Exception e) {
             printLog(m_logReader);
@@ -148,31 +151,20 @@ public class LogAuthenticationTest exten
     @Override
     protected List<String> getResponse(String request) throws IOException {
         List<String> result = new ArrayList<>();
-        InputStream in = null;
-        try {
-            in = m_connectionFactory.createConnection(new URL(request)).getInputStream();
-            byte[] response = new byte[in.available()];
-            in.read(response);
-
-            StringBuilder element = new StringBuilder();
-            for (byte b : response) {
-                switch (b) {
-                    case '\n':
-                        result.add(element.toString());
-                        element = new StringBuilder();
-                        break;
-                    default:
-                        element.append(b);
+
+        URLConnection conn = m_connectionFactory.createConnection(new URL(request));
+        try (InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader
reader = new BufferedReader(in)) {
+            String line;
+            do {
+                line = reader.readLine();
+                if (line != null) {
+                    result.add(line);
                 }
             }
+            while (line != null);
         }
         finally {
-            try {
-                in.close();
-            }
-            catch (Exception e) {
-                // no problem.
-            }
+            NetUtils.closeConnection(conn);
         }
         return result;
     }

Modified: ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/ObrAuthenticationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/ObrAuthenticationTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/ObrAuthenticationTest.java
(original)
+++ ace/trunk/org.apache.ace.authentication.itest/src/org/apache/ace/it/authentication/ObrAuthenticationTest.java
Tue Feb 23 18:01:50 2016
@@ -22,6 +22,7 @@ package org.apache.ace.it.authentication
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.jar.Attributes;
@@ -123,7 +124,7 @@ public class ObrAuthenticationTest exten
 
             URL testURL = new URL(m_obrURL, "index.xml");
 
-            assertTrue("Failed to access OBR in time!", waitForURL(m_connectionFactory, testURL,
403, 15000));
+            assertTrue("Failed to access OBR in time!", waitForURL(m_connectionFactory, testURL,
403));
 
             m_authConfigPID = configureFactory("org.apache.ace.connectionfactory",
                 "authentication.baseURL", m_obrURL.toExternalForm(),
@@ -131,7 +132,7 @@ public class ObrAuthenticationTest exten
                 "authentication.user.name", userName,
                 "authentication.user.password", password);
 
-            assertTrue("Failed to access OBR in time!", waitForURL(m_connectionFactory, testURL,
200, 15000));
+            assertTrue("Failed to access OBR in time!", waitForURL(m_connectionFactory, testURL,
200));
         }
         catch (Exception e) {
             printLog(m_logReader);
@@ -154,9 +155,10 @@ public class ObrAuthenticationTest exten
      * Test that we can retrieve the 'index.xml' from the OBR.
      */
     public void testAccessObrRepositoryWithCredentialsOk() throws Exception {
+        URL url = new URL("http://localhost:" + TestConstants.PORT + m_endpoint + "/index.xml");
+        URLConnection conn = null;
         try {
-            URL url = new URL("http://localhost:" + TestConstants.PORT + m_endpoint + "/index.xml");
-            URLConnection conn = m_connectionFactory.createConnection(url);
+            conn = m_connectionFactory.createConnection(url);
             assertNotNull(conn);
             Object content = conn.getContent();
             assertNotNull(content);
@@ -164,6 +166,8 @@ public class ObrAuthenticationTest exten
         catch (Exception e) {
             printLog(m_logReader);
             throw e;
+        } finally {
+            NetUtils.closeConnection(conn);
         }
     }
 
@@ -178,8 +182,8 @@ public class ObrAuthenticationTest exten
             URLConnection conn = url.openConnection();
             assertNotNull(conn);
 
-            // we expect a 401 for this URL...
-            NetUtils.waitForURL(url, 401, 15000);
+            // we expect a 403 for this URL...
+            assertTrue(NetUtils.waitForURL(url, HttpURLConnection.HTTP_FORBIDDEN));
 
             try {
                 // ...causing all other methods on URLConnection to fail...
@@ -189,6 +193,9 @@ public class ObrAuthenticationTest exten
             catch (IOException exception) {
                 // Ok; ignored...
             }
+            finally {
+                NetUtils.closeConnection(conn);
+            }
         }
         catch (Exception e) {
             printLog(m_logReader);
@@ -215,8 +222,8 @@ public class ObrAuthenticationTest exten
             URLConnection conn = url.openConnection();
             assertNotNull(conn);
 
-            // we expect a 401 for this URL...
-            NetUtils.waitForURL(url, 401, 5000);
+            // we expect a 403 for this URL...
+            assertTrue(NetUtils.waitForURL(url, HttpURLConnection.HTTP_FORBIDDEN));
 
             try {
                 // ...causing all other methods on URLConnection to fail...
@@ -226,6 +233,9 @@ public class ObrAuthenticationTest exten
             catch (IOException exception) {
                 // Ok; ignored...
             }
+            finally {
+                NetUtils.closeConnection(conn);
+            }
         }
         catch (Exception e) {
             printLog(m_logReader);

Modified: ace/trunk/org.apache.ace.client.repository.itest/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.repository.itest/bnd.bnd?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.client.repository.itest/bnd.bnd (original)
+++ ace/trunk/org.apache.ace.client.repository.itest/bnd.bnd Tue Feb 23 18:01:50 2016
@@ -34,6 +34,10 @@ Test-Cases: ${classes;CONCRETE;EXTENDS;o
 	org.apache.felix.useradmin,\
 	org.apache.felix.useradmin.filestore,\
 	org.apache.felix.log,\
+	org.apache.felix.gogo.runtime,\
+	org.apache.felix.gogo.command,\
+	org.apache.felix.gogo.shell,\
+	org.apache.felix.dependencymanager.shell,\
 	org.apache.ace.test;version=latest,\
 	org.apache.ace.authentication.api;version=latest,\
 	org.apache.ace.connectionfactory;version=latest,\

Modified: ace/trunk/org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/BaseRepositoryAdminTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/BaseRepositoryAdminTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/BaseRepositoryAdminTest.java
(original)
+++ ace/trunk/org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/BaseRepositoryAdminTest.java
Tue Feb 23 18:01:50 2016
@@ -20,7 +20,6 @@ package org.apache.ace.it.repositoryadmi
 
 import java.io.IOException;
 import java.net.ConnectException;
-import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.Dictionary;
@@ -59,6 +58,7 @@ import org.apache.ace.obr.storage.OBRFil
 import org.apache.ace.repository.Repository;
 import org.apache.ace.repository.RepositoryConstants;
 import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentState;
 import org.apache.felix.dm.ComponentStateListener;
@@ -139,15 +139,8 @@ public abstract class BaseRepositoryAdmi
 
         // Wait for the endpoint to respond.
         URL repoURL = new URL(baseURL + "index.xml");
-        int response = ((HttpURLConnection) repoURL.openConnection()).getResponseCode();
-        int tries = 0;
-        while ((response != 200) && (tries++ < 50)) {
-            response = ((HttpURLConnection) repoURL.openConnection()).getResponseCode();
-            Thread.sleep(100); // If we get interrupted, there will be a good reason for
it.
-        }
-        if (tries == 50) {
-            throw new IOException("The OBR servlet does not seem to be responding well. Last
response code: " + response);
-        }
+
+        assertTrue("The OBR servlet does not seem to be responding well!", NetUtils.waitForURL(repoURL));
     }
 
     /* Configure a new repository instance */

Modified: ace/trunk/org.apache.ace.client.rest.itest/src/org/apache/ace/client/rest/itest/RESTClientTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.rest.itest/src/org/apache/ace/client/rest/itest/RESTClientTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.client.rest.itest/src/org/apache/ace/client/rest/itest/RESTClientTest.java
(original)
+++ ace/trunk/org.apache.ace.client.rest.itest/src/org/apache/ace/client/rest/itest/RESTClientTest.java
Tue Feb 23 18:01:50 2016
@@ -317,7 +317,7 @@ public class RESTClientTest extends Inte
             createServerUser();
 
             // Wait until our RESTClientServlet is up and responding...
-            NetUtils.waitForURL(HOST, 200, 1000);
+            NetUtils.waitForURL(HOST);
 
             m_hasBeenSetup = true;
         }

Modified: ace/trunk/org.apache.ace.deployment.itest/src/org/apache/ace/it/deployment/Ace330Test.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.deployment.itest/src/org/apache/ace/it/deployment/Ace330Test.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.deployment.itest/src/org/apache/ace/it/deployment/Ace330Test.java
(original)
+++ ace/trunk/org.apache.ace.deployment.itest/src/org/apache/ace/it/deployment/Ace330Test.java
Tue Feb 23 18:01:50 2016
@@ -94,7 +94,7 @@ public class Ace330Test extends Integrat
     @Override
     protected void configureAdditionalServices() throws Exception {
         // Wait until one of important repositories is online...
-        NetUtils.waitForURL(String.format("%s/repository/query?customer=%s&name=deployment",
m_host, TEST_CUSTOMER), 200, 100);
+        NetUtils.waitForURL(String.format("%s/repository/query?customer=%s&name=deployment",
m_host, TEST_CUSTOMER));
     }
 
     protected Component[] getDependencies() {

Modified: ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
(original)
+++ ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
Tue Feb 23 18:01:50 2016
@@ -36,6 +36,7 @@ import java.util.concurrent.TimeUnit;
 import org.apache.ace.it.IntegrationTestBase;
 import org.apache.ace.repository.Repository;
 import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
@@ -61,13 +62,22 @@ public class RepositoryTest extends Inte
 
         URL url = new URL(m_host, "replication/query?customer=apache&name=test&filter=test");
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-        int responseCode = connection.getResponseCode();
-        assertResponseCode(HttpURLConnection.HTTP_BAD_REQUEST, responseCode);
+        try {
+            int responseCode = connection.getResponseCode();
+            assertResponseCode(HttpURLConnection.HTTP_BAD_REQUEST, responseCode);
+        } finally {
+            NetUtils.closeConnection(connection);
+        }
 
         url = new URL(m_host, "repository/query?customer=apache&name=test&filter=test");
+        
         connection = (HttpURLConnection) url.openConnection();
-        responseCode = connection.getResponseCode();
-        assertResponseCode(HttpURLConnection.HTTP_BAD_REQUEST, responseCode);
+        try {
+            int responseCode = connection.getResponseCode();
+            assertResponseCode(HttpURLConnection.HTTP_BAD_REQUEST, responseCode);
+        } finally {
+            NetUtils.closeConnection(connection);
+        }
 
         removeRepository("testInstance");
     }

Modified: ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
(original)
+++ ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
Tue Feb 23 18:01:50 2016
@@ -20,13 +20,15 @@
 package org.apache.ace.it.repository;
 
 import java.io.Closeable;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.ConnectException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
+import org.apache.ace.test.utils.NetUtils;
+
 final class Utils {
 
     private static final int COPY_BUFFER_SIZE = 4096;
@@ -44,22 +46,7 @@ final class Utils {
     }
 
     static void closeSilently(HttpURLConnection resource) {
-        if (resource != null) {
-            try {
-                flushStream(resource.getInputStream());
-            }
-            catch (IOException exception) {
-                // Ignore...
-            }
-            try {
-                InputStream es = resource.getErrorStream();
-                if (es != null) {
-                    flushStream(es);
-                }
-            } finally {
-                resource.disconnect();
-            }
-        }
+        NetUtils.closeConnection(resource);
     }
 
     /* copy in to out */
@@ -99,8 +86,9 @@ final class Utils {
 
             responseCode = connection.getResponseCode();
         }
-        catch (IOException e) {
-            responseCode = handleIOException(connection);
+        catch (FileNotFoundException e) {
+            // Ignore...
+            responseCode = HttpURLConnection.HTTP_NOT_FOUND;
         }
         finally {
             closeSilently(connection);
@@ -109,22 +97,6 @@ final class Utils {
         return responseCode;
     }
 
-    /**
-     * @see http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html
-     */
-    static int handleIOException(HttpURLConnection conn) {
-        int respCode = -2;
-        try {
-            respCode = conn.getResponseCode();
-            flushStream(conn.getErrorStream());
-        }
-        catch (IOException ex) {
-            // deal with the exception
-            ex.printStackTrace();
-        }
-        return respCode;
-    }
-
     static int put(URL host, String endpoint, String customer, String name, String version,
InputStream in) throws IOException {
         URL url = new URL(host, endpoint + "?customer=" + customer + "&name=" + name
+ "&version=" + version);
 
@@ -139,14 +111,13 @@ final class Utils {
 
         try (OutputStream out = connection.getOutputStream()) {
             copy(in, out);
-
             out.flush();
 
             rc = connection.getResponseCode();
             flushStream(connection.getInputStream());
         }
         catch (IOException e) {
-            rc = handleIOException(connection);
+            rc = connection.getResponseCode();
         }
         finally {
             closeSilently(in);
@@ -171,9 +142,6 @@ final class Utils {
 
             responseCode = connection.getResponseCode();
         }
-        catch (IOException e) {
-            responseCode = handleIOException(connection);
-        }
         finally {
             closeSilently(out);
             closeSilently(connection);
@@ -183,37 +151,8 @@ final class Utils {
     }
 
     static void waitForWebserver(URL host) throws IOException {
-        int retries = 1, rc = -1;
-        IOException ioe = null;
-        while (retries++ < 10) {
-            HttpURLConnection connection = openConnection(host);
-            try {
-                rc = connection.getResponseCode();
-                if (rc >= 0) {
-                    return;
-                }
-            }
-            catch (ConnectException e) {
-                ioe = e;
-                try {
-                    Thread.sleep(retries * 50);
-                }
-                catch (InterruptedException ie) {
-                    // We're asked to stop...
-                    return;
-                }
-            }
-            catch (IOException e) {
-                rc = handleIOException(connection);
-            }
-            finally {
-                if (connection != null) {
-                    connection.disconnect();
-                }
-            }
-        }
-        if (ioe != null) {
-            throw ioe;
+        if (!NetUtils.waitForURL(host, 404)) {
+            throw new IOException("URL " + host + " did not respond in time?!");
         }
     }
 
@@ -225,7 +164,6 @@ final class Utils {
         conn.setUseCaches(false);
         conn.setConnectTimeout(1000);
         conn.setReadTimeout(1000);
-
         return conn;
     }
 }

Modified: ace/trunk/org.apache.ace.test/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/bnd.bnd?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/bnd.bnd (original)
+++ ace/trunk/org.apache.ace.test/bnd.bnd Tue Feb 23 18:01:50 2016
@@ -15,7 +15,7 @@ Import-Package: \
 	org.apache.felix.service.command;resolution:=optional,\
 	*
 
-Bundle-Version: 1.2.0
+Bundle-Version: 1.3.0
 Bundle-Name: Apache ACE Test
 Bundle-Description: Provides base classes and utils for integration tests
 Private-Package: org.apache.ace.it.gogo

Modified: ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java (original)
+++ ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java Tue Feb 23
18:01:50 2016
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Method;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Dictionary;
@@ -37,6 +38,7 @@ import java.util.concurrent.CountDownLat
 import java.util.concurrent.TimeUnit;
 
 import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentDependencyDeclaration;
 import org.apache.felix.dm.ComponentState;
@@ -430,10 +432,8 @@ public class IntegrationTestBase extends
      */
     protected List<String> getResponse(URL requestURL) throws IOException {
         List<String> result = new ArrayList<>();
-        InputStream in = null;
-        try {
-            in = requestURL.openConnection().getInputStream();
-
+        URLConnection conn = requestURL.openConnection();
+        try (InputStream in = conn.getInputStream()) {
             final StringBuilder element = new StringBuilder();
             int b;
             while ((b = in.read()) > 0) {
@@ -451,12 +451,7 @@ public class IntegrationTestBase extends
             }
         }
         finally {
-            try {
-                in.close();
-            }
-            catch (Exception e) {
-                // no problem.
-            }
+            NetUtils.closeConnection(conn);
         }
         return result;
     }
@@ -570,7 +565,7 @@ public class IntegrationTestBase extends
     protected int countServices(Class<?> type) throws IOException, InvalidSyntaxException
{
         return countServices(String.format("(%s=%s)", Constants.OBJECTCLASS, type.getName()));
     }
-    
+
     /**
      * @param filter
      * @return the number of services that match the given filter, &gt;= 0.
@@ -626,8 +621,9 @@ public class IntegrationTestBase extends
             if (!listener.waitForEmpty(SERVICE_TIMEOUT, SECONDS)) {
                 fail("Not all components were started. Still missing the following:\n" +
listener.componentsString());
             }
-            
-            // XXX it appears we run into race conditions between the setup and configuration
of our services, use a little delay to get things settled seems to help here...
+
+            // XXX it appears we run into race conditions between the setup and configuration
of our services, use a
+            // little delay to get things settled seems to help here...
             TimeUnit.MILLISECONDS.sleep(500);
 
             configureAdditionalServices();

Modified: ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/NetUtils.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/NetUtils.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/NetUtils.java (original)
+++ ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/NetUtils.java Tue Feb 23 18:01:50
2016
@@ -19,14 +19,93 @@
 package org.apache.ace.test.utils;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Class containing utility methods concerning network related stuff.
  */
 public class NetUtils {
+    public static final int DEFAULT_WAIT_TIMEOUT = 2500;
+
+    /**
+     * Closes the given URL connection and ensures that its state is properly flushed.
+     * 
+     * @param connection
+     *            the URL connection to close, may be <code>null</code>.
+     */
+    public static void closeConnection(URLConnection connection) {
+        if (connection instanceof HttpURLConnection) {
+            HttpURLConnection conn = (HttpURLConnection) connection;
+            try {
+                if (conn.getErrorStream() != null) {
+                    flush(conn.getErrorStream());
+                }
+                if (conn.getInputStream() != null) {
+                    flush(conn.getInputStream());
+                }
+            }
+            catch (IOException exception) {
+                // Ignore... Not much we can do about this here...
+            }
+            finally {
+                conn.disconnect();
+            }
+        }
+    }
+
+    /**
+     * Flushes the given input stream by reading its contents until an end-of-file marker
is found.
+     * 
+     * @param is
+     *            the input stream to flush, cannot be <code>null</code>.
+     * @throws IOException
+     *             in case of I/O problems reading from the given input stream.
+     */
+    public static void flush(InputStream is) throws IOException {
+        final byte[] buf = new byte[4096];
+        int read = 0;
+        do {
+            read = is.read(buf);
+        }
+        while (read > 0);
+    }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means a status code of "200" is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @return <code>true</code> if the response of the URL has the specified
status code within the specified timeout
+     *         delay, <code>false</code> otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL(String url) throws MalformedURLException {
+        return waitForURL(new URL(url));
+    }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means the specified status code is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @param responseCode
+     *            The response code to be expected on the specified URL when it is available.
+     * @return True if the response of the URL has the specified status code within the specified
timeout delay, false
+     *         otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL(String url, int responseCode) throws MalformedURLException
{
+        return waitForURL(new URL(url), responseCode);
+    }
 
     /**
      * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
@@ -49,6 +128,38 @@ public class NetUtils {
 
     /**
      * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means a status code of "200" is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @return <code>true</code> if the response of the URL has the specified
status code within the specified timeout
+     *         delay, <code>false</code> otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL(URL url) {
+        return waitForURL(url, HttpURLConnection.HTTP_OK, DEFAULT_WAIT_TIMEOUT);
+    }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means the specified status code is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @param responseCode
+     *            The response code to be expected on the specified URL when it is available.
+     * @return True if the response of the URL has the specified status code within the specified
timeout delay, false
+     *         otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL(URL url, int responseCode) {
+        return waitForURL(url, responseCode, DEFAULT_WAIT_TIMEOUT);
+    }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
      * has been exceeded. Available in this context means the specified status code is returned
when accessing the URL.
      * 
      * @param url
@@ -65,22 +176,31 @@ public class NetUtils {
     public static boolean waitForURL(URL url, int responseCode, int timeout) {
         long deadline = System.currentTimeMillis() + timeout;
         while (System.currentTimeMillis() < deadline) {
+            URLConnection connection = null;
             try {
-                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-                connection.setRequestMethod("HEAD");
-                int rc = connection.getResponseCode();
+                connection = url.openConnection();
+                ((HttpURLConnection) connection).setRequestMethod("HEAD");
+                connection.setAllowUserInteraction(false);
+                connection.setUseCaches(false);
+
+                int rc = ((HttpURLConnection) connection).getResponseCode();
                 if (rc == responseCode) {
                     return true;
                 }
+                System.out.printf("Waiting for URL %s: %d (want %d)%n", connection.getURL(),
rc, responseCode);
             }
             catch (ClassCastException cce) {
-                throw new IllegalArgumentException("Expected url to be an HTTP url, not:
" + url.toString(), cce);
+                throw new IllegalArgumentException("Expected url to be an HTTP url, not:
" + connection.getURL(), cce);
             }
             catch (IOException ioe) {
                 // retry
             }
+            finally {
+                closeConnection(connection);
+            }
+
             try {
-                Thread.sleep(100);
+                TimeUnit.MILLISECONDS.sleep(250);
             }
             catch (InterruptedException ie) {
                 return false;
@@ -88,4 +208,34 @@ public class NetUtils {
         }
         return false;
     }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means a status code of 404 is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @return <code>true</code> if the response of the URL has the specified
status code within the specified timeout
+     *         delay, <code>false</code> otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL_NotFound(String url) throws MalformedURLException {
+        return waitForURL_NotFound(new URL(url));
+    }
+
+    /**
+     * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until
it is available or timeout
+     * has been exceeded. Available in this context means a status code of 404 is returned
when accessing the URL.
+     * 
+     * @param url
+     *            HTTP URL that should be tested for availability.
+     * @return <code>true</code> if the response of the URL has the specified
status code within the specified timeout
+     *         delay, <code>false</code> otherwise.
+     * @throws IllegalArgumentException
+     *             If the specified URL does not use the HTTP protocol.
+     */
+    public static boolean waitForURL_NotFound(URL url) {
+        return waitForURL(url, HttpURLConnection.HTTP_NOT_FOUND, DEFAULT_WAIT_TIMEOUT);
+    }
 }

Modified: ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/packageinfo
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/packageinfo?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/packageinfo (original)
+++ ace/trunk/org.apache.ace.test/src/org/apache/ace/test/utils/packageinfo Tue Feb 23 18:01:50
2016
@@ -1 +1 @@
-version 1.2.0
\ No newline at end of file
+version 1.3.0
\ No newline at end of file

Modified: ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/UserAdminRepositoryTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/UserAdminRepositoryTest.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/UserAdminRepositoryTest.java
(original)
+++ ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/UserAdminRepositoryTest.java
Tue Feb 23 18:01:50 2016
@@ -31,6 +31,7 @@ import org.apache.ace.it.IntegrationTest
 import org.apache.ace.range.SortedRangeSet;
 import org.apache.ace.repository.Repository;
 import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
 import org.apache.felix.dm.Component;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.UserAdmin;
@@ -65,7 +66,7 @@ public class UserAdminRepositoryTest ext
             assertEquals(200, conn.getResponseCode());
         }
         finally {
-            conn.disconnect();
+            NetUtils.closeConnection(conn);
         }
 
         try {

Modified: ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/Utils.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/Utils.java?rev=1731916&r1=1731915&r2=1731916&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/Utils.java (original)
+++ ace/trunk/org.apache.ace.useradmin.itest/src/org/apache/ace/it/useradmin/Utils.java Tue
Feb 23 18:01:50 2016
@@ -27,6 +27,8 @@ import java.net.ConnectException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
+import org.apache.ace.test.utils.NetUtils;
+
 final class Utils {
 
     private static final int COPY_BUFFER_SIZE = 4096;
@@ -44,9 +46,7 @@ final class Utils {
     }
 
     static void closeSilently(HttpURLConnection resource) {
-        if (resource != null) {
-            resource.disconnect();
-        }
+        NetUtils.closeConnection(resource);
     }
 
     /* copy in to out */




Mime
View raw message