1 19 package org.netbeans.modules.xslt.model.impl; 20 21 import java.util.List ; 22 23 import org.netbeans.modules.xslt.model.LiteralResultElement; 24 import org.netbeans.modules.xslt.model.Stylesheet; 25 import org.netbeans.modules.xslt.model.Template; 26 import org.netbeans.modules.xslt.model.XslComponent; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 33 34 35 39 public class XslComponentTest extends AbstractXslTestCase { 40 41 public XslComponentTest( String testName ) { 42 super(testName); 43 } 44 45 public void testGetContent() { 46 Template template = getTemplate( 0 ); 47 48 assert template != null : "Couldn't found template child"; 49 50 Document doc = getDOMDocument( getModel() ); 51 NodeList list = doc.getElementsByTagName( template.getPeer().getNodeName() ); 52 53 assert list.getLength() == 2; 54 55 assert template.getContent().trim().equals( getContent( list.item( 0 )) ); 56 } 57 58 public void testSetContent() { 59 Template template = getTemplate( 0 ); 60 61 List <XslComponent> childrenList = template.getChildren(); 62 63 getModel().startTransaction(); 64 template.setContent( "a" ); 65 getModel().endTransaction(); 66 67 Document doc = getDOMDocument( getModel() ); 68 NodeList list = doc.getElementsByTagName( template.getPeer().getNodeName() ); 69 70 assert getContent( list.item( 0 ) ).equals( "a" ); 71 72 assert template.getContent().trim().equals( "a" ); 73 74 list = list.item( 0 ).getChildNodes(); 75 76 int count = 0; 77 for( int i=0 ; i<list.getLength(); i++ ) { 78 Node node = list.item( i ); 79 if ( node instanceof Element ) { 80 node.getNodeName().equals( 81 childrenList.get( count ).getPeer().getNodeName()); 82 count++; 83 } 84 } 85 } 86 87 public void testGetContentInEmpty() { 88 Template template = getTemplate( 1 ); 89 90 String content = template.getContent(); 91 if ( content != null ) { 92 assert content.trim().length() == 0; 93 } 94 } 95 96 public void testSetContentInEmpty() { 97 String text = "new"; 98 Template template = getTemplate( 1 ); 99 100 getModel().startTransaction(); 101 template.setContent( text ); 102 getModel().endTransaction(); 103 104 template.getContent().trim().equals( text ); 105 106 Document doc = getDOMDocument( getModel() ); 107 NodeList list = doc.getElementsByTagName( template.getPeer().getNodeName() ); 108 109 assert list.getLength() == 2; 110 111 assert getContent( list.item( 1 )).equals( text ); 112 113 list = list.item( 1 ).getChildNodes(); 114 for( int i=0 ; i<list.getLength(); i++ ) { 115 assert list.item( i ).getNodeType() == Node.TEXT_NODE; 116 } 117 } 118 119 public void testGetTrailingText() { 120 List <LiteralResultElement> children = 121 getTemplate( 0 ).getChildren( LiteralResultElement.class ); 122 123 LiteralResultElement element = children.get( 0 ); 124 assert element!= null; 125 126 String text = element.getTrailingText(); 127 128 checkTrailingText(text); 129 130 } 131 132 public void testSetTrailingText() { 133 String text = "new_text"; 134 135 List <LiteralResultElement> children = 136 getTemplate( 0 ).getChildren( LiteralResultElement.class ); 137 138 LiteralResultElement element = children.get( 0 ); 139 assert element!= null; 140 141 getModel().startTransaction(); 142 element.setTrailingText(text); 143 getModel().endTransaction(); 144 145 assert element.getTrailingText().trim().equals( text ); 146 147 checkTrailingText( text ); 148 } 149 150 private void checkTrailingText( String text ) { 151 Document doc = getDOMDocument( getModel() ); 152 NodeList list = doc.getElementsByTagName( 153 getTemplate( 0 ).getPeer().getNodeName() ); 154 155 Node node = list.item( 0 ); 156 list = node.getChildNodes(); 157 int count=0; 158 StringBuilder builder = new StringBuilder (); 159 for( int i=0; i<list.getLength() ; i++ ) { 160 Node next = list.item( i ); 161 if ( next instanceof Element ) { 162 count++; 163 } 164 if ( count >1 && 165 ( next.getNodeType()== Node.TEXT_NODE 166 || next.getNodeType()== Node.CDATA_SECTION_NODE ) ) 167 { 168 builder.append( next.getNodeValue() ); 169 } 170 } 171 172 assert count == 2; 174 assert text.trim().equals( builder.toString().trim() ); 175 } 176 177 private Template getTemplate( int i ) { 178 Stylesheet stylesheet = getStyleSheet( TEST ); 179 180 List <Template> children = stylesheet.getChildren( Template.class ); 181 Template template = children.get( i ); 182 return template; 183 } 184 185 186 private String getContent( Node node ) { 187 NodeList list = node.getChildNodes(); 188 StringBuilder builder = new StringBuilder (); 189 for( int i=0 ; i<list.getLength(); i++ ) { 190 Node next = list.item( i ); 191 if ( next instanceof Element ) { 192 break; 193 } 194 if ( next.getNodeType() == Node.TEXT_NODE || next.getNodeType() == 195 Node.CDATA_SECTION_NODE ) 196 { 197 builder.append( next.getNodeValue() ); 198 } 199 200 } 201 return builder.toString().trim(); 202 } 203 } 204 | Popular Tags |