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