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