Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 259   Methods: 10
NCLOC: 157   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
TryCatchBodyTypeChecker.java 100% 98.7% 90% 97.8%
coverage coverage
 1    /*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.javalanglevels;
 38   
 39    import edu.rice.cs.javalanglevels.tree.*;
 40    import edu.rice.cs.javalanglevels.parser.JExprParser;
 41    import java.util.*;
 42    import java.io.*;
 43    import edu.rice.cs.plt.reflect.JavaVersion;
 44    import edu.rice.cs.plt.iter.*;
 45   
 46    import junit.framework.TestCase;
 47   
 48    /**Does TypeChecking for the context of a Try-Catch body. Common to all LanguageLevels.*/
 49    public class TryCatchBodyTypeChecker extends BodyTypeChecker {
 50   
 51   
 52    /* Constructor for TryCatchBodyTypeChecker. Delegates the initialization to the BodyTypeChecker
 53    * @param bodyData The enclosing BodyData for the context we are type checking.
 54    * @param file The File corresponding to the source file.
 55    * @param packageName The package name from the source file.
 56    * @param importedFiles The names of the files that are specifically imported (through a class import statement) in the source file.
 57    * @param importedPackages The names of all the packages that are imported through a package import statement in the source file.
 58    * @param vars The list of VariableData that have already been defined (used so we can make sure we don't use a variable before it has been defined).
 59    * @param thrown The list of exceptions thrown in this body
 60    */
 61  92 public TryCatchBodyTypeChecker(BodyData bodyData, File file, String packageName, LinkedList<String> importedFiles, LinkedList<String> importedPackages, LinkedList<VariableData> vars, LinkedList<Pair<SymbolData, JExpression>> thrown) {
 62  92 super(bodyData, file, packageName, importedFiles, importedPackages, vars, thrown);
 63    }
 64   
 65   
 66    /** Create a new instance of this class for visiting inner bodies. */
 67  60 protected BodyTypeChecker createANewInstanceOfMe(BodyData bodyData, File file, String pakage, LinkedList<String> importedFiles, LinkedList<String> importedPackages, LinkedList<VariableData> vars, LinkedList<Pair<SymbolData, JExpression>> thrown) {
 68  60 return new TryCatchBodyTypeChecker(bodyData, file, pakage, importedFiles, importedPackages, vars, thrown);
 69    }
 70   
 71    /** Overwritten here, becuase it is okay for there to be thrown exceptions in the middle of a try catch. */
 72  43 public TypeData forBracedBody(BracedBody that) {
 73  43 final TypeData[] items_result = makeArrayOfRetType(that.getStatements().length);
 74  43 for (int i = 0; i < that.getStatements().length; i++) {
 75  40 items_result[i] = that.getStatements()[i].visit(this);
 76    }
 77  43 return forBracedBodyOnly(that, items_result);
 78    }
 79   
 80    /** Make sure that every Exception in thrown is either in caught or in the list of what can be thrown from where we are.
 81    * Also make sure that every Exception that is declared to be thrown or caught is actually thrown.
 82    * Overrides the same method in BodyTypeChecker.
 83    * @param that The TryCatchStatement we are currently working with
 84    * @param caught_array The SymbolData[] of exceptions that are explicitely caught.
 85    * @param thrown The LinkedList of SymbolData of exceptions that are thrown. This will be modified.
 86    */
 87  11 protected void compareThrownAndCaught(TryCatchStatement that, SymbolData[] caught_array,
 88    LinkedList<Pair<SymbolData, JExpression>> thrown) {
 89  11 LinkedList<Pair<SymbolData, JExpression>> copyOfThrown = new LinkedList<Pair<SymbolData, JExpression>>();
 90  11 for (Pair<SymbolData, JExpression> p : thrown) {
 91  14 copyOfThrown.addLast(p);
 92    }
 93    //Make sure that every Exception in thrown is either caught or in the list of what can be thrown
 94  11 for (Pair<SymbolData, JExpression> p : copyOfThrown) {
 95  14 SymbolData sd = p.getFirst();
 96    // Iterate over the caught array and see if the current thrown exception is a subclass of one of the exceptions.
 97  14 for (SymbolData currCaughtSD : caught_array) {
 98  9 if (sd.isSubClassOf(currCaughtSD) || (!isUncaughtCheckedException(sd, new NullLiteral(SourceInfo.NONE)))) {
 99  5 thrown.remove(p);
 100    }
 101    }
 102    }
 103  11 makeSureCaughtStuffWasThrown(that, caught_array, copyOfThrown);
 104    }
 105   
 106    /**
 107    * Test the methods declared in the above class.
 108    */
 109    public static class TryCatchBodyTypeCheckerTest extends TestCase {
 110   
 111    private TryCatchBodyTypeChecker _tcbtc;
 112   
 113    private BodyData _bd1;
 114    private BodyData _bd2;
 115   
 116    private SymbolData _sd1;
 117    private SymbolData _sd2;
 118    private SymbolData _sd3;
 119    private SymbolData _sd4;
 120    private SymbolData _sd5;
 121    private SymbolData _sd6;
 122    private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 123    private ModifiersAndVisibility _protectedMav =
 124    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"});
 125    private ModifiersAndVisibility _privateMav =
 126    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"private"});
 127    private ModifiersAndVisibility _packageMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]);
 128    private ModifiersAndVisibility _abstractMav =
 129    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"abstract"});
 130    private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"});
 131   
 132   
 133  0 public TryCatchBodyTypeCheckerTest() { this(""); }
 134  3 public TryCatchBodyTypeCheckerTest(String name) { super(name); }
 135   
 136  3 public void setUp() {
 137  3 _sd1 = new SymbolData("i.like.monkey");
 138  3 _sd2 = new SymbolData("i.like.giraffe");
 139  3 _sd3 = new SymbolData("zebra");
 140  3 _sd4 = new SymbolData("u.like.emu");
 141  3 _sd5 = new SymbolData("");
 142  3 _sd6 = new SymbolData("cebu");
 143   
 144  3 _bd1 = new MethodData("monkey",
 145    _packageMav,
 146    new TypeParameter[0],
 147    _sd1,
 148    new VariableData[] { new VariableData("i", _publicMav, SymbolData.INT_TYPE, true, null), new VariableData(SymbolData.BOOLEAN_TYPE) },
 149    new String[0],
 150    _sd1,
 151    null); // no SourceInfo
 152  3 ((MethodData) _bd1).getParams()[0].setEnclosingData(_bd1);
 153  3 ((MethodData) _bd1).getParams()[1].setEnclosingData(_bd1);
 154   
 155  3 errors = new LinkedList<Pair<String, JExpressionIF>>();
 156  3 LanguageLevelConverter.symbolTable.clear();
 157  3 _bd1.addEnclosingData(_sd1);
 158  3 _bd1.addFinalVars(((MethodData)_bd1).getParams());
 159  3 _tcbtc = new TryCatchBodyTypeChecker(_bd1, new File(""), "", new LinkedList<String>(), new LinkedList<String>(), new LinkedList<VariableData>(), new LinkedList<Pair<SymbolData, JExpression>>());
 160  3 LanguageLevelConverter.OPT = new Options(JavaVersion.JAVA_5, EmptyIterable.<File>make());
 161  3 _tcbtc._importedPackages.addFirst("java.lang");
 162    }
 163   
 164   
 165  1 public void testCreateANewInstanceOfMe() {
 166    //make sure that the correct visitor is returned from createANewInstanceOfMe
 167  1 BodyTypeChecker btc = _tcbtc.createANewInstanceOfMe(_tcbtc._bodyData, _tcbtc._file, _tcbtc._package, _tcbtc._importedFiles, _tcbtc._importedPackages, _tcbtc._vars, _tcbtc._thrown);
 168  1 assertTrue("Should be an instance of ConstructorBodyTypeChecker", btc instanceof TryCatchBodyTypeChecker);
 169    }
 170   
 171  1 public void testForBracedBody() {
 172    //make sure it is okay to have a uncaught exception in a braced body
 173  1 BracedBody bb = new BracedBody(SourceInfo.NONE,
 174    new BodyItemI[] {
 175    new ThrowStatement(SourceInfo.NONE,
 176    new SimpleNamedClassInstantiation(SourceInfo.NONE,
 177    new ClassOrInterfaceType(SourceInfo.NONE,
 178    "java.util.prefs.BackingStoreException",
 179    new Type[0]),
 180    new ParenthesizedExpressionList(SourceInfo.NONE, new Expression[] {new StringLiteral(SourceInfo.NONE, "arg")})))});
 181   
 182  1 LanguageLevelVisitor llv =
 183    new LanguageLevelVisitor(new File(""),
 184    "",
 185    null, // enclosingClassName for top level traversal
 186    new LinkedList<String>(),
 187    new LinkedList<String>(),
 188    new HashSet<String>(),
 189    new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>(),
 190    new LinkedList<Command>());
 191  1 llv.errors = new LinkedList<Pair<String, JExpressionIF>>();
 192  1 llv._errorAdded=false;
 193  1 LanguageLevelConverter.symbolTable.clear();
 194  1 LanguageLevelConverter._newSDs.clear();
 195  1 LanguageLevelConverter.loadSymbolTable();
 196  1 llv.continuations = new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>();
 197  1 llv.visitedFiles = new LinkedList<Pair<LanguageLevelVisitor, edu.rice.cs.javalanglevels.tree.SourceFile>>();
 198    // llv._hierarchy = new Hashtable<String, TypeDefBase>();
 199  1 llv._classesInThisFile = new HashSet<String>();
 200   
 201  1 SymbolData o = symbolTable.get("java.lang.Object");
 202    // o.setIsContinuation(false);
 203    // o.setMav(_publicMav);
 204    // symbolTable.put("java.lang.Object", o);
 205   
 206  1 SymbolData string = new SymbolData("java.lang.String");
 207  1 string.setIsContinuation(false);
 208  1 string.setMav(_publicMav);
 209  1 string.setSuperClass(o); // a white lie for this test
 210  1 symbolTable.put("java.lang.String", string);
 211   
 212  1 SymbolData e = llv.getSymbolData("java.util.prefs.BackingStoreException", SourceInfo.NONE, true);
 213   
 214  1 bb.visit(_tcbtc);
 215  1 assertEquals("There should be no errors because it's ok to have uncaught exceptions in this visitor",
 216    0,
 217    errors.size());
 218    }
 219   
 220  1 public void testCompareThrownAndCaught() {
 221  1 BracedBody emptyBody = new BracedBody(SourceInfo.NONE, new BodyItemI[0]);
 222  1 Block b = new Block(SourceInfo.NONE, emptyBody);
 223   
 224  1 PrimitiveType intt = new PrimitiveType(SourceInfo.NONE, "int");
 225  1 UninitializedVariableDeclarator uvd =
 226    new UninitializedVariableDeclarator(SourceInfo.NONE, intt, new Word(SourceInfo.NONE, "i"));
 227  1 FormalParameter param =
 228    new FormalParameter(SourceInfo.NONE,
 229    new UninitializedVariableDeclarator(SourceInfo.NONE, intt, new Word(SourceInfo.NONE, "j")),
 230    false);
 231   
 232  1 NormalTryCatchStatement ntcs = new NormalTryCatchStatement(SourceInfo.NONE, b, new CatchBlock[] {new CatchBlock(SourceInfo.NONE, param, b)});
 233   
 234  1 SymbolData javaLangThrowable = _tcbtc.getSymbolData("java.lang.Throwable", ntcs, false, true);
 235  1 _tcbtc.symbolTable.put("java.lang.Throwable", javaLangThrowable);
 236  1 SymbolData exception = new SymbolData("my.crazy.exception");
 237  1 exception.setSuperClass(javaLangThrowable);
 238  1 SymbolData exception2 = new SymbolData("A&M.beat.Rice.in.BaseballException");
 239  1 exception2.setSuperClass(javaLangThrowable);
 240  1 SymbolData exception3 = new SymbolData("aegilha");
 241  1 exception3.setSuperClass(exception2);
 242  1 SymbolData[] caught_array = new SymbolData[] { exception, exception2 };
 243  1 LinkedList<Pair<SymbolData, JExpression>> thrown = new LinkedList<Pair<SymbolData, JExpression>>();
 244  1 thrown.addLast(new Pair<SymbolData, JExpression>(exception, ntcs));
 245  1 thrown.addLast(new Pair<SymbolData, JExpression>(exception2, ntcs));
 246  1 thrown.addLast(new Pair<SymbolData, JExpression>(exception3, ntcs));
 247   
 248  1 _tcbtc.compareThrownAndCaught(ntcs, caught_array, thrown);
 249    // System.err.println("thrown = " + thrown);
 250  1 assertTrue("Thrown should have no elements", thrown.isEmpty());
 251   
 252   
 253  1 _tcbtc.compareThrownAndCaught(ntcs, new SymbolData[] {exception2}, thrown);
 254  1 assertEquals("There should be one error", 1, errors.size());
 255  1 assertEquals("The error message should be correct", "The exception A&M.beat.Rice.in.BaseballException is never thrown in the body of the corresponding try block", errors.get(0).getFirst());
 256   
 257    }
 258    }
 259    }