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 * @summary Test Attributes2Impl. 33 */ 34 @Listeners({jaxp.library.BasePolicy.class}) 35 public class Attributes2ImplTest { 36 37 @Test 38 public void test01() { 39 System.out.println("===in test01()==="); 40 Attributes2Impl impl = new Attributes2Impl(); 41 impl.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 42 impl.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 43 impl.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 44 45 Assert.assertTrue(impl.isDeclared(0)); 46 impl.setDeclared(0, false); 47 Assert.assertFalse(impl.isDeclared(0)); 48 49 Assert.assertTrue(impl.isDeclared("Qname2")); 50 impl.setDeclared(1, false); 51 Assert.assertFalse(impl.isDeclared("Qname2")); 52 53 Assert.assertTrue(impl.isDeclared("http://www.cars.com/xml", "attr3")); 54 impl.setDeclared(2, false); 55 Assert.assertFalse(impl.isDeclared(2)); 56 57 try { 58 impl.isDeclared(3); 59 } catch (ArrayIndexOutOfBoundsException e) { 60 System.out.println("Expected ArrayIndexOutOfBoundsException"); 61 } 62 63 try { 64 impl.isDeclared("wrongQname"); 65 } catch (IllegalArgumentException e) { 66 System.out.println("Expected IllegalArgumentException"); 67 } 68 69 try { 70 impl.isDeclared("http://www.cars.com/xml", "attr4"); 71 } catch (IllegalArgumentException e) { 72 System.out.println("Expected IllegalArgumentException"); 73 } 74 75 impl.removeAttribute(2); 76 try { 77 impl.isDeclared(2); 78 } catch (ArrayIndexOutOfBoundsException e) { 79 System.out.println("Expected ArrayIndexOutOfBoundsException on index=2 after removing"); 80 } 81 } 82 83 @Test 84 public void test02() { 85 System.out.println("===in test02()==="); 86 Attributes2Impl impl = new Attributes2Impl(); 87 impl.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 88 impl.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 89 impl.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 90 91 Assert.assertTrue(impl.isSpecified(0)); 92 impl.setSpecified(0, false); 93 Assert.assertFalse(impl.isSpecified(0)); 94 95 Assert.assertTrue(impl.isSpecified("Qname2")); 96 impl.setSpecified(1, false); 97 Assert.assertFalse(impl.isSpecified("Qname2")); 98 99 Assert.assertTrue(impl.isSpecified("http://www.cars.com/xml", "attr3")); 100 impl.setSpecified(2, false); 101 Assert.assertFalse(impl.isSpecified(2)); 102 103 try { 104 impl.isSpecified(3); 105 } catch (ArrayIndexOutOfBoundsException e) { 106 System.out.println("Expected ArrayIndexOutOfBoundsException"); 107 } 108 109 try { 110 impl.isSpecified("wrongQname"); 111 } catch (IllegalArgumentException e) { 112 System.out.println("Expected IllegalArgumentException"); 113 } 114 115 try { 116 impl.isSpecified("http://www.cars.com/xml", "attr4"); 117 } catch (IllegalArgumentException e) { 118 System.out.println("Expected IllegalArgumentException"); 119 } 120 121 impl.removeAttribute(2); 122 try { 123 impl.isSpecified(2); 124 } catch (ArrayIndexOutOfBoundsException e) { 125 System.out.println("Expected ArrayIndexOutOfBoundsException on index=2 after removing"); 126 } 127 } 128 129 @Test 130 public void test03() { 131 System.out.println("===in test03()==="); 132 Attributes2Impl impl1 = new Attributes2Impl(); 133 impl1.addAttribute("http://www.cars.com/xml", "attr1", "Qname1", "type", "value"); 134 impl1.addAttribute("http://www.cars.com/xml", "attr2", "Qname2", "type", "value"); 135 impl1.addAttribute("http://www.cars.com/xml", "attr3", "Qname3", "type", "value"); 136 137 Attributes2Impl impl2 = new Attributes2Impl(impl1); 138 139 Attributes2Impl impl3 = new Attributes2Impl(); 140 impl3.setAttributes(impl1); 141 142 Assert.assertTrue(impl1.getQName(0).equals(impl2.getQName(0))); 143 Assert.assertTrue(impl1.getQName(0).equals(impl3.getQName(0))); 144 145 Assert.assertTrue(impl1.getQName(1).equals(impl2.getQName(1))); 146 Assert.assertTrue(impl1.getQName(1).equals(impl3.getQName(1))); 147 148 Assert.assertTrue(impl1.getQName(2).equals(impl2.getQName(2))); 149 Assert.assertTrue(impl1.getQName(2).equals(impl3.getQName(2))); 150 } 151 }