KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > GlobalIndentTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project:
4  * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2003 JavaPLT group at Rice University (javaplt@rice.edu)
9  * All rights reserved.
10  *
11  * Developed by: Java Programming Languages Team
12  * Rice University
13  * http://www.cs.rice.edu/~javaplt/
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal with the Software without restriction, including without
18  * limitation the rights to use, copy, modify, merge, publish, distribute,
19  * sublicense, and/or sell copies of the Software, and to permit persons to
20  * whom the Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * - Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimers.
25  * - Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimers in the
27  * documentation and/or other materials provided with the distribution.
28  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29  * names of its contributors may be used to endorse or promote products
30  * derived from this Software without specific prior written permission.
31  * - Products derived from this software may not be called "DrJava" nor
32  * use the term "DrJava" as part of their names without prior written
33  * permission from the JavaPLT group. For permission, write to
34  * javaplt@rice.edu.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39  * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42  * OTHER DEALINGS WITH THE SOFTWARE.
43  *
44 END_COPYRIGHT_BLOCK*/

45
46 package edu.rice.cs.drjava.model;
47
48 import javax.swing.text.BadLocationException JavaDoc;
49 import java.util.List JavaDoc;
50
51 import edu.rice.cs.drjava.model.definitions.indent.Indenter;
52 import edu.rice.cs.util.OperationCanceledException;
53
54 /**
55  * Tests the indenting functionality on the level of the GlobalModel.
56  * Not only are we testing that the document turns out right, but also
57  * that the cursor position in the document is consistent with a standard.
58  * @version $Id: GlobalIndentTest.java 3839 2006-05-14 20:28:51Z rcartwright $
59  */

60 public final class GlobalIndentTest extends GlobalModelTestCase {
61   private static final String JavaDoc FOO_EX_1 = "public class Foo {\n";
62   private static final String JavaDoc FOO_EX_2 = "int foo;\n";
63   private static final String JavaDoc BAR_CALL_1 = "bar(monkey,\n";
64   private static final String JavaDoc BAR_CALL_2 = "banana)\n";
65 // private static final String BEAT_1 = "void beat(Horse dead,\n";
66
// private static final String BEAT_2 = " Stick pipe)\n";
67

68   /** Tests that an indent increases the size of the tab when the cursor is at the start of the line. If the cursor is
69    * in the whitespace before the first word on a line, indent always moves the cursor up to the beginning of the first non-whitespace
70    * character.
71    * @throws BadLocationException
72    */

73   public void testIndentGrowTabAtStart() throws BadLocationException JavaDoc, OperationCanceledException {
74     OpenDefinitionsDocument openDoc = _getOpenDoc();
75     
76     openDoc.insertString(0, FOO_EX_1, null);
77     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
78     openDoc.setCurrentLocation(FOO_EX_1.length());
79     int loc = openDoc.getCurrentLocation();
80     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
81     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
82     _assertLocation(FOO_EX_1.length() + 2, openDoc);
83   }
84
85   /** Tests indent that increases the size of the tab when the cursor is in the middle of the line.
86    * The cursor stays in the same place.
87    * @throws BadLocationException
88    */

89   public void testIndentGrowTabAtMiddle() throws BadLocationException JavaDoc, OperationCanceledException {
90     OpenDefinitionsDocument openDoc = _getOpenDoc();
91     
92     openDoc.insertString(0, FOO_EX_1, null);
93     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
94     openDoc.setCurrentLocation(FOO_EX_1.length() + 5);
95     int loc = openDoc.getCurrentLocation();
96     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
97     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
98     _assertLocation(FOO_EX_1.length() + 6, openDoc);
99   }
100
101   /** Tests that an indent increases the size of the tab when the cursor is at the end of the line. The cursor stays
102    * in the same place.
103    * @throws BadLocationException
104    */

105   public void testIndentGrowTabAtEnd() throws BadLocationException JavaDoc, OperationCanceledException {
106     OpenDefinitionsDocument openDoc = _getOpenDoc();
107     
108     openDoc.insertString(0, FOO_EX_1, null);
109     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
110     openDoc.setCurrentLocation(openDoc.getLength() - 1);
111     int loc = openDoc.getCurrentLocation();
112     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
113     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
114     _assertLocation(openDoc.getLength() - 1, openDoc);
115   }
116
117   /** Tests that an indent increases the size of the tab when the cursor is at the start of the line. If the cursor
118    * is in whitespace before the first word on a line, an indent moves the cursor to the beginning of the first
119    * non-whitespace character.
120    * @throws BadLocationException
121    */

122   public void testIndentShrinkTabAtStart() throws BadLocationException JavaDoc, OperationCanceledException{
123     OpenDefinitionsDocument openDoc = _getOpenDoc();
124
125     openDoc.insertString(0, FOO_EX_1, null);
126     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
127     openDoc.setCurrentLocation(FOO_EX_1.length());
128     int loc = openDoc.getCurrentLocation();
129     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
130     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
131     _assertLocation(FOO_EX_1.length() + 2, openDoc);
132   }
133
134   /** Tests that an indent increases the size of the tab when the cursor is in the middle of the line. The cursor stays
135    * in the same place.
136    * @throws BadLocationException
137    */

138   public void testIndentShrinkTabAtMiddle() throws BadLocationException JavaDoc, OperationCanceledException {
139     OpenDefinitionsDocument openDoc = _getOpenDoc();
140
141     openDoc.insertString(0, FOO_EX_1, null);
142     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
143     openDoc.setCurrentLocation(FOO_EX_1.length() + 5);
144     int loc = openDoc.getCurrentLocation();
145     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
146     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
147     _assertLocation(FOO_EX_1.length() + 4, openDoc);
148   }
149
150   /** Tests that an indent increases the size of the tab when the cursor is at the end of the line. The cursor stays
151    * in the same place.
152    * @throws BadLocationException
153    */

154   public void testIndentShrinkTabAtEnd()
155       throws BadLocationException JavaDoc, OperationCanceledException {
156     OpenDefinitionsDocument openDoc = _getOpenDoc();
157
158     openDoc.insertString(0, FOO_EX_1, null);
159     openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null);
160     openDoc.setCurrentLocation(openDoc.getLength() - 1);
161     int loc = openDoc.getCurrentLocation();
162     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
163     _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc);
164     _assertLocation(openDoc.getLength() - 1, openDoc);
165   }
166
167   /** Tests that an indent matches up with the indent on the line above. The cursor is at the start of the line.
168    * @exception BadLocationException
169    */

170   public void testIndentSameAsLineAboveAtStart() throws BadLocationException JavaDoc, OperationCanceledException {
171     OpenDefinitionsDocument openDoc = _getOpenDoc();
172
173     openDoc.insertString(0, FOO_EX_2, null);
174     openDoc.insertString(FOO_EX_2.length(), " " + FOO_EX_2, null);
175     openDoc.setCurrentLocation(FOO_EX_2.length());
176     int loc = openDoc.getCurrentLocation();
177     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
178     _assertContents(FOO_EX_2 + FOO_EX_2, openDoc);
179     _assertLocation(FOO_EX_2.length(), openDoc);
180   }
181
182   /** Tests that an indent matches up with the indent on the line above. The cursor is at the end of the line.
183    * @exception BadLocationException
184    */

185   public void testIndentSameAsLineAboveAtEnd() throws BadLocationException JavaDoc, OperationCanceledException {
186     OpenDefinitionsDocument openDoc = _getOpenDoc();
187
188     openDoc.insertString(0, FOO_EX_2, null);
189     openDoc.insertString(FOO_EX_2.length(), " " + FOO_EX_2, null);
190     openDoc.setCurrentLocation(openDoc.getLength() - 1);
191     int loc = openDoc.getCurrentLocation();
192     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
193     _assertContents(FOO_EX_2 + FOO_EX_2, openDoc);
194     _assertLocation(openDoc.getLength() - 1, openDoc);
195   }
196
197   /**
198    * Do an indent that follows the behavior in line with parentheses.
199    * The cursor is at the start of the line.
200    * @exception BadLocationException
201    */

202   public void testIndentInsideParenAtStart() throws BadLocationException JavaDoc, OperationCanceledException {
203     OpenDefinitionsDocument openDoc = _getOpenDoc();
204
205     openDoc.insertString(0, BAR_CALL_1, null);
206     openDoc.insertString(BAR_CALL_1.length(), BAR_CALL_2, null);
207     openDoc.setCurrentLocation(BAR_CALL_1.length());
208     int loc = openDoc.getCurrentLocation();
209     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
210     _assertContents(BAR_CALL_1 + " " + BAR_CALL_2, openDoc);
211     _assertLocation(BAR_CALL_1.length() + 4, openDoc);
212   }
213
214   /** Do an indent that follows the behavior in line with parentheses. The cursor is at the end of the line.
215    * @exception BadLocationException
216    */

217   public void testIndentInsideParenAtEnd() throws BadLocationException JavaDoc, OperationCanceledException {
218     OpenDefinitionsDocument openDoc = _getOpenDoc();
219
220     openDoc.insertString(0, BAR_CALL_1, null);
221     openDoc.insertString(BAR_CALL_1.length(), BAR_CALL_2, null);
222     openDoc.setCurrentLocation(openDoc.getLength() - 1);
223     int loc = openDoc.getCurrentLocation();
224     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
225     _assertContents(BAR_CALL_1 + " " + BAR_CALL_2, openDoc);
226     _assertLocation(openDoc.getLength() - 1, openDoc);
227   }
228
229   /** Indent does nothing to change the document when everything is in place. */
230   public void testIndentDoesNothing() throws BadLocationException JavaDoc, OperationCanceledException {
231     OpenDefinitionsDocument openDoc = _getOpenDoc();
232
233     openDoc.insertString(0, FOO_EX_2 + FOO_EX_2, null);
234     openDoc.setCurrentLocation(openDoc.getLength() - 1);
235     int loc = openDoc.getCurrentLocation();
236     openDoc.indentLines(loc, loc, Indenter.OTHER, null);
237     _assertContents(FOO_EX_2 + FOO_EX_2, openDoc);
238     _assertLocation(openDoc.getLength() - 1, openDoc);
239   }
240
241
242   /**
243    * The quintessential "make the squiggly go to the start, even though
244    * method arguments extend over two lines" test. This behavior is not
245    * correctly followed yet, so until it is, leave this method commented.
246    * @exception BadLocationException
247    *
248   public void testIndentSquigglyAfterTwoLines()
249       throws BadLocationException, OperationCanceledException {
250     OpenDefinitionsDocument openDoc = _getOpenDoc();
251
252     openDoc.insertString(0, BEAT_1, null);
253     openDoc.insertString(BEAT_1.length(), BEAT_2, null);
254     openDoc.insertString(openDoc.getLength(), "{", null);
255     int loc = openDoc.getCurrentLocation();
256     openDoc.indentLines(loc, loc);
257     _assertContents(BEAT_1 + BEAT_2 + "{", openDoc);
258     _assertLocation(openDoc.getLength(), openDoc);
259   }
260 */

261
262   /**
263    * Indents block comments with stars as they should.
264    * Uncomment this method when the correct functionality is implemented.
265    */

266 // public void testIndentBlockCommentStar()
267
// throws BadLocationException, OperationCanceledException {
268
// OpenDefinitionsDocument openDoc = _getOpenDoc();
269
// openDoc.insertString(0, "/*\n*\n*/\n " + FOO_EX_2, null);
270
// int loc = openDoc.getCurrentLocation();
271
// openDoc.indentLines(0, openDoc.getLength());
272
// _assertContents("/*\n *\n */\n" + FOO_EX_2, openDoc);
273
// _assertLocation(openDoc.getLength(), openDoc);
274
// }
275

276   /** Get the only open definitions document. */
277   private OpenDefinitionsDocument _getOpenDoc() {
278     _assertNumOpenDocs(1);
279     OpenDefinitionsDocument doc = _model.newFile();
280     doc.setIndent(2);
281     List JavaDoc<OpenDefinitionsDocument> docs = _model.getOpenDefinitionsDocuments();
282     _assertNumOpenDocs(2);
283     return docs.get(0);
284   }
285
286   private void _assertNumOpenDocs(int num) {
287     assertEquals("number of open documents", num, _model.getOpenDefinitionsDocuments().size());
288   }
289
290   private void _assertContents(String JavaDoc expected, OpenDefinitionsDocument document) throws BadLocationException JavaDoc {
291     assertEquals("document contents", expected, document.getText());
292   }
293
294   private void _assertLocation(int loc, OpenDefinitionsDocument openDoc) {
295     assertEquals("current def'n loc", loc, openDoc.getCurrentLocation());
296   }
297 }
298
Popular Tags