/** @tests javax.security.auth.SubjectDomainCombiner#getSubject() */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "Verifies that Subject associated with this SubjectDomainCombiner is returned",
      method = "getSubject",
      args = {})
  public void test_getSubject_01() {
    Subject s = new Subject();
    SubjectDomainCombiner c = new SubjectDomainCombiner(s);

    assertEquals(s, c.getSubject());
  }
  /** @tests javax.security.auth.SubjectDomainCombiner#SubjectDomainCombiner(Subject subject) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "SubjectDomainCombiner",
      args = {Subject.class})
  public void test_Constructor_01() {
    Subject s = new Subject();
    SubjectDomainCombiner c = new SubjectDomainCombiner(s);

    try {
      assertEquals(s, c.getSubject());
    } catch (SecurityException se) {

    }
  }
  /** @tests javax.security.auth.SubjectDomainCombiner#combine() */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "both currentDomains and assignedDomains are not null",
      method = "combine",
      args = {ProtectionDomain[].class, ProtectionDomain[].class})
  public void test_combine_01() {

    URL url;
    try {
      url = new URL(locationUrl);
    } catch (MalformedURLException mue) {
      throw new Error(mue);
    }
    CodeSource cs = new CodeSource(url, (java.security.cert.Certificate[]) null);

    class MyClassLoader extends ClassLoader {
      public MyClassLoader() {
        super();
      }
    }

    ClassLoader current_pd_cl = new MyClassLoader();
    ClassLoader assigned_pd_cl = new MyClassLoader();

    // current domains
    ProtectionDomain[] current_pd =
        createProtectionDomains(cs, current_pd_cl, currentDomainX500names, currentDomainPerms);

    // assigned domains
    ProtectionDomain[] assigned_pd =
        createProtectionDomains(cs, assigned_pd_cl, assignedDomainX500names, assignedDomainPerms);

    // subject
    Subject s = createSubject();

    // combine
    SubjectDomainCombiner c = new SubjectDomainCombiner(s);

    ProtectionDomain[] r_pd = c.combine(current_pd, assigned_pd);
    if (DEBUG) {
      System.out.println("=========== c_pd");
      dumpPD(current_pd);
      System.out.println("=========== a_pd");
      dumpPD(assigned_pd);
      System.out.println("=========== r_pd");
      dumpPD(r_pd);
      System.out.println("===========");
    }

    for (int i = 0; i < r_pd.length; i++) {
      ProtectionDomain pd = r_pd[i];
      // check CodeSource
      assertTrue("code source mismatch", pd.getCodeSource().equals(cs));
      boolean cpd = false;
      // check ClassLoader
      if (pd.getClassLoader().equals(current_pd_cl)) {
        cpd = true;
      } else if (pd.getClassLoader().equals(assigned_pd_cl)) {
        cpd = false;
      } else {
        fail("class loader mismatch");
      }

      // check principals
      Principal[] principals = pd.getPrincipals();
      String[] names;
      if (cpd == true) names = SubjectX500names;
      else names = assignedDomainX500names;

      for (int j = 0; j < principals.length; j++) {
        if (contains(names, principals[j].getName()) == false)
          fail("principal mismatch (" + j + ") " + principals[j].getName());
      }

      // check permissions
      PermissionCollection perms = pd.getPermissions();

      Enumeration<Permission> p = perms.elements();
      while (p.hasMoreElements()) {
        Permission pp = p.nextElement();

        String pn = pp.getName();

        if (cpd == true) {
          if (contains(currentDomainPerms, pn) == false)
            fail("current domains permissions mismatch " + pn);
        } else {
          if (contains(assignedDomainPerms, pn) == false)
            fail("assigned domains permissions mismatch " + pn);
        }
      }
    }
  }