KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > indent > ActionStartPrevLinePlusBackupTest


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-2006 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
35
36 package edu.rice.cs.drjava.model.definitions.indent;
37
38 /**
39  * Test class for an indent rule with a really long name. :-)
40  * Inherits from ActionStartPrevLinePlusTest, since this rule's functionality
41  * should be a strict extension of ActionStartPrevLinePlus.
42  * @version $Id: ActionStartPrevLinePlusBackupTest.java 3901 2006-06-30 05:28:11Z rcartwright $
43  */

44 public class ActionStartPrevLinePlusBackupTest extends ActionStartPrevLinePlusTest {
45   
46   /**
47    * Factory to enable reuse of methods from ActionStartPrevLinePlusTest.
48    * This creates an action that should behave identically to an instance of
49    * ActionStartPrevLinePlus.
50    * @param suffix the text to be added by this rule after indent padding
51    * @see ActionStartPrevLinePlus#ActionStartPrevLinePlus(String)
52    */

53   private IndentRuleAction makeAction(String JavaDoc suffix) {
54     return new ActionStartPrevLinePlusBackup(suffix, suffix.length());
55   }
56   
57   /**
58    * Factory to enable reuse of methods from ActionStartPrevLinePlusBackupTest.
59    * This works similarly to {@link #makeAction(String)}.
60    * @param suffix the text to be added by this rule after indent padding
61    * @param position the character within the suffix string before which to
62    * place the cursor
63    * @see ActionStartPrevLinePlusBackup#ActionStartPrevLinePlusBackup(String, int)
64    */

65   private IndentRuleAction makeBackupAction(String JavaDoc suffix, int position) {
66     return new ActionStartPrevLinePlusBackup(suffix, position);
67   }
68   
69   private String JavaDoc _noIndent = "foo\nbar";
70   private String JavaDoc _evenIndent = " foo\n bar";
71   private String JavaDoc _unevenIndent = " foo\nbar";
72   private String JavaDoc _noIndentRes = "foo\nabc bar";
73   private String JavaDoc _evenIndentRes = " foo\n abc bar";
74   private String JavaDoc _unevenIndentRes = " foo\n abc bar";
75   
76   /**
77    * Attempts to move current location to the start of the suffix.
78    */

79   public void testMoveToStart() throws javax.swing.text.BadLocationException JavaDoc {
80     moveTestHelper(_noIndent, _noIndentRes, 0, 7, 0, 4);
81     moveTestHelper(_evenIndent, _evenIndentRes, 0, 11, 0, 8);
82     moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 9, 0, 8);
83   }
84   
85   /**
86    * Attempts to move current location to the end of the suffix.
87    */

88   public void testMoveToEnd() throws javax.swing.text.BadLocationException JavaDoc {
89     moveTestHelper(_noIndent, _noIndentRes, 0, 4, 4, 8);
90     moveTestHelper(_evenIndent, _evenIndentRes, 0, 6, 4, 12);
91     moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 6, 4, 12);
92   }
93   
94   /**
95    * Attempts to move current location to the middle of the suffix.
96    */

97   public void testMoveToMiddle() throws javax.swing.text.BadLocationException JavaDoc {
98     moveTestHelper(_noIndent, _noIndentRes, 0, 4, 2, 6);
99     moveTestHelper(_evenIndent, _evenIndentRes, 0, 6, 2, 10);
100     moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 6, 2, 10);
101   }
102   
103   /**
104    * Helper method for "MoveTo" tests.
105    * @param text the test text
106    * @param result the result text
107    * @param deltaLen the change in text length
108    * @param before location to set before indenting
109    * @param position param to pass to makeBackupAction
110    * @param after location to expect after indenting
111    */

112   private void moveTestHelper(String JavaDoc text, String JavaDoc result, int deltaLen,
113                               int before, int position, int after)
114       throws javax.swing.text.BadLocationException JavaDoc {
115     _setDocText(text);
116     _doc.setCurrentLocation(before); // end of bar line
117

118     String JavaDoc suffix = "abc ";
119     makeBackupAction(suffix, position).indentLine(_doc, Indenter.OTHER);
120     assertEquals("text length",
121                  text.length() + deltaLen + suffix.length(),
122                  _doc.getLength());
123     assertEquals("text contents", result, _doc.getText());
124     assertEquals("location", after, _doc.getCurrentLocation());
125   }
126 }
127
Popular Tags