Coverage report

  %line %branch
net.sf.infrared.agent.TreeBuilder
82% 
87% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  * 
 17  
  * 
 18  
  * Original Author: kamal.govindraj (Tavant Technologies) 
 19  
  * Contributor(s):  binil.thomas
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.agent;
 23  
 
 24  
 import net.sf.infrared.base.model.ExecutionTimer;
 25  
 import net.sf.infrared.base.util.LoggingFactory;
 26  
 import net.sf.infrared.base.util.Tree;
 27  
 import net.sf.infrared.base.util.TreeNode;
 28  
 
 29  
 import org.apache.log4j.Logger;
 30  
 
 31  
 /**
 32  
  * 
 33  
  * @author kamal.govindraj
 34  
  * @author binil.thomas
 35  
  */
 36  1
 public class TreeBuilder {
 37  2
     private static final Logger log = LoggingFactory.getLogger(TreeBuilder.class);
 38  
 
 39  1
     private Tree tree = new Tree();
 40  
 
 41  
     // This treeNode represents the current operation in the operation tree.
 42  1
     private TreeNode currNode = null;
 43  
 
 44  1
     private long pruneThreshold = -1;
 45  
 
 46  
     public void begin(ExecutionTimer timer) {
 47  6
         TreeNode node = TreeNode.createTreeNode(timer);
 48  6
         if (currNode == null) {
 49  1
             tree.setRoot(node);
 50  1
             if (log.isDebugEnabled()) {
 51  0
                 log.debug(this + " - Added node " + timer + " as root");
 52  
             }
 53  
         } else {
 54  5
             currNode.addChild(node);
 55  
         }
 56  6
         currNode = node;
 57  6
     }
 58  
 
 59  
     public boolean end(ExecutionTimer timer) {
 60  4
         if (isCallTraceFaulty(timer)) {
 61  1
             log.error(this + " - Call trace is faulty");
 62  1
             return false;
 63  
         }
 64  
 
 65  3
         TreeNode childNode = currNode;
 66  3
         currNode = currNode.getParent();
 67  
 
 68  
         // Method time was below the pruneBelowTime, hence we need to remove it
 69  3
         if (timer.getInclusiveTime() <= getPruneBelowTime()) {            
 70  2
             removeFromCallTrace(childNode);
 71  2
             if (log.isDebugEnabled()) {
 72  0
                 log.debug("Removed execution " + timer.getContext() + " from tree " +
 73  
                         " because the time (" + timer.getInclusiveTime() + 
 74  
                         ") <= prune threshold (" + getPruneBelowTime() + ")");
 75  
             }
 76  
         }
 77  
 
 78  3
         return true;
 79  
     }
 80  
 
 81  
     public Tree reset() {
 82  1
         if (log.isDebugEnabled()) {
 83  0
             log.debug(this + " - Resetting tree");
 84  
         }
 85  1
         Tree oldTree = tree;
 86  1
         tree = new Tree();
 87  1
         currNode = null;
 88  1
         return oldTree;
 89  
     }
 90  
 
 91  
     public String toString() {
 92  1
         return "TreeBuilder for thread " + Thread.currentThread();
 93  
     }
 94  
 
 95  
     long getPruneBelowTime() {
 96  0
         return pruneThreshold;
 97  
     }
 98  
 
 99  
     void setPruneBelowTime(long time) {
 100  0
         pruneThreshold = time;
 101  0
     }
 102  
 
 103  
     private void removeFromCallTrace(TreeNode childNode) {
 104  2
         if (currNode != null) {
 105  2
             currNode.removeChild(childNode);
 106  
         }
 107  2
     }
 108  
 
 109  
     private boolean isCallTraceFaulty(ExecutionTimer currentExec) {
 110  4
         if (currNode == null) {
 111  0
             return true;
 112  
         }
 113  
 
 114  4
         ExecutionTimer expectedExec = (ExecutionTimer) currNode.getValue();
 115  
 
 116  
         // if (! currentExec.getEvent().equals( expectedExec.getEvent() )) {
 117  4
         if (currentExec != expectedExec) {
 118  1
             if (log.isDebugEnabled()) {
 119  0
                 log.debug(this + " - Mismatch on stack for Thread "
 120  
                         + Thread.currentThread().getName() + ". Expected "
 121  
                         + expectedExec.getContext() + ", encountered " + currentExec.getContext());
 122  
             }
 123  1
             return true;
 124  
         }
 125  3
         return false;
 126  
     }
 127  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.