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