001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions;
038:
039: import edu.rice.cs.drjava.DrJava;
040: import edu.rice.cs.drjava.DrJavaTestCase;
041: import edu.rice.cs.drjava.config.OptionConstants;
042: import edu.rice.cs.drjava.model.DJDocument;
043: import edu.rice.cs.drjava.model.GlobalEventNotifier;
044:
045: import javax.swing.text.BadLocationException;
046:
047: /**
048: * Test the comment lines / uncomment lines functionality.
049: * @version $Id: CommentTest.java 4255 2007-08-28 19:17:37Z mgricken $
050: */
051: public final class CommentTest extends DrJavaTestCase {
052: protected DefinitionsDocument doc;
053: private Integer _indentLevel = new Integer(2);
054: // private Integer configIndent;
055: private GlobalEventNotifier _notifier;
056:
057: /**
058: * Resents configuration settings and sets up the indent level so that we
059: * can predict the correct behavior for indenting.
060: */
061: public void setUp() throws Exception {
062: super .setUp();
063: DrJava.getConfig().resetToDefaults();
064: _notifier = new GlobalEventNotifier();
065: doc = new DefinitionsDocument(_notifier);
066: DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,
067: _indentLevel);
068: }
069:
070: /**
071: * Tests the Comment Out Line(s) command with a single line.
072: */
073: public void testCommentOutSingleLine() throws BadLocationException {
074: String text = "Here is some abritrary text that should be commented.\n"
075: + "/* It is on multiple lines, and contains slashes // and other\n"
076: + "various */ obnoxious characters.\n";
077:
078: String commented = "Here is some abritrary text that should be commented.\n"
079: + "///* It is on multiple lines, and contains slashes // and other\n"
080: + "various */ obnoxious characters.\n";
081:
082: doc.insertString(0, text, null);
083: _assertContents("Sample text is inserted improperly.", text,
084: doc);
085: doc.commentLines(70, 75);
086: _assertContents(
087: "Only the second line should be wing-commented!",
088: commented, doc);
089: }
090:
091: /**
092: * Tests the Comment Out Line(s) command with multiple lines.
093: */
094: public void testCommentOutMultipleLines()
095: throws BadLocationException {
096: String text = "Here is some abritrary text that should be commented.\n"
097: + "/* It is on multiple lines, and contains slashes // and other\n"
098: + "various */ obnoxious characters.\n";
099:
100: String commented = "//Here is some abritrary text that should be commented.\n"
101: + "///* It is on multiple lines, and contains slashes // and other\n"
102: + "//various */ obnoxious characters.\n";
103:
104: doc.insertString(0, text, null);
105: _assertContents("Sample text is inserted improperly.", text,
106: doc);
107: doc.commentLines(0, doc.getLength());
108: _assertContents("These lines should be wing-commented!",
109: commented, doc);
110: }
111:
112: /**
113: * Tests the Uncomment Line(s) command with a single line.
114: * These sample lines should be ignored by the algorithm.
115: */
116: public void testUncommentIgnoreSingleLine()
117: throws BadLocationException {
118: String text = "Here is some abritrary text that should not be uncommented.\n"
119: + "/* It is on multiple lines, and contains slashes // and other\n"
120: + "* various */ obnoxious characters,\n"
121: + "sometimes // in block comments and sometimes not.";
122:
123: doc.insertString(0, text, null);
124: _assertContents("Sample text is inserted improperly.", text,
125: doc);
126: doc.uncommentLines(70, 75);
127: _assertContents(
128: "These lines should be unchanged by uncomment!", text,
129: doc);
130: }
131:
132: /**
133: * Tests the Uncomment Line(s) command with multiple lines.
134: * These sample lines should be ignored by the algorithm.
135: */
136: public void testUncommentIgnoreMultipleLines()
137: throws BadLocationException {
138: String text = "Here is some abritrary text that should not be uncommented.\n"
139: + "/* It is on multiple lines, and contains slashes // and other\n"
140: + "* various */ obnoxious characters,\n"
141: + "sometimes // in block comments and sometimes not.";
142:
143: doc.insertString(0, text, null);
144: _assertContents("Sample text is inserted improperly.", text,
145: doc);
146: doc.uncommentLines(0, doc.getLength());
147: _assertContents(
148: "These lines should be unchanged by uncomment!", text,
149: doc);
150: }
151:
152: /**
153: * Tests the Uncomment Line(s) command with a single line.
154: * One of these sample lines should be uncommented and indented by the algorithm.
155: */
156: public void testUncommentSingleLine() throws BadLocationException {
157: String text = "// // Here is some abritrary text that should be uncommented.\n"
158: + "// /* along with a little bit of code, just to spice\n"
159: + " //* things up.\n"
160: + "// */ \n"
161: + "// System.out.println(\"Aren't comments fun? // (yeah!)\")";
162:
163: String uncommented = "// // Here is some abritrary text that should be uncommented.\n"
164: + "// /* along with a little bit of code, just to spice\n"
165: + " //* things up.\n"
166: + "// */ \n"
167: + " System.out.println(\"Aren't comments fun? // (yeah!)\")";
168:
169: doc.insertString(0, text, null);
170: _assertContents("Sample text is inserted improperly.", text,
171: doc);
172: doc.uncommentLines(doc.getLength() - 1, doc.getLength());
173: _assertContents("The last line should have no commenting!",
174: uncommented, doc);
175: }
176:
177: /**
178: * Tests the Uncomment Line(s) command with multiple lines.
179: * These sample lines should be uncommented and indented by the algorithm.
180: */
181: public void testUncommentMultipleLines()
182: throws BadLocationException {
183: String text = "//// Here is some abritrary text that should be uncommented.\n"
184: + "// /* along with a little bit of code, just to spice\n"
185: + "// * things up.\n"
186: + "// */ \n"
187: + "// System.out.println(\"Aren't comments fun? // (yeah!)\")";
188:
189: String uncommented = "// Here is some abritrary text that should be uncommented.\n"
190: + " /* along with a little bit of code, just to spice\n"
191: + " * things up.\n"
192: + " */ \n"
193: + " System.out.println(\"Aren't comments fun? // (yeah!)\")";
194:
195: doc.insertString(0, text, null);
196: _assertContents("Sample text is inserted improperly.", text,
197: doc);
198: doc.uncommentLines(0, doc.getLength());
199: _assertContents(
200: "These lines should have at most one level of commenting!",
201: uncommented, doc);
202: }
203:
204: private static void _assertContents(String msg, String expected,
205: DJDocument document) throws BadLocationException {
206: assertEquals(msg, expected, document.getText());
207: }
208: }
|