KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > InteractionsDJDocumentTest


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.repl;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.drjava.model.GlobalModel;
38 import edu.rice.cs.drjava.ui.InteractionsPane;
39 import edu.rice.cs.drjava.ui.MainFrame;
40 import edu.rice.cs.util.text.EditDocumentException;
41
42 import java.io.File JavaDoc;
43
44 /** Tests the functionality of the InteractionsDJDocument. */
45 public final class InteractionsDJDocumentTest extends DrJavaTestCase {
46
47   protected InteractionsDJDocument _adapter;
48   protected InteractionsModel _model;
49   protected InteractionsDocument _doc;
50   protected InteractionsPane _pane;
51   protected MainFrame mf;
52   
53   /** Initialize fields for each test. */
54   protected void setUp() throws Exception JavaDoc {
55     super.setUp();
56     mf = new MainFrame();
57     GlobalModel gm = mf.getModel();
58     _model = gm.getInteractionsModel();
59     _adapter = gm.getSwingInteractionsDocument();
60     _doc = gm.getInteractionsDocument();
61   }
62   
63   private boolean _interpreterRestarted = false;
64   
65   /** Tests that the styles list is updated and reset properly */
66   public void testStylesListContentAndReset() throws EditDocumentException, InterruptedException JavaDoc {
67     /* The banner and the prompt are inserted in the styles list when the document is constructed; the corresponding
68        offsets are computed in the tests below.
69      */

70     
71     final Object JavaDoc _restartLock = new Object JavaDoc();
72     
73     assertEquals("StylesList before insert should contain 2 pairs", 2, _adapter.getStylesList().size());
74     
75     int blen = _model.getStartUpBanner().length();
76     
77     /** Elt1, Elt2 are first two elements pushed on the StylesList stack */
78     String JavaDoc styleElt1 = "((0, " + blen + "), object.return.style)";
79     String JavaDoc styleElt2 = "((" + blen + ", " + (blen + 2) + "), default)";
80
81     assertEquals("The first element pushed on StylesList before insertion should be", styleElt1,
82                  _adapter.getStylesList().get(1).toString());
83     assertEquals("The second element pushed on StylesList before insertion should be", styleElt2,
84                  _adapter.getStylesList().get(0).toString());
85     
86     // Insert some text
87
_doc.insertText(_doc.getLength(), "5", InteractionsDocument.NUMBER_RETURN_STYLE);
88     
89     /* Third element pushed StylesList stack before reset */
90     String JavaDoc styleElt3 = "((" + (blen + 2) + ", " + (blen + 3) + "), number.return.style)";
91
92     assertEquals("StylesList before reset should contain 3 pairs", 3, _adapter.getStylesList().size());
93     
94     assertEquals("The first element pushed on StylesList before reset should be", styleElt1,
95                  _adapter.getStylesList().get(2).toString());
96     assertEquals("The second element pushed on StylesList before reset should be", styleElt2,
97                  _adapter.getStylesList().get(1).toString());
98     assertEquals("The last element pushed on StylesList before reset should be", styleElt3,
99                  _adapter.getStylesList().get(0).toString());
100     
101 // System.err.println("Doc text: " + _adapter.getText());
102
// System.err.println("The styles list is: " + _adapter.getStylesList());
103
// System.err.println("The length of the startUp banner is: " + InteractionsModel.getStartUpBanner().length());
104

105     /* Reset interactions and wait until it completes */
106
107     InteractionsListener restartCommand = new DummyInteractionsListener() {
108       public void interpreterReady(File JavaDoc wd) {
109         synchronized(_restartLock) {
110           _interpreterRestarted = true;
111           _restartLock.notify();
112         }
113       }};
114     _model.addListener(restartCommand);
115                                    
116     // Reset should clear
117
_model.setWaitingForFirstInterpreter(false);
118     
119     synchronized(_restartLock) { _interpreterRestarted = false; }
120       
121     // Reset the interactions pane, restarting the interpreter
122
File JavaDoc f = _model.getWorkingDirectory();
123     _model.resetInterpreter(f);
124
125     //. Wait until interpreter has restarted
126
synchronized(_restartLock) { while (! _interpreterRestarted) _restartLock.wait(); }
127     _model.removeListener(restartCommand);
128     
129 // System.err.println("Doc text: " + _adapter.getText());
130
// System.err.println("Text length: " + _adapter.getLength());
131
// System.err.println("The styles list is: " + _adapter.getStylesList());
132

133     assertEquals("StylesList after reset should contain 2 pairs", 2, _adapter.getStylesList().size());
134     
135     assertEquals("The first element pushed on StylesList after reset should be", styleElt1,
136                  _adapter.getStylesList().get(1).toString());
137     assertEquals("The second element pushed on StylesList after reset should be", styleElt2,
138                  _adapter.getStylesList().get(0).toString());
139     
140     
141
142   }
143
144   /**
145    * Tests that a null style is not added to the list. Fix for bug #995719
146    */

147   public void testCannotAddNullStyleToList() throws EditDocumentException {
148     // the banner and the prompt are inserted in the styles list when the document is constructed
149
assertEquals("StylesList before insert should contain 2 pairs",
150                  2, _adapter.getStylesList().size());
151
152     // Insert some text
153
_doc.insertText(_doc.getLength(), "5", InteractionsDocument.NUMBER_RETURN_STYLE);
154
155     assertEquals("StylesList should contain 3 pairs",
156                  3, _adapter.getStylesList().size());
157
158     // Insert some text with a null style
159
_doc.insertText(_doc.getLength(), "6", null);
160
161     assertEquals("StylesList should still contain 3 pairs - null string should not have been inserted",
162                  3, _adapter.getStylesList().size());
163   }
164 }
Popular Tags