Coverage Report - com.jeantessier.dependency.TestCycle
 
Classes in this File Line Coverage Branch Coverage Complexity
TestCycle
98%
131/133
50%
6/12
1.077
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *  
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *  
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *  
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *  
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *  
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.dependency;
 34  
 
 35  
 import java.util.*;
 36  
 
 37  
 import junit.framework.*;
 38  
 
 39  12
 public class TestCycle extends TestCase {
 40  
     private Node a;
 41  
     private Node b;
 42  
     private Node c;
 43  
     private Node d;
 44  
     private Node e;
 45  
 
 46  
     protected void setUp() throws Exception {
 47  12
         NodeFactory factory = new NodeFactory();
 48  
 
 49  12
         a = factory.createPackage("a");
 50  12
         b = factory.createPackage("b");
 51  12
         c = factory.createPackage("c");
 52  12
         d = factory.createPackage("d");
 53  12
         e = factory.createPackage("e");
 54  12
     }
 55  
 
 56  
     public void testConstructEmptyCycle() {
 57  
         try {
 58  1
             new Cycle(new ArrayList<Node>());
 59  0
             fail("Constructed empty cycle");
 60  1
         } catch (Exception ex) {
 61  
             // expected
 62  0
         }
 63  1
     }
 64  
 
 65  
     public void testConstructLength1Cycle() {
 66  1
         List<Node> path = new ArrayList<Node>();
 67  1
         path.add(a);
 68  1
         Cycle cycle = new Cycle(path);
 69  
 
 70  1
         assertEquals("length", 1, cycle.getLength());
 71  1
         assertEquals("a", a, cycle.getPath().iterator().next());
 72  1
     }
 73  
 
 74  
     public void testEquals_Identical() {
 75  1
         List<Node> path1 = new ArrayList<Node>();
 76  1
         path1.add(a);
 77  1
         path1.add(b);
 78  1
         Cycle cycle1 = new Cycle(path1);
 79  
 
 80  1
         List<Node> path2 = new ArrayList<Node>();
 81  1
         path2.add(a);
 82  1
         path2.add(b);
 83  1
         Cycle cycle2 = new Cycle(path2);
 84  
 
 85  1
         assertTrue(cycle1.equals(cycle2));
 86  1
         assertTrue(cycle2.equals(cycle1));
 87  1
     }
 88  
 
 89  
     public void testEquals_Reversed() {
 90  1
         List<Node> path1 = new ArrayList<Node>();
 91  1
         path1.add(a);
 92  1
         path1.add(b);
 93  1
         Cycle cycle1 = new Cycle(path1);
 94  
 
 95  1
         List<Node> path2 = new ArrayList<Node>();
 96  1
         path2.add(b);
 97  1
         path2.add(a);
 98  1
         Cycle cycle2 = new Cycle(path2);
 99  
 
 100  1
         assertTrue(cycle1.equals(cycle2));
 101  1
         assertTrue(cycle2.equals(cycle1));
 102  1
     }
 103  
 
 104  
     public void testEquals_SameLength() {
 105  1
         List<Node> path1 = new ArrayList<Node>();
 106  1
         path1.add(a);
 107  1
         path1.add(b);
 108  1
         Cycle cycle1 = new Cycle(path1);
 109  
 
 110  1
         List<Node> path2 = new ArrayList<Node>();
 111  1
         path2.add(c);
 112  1
         path2.add(d);
 113  1
         Cycle cycle2 = new Cycle(path2);
 114  
 
 115  1
         assertFalse(cycle1.equals(cycle2));
 116  1
         assertFalse(cycle2.equals(cycle1));
 117  1
     }
 118  
 
 119  
     public void testEquals_DifferentLength() {
 120  1
         List<Node> path1 = new ArrayList<Node>();
 121  1
         path1.add(a);
 122  1
         path1.add(b);
 123  1
         Cycle cycle1 = new Cycle(path1);
 124  
 
 125  1
         List<Node> path2 = new ArrayList<Node>();
 126  1
         path2.add(c);
 127  1
         path2.add(d);
 128  1
         path2.add(e);
 129  1
         Cycle cycle2 = new Cycle(path2);
 130  
 
 131  1
         assertFalse(cycle1.equals(cycle2));
 132  1
         assertFalse(cycle2.equals(cycle1));
 133  1
     }
 134  
 
 135  
     public void testEquals_LengthTrumpsContent() {
 136  1
         List<Node> path1 = new ArrayList<Node>();
 137  1
         path1.add(a);
 138  1
         path1.add(b);
 139  1
         path1.add(c);
 140  1
         Cycle cycle1 = new Cycle(path1);
 141  
 
 142  1
         List<Node> path2 = new ArrayList<Node>();
 143  1
         path2.add(d);
 144  1
         path2.add(e);
 145  1
         Cycle cycle2 = new Cycle(path2);
 146  
 
 147  1
         assertFalse(cycle1.equals(cycle2));
 148  1
         assertFalse(cycle2.equals(cycle1));
 149  1
     }
 150  
 
 151  
     public void testCompareTo_Identical() {
 152  1
         List<Node> path1 = new ArrayList<Node>();
 153  1
         path1.add(a);
 154  1
         path1.add(b);
 155  1
         Cycle cycle1 = new Cycle(path1);
 156  
 
 157  1
         List<Node> path2 = new ArrayList<Node>();
 158  1
         path2.add(a);
 159  1
         path2.add(b);
 160  1
         Cycle cycle2 = new Cycle(path2);
 161  
 
 162  1
         assertEquals(0, cycle1.compareTo(cycle2));
 163  1
         assertEquals(0, cycle2.compareTo(cycle1));
 164  1
     }
 165  
 
 166  
     public void testCompareTo_Reversed() {
 167  1
         List<Node> path1 = new ArrayList<Node>();
 168  1
         path1.add(a);
 169  1
         path1.add(b);
 170  1
         Cycle cycle1 = new Cycle(path1);
 171  
 
 172  1
         List<Node> path2 = new ArrayList<Node>();
 173  1
         path2.add(b);
 174  1
         path2.add(a);
 175  1
         Cycle cycle2 = new Cycle(path2);
 176  
 
 177  1
         assertEquals(0, cycle1.compareTo(cycle2));
 178  1
         assertEquals(0, cycle2.compareTo(cycle1));
 179  1
     }
 180  
 
 181  
     public void testCompareTo_SameLength() {
 182  1
         List<Node> path1 = new ArrayList<Node>();
 183  1
         path1.add(a);
 184  1
         path1.add(b);
 185  1
         Cycle cycle1 = new Cycle(path1);
 186  
 
 187  1
         List<Node> path2 = new ArrayList<Node>();
 188  1
         path2.add(c);
 189  1
         path2.add(d);
 190  1
         Cycle cycle2 = new Cycle(path2);
 191  
 
 192  1
         assertTrue(cycle1.compareTo(cycle2) < 0);
 193  1
         assertTrue(cycle2.compareTo(cycle1) > 0);
 194  1
     }
 195  
 
 196  
     public void testCompareTo_DifferentLength() {
 197  1
         List<Node> path1 = new ArrayList<Node>();
 198  1
         path1.add(a);
 199  1
         path1.add(b);
 200  1
         Cycle cycle1 = new Cycle(path1);
 201  
 
 202  1
         List<Node> path2 = new ArrayList<Node>();
 203  1
         path2.add(c);
 204  1
         path2.add(d);
 205  1
         path2.add(e);
 206  1
         Cycle cycle2 = new Cycle(path2);
 207  
 
 208  1
         assertTrue(cycle1.compareTo(cycle2) < 0);
 209  1
         assertTrue(cycle2.compareTo(cycle1) > 0);
 210  1
     }
 211  
 
 212  
     public void testCompareTo_LengthTrumpsContent() {
 213  1
         List<Node> path1 = new ArrayList<Node>();
 214  1
         path1.add(a);
 215  1
         path1.add(b);
 216  1
         path1.add(c);
 217  1
         Cycle cycle1 = new Cycle(path1);
 218  
 
 219  1
         List<Node> path2 = new ArrayList<Node>();
 220  1
         path2.add(d);
 221  1
         path2.add(e);
 222  1
         Cycle cycle2 = new Cycle(path2);
 223  
 
 224  1
         assertTrue(cycle1.compareTo(cycle2) > 0);
 225  1
         assertTrue(cycle2.compareTo(cycle1) < 0);
 226  1
     }
 227  
 }