KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > editor > StrutsEditorUtilitiesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.struts.editor;
21
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
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 /**
33  *
34  * @author petr
35  */

36 public class StrutsEditorUtilitiesTest extends NbTestCase {
37
38     File JavaDoc testDir;
39     FileObject testDirFO;
40
41     public StrutsEditorUtilitiesTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47         testDir = new File JavaDoc (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     /** Test when the cursor is inside of declaration <action ..... />
54      */

55     public void testGetActionPath() {
56         BaseDocument doc = createBaseDocument(new File JavaDoc(testDir, "struts-config.xml"));
57         String JavaDoc path = null;
58         String JavaDoc text = null;
59         try {
60             text = doc.getText(0, doc.getLength() - 1);
61         } catch (BadLocationException JavaDoc 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     /** Test when the cursor is inside of declaration <form-bean ..... />
86      */

87     public void testGetActionFormBeanName() {
88         BaseDocument doc = createBaseDocument(new File JavaDoc(testDir, "struts-config.xml"));
89         String JavaDoc path = null;
90         String JavaDoc text = null;
91         try {
92             text = doc.getText(0, doc.getLength() - 1);
93         } catch (BadLocationException JavaDoc 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 JavaDoc file){
148         BaseDocument doc = new BaseDocument(XMLKit.class, false);
149         File JavaDoc strutsConfig = new File JavaDoc(testDir, "struts-config.xml");
150         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
151         try {
152             FileReader JavaDoc reader = new FileReader JavaDoc (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 JavaDoc ex) {
162             fail("Exception occured during createBaseDocument: " + ex.toString());
163         }
164         catch (BadLocationException JavaDoc ex) {
165             fail("Exception occured during createBaseDocument: " + ex.toString());
166         }
167         return null;
168     }
169     
170 }
171
Popular Tags