1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Document; 16 import org.dom4j.Element; 17 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 18 19 import java.io.IOException ; 20 21 27 public class TestXMLSpaceAttribute extends AbstractTestCase { 28 29 public static void main( String [] args ) { 30 TestRunner.run( suite() ); 31 } 32 33 public static Test suite() { 34 return new TestSuite( TestXMLSpaceAttribute.class ); 35 } 36 37 public TestXMLSpaceAttribute(String name) { 38 super(name); 39 } 40 41 public void testWithTextTrimOn() throws Exception { 44 try { 45 String xmlString = "<top >" + 46 "<row><col> This is a test!</col></row>"+ 47 "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+ 48 "<row><col> This is a test!</col></row>"+ 49 "</top>"; 50 Document doc1 = O3DocumentHelper.parseText(xmlString); 51 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col"); 52 String expected=" New Text TrimOn! "; 53 c2.setText(expected); 54 55 String xml = rewriteToXmlString(doc1,true); 56 57 Document doc2 = O3DocumentHelper.parseText(xml); 58 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col"); 59 String actual=c4.getText(); 60 61 assertEquals("compared element text expecting whitespace",expected,actual); 62 63 expected = expected.trim(); 64 actual=c4.getTextTrim(); 65 assertEquals("compared element getTextTrim",expected,actual); 66 67 expected="This is a test!"; 68 Element c5 = (Element)doc2.selectSingleNode("/top/row[3]/col"); 69 actual=c5.getText(); 70 assertEquals("compared element text expecting trimmed whitespace", 71 expected,actual); 72 } 73 catch (Exception ex) { 74 ex.printStackTrace(); 75 this.assertTrue(ex.getMessage(),false); 76 } 77 } 78 79 80 public void testWithTextTrimOff() throws Exception { 82 try { 83 String xmlString = "<top >" + 84 "<row><col> This is a test!</col></row>"+ 85 "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+ 86 "<row><col> This is a test!</col></row>"+ 87 "</top>"; 88 Document doc1 = O3DocumentHelper.parseText(xmlString); 89 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col"); 90 String expected=" New Text TrimOff! "; 91 c2.setText(expected); 92 String xml = rewriteToXmlString(doc1,false); 93 94 Document doc2 = O3DocumentHelper.parseText(xml); 95 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col"); 96 String actual=c4.getText(); 97 98 assertEquals("compared element text expecting whitespace",expected,actual); 99 } 100 catch (Exception ex) { 101 ex.printStackTrace(); 102 this.assertTrue(ex.getMessage(),false); 103 } 104 } 105 106 public void testWithTextTrimOnFollow() throws Exception { 108 try { 109 String xmlString = "<top >" + 110 "<row><col> This is a test!</col></row>"+ 111 "<row>"+ 112 "<col xml:space=\'preserve\' >"+ 113 "<a><b> This is embedded!</b></a>"+ 114 "<a><b> This is space=preserve too!</b></a>"+ 115 "</col>"+ 116 "</row>"+ 117 "<row><col> This is a test!</col></row>"+ 118 "</top>"; 119 Document doc1 = O3DocumentHelper.parseText(xmlString); 120 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b"); 121 String expected=" New Text TrimOnFollow! "; 122 c2.setText(expected); 123 String xml = rewriteToXmlString(doc1,true); 124 125 Document doc2 = O3DocumentHelper.parseText(xml); 126 127 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b"); 128 String actual=c4.getText(); 129 130 assertEquals("compared element text expecting whitespace",expected,actual); 131 132 Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[2]/b"); 133 134 expected=" This is space=preserve too!"; 135 actual=c8.getText(); 136 assertEquals("compared element text follow trimmed whitespace",expected,actual); 137 138 expected = expected.trim(); 139 actual=c8.getTextTrim(); 140 assertEquals("compared element getTextTrim",expected,actual); 141 Element c12=(Element)doc2.selectSingleNode("/top/row[3]/col"); 142 143 expected="This is a test!"; 144 actual=c12.getText(); 145 assertEquals("compared element text follow trimmed whitespace",expected,actual); 146 } 147 catch (Exception ex) { 148 ex.printStackTrace(); 149 this.assertTrue(ex.getMessage(),false); 150 } 151 } 152 public void testWithTextTrimOnNested() throws Exception { 154 try { 155 String xmlString = "<top >" + 156 "<row><col> This is a test!</col></row>"+ 157 "<row>"+ 158 "<col xml:space='preserve' >"+ 159 "<a>"+ 160 "<b> This is embedded! </b>"+ 161 "<b xml:space='default' > This should do global default! </b>"+ 162 "<b> This is embedded! </b>"+ 163 "</a>"+ 164 "</col>"+ 165 "</row>"+ 166 "<row><col> This is a test!</col></row>"+ 167 "</top>"; 168 Document doc1 = O3DocumentHelper.parseText(xmlString); 169 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b"); 170 String expected=" New Text TrimOnNested! "; 171 c2.setText(expected); 172 String xml = rewriteToXmlString(doc1,true); 173 174 Document doc2 = O3DocumentHelper.parseText(xml); 175 176 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[1]"); 177 String actual=c4.getText(); 178 assertEquals("compared element text expecting whitespace",expected,actual); 179 180 Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[2]"); 181 expected="This should do global default!"; 182 actual=c8.getText(); 183 assertEquals("compared element text nested trimmed whitespace",expected,actual); 184 185 Element c12=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[3]"); 186 expected=" This is embedded! "; 187 actual=c12.getText(); 188 assertEquals("compared element text nested preserved whitespace",expected,actual); 189 } 190 catch (Exception ex) { 191 ex.printStackTrace(); 192 this.assertTrue(ex.getMessage(),false); 193 } 194 } 195 198 private String rewriteToXmlString(Document doc,boolean trimOn) throws IOException { 199 org.dom4j.io.OutputFormat of = org.dom4j.io.OutputFormat.createCompactFormat(); 200 of.setIndent(true); 201 of.setNewlines(true); 202 of.setExpandEmptyElements(false); 203 of.setSuppressDeclaration(false); 204 of.setOmitEncoding(false); 205 of.setEncoding("UTF-8"); 206 of.setTrimText(trimOn); 207 java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream (); 208 java.io.BufferedOutputStream bos= new java.io.BufferedOutputStream (os); 209 org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(of); 210 211 xmlWriter.setOutputStream(bos); 212 xmlWriter.write(doc); 213 xmlWriter.close(); 214 String xml = os.toString(); 215 216 218 return xml; 219 } 220 221 public void testWithEscapeTextTrimOn() throws Exception { 223 try { 224 String xmlString = "<top >" + 225 "<row><col> This is a test!</col></row>"+ 226 "<row><col xml:space=\'preserve\' > This is a test!\r\nWith a new line, special character like & , and\t tab.</col></row>"+ 227 "<row><col> This is a test!\r\nWith a new line, special character like & , and\t tab.</col></row>"+ 228 "</top>"; 229 Document doc1 = O3DocumentHelper.parseText(xmlString); 230 String xml = rewriteToXmlString(doc1,true); 231 Document doc2 = O3DocumentHelper.parseText(xml); 232 233 Element c2=(Element)doc2.selectSingleNode("/top/row[2]/col"); 234 String expected=" This is a test!\nWith a new line, special character like & , and\t tab."; 235 String actual=c2.getText(); 236 assertEquals("compared element text expecting whitespace",expected,actual); 237 238 Element c4=(Element)doc2.selectSingleNode("/top/row[3]/col"); 239 expected="This is a test! With a new line, special character like & , and tab."; 240 actual=c4.getText(); 241 assertEquals("compared element text expecting whitespace",expected,actual); 242 } 243 catch (Exception ex) { 244 ex.printStackTrace(); 245 this.assertTrue(ex.getMessage(),false); 246 } 247 } 248 249 250 } 251 252 253 254 255 299 | Popular Tags |