@@ -0,0 +1,2402 @@
+package org.owasp.html;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport javax.annotation.Nullable;%0A%0Aimport junit.framework.TestCase;%0A%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.Lists;%0A%0Aimport static org.owasp.html.ElementPolicy.REJECT_ALL_ELEMENT_POLICY;%0Aimport static org.owasp.html.ElementPolicy.IDENTITY_ELEMENT_POLICY;%0Aimport static org.owasp.html.ElementPolicy.Util.join;%0A%0Apublic final class ElementPolicyTest extends TestCase %7B%0A%0A  static class HasCharElementPolicy implements ElementPolicy %7B%0A    final char ch;%0A%0A    HasCharElementPolicy(char ch) %7B%0A      this.ch = ch;%0A    %7D%0A%0A    public @Nullable%0A    String apply(String elementName, List%3CString%3E attrs) %7B%0A      attrs.clear();%0A      return elementName.indexOf(ch) %3E= 0 ? elementName : null;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A      return %22(has '%22 + ch + %22')%22;%0A    %7D%0A  %7D%0A%0A  private void assertPassed(ElementPolicy p, String... expected) %7B%0A    List%3CString%3E attrs = Lists.newArrayList();%0A    ImmutableList.Builder%3CString%3E actual = ImmutableList.builder();%0A    for (String elName : TEST_EL_NAMES) %7B%0A      if (p.apply(elName, attrs) != null) %7B%0A        actual.add(elName);%0A      %7D%0A    %7D%0A    assertEquals(p.toString(), Arrays.asList(expected), actual.build());%0A  %7D%0A%0A  private static List%3CString%3E TEST_EL_NAMES = ImmutableList.of(%0A      %22abacus%22, %22abracadabra%22, %22bar%22, %22foo%22, %22far%22, %22cadr%22, %22cdr%22);%0A%0A  public final void testJoin() %7B%0A    ElementPolicy a = new HasCharElementPolicy('a');%0A    ElementPolicy b = new HasCharElementPolicy('b');%0A    ElementPolicy c = new HasCharElementPolicy('c');%0A    ElementPolicy d = new HasCharElementPolicy('d');%0A    assertPassed(REJECT_ALL_ELEMENT_POLICY);%0A    assertPassed(IDENTITY_ELEMENT_POLICY,%0A                 TEST_EL_NAMES.toArray(new String%5B0%5D));%0A    assertPassed(a, %22abacus%22, %22abracadabra%22, %22bar%22, %22far%22, %22cadr%22);%0A    assertPassed(c, %22abacus%22, %22abracadabra%22, %22cadr%22, %22cdr%22);%0A    assertPassed(d, %22abracadabra%22, %22cadr%22, %22cdr%22);%0A    ElementPolicy a_b = join(a, b);%0A    ElementPolicy b_a = join(b, a);%0A    assertPassed(a_b, %22abacus%22, %22abracadabra%22, %22bar%22);%0A    assertPassed(b_a, %22abacus%22, %22abracadabra%22, %22bar%22);%0A    assertPassed(join(b_a, b_a), %22abacus%22, %22abracadabra%22, %22bar%22);%0A    assertPassed(join(a_b, c), %22abacus%22, %22abracadabra%22);%0A    assertPassed(join(c, REJECT_ALL_ELEMENT_POLICY));%0A    assertPassed(join(REJECT_ALL_ELEMENT_POLICY, a_b));%0A  %7D%0A%0A%7D%0A

test anding of policy factories that have overlapping element and attribute policies