JSEC-122 - fixed NPE
git-svn-id: https://svn.apache.org/repos/asf/incubator/jsecurity/trunk@711065 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/org/jsecurity/authc/SimpleAuthenticationInfo.java b/src/org/jsecurity/authc/SimpleAuthenticationInfo.java
index 32cce9b..0a442cb 100644
--- a/src/org/jsecurity/authc/SimpleAuthenticationInfo.java
+++ b/src/org/jsecurity/authc/SimpleAuthenticationInfo.java
@@ -70,38 +70,43 @@
@SuppressWarnings("unchecked")
public void merge(AuthenticationInfo info) {
- if (info == null || info.getPrincipals() == null) {
+ if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
return;
}
if (this.principals == null) {
- this.principals = new SimplePrincipalCollection();
- }
-
- if (this.principals instanceof MutablePrincipalCollection) {
- ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
+ this.principals = info.getPrincipals();
} else {
- throw new IllegalStateException("Attempt to merge authentication info, but PrincipalCollection is not an " +
- "instance of MutablePrincipalCollection.");
+ if (this.principals instanceof MutablePrincipalCollection) {
+ ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
+ } else {
+ this.principals = new SimplePrincipalCollection(this.principals);
+ }
}
Object thisCredentials = getCredentials();
Object otherCredentials = info.getCredentials();
+ if (otherCredentials == null) {
+ return;
+ }
+
+ if (thisCredentials == null) {
+ this.credentials = otherCredentials;
+ return;
+ }
+
if (!(thisCredentials instanceof Collection)) {
Set newSet = new HashSet();
- if (thisCredentials != null) {
- newSet.add(thisCredentials);
- setCredentials(newSet);
- }
+ newSet.add(thisCredentials);
+ setCredentials(newSet);
}
// At this point, the credentials should be a collection
Collection credentialCollection = (Collection) getCredentials();
if (otherCredentials instanceof Collection) {
credentialCollection.addAll((Collection) otherCredentials);
-
- } else if (otherCredentials != null) {
+ } else {
credentialCollection.add(otherCredentials);
}
}
diff --git a/test/org/jsecurity/authc/SimpleAuthenticationInfoTest.java b/test/org/jsecurity/authc/SimpleAuthenticationInfoTest.java
new file mode 100644
index 0000000..2d449c8
--- /dev/null
+++ b/test/org/jsecurity/authc/SimpleAuthenticationInfoTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.jsecurity.authc;
+
+import org.junit.Test;
+
+/**
+ * @author Les Hazlewood
+ * @since 0.9
+ */
+public class SimpleAuthenticationInfoTest {
+
+ @Test
+ public void testMergeWithEmptyInstances() {
+ SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
+ SimpleAuthenticationInfo local = new SimpleAuthenticationInfo();
+ aggregate.merge(local);
+ }
+
+ /**
+ * Verifies fix for JSEC-122
+ */
+ @Test
+ public void testMergeWithAggregateNullCredentials() {
+ SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
+ SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
+ aggregate.merge(local);
+ }
+}