1 /* 2 * Copyright (c) 2015, 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 package org.w3c.dom.ptests; 24 25 import static org.testng.Assert.assertEquals; 26 import static org.testng.Assert.fail; 27 import static org.w3c.dom.DOMException.INDEX_SIZE_ERR; 28 import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; 29 30 import java.io.IOException; 31 32 import javax.xml.parsers.ParserConfigurationException; 33 34 import jaxp.library.JAXPFileBaseTest; 35 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 import org.w3c.dom.CharacterData; 39 import org.w3c.dom.DOMException; 40 import org.xml.sax.SAXException; 41 42 /* 43 * @summary common test for the CharacterData Interface 44 */ 45 public abstract class AbstractCharacterDataTest extends JAXPFileBaseTest { 46 @DataProvider(name = "data-for-length") 47 public Object[][] getDataForTestLength() { 48 return new Object[][] { 49 { "", 0 }, 50 { "test", 4 } }; 51 } 52 53 @Test(dataProvider = "data-for-length") 54 public void testGetLength(String text, int length) throws Exception { 55 CharacterData cd = createCharacterData(text); 56 assertEquals(cd.getLength(), length); 57 58 } 59 60 @Test 61 public void testAppendData() throws Exception { 62 CharacterData cd = createCharacterData("DOM"); 63 cd.appendData("2"); 64 assertEquals(cd.getData(), "DOM2"); 65 66 } 67 68 @DataProvider(name = "data-for-delete") 69 public Object[][] getDataForTestDelete() { 70 return new Object[][] { 71 { "DOM", 2, 1, "DO" }, 72 { "DOM", 0, 2, "M" }, 73 { "DOM", 2, 3, "DO" } }; 74 } 75 76 @Test(dataProvider = "data-for-delete") 77 public void testDeleteData(String text, int offset, int count, String result) throws Exception { 78 CharacterData cd = createCharacterData(text); 79 cd.deleteData(offset, count); 80 assertEquals(cd.getData(), result); 81 } 82 83 @DataProvider(name = "data-for-replace") 84 public Object[][] getDataForTestReplace() { 85 return new Object[][] { 86 { "DOM", 0, 3, "SAX", "SAX" }, 87 { "DOM", 1, 1, "AA", "DAAM" }, 88 { "DOM", 1, 2, "A", "DA" }, 89 { "DOM", 2, 2, "SAX", "DOSAX" } }; 90 } 91 92 @Test(dataProvider = "data-for-replace") 93 public void testReplaceData(String text, int offset, int count, String arg, String result) throws Exception { 94 CharacterData cd = createCharacterData(text); 95 cd.replaceData(offset, count, arg); 96 assertEquals(cd.getData(), result); 97 } 98 99 @DataProvider(name = "data-for-replace-neg") 100 public Object[][] getDataForTestReplaceNeg() { 101 return new Object[][] { 102 { "DOM", -1, 3, "SAX" }, 103 { "DOM", 0, -1, "SAX" }, 104 { "DOM", 4, 1, "SAX" } }; 105 } 106 107 @Test(dataProvider = "data-for-replace-neg") 108 public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception { 109 CharacterData cd = createCharacterData(text); 110 try { 111 cd.replaceData(offset, count, arg); 112 fail(DOMEXCEPTION_EXPECTED); 113 } catch (DOMException e) { 114 assertEquals(e.code, INDEX_SIZE_ERR); 115 } 116 } 117 118 @DataProvider(name = "data-for-insert") 119 public Object[][] getDataForTestInsert() { 120 return new Object[][] { 121 { "DOM", 0, "SAX", "SAXDOM" }, 122 { "DOM", 3, "SAX", "DOMSAX" } }; 123 } 124 125 @Test(dataProvider = "data-for-insert") 126 public void testInsertData(String text, int offset, String arg, String result) throws Exception { 127 CharacterData cd = createCharacterData(text); 128 cd.insertData(offset, arg); 129 assertEquals(cd.getData(), result); 130 } 131 132 @DataProvider(name = "data-for-insert-neg") 133 public Object[][] getDataForTestInsertNeg() { 134 return new Object[][] { 135 { "DOM", -1 }, 136 { "DOM", 4 } }; 137 } 138 139 @Test(dataProvider = "data-for-insert-neg") 140 public void testInsertDataNeg(String text, int offset) throws Exception { 141 CharacterData cd = createCharacterData(text); 142 try { 143 cd.insertData(offset, "TEST"); 144 fail(DOMEXCEPTION_EXPECTED); 145 } catch (DOMException e) { 146 assertEquals(e.code, INDEX_SIZE_ERR); 147 } 148 } 149 150 @Test 151 public void testSetData() throws Exception { 152 CharacterData cd = createCharacterData("DOM"); 153 cd.setData("SAX"); 154 assertEquals(cd.getData(), "SAX"); 155 } 156 157 @DataProvider(name = "data-for-substring") 158 public Object[][] getDataForTestSubstring() { 159 return new Object[][] { 160 { "DOM Level 2", 0, 3, "DOM" }, 161 { "DOM", 0, 3, "DOM" }, 162 { "DOM", 2, 5, "M" } }; 163 } 164 165 @Test(dataProvider = "data-for-substring") 166 public void testSubstringData(String text, int offset, int count, String result) throws Exception { 167 CharacterData cd = createCharacterData(text); 168 String retStr = cd.substringData(offset, count); 169 assertEquals(retStr, result); 170 } 171 172 @DataProvider(name = "data-for-substring-neg") 173 public Object[][] getDataForTestSubstringNeg() { 174 return new Object[][] { 175 { "DOM Level 2", -1, 3 }, 176 { "DOM", 0, -1 }, 177 { "DOM", 3, 1 } }; 178 } 179 180 @Test(dataProvider = "data-for-substring-neg") 181 public void testSubstringDataNeg(String text, int offset, int count) throws Exception { 182 CharacterData cd = createCharacterData(text); 183 try { 184 cd.substringData(offset, count); 185 fail(DOMEXCEPTION_EXPECTED); 186 } catch (DOMException e) { 187 assertEquals(e.code, INDEX_SIZE_ERR); 188 } 189 190 } 191 192 abstract protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException; 193 194 }