1 /*
   2  * Copyright (c) 2015, 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 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 org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 import org.w3c.dom.CharacterData;
  37 import org.w3c.dom.DOMException;
  38 import org.xml.sax.SAXException;
  39 
  40 /*
  41  * @summary common test for the CharacterData Interface
  42  */
  43 public abstract class AbstractCharacterDataTest {
  44     @DataProvider(name = "data-for-length")
  45     public Object[][] getDataForTestLength() {
  46         return new Object[][] {
  47                 { "", 0 },
  48                 { "test", 4 } };
  49     }
  50 
  51     /*
  52      * Verify getLength method works as the spec, for an empty string, should
  53      * return zero
  54      */
  55     @Test(dataProvider = "data-for-length")
  56     public void testGetLength(String text, int length) throws Exception {
  57         CharacterData cd = createCharacterData(text);
  58         assertEquals(cd.getLength(), length);
  59 
  60     }
  61 
  62     /*
  63      * Test appendData method and verify by getData method.
  64      */
  65     @Test
  66     public void testAppendData() throws Exception {
  67         CharacterData cd = createCharacterData("DOM");
  68         cd.appendData("2");
  69         assertEquals(cd.getData(), "DOM2");
  70 
  71     }
  72 
  73     @DataProvider(name = "data-for-delete")
  74     public Object[][] getDataForTestDelete() {
  75         return new Object[][] {
  76                 { "DOM", 2, 1, "DO" },
  77                 { "DOM", 0, 2, "M" },
  78                 { "DOM", 2, 3, "DO" } };
  79     }
  80 
  81     /*
  82      * Verify deleteData method works as the spec.
  83      */
  84     @Test(dataProvider = "data-for-delete")
  85     public void testDeleteData(String text, int offset, int count, String result) throws Exception {
  86         CharacterData cd = createCharacterData(text);
  87         cd.deleteData(offset, count);
  88         assertEquals(cd.getData(), result);
  89     }
  90 
  91     @DataProvider(name = "data-for-replace")
  92     public Object[][] getDataForTestReplace() {
  93         return new Object[][] {
  94                 { "DOM", 0, 3, "SAX", "SAX" },
  95                 { "DOM", 1, 1, "AA", "DAAM" },
  96                 { "DOM", 1, 2, "A", "DA" },
  97                 { "DOM", 2, 2, "SAX", "DOSAX" } };
  98     }
  99 
 100     /*
 101      * Verify replaceData method works as the spec.
 102      */
 103     @Test(dataProvider = "data-for-replace")
 104     public void testReplaceData(String text, int offset, int count, String arg, String result) throws Exception {
 105         CharacterData cd = createCharacterData(text);
 106         cd.replaceData(offset, count, arg);
 107         assertEquals(cd.getData(), result);
 108     }
 109 
 110     @DataProvider(name = "data-for-replace-neg")
 111     public Object[][] getDataForTestReplaceNeg() {
 112         return new Object[][] {
 113                 { "DOM", -1, 3, "SAX" }, //offset if neg
 114                 { "DOM", 0, -1, "SAX" }, //count is neg
 115                 { "DOM", 4, 1, "SAX" } };//offset is greater than length
 116     }
 117 
 118     /*
 119      * Test for replaceData method: verifies that DOMException with
 120      * INDEX_SIZE_ERR is thrown if offset or count is out of the bound.
 121      */
 122     @Test(dataProvider = "data-for-replace-neg")
 123     public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception {
 124         CharacterData cd = createCharacterData(text);
 125         try {
 126             cd.replaceData(offset, count, arg);
 127             fail(DOMEXCEPTION_EXPECTED);
 128         } catch (DOMException e) {
 129             assertEquals(e.code, INDEX_SIZE_ERR);
 130         }
 131     }
 132 
 133     @DataProvider(name = "data-for-insert")
 134     public Object[][] getDataForTestInsert() {
 135         return new Object[][] {
 136                 { "DOM", 0, "SAX", "SAXDOM" },
 137                 { "DOM", 3, "SAX", "DOMSAX" } };
 138     }
 139 
 140     /*
 141      * Verify insertData method works as the spec.
 142      */
 143     @Test(dataProvider = "data-for-insert")
 144     public void testInsertData(String text, int offset, String arg, String result) throws Exception {
 145         CharacterData cd = createCharacterData(text);
 146         cd.insertData(offset, arg);
 147         assertEquals(cd.getData(), result);
 148     }
 149 
 150     @DataProvider(name = "data-for-insert-neg")
 151     public Object[][] getDataForTestInsertNeg() {
 152         return new Object[][] {
 153                 { "DOM", -1 }, //offset is neg
 154                 { "DOM", 4 } };//offset is greater than length
 155     }
 156 
 157     /*
 158      * Test for insertData method: verifies that DOMException with
 159      * INDEX_SIZE_ERR is thrown if offset is out of the bound.
 160      */
 161     @Test(dataProvider = "data-for-insert-neg")
 162     public void testInsertDataNeg(String text, int offset) throws Exception {
 163         CharacterData cd = createCharacterData(text);
 164         try {
 165             cd.insertData(offset, "TEST");
 166             fail(DOMEXCEPTION_EXPECTED);
 167         } catch (DOMException e) {
 168             assertEquals(e.code, INDEX_SIZE_ERR);
 169         }
 170     }
 171 
 172     /*
 173      * Test setData method and verify by getData method.
 174      */
 175     @Test
 176     public void testSetData() throws Exception {
 177         CharacterData cd = createCharacterData("DOM");
 178         cd.setData("SAX");
 179         assertEquals(cd.getData(), "SAX");
 180     }
 181 
 182     @DataProvider(name = "data-for-substring")
 183     public Object[][] getDataForTestSubstring() {
 184         return new Object[][] {
 185                 { "DOM Level 2", 0, 3, "DOM" },
 186                 { "DOM", 0, 3, "DOM" },
 187                 { "DOM", 2, 5, "M" } };
 188     }
 189 
 190     /*
 191      * Verify substringData method works as the spec.
 192      */
 193     @Test(dataProvider = "data-for-substring")
 194     public void testSubstringData(String text, int offset, int count, String result) throws Exception {
 195         CharacterData cd = createCharacterData(text);
 196         String retStr = cd.substringData(offset, count);
 197         assertEquals(retStr, result);
 198     }
 199 
 200     @DataProvider(name = "data-for-substring-neg")
 201     public Object[][] getDataForTestSubstringNeg() {
 202         return new Object[][] {
 203                 { "DOM Level 2", -1, 3 }, //offset is neg
 204                 { "DOM", 0, -1 }, //count is neg
 205                 { "DOM", 3, 1 } }; //offset exceeds length
 206     }
 207 
 208     /*
 209      * Test for substringData method: verifies that DOMException with
 210      * INDEX_SIZE_ERR is thrown if offset or count is out of the bound.
 211      */
 212     @Test(dataProvider = "data-for-substring-neg")
 213     public void testSubstringDataNeg(String text, int offset, int count) throws Exception {
 214         CharacterData cd = createCharacterData(text);
 215         try {
 216             cd.substringData(offset, count);
 217             fail(DOMEXCEPTION_EXPECTED);
 218         } catch (DOMException e) {
 219             assertEquals(e.code, INDEX_SIZE_ERR);
 220         }
 221 
 222     }
 223 
 224     /*
 225      * Return a concrete CharacterData instance.
 226      */
 227     abstract protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException;
 228 
 229 }