KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > CommentTest


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

33
34 package edu.rice.cs.drjava.model.definitions;
35
36 import edu.rice.cs.drjava.DrJava;
37 import edu.rice.cs.drjava.DrJavaTestCase;
38 import edu.rice.cs.drjava.config.OptionConstants;
39 import edu.rice.cs.drjava.model.DJDocument;
40 import edu.rice.cs.drjava.model.GlobalEventNotifier;
41
42 import javax.swing.text.BadLocationException JavaDoc;
43
44 /**
45  * Test the comment lines / uncomment lines functionality.
46  * @version $Id: CommentTest.java 3553 2006-02-20 21:22:09Z rcartwright $
47  */

48 public final class CommentTest extends DrJavaTestCase {
49   protected DefinitionsDocument doc;
50   private Integer JavaDoc _indentLevel = new Integer JavaDoc(2);
51 // private Integer configIndent;
52
private GlobalEventNotifier _notifier;
53
54   /**
55    * Resents configuration settings and sets up the indent level so that we
56    * can predict the correct behavior for indenting.
57    */

58   public void setUp() throws Exception JavaDoc {
59     super.setUp();
60     DrJava.getConfig().resetToDefaults();
61     _notifier = new GlobalEventNotifier();
62     doc = new DefinitionsDocument(_notifier);
63     DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,_indentLevel);
64   }
65
66   /**
67    * Tests the Comment Out Line(s) command with a single line.
68    */

69   public void testCommentOutSingleLine() throws BadLocationException JavaDoc {
70     String JavaDoc text =
71       "Here is some abritrary text that should be commented.\n" +
72       "/* It is on multiple lines, and contains slashes // and other\n" +
73       "various */ obnoxious characters.\n";
74
75     String JavaDoc commented =
76       "Here is some abritrary text that should be commented.\n" +
77       "///* It is on multiple lines, and contains slashes // and other\n" +
78       "various */ obnoxious characters.\n";
79
80     doc.insertString(0, text, null);
81     _assertContents("Sample text is inserted improperly.", text, doc);
82     doc.commentLines(70, 75);
83     _assertContents("Only the second line should be wing-commented!", commented, doc);
84   }
85
86   /**
87    * Tests the Comment Out Line(s) command with multiple lines.
88    */

89   public void testCommentOutMultipleLines() throws BadLocationException JavaDoc {
90     String JavaDoc text =
91       "Here is some abritrary text that should be commented.\n" +
92       "/* It is on multiple lines, and contains slashes // and other\n" +
93       "various */ obnoxious characters.\n";
94
95     String JavaDoc commented =
96       "//Here is some abritrary text that should be commented.\n" +
97       "///* It is on multiple lines, and contains slashes // and other\n" +
98       "//various */ obnoxious characters.\n";
99
100     doc.insertString(0, text, null);
101     _assertContents("Sample text is inserted improperly.", text, doc);
102     doc.commentLines(0, doc.getLength());
103     _assertContents("These lines should be wing-commented!", commented, doc);
104   }
105
106   /**
107    * Tests the Uncomment Line(s) command with a single line.
108    * These sample lines should be ignored by the algorithm.
109    */

110   public void testUncommentIgnoreSingleLine() throws BadLocationException JavaDoc {
111     String JavaDoc text =
112       "Here is some abritrary text that should not be uncommented.\n" +
113       "/* It is on multiple lines, and contains slashes // and other\n" +
114       "* various */ obnoxious characters,\n" +
115       "sometimes // in block comments and sometimes not.";
116
117     doc.insertString(0, text, null);
118     _assertContents("Sample text is inserted improperly.", text, doc);
119     doc.uncommentLines(70, 75);
120     _assertContents("These lines should be unchanged by uncomment!", text, doc);
121   }
122
123   /**
124    * Tests the Uncomment Line(s) command with multiple lines.
125    * These sample lines should be ignored by the algorithm.
126    */

127   public void testUncommentIgnoreMultipleLines() throws BadLocationException JavaDoc {
128     String JavaDoc text =
129       "Here is some abritrary text that should not be uncommented.\n" +
130       "/* It is on multiple lines, and contains slashes // and other\n" +
131       "* various */ obnoxious characters,\n" +
132       "sometimes // in block comments and sometimes not.";
133
134     doc.insertString(0, text, null);
135     _assertContents("Sample text is inserted improperly.", text, doc);
136     doc.uncommentLines(0, doc.getLength());
137     _assertContents("These lines should be unchanged by uncomment!", text, doc);
138   }
139
140   /**
141    * Tests the Uncomment Line(s) command with a single line.
142    * One of these sample lines should be uncommented and indented by the algorithm.
143    */

144   public void testUncommentSingleLine() throws BadLocationException JavaDoc {
145     String JavaDoc text =
146       "// // Here is some abritrary text that should be uncommented.\n" +
147       "// /* along with a little bit of code, just to spice\n" +
148       " //* things up.\n" +
149       "// */ \n" +
150       "// System.out.println(\"Aren't comments fun? // (yeah!)\")";
151
152     String JavaDoc uncommented =
153       "// // Here is some abritrary text that should be uncommented.\n" +
154       "// /* along with a little bit of code, just to spice\n" +
155       " //* things up.\n" +
156       "// */ \n" +
157       " System.out.println(\"Aren't comments fun? // (yeah!)\")";
158
159     doc.insertString(0, text, null);
160     _assertContents("Sample text is inserted improperly.", text, doc);
161     doc.uncommentLines(doc.getLength()-1, doc.getLength());
162     _assertContents("The last line should have no commenting!",
163                     uncommented, doc);
164   }
165
166   /**
167    * Tests the Uncomment Line(s) command with multiple lines.
168    * These sample lines should be uncommented and indented by the algorithm.
169    */

170   public void testUncommentMultipleLines() throws BadLocationException JavaDoc {
171     String JavaDoc text =
172       "//// Here is some abritrary text that should be uncommented.\n" +
173       "// /* along with a little bit of code, just to spice\n" +
174       "// * things up.\n" +
175       "// */ \n" +
176       "// System.out.println(\"Aren't comments fun? // (yeah!)\")";
177
178     String JavaDoc uncommented =
179       "// Here is some abritrary text that should be uncommented.\n" +
180       " /* along with a little bit of code, just to spice\n" +
181       " * things up.\n" +
182       " */ \n" +
183       " System.out.println(\"Aren't comments fun? // (yeah!)\")";
184
185     doc.insertString(0, text, null);
186     _assertContents("Sample text is inserted improperly.", text, doc);
187     doc.uncommentLines(0, doc.getLength());
188     _assertContents("These lines should have at most one level of commenting!", uncommented, doc);
189   }
190
191   private static void _assertContents(String JavaDoc msg, String JavaDoc expected, DJDocument document)
192     throws BadLocationException JavaDoc {
193     assertEquals(msg, expected, document.getText());
194   }
195 }
196
Popular Tags