1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package sax; 25 26 import org.testng.Assert; 27 import org.testng.annotations.Listeners; 28 import org.testng.annotations.Test; 29 import org.xml.sax.ext.Attributes2Impl; 30 31 /* 32 * @test 33 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 34 * @run testng/othervm -DrunSecMngr=true sax.Attributes2ImplTest 35 * @run testng/othervm sax.Attributes2ImplTest 36 * @summary Test Attributes2Impl. 37 */ 38 @Listeners({jaxp.library.BasePolicy.class}) 39 public class Attributes2ImplTest { 40 41 @Test 42 public void test01() { 43 System.out.println("===in test01()==="); 44 Attributes2Impl impl = new Attributes2Impl(); 45 impl.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 46 impl.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 47 impl.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 48 49 Assert.assertTrue(impl.isDeclared(0)); 50 impl.setDeclared(0, false); 51 Assert.assertFalse(impl.isDeclared(0)); 52 53 Assert.assertTrue(impl.isDeclared("Qname2")); 54 impl.setDeclared(1, false); 55 Assert.assertFalse(impl.isDeclared("Qname2")); 56 57 Assert.assertTrue(impl.isDeclared("http://www.cars.com/xml", "attr3")); 58 impl.setDeclared(2, false); 59 Assert.assertFalse(impl.isDeclared(2)); 60 61 try { 62 impl.isDeclared(3); 63 } catch (ArrayIndexOutOfBoundsException e) { 64 System.out.println("Expected ArrayIndexOutOfBoundsException"); 65 } 66 67 try { 68 impl.isDeclared("wrongQname"); 69 } catch (IllegalArgumentException e) { 70 System.out.println("Expected IllegalArgumentException"); 71 } 72 73 try { 74 impl.isDeclared("http://www.cars.com/xml", "attr4"); 75 } catch (IllegalArgumentException e) { 76 System.out.println("Expected IllegalArgumentException"); 77 } 78 79 impl.removeAttribute(2); 80 try { 81 impl.isDeclared(2); 82 } catch (ArrayIndexOutOfBoundsException e) { 83 System.out.println("Expected ArrayIndexOutOfBoundsException on index=2 after removing"); 84 } 85 } 86 87 @Test 88 public void test02() { 89 System.out.println("===in test02()==="); 90 Attributes2Impl impl = new Attributes2Impl(); 91 impl.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 92 impl.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 93 impl.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 94 95 Assert.assertTrue(impl.isSpecified(0)); 96 impl.setSpecified(0, false); 97 Assert.assertFalse(impl.isSpecified(0)); 98 99 Assert.assertTrue(impl.isSpecified("Qname2")); 100 impl.setSpecified(1, false); 101 Assert.assertFalse(impl.isSpecified("Qname2")); 102 103 Assert.assertTrue(impl.isSpecified("http://www.cars.com/xml", "attr3")); 104 impl.setSpecified(2, false); 105 Assert.assertFalse(impl.isSpecified(2)); 106 107 try { 108 impl.isSpecified(3); 109 } catch (ArrayIndexOutOfBoundsException e) { 110 System.out.println("Expected ArrayIndexOutOfBoundsException"); 111 } 112 113 try { 114 impl.isSpecified("wrongQname"); 115 } catch (IllegalArgumentException e) { 116 System.out.println("Expected IllegalArgumentException"); 117 } 118 119 try { 120 impl.isSpecified("http://www.cars.com/xml", "attr4"); 121 } catch (IllegalArgumentException e) { 122 System.out.println("Expected IllegalArgumentException"); 123 } 124 125 impl.removeAttribute(2); 126 try { 127 impl.isSpecified(2); 128 } catch (ArrayIndexOutOfBoundsException e) { 129 System.out.println("Expected ArrayIndexOutOfBoundsException on index=2 after removing"); 130 } 131 } 132 133 @Test 134 public void test03() { 135 System.out.println("===in test03()==="); 136 Attributes2Impl impl1 = new Attributes2Impl(); 137 impl1.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 138 impl1.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 139 impl1.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 140 141 Attributes2Impl impl2 = new Attributes2Impl(impl1); 142 143 Attributes2Impl impl3 = new Attributes2Impl(); 144 impl3.setAttributes(impl1); 145 146 Assert.assertTrue(impl1.getQName(0).equals(impl2.getQName(0))); 147 Assert.assertTrue(impl1.getQName(0).equals(impl3.getQName(0))); 148 149 Assert.assertTrue(impl1.getQName(1).equals(impl2.getQName(1))); 150 Assert.assertTrue(impl1.getQName(1).equals(impl3.getQName(1))); 151 152 Assert.assertTrue(impl1.getQName(2).equals(impl2.getQName(2))); 153 Assert.assertTrue(impl1.getQName(2).equals(impl3.getQName(2))); 154 } 155 }