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      * Negative test for replaceData method.
 119      */
 120     @Test(dataProvider = "data-for-replace-neg")
 121     public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception {
 122         CharacterData cd = createCharacterData(text);
 123         try {
 124             cd.replaceData(offset, count, arg);
 125             fail(DOMEXCEPTION_EXPECTED);
 126         } catch (DOMException e) {
 127             assertEquals(e.code, INDEX_SIZE_ERR);
 128         }
 129     }
 130 
 131     @DataProvider(name = "data-for-insert")
 132     public Object[][] getDataForTestInsert() {
 133         return new Object[][] { 
 134                 { "DOM", 0, "SAX", "SAXDOM" }, 
 135                 { "DOM", 3, "SAX", "DOMSAX" } };
 136     }
 137 
 138     /*
 139      * Verify insertData method works as the spec.
 140      */
 141     @Test(dataProvider = "data-for-insert")
 142     public void testInsertData(String text, int offset, String arg, String result) throws Exception {
 143         CharacterData cd = createCharacterData(text);
 144         cd.insertData(offset, arg);
 145         assertEquals(cd.getData(), result);
 146     }
 147 
 148     @DataProvider(name = "data-for-insert-neg")
 149     public Object[][] getDataForTestInsertNeg() {
 150         return new Object[][] { 
 151                 { "DOM", -1 }, //offset is neg
 152                 { "DOM", 4 } };//offset is greater than length
 153     }
 154 
 155     /*
 156      * Negative test for insertData method.
 157      */
 158     @Test(dataProvider = "data-for-insert-neg")
 159     public void testInsertDataNeg(String text, int offset) throws Exception {
 160         CharacterData cd = createCharacterData(text);
 161         try {
 162             cd.insertData(offset, "TEST");
 163             fail(DOMEXCEPTION_EXPECTED);
 164         } catch (DOMException e) {
 165             assertEquals(e.code, INDEX_SIZE_ERR);
 166         }
 167     }
 168 
 169     @Test
 170     public void testSetData() throws Exception {
 171         CharacterData cd = createCharacterData("DOM");
 172         cd.setData("SAX");
 173         assertEquals(cd.getData(), "SAX");
 174     }
 175 
 176     @DataProvider(name = "data-for-substring")
 177     public Object[][] getDataForTestSubstring() {
 178         return new Object[][] { 
 179                 { "DOM Level 2", 0, 3, "DOM" }, 
 180                 { "DOM", 0, 3, "DOM" }, 
 181                 { "DOM", 2, 5, "M" } };
 182     }
 183 
 184     /*
 185      * Verify substringData method works as the spec.
 186      */
 187     @Test(dataProvider = "data-for-substring")
 188     public void testSubstringData(String text, int offset, int count, String result) throws Exception {
 189         CharacterData cd = createCharacterData(text);
 190         String retStr = cd.substringData(offset, count);
 191         assertEquals(retStr, result);
 192     }
 193 
 194     @DataProvider(name = "data-for-substring-neg")
 195     public Object[][] getDataForTestSubstringNeg() {
 196         return new Object[][] { 
 197                 { "DOM Level 2", -1, 3 }, //offset is neg
 198                 { "DOM", 0, -1 }, //count is neg
 199                 { "DOM", 3, 1 } }; //offset exceeds length
 200     }
 201 
 202     /*
 203      * Negative test for substringData method.
 204      */
 205     @Test(dataProvider = "data-for-substring-neg")
 206     public void testSubstringDataNeg(String text, int offset, int count) throws Exception {
 207         CharacterData cd = createCharacterData(text);
 208         try {
 209             cd.substringData(offset, count);
 210             fail(DOMEXCEPTION_EXPECTED);
 211         } catch (DOMException e) {
 212             assertEquals(e.code, INDEX_SIZE_ERR);
 213         }
 214 
 215     }
 216     
 217     /*
 218      * Return a concrete CharacterData instance.
 219      */
 220     abstract protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException;
 221 
 222 }