/*
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.
 */

/**
* @author Stepan M. Mishura
* @version $Revision$
*/

package org.apache.harmony.auth.tests.javax.security.auth;

import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlException;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Principal;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;

import javax.security.auth.AuthPermission;
import javax.security.auth.Subject;
import javax.security.auth.SubjectDomainCombiner;

import org.apache.harmony.auth.internal.SecurityTest;

/**
 * Tests SubjectDomainCombiner class
 */

public class SubjectDomainCombinerTest extends SecurityTest {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(
                org.apache.harmony.auth.tests.javax.security.auth.SubjectDomainCombinerTest.class);
    }

    /**
     * @tests javax.security.auth.SubjectDomainCombiner#SubjectDomainCombiner(
     *        javax.security.auth.Subject)
     */
    public final void test_ConstructorLjavax_security_auth_Subject() {

        Subject subject = new Subject();

        SubjectDomainCombiner combiner = new SubjectDomainCombiner(subject);

        assertTrue("Subject is not null", subject == combiner.getSubject());

        try {
            // Regression for HARMONY-219
            new SubjectDomainCombiner(null);
            fail("Constructor should throw NullPointerException");
        } catch (NullPointerException e) {
        }
    }

    /**
     * Testing combine(ProtectionDomain[], ProtectionDomain[]) 
     */
    public final void testCombine() throws Exception {

        Principal principal = new Principal() {
            public String getName() {
                return "principal";
            }
        };

        Subject subject = new Subject();

        subject.getPrincipals().add(principal);

        SubjectDomainCombiner combiner = new SubjectDomainCombiner(subject);

        ProtectionDomain[] pd;

        // both prarameters are null
        pd = combiner.combine(null, null);
        assertEquals("Returned protection domain array", null, pd);

        // check assigned principals
        URL url = new URL("file://foo.txt");

        CodeSource source = new CodeSource(url, (Certificate[]) null);
        PermissionCollection permissions = new Permissions();
        ClassLoader classLoader = new URLClassLoader(new URL[] { url });

        Principal p = new Principal() {
            public String getName() {
                return "p";
            }
        };
        Principal[] principals = new Principal[] { p };

        ProtectionDomain domain = new ProtectionDomain(source, permissions,
                classLoader, principals);

        pd = combiner.combine(new ProtectionDomain[] { domain }, null);

        assertTrue("CodeSource", source == pd[0].getCodeSource());
        assertTrue("PermissionCollection", permissions == pd[0]
                .getPermissions());
        assertTrue("ClassLoader", classLoader == pd[0].getClassLoader());

        assertEquals("Size", 1, pd[0].getPrincipals().length);
        assertTrue("Principal", principal == (pd[0].getPrincipals())[0]);

        // check inherited domains
        pd = combiner.combine(null, new ProtectionDomain[] { domain });
        assertTrue("Inherited domain", domain == pd[0]);
    }

    public final void testSecurityException() {

        denyPermission(new AuthPermission("getSubjectFromDomainCombiner"));

        Subject subject = new Subject();

        SubjectDomainCombiner combiner = new SubjectDomainCombiner(subject);

        try {
            combiner.getSubject();
        } catch (AccessControlException e) {
            assertEquals(e, AuthPermission.class);
        }
    }
}
@@ -1914,19 +1914,19 @@
   assert
-Tru
+Sam
 e(%22Subje
@@ -1931,20 +1931,8 @@
 ject
- is not null
 %22, s
@@ -1937,19 +1937,17 @@
  subject
- ==
+,
  combine
@@ -2211,16 +2211,57 @@
   * 
-Testing 
+@tests javax.security.auth.SubjectDomainCombiner#
 comb
@@ -2283,16 +2283,30 @@
 omain%5B%5D,
+%0A     *       
  Protect
@@ -2317,17 +2317,16 @@
 omain%5B%5D)
- 
 %0A     */
@@ -2741,16 +2741,27 @@
       //
+ test case:
  both pr
@@ -2779,37 +2779,43 @@
 re null%0A        
-pd = 
+assertNull(
 combiner.combine
@@ -2822,25 +2822,27 @@
 (null, null)
-;
+);%0A
 %0A        ass
@@ -2842,79 +2842,21 @@
     
-assertEquals(%22Returned protection domain array%22, null, pd);%0A%0A        //
+// test case:
  che
@@ -3521,27 +3521,27 @@
       assert
-Tru
+Sam
 e(%22CodeSourc
@@ -3550,19 +3550,17 @@
 , source
- ==
+,
  pd%5B0%5D.g
@@ -3586,27 +3586,27 @@
       assert
-Tru
+Sam
 e(%22Permissio
@@ -3630,19 +3630,17 @@
 missions
- ==
+,
  pd%5B0%5D%0A 
@@ -3688,19 +3688,19 @@
   assert
-Tru
+Sam
 e(%22Class
@@ -3719,19 +3719,17 @@
 ssLoader
- ==
+,
  pd%5B0%5D.g
@@ -3824,19 +3824,19 @@
   assert
-Tru
+Sam
 e(%22Princ
@@ -3851,19 +3851,17 @@
 rincipal
- ==
+,
  (pd%5B0%5D.
@@ -3893,16 +3893,27 @@
       //
+ test case:
  check i
@@ -4019,11 +4019,11 @@
 sert
-Tru
+Sam
 e(%22I
@@ -4050,11 +4050,9 @@
 main
- ==
+,
  pd%5B
@@ -4201,82 +4201,25 @@
     
-Subject subject = new Subject();%0A%0A        SubjectDomainCombiner combiner =
+try %7B%0A           
  new
@@ -4245,53 +4245,22 @@
 ner(
-s
+new S
 ubject
-);%0A%0A        try %7B%0A            combiner
+())
 .get

Change assertTrue->assertSame + minor updates