ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1329269 - in /ace/trunk: ace-connectionfactory/ ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ ace-connectionfactory/src/test/ ace-connectionfactory/src/test/java/ ace-connectionfactory/src/test/java/org/ ace-co...
Date Mon, 23 Apr 2012 15:04:12 GMT
Author: jawi
Date: Mon Apr 23 15:04:11 2012
New Revision: 1329269

URL: http://svn.apache.org/viewvc?rev=1329269&view=rev
Log:
ACE-255: added some additional JUnit tests.

Added:
    ace/trunk/ace-connectionfactory/src/test/
    ace/trunk/ace-connectionfactory/src/test/java/
    ace/trunk/ace-connectionfactory/src/test/java/org/
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java
  (with props)
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java
  (with props)
    ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java
  (with props)
    ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java
  (with props)
Modified:
    ace/trunk/ace-connectionfactory/pom.xml
    ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImpl.java
    ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentials.java
    ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactory.java

Modified: ace/trunk/ace-connectionfactory/pom.xml
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/pom.xml?rev=1329269&r1=1329268&r2=1329269&view=diff
==============================================================================
--- ace/trunk/ace-connectionfactory/pom.xml (original)
+++ ace/trunk/ace-connectionfactory/pom.xml Mon Apr 23 15:04:11 2012
@@ -42,6 +42,7 @@
         
     <properties>
         <import.package>
+        	!org.junit,
         	org.apache.ace.connectionfactory;version=${project.version},
         	org.osgi.framework,
         	org.osgi.service.cm,

Modified: ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImpl.java?rev=1329269&r1=1329268&r2=1329269&view=diff
==============================================================================
--- ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImpl.java
(original)
+++ ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImpl.java
Mon Apr 23 15:04:11 2012
@@ -120,18 +120,16 @@ public class ConnectionFactoryImpl imple
             creds = m_credentialMapping.get(pid);
         }
 
-        if (creds == null) {
-            try {
-                creds = UrlCredentialsFactory.getCredentials(properties);
-                
-                synchronized (m_credentialMapping) {
-                    m_credentialMapping.put(pid, creds);
-                }
-            }
-            catch (MissingValueException e) {
-                throw new ConfigurationException(e.getProperty(), e.getMessage());
+        try {
+            creds = UrlCredentialsFactory.getCredentials(properties);
+            
+            synchronized (m_credentialMapping) {
+                m_credentialMapping.put(pid, creds);
             }
         }
+        catch (MissingValueException e) {
+            throw new ConfigurationException(e.getProperty(), e.getMessage());
+        }
     }
     
     /**
@@ -141,12 +139,12 @@ public class ConnectionFactoryImpl imple
      * @return a {@link UrlCredentials} instance for the given URL, or <code>null</code>

      *         if none were found, or if none were necessary.
      */
-    private UrlCredentials getCredentials(URL url) {
+    final UrlCredentials getCredentials(URL url) {
         Collection<UrlCredentials> creds;
         synchronized (m_credentialMapping) {
             creds = new ArrayList<UrlCredentials>(m_credentialMapping.values());
         }
-        
+
         for (UrlCredentials c : creds) {
             if (c.matches(url)) {
                 return c;

Modified: ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentials.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentials.java?rev=1329269&r1=1329268&r2=1329269&view=diff
==============================================================================
--- ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentials.java
(original)
+++ ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentials.java
Mon Apr 23 15:04:11 2012
@@ -55,6 +55,12 @@ final class UrlCredentials {
      * @param credentials the credentials to use, cannot be <code>null</code>,
but may be empty.
      */
     public UrlCredentials(AuthType type, URL baseURL, Object... credentials) {
+        if (type == null) {
+            throw new IllegalArgumentException("Type cannot be null!");
+        }
+        if (baseURL == null) {
+            throw new IllegalArgumentException("BaseURL cannot be null!");
+        }
         m_type = type;
         m_baseURL = baseURL;
         m_credentials = (credentials == null) ? new Object[0] : credentials.clone();
@@ -128,4 +134,11 @@ final class UrlCredentials {
         result = prime * result + Arrays.hashCode(m_credentials);
         return result;
     }
+
+    /**
+     * @return the base URL these credentials apply to, cannot be <code>null</code>.
+     */
+    final URL getBaseURL() {
+        return m_baseURL;
+    }
 }

Modified: ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactory.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactory.java?rev=1329269&r1=1329268&r2=1329269&view=diff
==============================================================================
--- ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactory.java
(original)
+++ ace/trunk/ace-connectionfactory/src/main/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactory.java
Mon Apr 23 15:04:11 2012
@@ -85,6 +85,13 @@ final class UrlCredentialsFactory {
      * @throws MissingValueException in case the given properties is missing values.
      */
     public static UrlCredentials getCredentials(Dictionary props, String prefix) throws MissingValueException
{
+        if (props == null) {
+            throw new IllegalArgumentException("Properties cannot be null!");
+        }
+        if (prefix == null) {
+            throw new IllegalArgumentException("Prefix cannot be null!");
+        }
+
         AuthType type;
         URL baseURL; 
         Object[] creds;

Added: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java?rev=1329269&view=auto
==============================================================================
--- ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java
(added)
+++ ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java
Mon Apr 23 15:04:11 2012
@@ -0,0 +1,149 @@
+/*
+ * 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.connectionfactory.impl;
+
+import static org.junit.Assert.*;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Properties;
+
+import org.junit.Test;
+
+/**
+ * Test cases for {@link ConnectionFactoryImpl}.
+ */
+public class ConnectionFactoryImplTest {
+
+    private static final URL TEST_URL;
+    
+    static {
+        try {
+            TEST_URL = new URL("http://localhost:8080/");
+        }
+        catch (MalformedURLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateConnectionNullUrlFail() throws Exception {
+        new ConnectionFactoryImpl().createConnection(null);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL,
org.osgi.service.useradmin.User)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateConnectionNullUserFail() throws Exception {
+        new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo"), null);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL,
org.osgi.service.useradmin.User)}.
+     */
+    @Test
+    public void testCreateConnectionOk() throws Exception {
+        URLConnection conn = new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo"));
+        assertNotNull(conn);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#deleted(java.lang.String)}.
+     */
+    @Test
+    public void testDeleted() throws Exception {
+        ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl();
+
+        Properties props = createBasicAuthConfig(TEST_URL.toExternalForm());
+
+        connFactory.updated("pid1", props);
+        
+        UrlCredentials credentials = connFactory.getCredentials(TEST_URL);
+        assertNotNull(credentials);
+        
+        connFactory.deleted("pid1");
+        
+        credentials = connFactory.getCredentials(TEST_URL);
+        assertNull(credentials);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String,
java.util.Dictionary)}.
+     */
+    @Test
+    public void testUpdatedInsertsCredentialsOk() throws Exception {
+        ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl();
+        
+        UrlCredentials credentials = connFactory.getCredentials(TEST_URL);
+        assertNull(credentials);
+        
+        Properties props = createBasicAuthConfig(TEST_URL.toExternalForm());
+
+        connFactory.updated("pid1", props);
+        
+        credentials = connFactory.getCredentials(TEST_URL);
+        assertNotNull(credentials);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String,
java.util.Dictionary)}.
+     */
+    @Test
+    public void testUpdatedUpdatesCredentialsOk() throws Exception {
+        ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl();
+
+        Properties props = createBasicAuthConfig(TEST_URL.toExternalForm());
+
+        connFactory.updated("pid1", props);
+        
+        UrlCredentials credentials1 = connFactory.getCredentials(TEST_URL);
+        assertNotNull(credentials1);
+        
+        URL newURL = new URL("http://localhost:8181/test/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, newURL.toExternalForm());
+
+        connFactory.updated("pid1", props);
+
+        UrlCredentials credentials2 = connFactory.getCredentials(TEST_URL);
+        assertNull(credentials2);
+
+        credentials2 = connFactory.getCredentials(newURL);
+        assertNotNull(credentials2);
+        
+        assertNotSame(credentials1, credentials2);
+    }
+
+    /**
+     * @return a dictionary containing a configuration for basic authentication, never <code>null</code>.
+     */
+    private Properties createBasicAuthConfig(String url) {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, url);
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_NAME, "foo");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_PASSWORD, "bar");
+        return props;
+    }
+}

Propchange: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/ConnectionFactoryImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java?rev=1329269&view=auto
==============================================================================
--- ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java
(added)
+++ ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java
Mon Apr 23 15:04:11 2012
@@ -0,0 +1,122 @@
+/*
+ * 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.connectionfactory.impl;
+
+import java.util.Properties;
+
+import org.apache.ace.connectionfactory.impl.UrlCredentialsFactory.MissingValueException;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link UrlCredentialsFactory}.
+ */
+public class UrlCredentialsFactoryTest {
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test(expected = MissingValueException.class)
+    public void testGetCredentialsWithDictionaryBasicTypeMissingPasswordFail() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, "http://localhost:8080/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_NAME, "bar");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test(expected = MissingValueException.class)
+    public void testGetCredentialsWithDictionaryBasicTypeMissingUserNameFail() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, "http://localhost:8080/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_PASSWORD, "bar");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test
+    public void testGetCredentialsWithDictionaryBasicTypeOk() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, "http://localhost:8080/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_NAME, "foo");
+        props.put(UrlCredentialsFactory.KEY_AUTH_USER_PASSWORD, "bar");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetCredentialsWithDictionaryInvalidAuthTypeFail() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, "http://localhost:8080/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "nonsense");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test(expected = MissingValueException.class)
+    public void testGetCredentialsWithDictionaryMissingBaseUrlFail() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "none");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetCredentialsWithNullDictionaryFail() {
+        UrlCredentialsFactory.getCredentials(null);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary,
java.lang.String)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetCredentialsWithNullPrefixFail() {
+        UrlCredentialsFactory.getCredentials(new Properties(), null);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}.
+     */
+    @Test
+    public void testGetCredentialsWithValidDictionaryOk() {
+        Properties props = new Properties();
+        props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, "http://localhost:8080/");
+        props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "none");
+
+        UrlCredentialsFactory.getCredentials(props);
+    }
+}

Propchange: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java?rev=1329269&view=auto
==============================================================================
--- ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java
(added)
+++ ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java
Mon Apr 23 15:04:11 2012
@@ -0,0 +1,102 @@
+/*
+ * 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.connectionfactory.impl;
+
+import static org.junit.Assert.*;
+
+import java.net.URL;
+
+import org.apache.ace.connectionfactory.impl.UrlCredentials.AuthType;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link UrlCredentials}.
+ */
+public class UrlCredentialsTest {
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(java.net.URL)}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUrlCredentialsNullURLFail() throws Exception {
+        new UrlCredentials(null);
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(java.net.URL)}.
+     */
+    @Test
+    public void testUrlCredentialsURLOk() throws Exception {
+        new UrlCredentials(new URL("http://localhost:8080/"));
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(org.apache.ace.connectionfactory.impl.UrlCredentials.AuthType,
java.net.URL, java.lang.Object[])}.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUrlCredentialsNullTypeFail() throws Exception {
+        new UrlCredentials(null, new URL("http://localhost:8080/"));
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(org.apache.ace.connectionfactory.impl.UrlCredentials.AuthType,
java.net.URL, java.lang.Object[])}.
+     */
+    @Test
+    public void testUrlCredentialsTypeAndURLOk() throws Exception {
+        new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"));
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#matches(java.net.URL)}.
+     */
+    @Test
+    public void testMatchesNullURLOk() throws Exception {
+        UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"));
+        assertFalse(creds.matches(null));
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#matches(java.net.URL)}.
+     */
+    @Test
+    public void testMatchesValidURLOk() throws Exception {
+        UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"));
+        assertTrue(creds.matches(new URL("http://localhost:8080/obr")));
+        assertFalse(creds.matches(new URL("http://localhost:8080")));
+        assertFalse(creds.matches(new URL("http://localhost:8081/")));
+    }
+
+    /**
+     * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#getCredentials()}.
+     */
+    @Test
+    public void testGetCredentialsOk() throws Exception {
+        UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"));
+        assertArrayEquals(new Object[0], creds.getCredentials());
+
+        creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), "foo");
+        assertArrayEquals(new Object[] { "foo" }, creds.getCredentials());
+
+        creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), (Object[])
null );
+        assertArrayEquals(new Object[0], creds.getCredentials());
+
+        creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), (Object)
null);
+        assertArrayEquals(new Object[] { null }, creds.getCredentials());
+    }
+}

Propchange: ace/trunk/ace-connectionfactory/src/test/java/org/apache/ace/connectionfactory/impl/UrlCredentialsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java?rev=1329269&view=auto
==============================================================================
--- ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java
(added)
+++ ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java
Mon Apr 23 15:04:11 2012
@@ -0,0 +1,334 @@
+/*
+ * 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.it.authentication;
+
+import static org.apache.ace.it.Options.jetty;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.ace.client.repository.SessionFactory;
+import org.apache.ace.connectionfactory.ConnectionFactory;
+import org.apache.ace.discovery.property.constants.DiscoveryConstants;
+import org.apache.ace.http.listener.constants.HttpConstants;
+import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.apache.ace.it.IntegrationTestBase;
+import org.apache.ace.it.Options.Ace;
+import org.apache.ace.it.Options.Felix;
+import org.apache.ace.it.Options.Knopflerfish;
+import org.apache.ace.it.Options.Osgi;
+import org.apache.ace.log.Log;
+import org.apache.ace.log.LogDescriptor;
+import org.apache.ace.log.LogEvent;
+import org.apache.ace.repository.Repository;
+import org.apache.ace.repository.impl.constants.RepositoryConstants;
+import org.apache.ace.server.log.store.LogStore;
+import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.NetUtils;
+import org.apache.felix.dm.Component;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.Constants;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.useradmin.UserAdmin;
+
+/**
+ * Integration tests for the audit log. Both a server and a target are setup
+ * on the same machine. The audit log is run and we check if it is indeed
+ * replicated to the server.
+ */
+@RunWith(JUnit4TestRunner.class)
+public class LogAuthenticationTest extends IntegrationTestBase {
+
+    private static final String AUDITLOG_ENDPOINT = "/auditlog";
+
+    private static final String HOST = "localhost";
+    private static final String TARGET_ID = "target-id";
+
+    private volatile Log m_auditLog;
+    private volatile LogStore m_serverStore;
+    private volatile Runnable m_auditLogSyncTask;
+    private volatile Repository m_userRepository;
+    private volatile UserAdmin m_userAdmin;
+    private volatile ConnectionFactory m_connectionFactory;
+
+    /**
+     * @return the PAX Exam configuration options, never <code>null</code>.
+     */
+    @Configuration
+    public Option[] configuration() {
+        return options(
+            systemProperty("org.osgi.service.http.port").value("" + TestConstants.PORT),
+            provision(
+                // Misc bundles...
+                Osgi.compendium(),
+                Felix.dependencyManager(),
+                jetty(),
+                Felix.configAdmin(),
+                Felix.preferences(),
+                Felix.eventAdmin(),
+                Knopflerfish.useradmin(),
+                Knopflerfish.log(),
+                // ACE core bundles...
+                Ace.util(),
+                Ace.authentication(),
+                Ace.authenticationProcessorBasicAuth(),
+                Ace.connectionFactory(),
+                Ace.rangeApi(),
+                Ace.discoveryApi(),
+                Ace.discoveryProperty(),
+                Ace.identificationApi(),
+                Ace.identificationProperty(),
+                Ace.log(),
+                Ace.logListener(),
+                Ace.logServlet(),
+                Ace.serverLogStore(),
+                Ace.logTask(),
+                Ace.targetLog(),
+                Ace.targetLogStore(),
+                Ace.httplistener(),
+                Ace.repositoryApi(),
+                Ace.repositoryImpl(),
+                Ace.repositoryServlet(),
+                Ace.configuratorServeruseradmin(),
+                Ace.obrMetadata(),
+                Ace.obrServlet(),
+                Ace.obrStorage(),
+                Ace.clientRepositoryApi(),
+                Ace.clientRepositoryImpl(),
+                Ace.clientRepositoryHelperBase(),
+                Ace.clientRepositoryHelperBundle(),
+                Ace.clientRepositoryHelperConfiguration(),
+                Ace.scheduler(),
+                Ace.resourceprocessorUseradmin(),
+                Ace.configuratorUseradminTask()
+            )
+        );
+    }
+
+    /**
+     * Tests that accessing the log servlet with authentication works when given the right
credentials.
+     */
+    @Test
+    public void testAccessLogServletWithCorrectCredentialsOk() throws Exception {
+        String tid1 = "42";
+        String tid2 = "47";
+
+        // prepare the store
+        List<LogEvent> events = new ArrayList<LogEvent>();
+        events.add(new LogEvent(tid1, 1, 1, 1, 1, new Properties()));
+        events.add(new LogEvent(tid2, 1, 1, 1, 1, new Properties()));
+        m_serverStore.put(events);
+
+        List<String> result = getResponse("http://localhost:" + TestConstants.PORT
+ "/auditlog/query");
+        assert result.size() > 1 : "We expect at least two logs on the server.";
+    }
+
+    /**
+     * Tests that the log synchronization works when the log servlet has authentication enabled.
+     */
+    @Test
+    public void testLogSynchronizationOk() throws Exception {
+        final int type = 12345;
+        
+        // now log another event
+        Properties props = new Properties();
+        props.put("one", "value1");
+        props.put("two", "value2");
+        m_auditLog.log(type, props);
+
+        boolean found = false;
+        
+        long startTime = System.currentTimeMillis();
+        long waitTime = 5000; // milliseconds
+        
+        while (!found && ((System.currentTimeMillis() - startTime) < waitTime))
{
+            // synchronize again
+            m_auditLogSyncTask.run();
+
+            // get and evaluate results (note that there is some concurrency that might interfere
with this test)
+            List<LogDescriptor> ranges2 = m_serverStore.getDescriptors();
+            if (ranges2.isEmpty()) {
+                continue;
+            }
+
+            List<LogEvent> events = m_serverStore.get(ranges2.get(0));
+            for (LogEvent event : events) {
+                if (event.getType() == type) {
+                    Dictionary properties = event.getProperties();
+                    assertEquals("value1", properties.get("one"));
+                    assertEquals("value2", properties.get("two"));
+                    found = true;
+                    break;
+                }
+            }
+
+            // wait if we have not found anything yet
+            if (!found) {
+                TimeUnit.MILLISECONDS.sleep(100);
+            }
+        }
+
+        assertTrue("We could not retrieve our audit log event (after 5 seconds).", found);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void before() throws Exception {
+        
+        String baseURL = "http://" + HOST + ":" + TestConstants.PORT;
+
+        getService(SessionFactory.class).createSession("test-session-ID");
+
+        configureFactory("org.apache.ace.server.repository.factory",
+            RepositoryConstants.REPOSITORY_NAME, "users",
+            RepositoryConstants.REPOSITORY_CUSTOMER, "apache",
+            RepositoryConstants.REPOSITORY_MASTER, "true");
+
+        configure("org.apache.ace.configurator.useradmin.task.UpdateUserAdminTask",
+            "repositoryName", "users",
+            "repositoryCustomer", "apache");
+        
+        configure("org.apache.ace.scheduler",
+            "org.apache.ace.configurator.useradmin.task.UpdateUserAdminTask", "100");
+
+        configure(DiscoveryConstants.DISCOVERY_PID,
+            DiscoveryConstants.DISCOVERY_URL_KEY, baseURL);
+        configure(IdentificationConstants.IDENTIFICATION_PID,
+            IdentificationConstants.IDENTIFICATION_TARGETID_KEY, TARGET_ID);
+
+        configureFactory("org.apache.ace.target.log.store.factory",
+            "name", "auditlog");
+        configureFactory("org.apache.ace.target.log.factory",
+            "name", "auditlog");
+        configureFactory("org.apache.ace.target.log.sync.factory",
+            "name", "auditlog");
+        configureFactory("org.apache.ace.server.log.servlet.factory",
+            "name", "auditlog",
+            HttpConstants.ENDPOINT, AUDITLOG_ENDPOINT,
+            "authentication.enabled", "true");
+        configureFactory("org.apache.ace.server.log.store.factory",
+            "name", "auditlog");
+
+        URL testURL = new URL(baseURL.concat(AUDITLOG_ENDPOINT));
+        assertTrue("Failed to access auditlog in time!", NetUtils.waitForURL(testURL, 401,
15000));
+        
+        String userName = "d";
+        String password = "f";
+
+        importSingleUser(userName, password);
+
+        configureFactory("org.apache.ace.connectionfactory", 
+            "authentication.baseURL", baseURL.concat(AUDITLOG_ENDPOINT), 
+            "authentication.type", "basic",
+            "authentication.user.name", userName,
+            "authentication.user.password", password);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Component[] getDependencies() {
+        return new Component[] {
+            createComponent()
+                .setImplementation(this)
+                .add(createServiceDependency().setService(UserAdmin.class).setRequired(true))
+                .add(createServiceDependency()
+                    .setService(Repository.class, "(&(" + RepositoryConstants.REPOSITORY_NAME
+ "=users)(" + RepositoryConstants.REPOSITORY_CUSTOMER + "=apache))")
+                    .setRequired(true))
+                .add(createServiceDependency().setService(ConnectionFactory.class).setRequired(true))
+                .add(createServiceDependency().setService(HttpService.class).setRequired(true))
+                .add(createServiceDependency().setService(Log.class, "(&(" + Constants.OBJECTCLASS
+ "=" + Log.class.getName() + ")(name=auditlog))").setRequired(true))
+                .add(createServiceDependency().setService(LogStore.class, "(&(" + Constants.OBJECTCLASS
+ "=" + LogStore.class.getName() + ")(name=auditlog))").setRequired(true))
+                .add(createServiceDependency().setService(Runnable.class, "(&(" + Constants.OBJECTCLASS
+ "=" + Runnable.class.getName() + ")(taskName=auditlog))").setRequired(true))
+        };
+    }
+
+    private List<String> getResponse(String request) throws IOException {
+        List<String> result = new ArrayList<String>();
+        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);
+                }
+            }
+        }
+        finally {
+            try {
+                in.close();
+            }
+            catch (Exception e) {
+                // no problem.
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Imports a single user into the user repository.
+     * 
+     * @param userName the name of the user to import;
+     * @param password the password of the user to import.
+     * @throws Exception in case of exceptions during the import.
+     */
+    private void importSingleUser(String userName, String password) throws Exception {
+        ByteArrayInputStream bis = new ByteArrayInputStream((
+            "<roles>" +
+                "<user name=\"" + userName + "\">" +
+                "<properties><username>" + userName + "</username></properties>"
+
+                "<credentials><password type=\"String\">" + password + "</password></credentials>"
+
+                "</user>" +
+            "</roles>").getBytes());
+
+        assertTrue("Committing test user data failed!", m_userRepository.commit(bis, m_userRepository.getRange().getHigh()));
+
+        int count = 0;
+        while ((m_userAdmin.getRole(userName) == null) && (count++ < 60)) {
+            Thread.sleep(100);
+        }
+        assertTrue("Failed to obtain user from userAdmin!", count != 60);
+    }
+}

Propchange: ace/trunk/ace-integrationtests/src/test/java/org/apache/ace/it/authentication/LogAuthenticationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message