Coverage Report - net.sf.cotta.test.AssertionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AssertionFactory
32%
8/25
0%
0/4
1.158
 
 1  
 package net.sf.cotta.test;
 2  
 
 3  
 import net.sf.cotta.test.assertion.*;
 4  
 
 5  
 import java.util.ArrayList;
 6  
 import java.util.Iterator;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 
 10  
 /**
 11  
  * The factory class that creates the assertion objects with the given value.  To use this class,
 12  
  * add the following static declaration in the base class of your test classes
 13  
  * <p/>
 14  
  * <code>public static AssertionFactory ensure = new AssertionFactory();</code>
 15  
  * <p/>
 16  
  * Now, in all your test classes that extend this base class, you can write your assertions
 17  
  * in the form of like
 18  
  * <p/>
 19  
  * <code>ensure.that(actual).eq(expected)</code>
 20  
  * <p/>
 21  
  * You can expand this assertion API by extending AssertionFactory class as well as the assertion classes
 22  
  */
 23  2
 public class AssertionFactory {
 24  
   /**
 25  
    * Creates the assertion for the given int value
 26  
    *
 27  
    * @param value int value to assert
 28  
    * @return IntegerAssert instance
 29  
    */
 30  
   public IntegerAssert that(int value) {
 31  0
     return new IntegerAssert(value);
 32  
   }
 33  
 
 34  
   /**
 35  
    * Creates the assertion for the given object
 36  
    *
 37  
    * @param value object value to assert
 38  
    * @return ObjectAssert instance
 39  
    */
 40  
   public ObjectAssert that(Object value) {
 41  0
     return new ObjectAssert(value);
 42  
   }
 43  
 
 44  
   /**
 45  
    * Creates the assertion for the given code block
 46  
    *
 47  
    * @param block CodeBlock instance to assert
 48  
    * @return CodeBlockAssert instance
 49  
    */
 50  
   public CodeBlockAssertion that(CodeBlock block) {
 51  2
     return new CodeBlockAssertion(block);
 52  
   }
 53  
 
 54  
   /**
 55  
    * Creates the assertion for the given code block
 56  
    *
 57  
    * @param block CodeBlock instance to assert
 58  
    * @return CodeBlockAssert instance
 59  
    */
 60  
   public CodeBlockAssertion code(CodeBlock block) {
 61  1
     return that(block);
 62  
   }
 63  
 
 64  
   /**
 65  
    * Creates the ListAssert instance for the given array
 66  
    *
 67  
    * @param value array value to assert
 68  
    * @param <T>   base type of the array
 69  
    * @return ListAssert instance
 70  
    */
 71  
   public <T> ListAssert<T> that(T[] value) {
 72  4
     return new ListAssert<T>(value);
 73  
   }
 74  
 
 75  
   /**
 76  
    * Creates the ListAssert instance for the given list
 77  
    *
 78  
    * @param value List value to assert
 79  
    * @param <T>   element type of the list
 80  
    * @return ListAssert instance
 81  
    */
 82  
   public <T> ListAssert<T> that(List<T> value) {
 83  1
     return new ListAssert<T>(value);
 84  
   }
 85  
 
 86  
   /**
 87  
    * Creates the ListAssert instance for the given iterable
 88  
    * instance after converting it to a list
 89  
    *
 90  
    * @param value iterable to assert
 91  
    * @param <T>   elemet type of the iterator
 92  
    * @return ListAssert instance
 93  
    */
 94  
   public <T> ListAssert<T> that(Iterable<T> value) {
 95  0
     return that(value.iterator());
 96  
   }
 97  
 
 98  
   /**
 99  
    * Creates the ListAssert instance for the given iterator
 100  
    * instance after converting it to a list
 101  
    *
 102  
    * @param value iterable to assert
 103  
    * @param <T>   elemet type of the iterator
 104  
    * @return ListAssert instance
 105  
    */
 106  
   public <T> ListAssert<T> that(Iterator<T> value) {
 107  0
     return new ListAssert<T>(toList(value));
 108  
   }
 109  
 
 110  
   private <T> List<T> toList(Iterator<T> value) {
 111  0
     if (value == null) {
 112  0
       return null;
 113  
     }
 114  0
     List<T> list = new ArrayList<T>();
 115  0
     while (value.hasNext()) {
 116  0
       list.add(value.next());
 117  
     }
 118  0
     return list;
 119  
   }
 120  
 
 121  
   /**
 122  
    * Creates the ByteListAssert instance for the given byte array
 123  
    *
 124  
    * @param value the byte array for assertion
 125  
    * @return ByteListAssert instance
 126  
    */
 127  
   public ByteListAssert that(byte[] value) {
 128  1
     return new ByteListAssert(value);
 129  
   }
 130  
 
 131  
   /**
 132  
    * Creates the StringAssert instance for the given byte array
 133  
    * by coverting the byte array to String using default encoding
 134  
    *
 135  
    * @param value the string value to assert
 136  
    * @return StringAssert instance
 137  
    * @see StringAssert#StringAssert(byte[])
 138  
    */
 139  
   public StringAssert string(byte[] value) {
 140  1
     return new StringAssert(value);
 141  
   }
 142  
 
 143  
   /**
 144  
    * Creates the StringAssert instance for the given string
 145  
    *
 146  
    * @param value the string value to assert
 147  
    * @return StringAssert instance
 148  
    */
 149  
   public StringAssert that(String value) {
 150  0
     return new StringAssert(value);
 151  
   }
 152  
 
 153  
 
 154  
   /**
 155  
    * Creates the LongAssert instance for the given long value
 156  
    *
 157  
    * @param value long value to assert
 158  
    * @return LongAssert instance
 159  
    */
 160  
   public LongAssert that(long value) {
 161  0
     return new LongAssert(value);
 162  
   }
 163  
 
 164  
   /**
 165  
    * Creates the ExceptionAssert instance for the given exception
 166  
    *
 167  
    * @param value Exception value to assert
 168  
    * @return ExceptionAssert instance
 169  
    */
 170  
   public ExceptionAssert that(Exception value) {
 171  0
     return new ExceptionAssert(value);
 172  
   }
 173  
 
 174  
   /**
 175  
    * Creates the BooleanAssert instance for the given boolean value
 176  
    *
 177  
    * @param value boolean value to assert
 178  
    * @return BooleanAssert instance
 179  
    */
 180  
   public BooleanAssert that(boolean value) {
 181  0
     return new BooleanAssert(value);
 182  
   }
 183  
 
 184  
   /**
 185  
    * Creates the CharAssert instance for the given char value
 186  
    *
 187  
    * @param value char value to assert
 188  
    * @return CharAssert instance
 189  
    */
 190  
   public CharAssert that(char value) {
 191  0
     return new CharAssert(value);
 192  
   }
 193  
 
 194  
   /**
 195  
    * Creates the CharAssert instance for the given int value
 196  
    * after converting it to char
 197  
    *
 198  
    * @param value int value to assert
 199  
    * @return CharAssert instance
 200  
    */
 201  
   public CharAssert character(int value) {
 202  0
     return new CharAssert((char) value);
 203  
   }
 204  
 
 205  
   /**
 206  
    * Creates the SetAssert instance of the given list
 207  
    *
 208  
    * @param value list to assert
 209  
    * @param <T>   type of the list elements
 210  
    * @return SetAssert instance
 211  
    */
 212  
   public <T> SetAssert<T> set(List<T> value) {
 213  1
     return new SetAssert<T>(value);
 214  
   }
 215  
 
 216  
   /**
 217  
    * Creates the MapAssert instance of the given map
 218  
    *
 219  
    * @param value map to assert
 220  
    * @param <K>   type of the keys in the map
 221  
    * @param <V>   type of the values in the map
 222  
    * @return MapAssert instance
 223  
    */
 224  
   public <K, V> MapAssert<K, V> that(Map<K, V> value) {
 225  0
     return new MapAssert<K, V>(value);
 226  
   }
 227  
 }