1 19 20 package org.netbeans.modules.web.struts.editor; 21 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import javax.swing.text.BadLocationException ; 26 import org.netbeans.editor.BaseDocument; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.modules.xml.text.syntax.XMLKit; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 32 36 public class StrutsEditorUtilitiesTest extends NbTestCase { 37 38 File testDir; 39 FileObject testDirFO; 40 41 public StrutsEditorUtilitiesTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 testDir = new File (this.getDataDir().getPath()); 48 assertTrue("have a dir " + testDir, testDir.isDirectory()); 49 testDirFO = FileUtil.toFileObject(testDir); 50 assertNotNull("testDirFO is null", testDirFO); 51 } 52 53 55 public void testGetActionPath() { 56 BaseDocument doc = createBaseDocument(new File (testDir, "struts-config.xml")); 57 String path = null; 58 String text = null; 59 try { 60 text = doc.getText(0, doc.getLength() - 1); 61 } catch (BadLocationException ex) { 62 fail(ex.toString()); 63 } 64 int where; 65 66 where = text.indexOf("/login"); 67 path = StrutsEditorUtilities.getActionPath(doc, where); 68 assertEquals("/login", path); 69 path = StrutsEditorUtilities.getActionPath(doc, where+1); 70 assertEquals("/login", path); 71 path = StrutsEditorUtilities.getActionPath(doc, where+6); 72 assertEquals("/login", path); 73 74 where = text.indexOf("action type=\"com"); 75 path = StrutsEditorUtilities.getActionPath(doc, where); 76 assertEquals("/login", path); 77 path = StrutsEditorUtilities.getActionPath(doc, where-1); 78 assertEquals("/login", path); 79 path = StrutsEditorUtilities.getActionPath(doc, where+7); 80 assertEquals("/login", path); 81 path = StrutsEditorUtilities.getActionPath(doc, where+10); 82 assertEquals("/login", path); 83 } 84 85 87 public void testGetActionFormBeanName() { 88 BaseDocument doc = createBaseDocument(new File (testDir, "struts-config.xml")); 89 String path = null; 90 String text = null; 91 try { 92 text = doc.getText(0, doc.getLength() - 1); 93 } catch (BadLocationException ex) { 94 fail(ex.toString()); 95 } 96 int where; 97 98 where = text.indexOf("name=\"FirstBean\""); 99 path = StrutsEditorUtilities.getActionFormBeanName(doc, where); 100 assertEquals("FirstBean", path); 101 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+5); 102 assertEquals("FirstBean", path); 103 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+10); 104 assertEquals("FirstBean", path); 105 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+16); 106 assertEquals("FirstBean", path); 107 path = StrutsEditorUtilities.getActionFormBeanName(doc, where-1); 108 assertEquals("FirstBean", path); 109 path = StrutsEditorUtilities.getActionFormBeanName(doc, where - 20); 110 assertEquals("FirstBean", path); 111 path = StrutsEditorUtilities.getActionFormBeanName(doc, where-35); 112 assertEquals("FirstBean", path); 113 114 where = text.indexOf("initial=\"33\""); 115 path = StrutsEditorUtilities.getActionFormBeanName(doc, where); 116 assertEquals("SecondBean", path); 117 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+5); 118 assertEquals("SecondBean", path); 119 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+10); 120 assertEquals("SecondBean", path); 121 122 where = text.indexOf("name=\"name\""); 123 path = StrutsEditorUtilities.getActionFormBeanName(doc, where); 124 assertEquals("SecondBean", path); 125 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+5); 126 assertEquals("SecondBean", path); 127 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+10); 128 assertEquals("SecondBean", path); 129 130 where = text.indexOf("/form-bean>"); 131 path = StrutsEditorUtilities.getActionFormBeanName(doc, where); 132 assertEquals("SecondBean", path); 133 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+5); 134 assertEquals("SecondBean", path); 135 136 where = text.indexOf("name=\"SecondBean\">"); 137 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+10); 138 assertEquals("SecondBean", path); 139 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+15); 140 assertEquals("SecondBean", path); 141 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+17); 142 assertEquals("SecondBean", path); 143 path = StrutsEditorUtilities.getActionFormBeanName(doc, where+18); 144 assertEquals("SecondBean", path); 145 } 146 147 private BaseDocument createBaseDocument(File file){ 148 BaseDocument doc = new BaseDocument(XMLKit.class, false); 149 File strutsConfig = new File (testDir, "struts-config.xml"); 150 StringBuffer buffer = new StringBuffer (); 151 try { 152 FileReader reader = new FileReader (strutsConfig); 153 char[] buf = new char [100]; 154 int count = -1; 155 while ((count = reader.read(buf)) != -1){ 156 buffer.append(buf, 0, count); 157 } 158 reader.close(); 159 doc.insertString(0, buffer.toString(), null); 160 return doc; 161 } catch (IOException ex) { 162 fail("Exception occured during createBaseDocument: " + ex.toString()); 163 } 164 catch (BadLocationException ex) { 165 fail("Exception occured during createBaseDocument: " + ex.toString()); 166 } 167 return null; 168 } 169 170 } 171 | Popular Tags |