Coverage Report - edu.rice.cs.drjava.model.repl.InteractionsDJDocumentTest
 
Classes in this File Line Coverage Branch Coverage Complexity
InteractionsDJDocumentTest
0%
0/53
0%
0/2
1.429
InteractionsDJDocumentTest$1
0%
0/9
0%
0/4
1.429
InteractionsDJDocumentTest$2
0%
0/6
N/A
1.429
 
 1  0
 /*BEGIN_COPYRIGHT_BLOCK
 2  
  *
 3  
  * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions are met:
 8  
  *    * Redistributions of source code must retain the above copyright
 9  
  *      notice, this list of conditions and the following disclaimer.
 10  
  *    * Redistributions in binary form must reproduce the above copyright
 11  
  *      notice, this list of conditions and the following disclaimer in the
 12  
  *      documentation and/or other materials provided with the distribution.
 13  
  *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14  
  *      names of its contributors may be used to endorse or promote products
 15  
  *      derived from this software without specific prior written permission.
 16  
  * 
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21  
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24  
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25  
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26  
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
  *
 29  
  * This software is Open Source Initiative approved Open Source Software.
 30  
  * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31  
  * 
 32  
  * This file is part of DrJava.  Download the current version of this project
 33  
  * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34  
  * 
 35  
  * END_COPYRIGHT_BLOCK*/
 36  
 
 37  
 package edu.rice.cs.drjava.model.repl;
 38  
 
 39  
 import edu.rice.cs.drjava.DrJavaTestCase;
 40  
 import edu.rice.cs.drjava.model.GlobalModel;
 41  
 import edu.rice.cs.drjava.ui.MainFrame;
 42  
 import edu.rice.cs.util.text.EditDocumentException;
 43  
 import edu.rice.cs.util.swing.Utilities;
 44  
 
 45  
 import java.io.File;
 46  
 
 47  
 /** Tests the functionality of the InteractionsDJDocument. */
 48  0
 public final class InteractionsDJDocumentTest extends DrJavaTestCase {
 49  
 
 50  
   protected InteractionsDJDocument _adapter;
 51  
   protected InteractionsModel _model;
 52  
   protected InteractionsDocument _doc;
 53  
   protected MainFrame mf;
 54  
   
 55  
   /** Initialize fields for each test. */
 56  
   protected void setUp() throws Exception {
 57  0
     super.setUp();
 58  
     // MainFrame creation must run in event thread because event thread is already running
 59  0
     Utilities.invokeAndWait(new Runnable() {
 60  
       public void run() {
 61  0
         mf = new MainFrame();
 62  0
         GlobalModel gm = mf.getModel();
 63  0
         _model = gm.getInteractionsModel();
 64  0
         _adapter = gm.getSwingInteractionsDocument();
 65  0
         _doc = gm.getInteractionsDocument();
 66  0
         assert _model._pane != null;  // MainFrame creates an interactions controller which creates the pane.
 67  0
       }
 68  
     });
 69  0
   }
 70  
   
 71  0
   private boolean _interpreterRestarted = false;
 72  
   
 73  
   public void test1() {
 74  0
     try { helpTestStylesListContentAndReset(); }
 75  0
     catch(Throwable t) { t.printStackTrace(); }
 76  0
   }
 77  
   
 78  
   /** Tests that the styles list is updated and reset properly */
 79  
   public void helpTestStylesListContentAndReset() throws EditDocumentException, InterruptedException {
 80  
 //    System.err.println("testStylesList started");
 81  
     /* The banner and the prompt are inserted in the styles list when the document is constructed; the corresponding
 82  
        offsets are computed in the tests below. 
 83  
      */
 84  
     
 85  0
     final Object _restartLock = new Object();
 86  
     
 87  0
     assertEquals("StylesList before insert should contain 2 pairs", 2, _adapter.getStyles().length);
 88  
 //    System.err.println("Styles:\n" + Arrays.toString(_adapter.getStyles()));
 89  
     
 90  0
     int blen = _model.getStartUpBanner().length();
 91  
 //    System.err.println("StartUpBanner:\n'" + _model.getStartUpBanner() + "'");
 92  
 //    System.err.println("length = " +  _model.getStartUpBanner().length());
 93  
 //    System.err.println("Banner:\n'" + _model.getBanner() + "'");
 94  
 //    System.err.println("length = " +  _model.getBanner().length());                   
 95  
     
 96  
     /** Elt1, Elt2 are first two elements pushed on the StylesList stack */
 97  0
     String styleElt1 = "((0, " + blen + "), object.return.style)";
 98  0
     String styleElt2 = "((" + blen + ", " + (blen + 2) + "), default)";
 99  
 
 100  0
     assertEquals("The first element pushed on StylesList before insertion should be", styleElt1,
 101  0
                  _adapter.getStyles()[1].toString());
 102  0
     assertEquals("The second element pushed on StylesList before insertion should be", styleElt2,
 103  0
                  _adapter.getStyles()[0].toString());
 104  
     
 105  
     // Insert some text
 106  0
     _doc.append("5", InteractionsDocument.NUMBER_RETURN_STYLE);
 107  
     
 108  
     /* Third element pushed StylesList stack before reset */
 109  0
     String styleElt3 = "((" + (blen + 2) + ", " + (blen + 3) + "), number.return.style)";
 110  
 
 111  0
     assertEquals("StylesList before reset should contain 3 pairs", 3, _adapter.getStyles().length);
 112  
     
 113  0
     assertEquals("The first element pushed on StylesList before reset should be", styleElt1,
 114  0
                  _adapter.getStyles()[2].toString());
 115  0
     assertEquals("The second element pushed on StylesList before reset should be", styleElt2,
 116  0
                  _adapter.getStyles()[1].toString());
 117  0
     assertEquals("The last element pushed on StylesList before reset should be", styleElt3,
 118  0
                  _adapter.getStyles()[0].toString());
 119  
     
 120  
 //    System.err.println("Doc text: " + _adapter.getText());
 121  
 //    System.err.println("The styles list is: " + _adapter.getStylesList());
 122  
 //    System.err.println("The length of the startUp banner is: " + InteractionsModel.getStartUpBanner().length());
 123  
     
 124  
     /* Reset interactions and wait until it completes */
 125  
 
 126  
 //    System.err.println("reset interactions test reached");
 127  0
     InteractionsListener restartCommand = new DummyInteractionsListener() {
 128  
       public void interpreterReady(File wd) {
 129  0
         synchronized(_restartLock) {
 130  0
           _interpreterRestarted = true;
 131  0
           _restartLock.notify();
 132  
         }
 133  0
       }};
 134  0
     _model.addListener(restartCommand);
 135  
                                    
 136  
     // Reset should clear
 137  0
     _model.setWaitingForFirstInterpreter(false);
 138  
     
 139  0
     synchronized(_restartLock) { _interpreterRestarted = false; }
 140  
       
 141  
     // Reset the interactions pane, restarting the interpreter
 142  0
     File f = _model.getWorkingDirectory();
 143  0
     _model.resetInterpreter(f, true);
 144  
     
 145  
 //    System.err.println("Interpreter reset");
 146  
 
 147  
     // Wait until interpreter has restarted
 148  0
     synchronized(_restartLock) { while (! _interpreterRestarted) _restartLock.wait(); }
 149  0
     _model.removeListener(restartCommand);
 150  
     
 151  
 //    System.err.println("Doc text: " + _adapter.getText());
 152  
 //    System.err.println("Text length: " + _adapter.getLength());
 153  
 //    System.err.println("The styles list is: " + _adapter.getStylesList());
 154  
    
 155  0
     Utilities.clearEventQueue();  // assures that pending updates to _pane have been performed
 156  0
     assertEquals("StylesList after reset should contain 2 pairs", 2, _adapter.getStyles().length);
 157  
     
 158  0
     assertEquals("The first element pushed on StylesList after reset should be", styleElt1,
 159  0
                  _adapter.getStyles()[1].toString());
 160  0
     assertEquals("The second element pushed on StylesList after reset should be", styleElt2,
 161  0
                  _adapter.getStyles()[0].toString());
 162  
 //    System.err.println("testStylesList complete");
 163  0
   }
 164  
 
 165  
   public void test2() {
 166  0
     try { helpTestCannotAddNullStyleToList(); }
 167  0
     catch(Throwable t) { t.printStackTrace(); }
 168  0
   }
 169  
   /** Tests that a null style is not added to the list. Fix for bug #995719. */
 170  
   public void helpTestCannotAddNullStyleToList() throws EditDocumentException {
 171  
 //    System.err.println("testCannotAddNull started");
 172  
     // the banner and the prompt are inserted in the styles list when the document is constructed
 173  0
     assertEquals("StylesList before insert should contain 2 pairs", 2, _adapter.getStyles().length);
 174  
 
 175  
     // Insert some text
 176  0
     _doc.append("5", InteractionsDocument.NUMBER_RETURN_STYLE);
 177  0
     Utilities.clearEventQueue();
 178  
 
 179  0
     assertEquals("StylesList should contain 3 pairs", 3, _adapter.getStyles().length);
 180  
 
 181  
     // Insert some text with a null style
 182  0
     _doc.append("6", null);
 183  0
      Utilities.clearEventQueue();
 184  
 
 185  0
     assertEquals("StylesList should still contain 3 pairs - null string should not have been inserted",
 186  0
                  3, _adapter.getStyles().length);
 187  0
   }
 188  
 }