1 19 20 package org.netbeans.modules.xml.text.indent; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import javax.swing.JTextArea ; 27 import junit.framework.*; 28 import java.io.IOException ; 29 import java.io.Writer ; 30 import java.util.regex.Pattern ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.text.AbstractDocument.DefaultDocumentEvent; 33 import javax.swing.text.AttributeSet ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.Document ; 36 import javax.swing.text.JTextComponent ; 37 import org.netbeans.editor.BaseDocument; 38 import org.netbeans.editor.BaseKit; 39 import org.netbeans.editor.EditorUI; 40 import org.netbeans.editor.Syntax; 41 import org.netbeans.editor.SyntaxSupport; 42 import org.netbeans.editor.TokenID; 43 import org.netbeans.editor.TokenItem; 44 import org.netbeans.editor.ext.ExtFormatter; 45 import org.netbeans.editor.Utilities; 46 import org.netbeans.editor.ext.AbstractFormatLayer; 47 import org.netbeans.editor.ext.FormatSupport; 48 import org.netbeans.editor.ext.FormatWriter; 49 import org.netbeans.junit.NbTestCase; 50 import org.netbeans.modules.xml.text.syntax.XMLKit; 51 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport; 52 53 import org.openide.ErrorManager; 54 import org.openide.util.NbBundle; 55 import org.openide.util.RequestProcessor; 56 57 61 public class XMLFormatterTest extends NbTestCase { 62 65 public final static String LINE_BREAK_METATAG = "|"; 66 67 public XMLFormatterTest(String testName) { 68 super(testName); 69 } 70 71 protected void setUp() throws Exception { 72 } 73 74 protected void tearDown() throws Exception { 75 } 76 77 public static Test suite() { 78 TestSuite suite = new TestSuite(XMLFormatterTest.class); 79 80 return suite; 81 } 82 83 public void testReformatSample1(){ 84 testReformat("web.xml"); 85 } 86 87 public void testReformatSample2(){ 88 testReformat("netbeans_build.xml"); 89 } 90 91 private String extractCRMetatag(String text){ 92 int indexOfMetatag = text.indexOf(LINE_BREAK_METATAG); 93 94 if (indexOfMetatag == -1){ 95 return text; 96 } 97 98 String firstPart = text.substring(0, indexOfMetatag); 99 String secondPart = text.substring(indexOfMetatag + LINE_BREAK_METATAG.length()); 100 101 if (secondPart.indexOf(LINE_BREAK_METATAG) > -1){ 102 throw new IllegalArgumentException ("text contains more than one line break tag"); 103 } 104 105 return firstPart + secondPart; 106 } 107 108 private void testReformat(String testFileName) { 109 System.out.println("testReformat(" + testFileName + ")"); 110 XMLFormatter formatter = new XMLFormatter(XMLKit.class); 111 BaseDocument doc = createEmptyBaseDocument(); 112 113 try{ 114 String txtRawXML = readStringFromFile(new File (new File ( 115 getTestFilesDir(), "testReformat"), testFileName)); 116 117 doc.insertString(0, txtRawXML, null); 118 formatter.reformat(doc, 0, doc.getLength(), false); 119 getRef().print(doc.getText(0, doc.getLength())); 120 } 121 catch (Exception e){ 122 fail(e.getMessage()); } 124 125 compareReferenceFiles(); 126 } 127 128 private String readStringFromFile(File file) throws IOException { 129 StringBuffer buff = new StringBuffer (); 130 131 BufferedReader rdr = new BufferedReader (new FileReader (file)); 132 133 String line; 134 135 try{ 136 while ((line = rdr.readLine()) != null){ 137 buff.append(line + "\n"); 138 } 139 } finally{ 140 rdr.close(); 141 } 142 143 return buff.toString(); 144 } 145 146 private File getTestFilesDir(){ 147 return new File (new File (getDataDir(), "input"), "XMLFormatterTest"); 148 } 149 150 private BaseDocument createEmptyBaseDocument(){ 151 return new BaseDocument(XMLKit.class, false); 152 } 153 } 154 | Popular Tags |