Author: angela
Date: Wed Nov 8 13:55:55 2017
New Revision: 1814581
URL: http://svn.apache.org/viewvc?rev=1814581&view=rev
Log:
OAK-6918 : AbstractCredentials lack nullability annotations and test coverage
Added:
jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java
(with props)
Modified:
jackrabbit/oak/trunk/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java
Modified:
jackrabbit/oak/trunk/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java?rev=1814581&r1=1814580&r2=1814581&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java
(original)
+++
jackrabbit/oak/trunk/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java
Wed Nov 8 13:55:55 2017
@@ -19,14 +19,17 @@ package org.apache.jackrabbit.oak.spi.se
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.jcr.Credentials;
public abstract class AbstractCredentials implements Credentials {
- protected final Map attributes = new HashMap();
+ protected final Map<String,Object> attributes = new HashMap();
protected final String userId;
- public AbstractCredentials(String userId) {
+ public AbstractCredentials(@Nonnull String userId) {
this.userId = userId;
}
@@ -35,19 +38,21 @@ public abstract class AbstractCredential
*
* @return the userId.
*/
+ @Nonnull
public String getUserId() {
return userId;
}
/**
- * Stores an attribute in this credentials instance.
+ * Stores an attribute in this credentials instance. If the specified
+ * {@code value} is {@code null} the attribute will be removed.
*
* @param name
- * a <code>String</code> specifying the name of the attribute
+ * a {@code String} specifying the name of the attribute
* @param value
- * the <code>Object</code> to be stored
+ * the {@code Object} to be stored
*/
- public void setAttribute(String name, Object value) {
+ public void setAttribute(@Nonnull String name, @Nullable Object value) {
// name cannot be null
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
@@ -73,7 +78,8 @@ public abstract class AbstractCredential
* @return an <code>Object</code> containing the value of the attribute, or
* <code>null</code> if the attribute does not exist
*/
- public Object getAttribute(String name) {
+ @CheckForNull
+ public Object getAttribute(@Nonnull String name) {
synchronized (attributes) {
return (attributes.get(name));
}
@@ -86,16 +92,17 @@ public abstract class AbstractCredential
* a <code>String</code> specifying the name of the attribute to
* remove
*/
- public void removeAttribute(String name) {
+ public void removeAttribute(@Nonnull String name) {
synchronized (attributes) {
attributes.remove(name);
}
}
/**
- * @return the attributes available to this credentials instance
+ * @return an immutable map containing the attributes available to this
credentials instance
*/
- public Map getAttributes() {
+ @Nonnull
+ public Map<String,Object> getAttributes() {
return Collections.unmodifiableMap(attributes);
}
@@ -104,10 +111,9 @@ public abstract class AbstractCredential
*
* @param attributes The attributes to be stored
*/
- public void setAttributes(Map attributes) {
+ public void setAttributes(@Nonnull Map<String,Object> attributes) {
synchronized (attributes) {
this.attributes.putAll(attributes);
}
}
-
}
Added:
jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java?rev=1814581&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java
(added)
+++
jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java
Wed Nov 8 13:55:55 2017
@@ -0,0 +1,94 @@
+/*
+ * 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.jackrabbit.oak.spi.security.authentication.credentials;
+
+import java.util.Date;
+import java.util.Map;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class AbstractCredentialsTest {
+
+ private static final String USER_ID = "userId";
+
+ private AbstractCredentials credentials = new AbstractCredentials(USER_ID)
{
+ };
+
+ @Test
+ public void testGetUserId() {
+ assertEquals(USER_ID, credentials.getUserId());
+ }
+
+ @Test
+ public void testAttributesAreEmpty() {
+ assertTrue(credentials.getAttributes().isEmpty());
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testAttributesAreImmutable() {
+ credentials.getAttributes().put("attr", "value");
+ }
+
+ @Test
+ public void testSetAttribute() {
+ Object value = new Date();
+ credentials.setAttribute("attr", value);
+
+ assertEquals(value, credentials.getAttribute("attr"));
+
+ Map<String,Object> attributes = credentials.getAttributes();
+ assertTrue(attributes.containsKey("attr"));
+ assertEquals(1, attributes.size());
+ assertEquals(value, attributes.get("attr"));
+ }
+
+ @Test
+ public void testSetNullAttributeValue() {
+ credentials.setAttribute("attr", null);
+ assertTrue(credentials.getAttributes().isEmpty());
+
+
+ credentials.setAttribute("attr", 25);
+ credentials.setAttribute("attr", null);
+
+ assertNull(credentials.getAttribute("attr"));
+ assertTrue(credentials.getAttributes().isEmpty());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetNullAttributeName() {
+ credentials.setAttribute(null, "value");
+ }
+
+ @Test
+ public void testSetAttributes() {
+ Map<String,Object> attributes = ImmutableMap.of("attr", true);
+ credentials.setAttributes(attributes);
+
+ Map<String,Object> attr = credentials.getAttributes();
+ assertFalse(attr.isEmpty());
+ assertEquals(attributes, attr);
+ assertNotSame(attributes, attr);
+ }
+}
\ No newline at end of file
Propchange:
jackrabbit/oak/trunk/oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentialsTest.java
------------------------------------------------------------------------------
svn:eol-style = native