Coverage Report - org.webslinger.util.GenericTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericTestCase
87%
48/55
100%
12/12
0
 
 1  
 package org.webslinger.util;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Arrays;
 5  
 import java.util.Collection;
 6  
 import java.util.HashMap;
 7  
 import java.util.HashSet;
 8  
 import java.util.Iterator;
 9  
 import java.util.LinkedHashMap;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 
 13  
 import junit.framework.TestCase;
 14  
 
 15  
 import org.webslinger.collections.ArrayUtil;
 16  
 
 17  
 public abstract class GenericTestCase extends TestCase {
 18  
     protected GenericTestCase(String name) {
 19  60
         super(name);
 20  60
     }
 21  
 
 22  
     public static <T> void assertEquals(List<T> wanted, Object got) {
 23  0
         assertEquals(null, wanted, got);
 24  0
     }
 25  
 
 26  
     public static <T> void assertEquals(String msg, List<T> wanted, Object got) {
 27  11
         msg = msg == null ? "" : msg + ' ';
 28  11
         assertNotNull(msg + "expected a value", got);
 29  11
         if (got.getClass().isArray()) got = ArrayUtil.asList(got);
 30  11
         if (!(got instanceof Collection)) fail(msg + "expected a collection, got a " + got.getClass());
 31  11
         Iterator<T> leftIt = wanted.iterator();
 32  11
         Iterator rightIt = ((Collection) got).iterator();
 33  11
         int i = 0;
 34  31
         while (leftIt.hasNext() && rightIt.hasNext()) {
 35  20
             T left = leftIt.next();
 36  20
             Object right = rightIt.next();
 37  20
             assertEquals(msg + "item " + i, left, right);
 38  20
         }
 39  11
         assertFalse(msg + "not enough items", leftIt.hasNext());
 40  11
         assertFalse(msg + "too many items", rightIt.hasNext());
 41  11
     }
 42  
 
 43  
     public static <T> void assertEquals(Map<T, ?> wanted, Object got) {
 44  1
         assertEquals(null, wanted, got);
 45  1
     }
 46  
 
 47  
     public static <T> void assertEquals(String msg, Map<T, ?> wanted, Object got) {
 48  7
         msg = msg == null ? "" : msg + ' ';
 49  7
         assertNotNull(msg + "expected a value", got);
 50  7
         if (!(got instanceof Map)) fail(msg + "expected a map");
 51  7
         Map<?, ?> gotMap = (Map) got;
 52  7
         HashSet<T> leftKeys = new HashSet<T>(wanted.keySet());
 53  7
         HashSet<Object> rightKeys = new HashSet<Object>(gotMap.keySet());
 54  7
         for(T key: leftKeys) {
 55  16
             assertTrue(msg + "got key(" + key + ")", rightKeys.remove(key));
 56  16
             assertEquals(msg + "key(" + key + ") value", wanted.get(key), gotMap.get(key));
 57  
         }
 58  7
         assertTrue(msg + "extra entries", rightKeys.isEmpty());
 59  7
     }
 60  
 
 61  
     public static void assertEquals(String msg, String wanted, String got) {
 62  356
         TestCase.assertEquals(msg, wanted, got);
 63  355
     }
 64  
 
 65  
     public static void assertEquals(Object wanted, Object got) {
 66  0
         assertEquals(null, wanted, got);
 67  0
     }
 68  
 
 69  
     public static void assertEquals(String msg, Object wanted, Object got) {
 70  71
         if (wanted instanceof List) {
 71  8
             assertEquals(msg, (List<?>) wanted, got);
 72  63
         } else if (wanted instanceof Map) {
 73  4
             assertEquals(msg, (Map<?, ?>) wanted, got);
 74  59
         } else if (wanted == null) {
 75  1
             TestCase.assertEquals(msg, wanted, got);
 76  58
         } else if (wanted.getClass().isArray()) {
 77  2
             assertEquals(msg, ArrayUtil.asList(wanted), got);
 78  
         } else {
 79  56
             TestCase.assertEquals(msg, wanted, got);
 80  
         }
 81  71
     }
 82  
 
 83  
     protected static <T> List<T> list(T value) {
 84  0
         ArrayList<T> list = new ArrayList<T>(1);
 85  0
         list.add(value);
 86  0
         return list;
 87  
     }
 88  
 
 89  
     protected static <T> List<T> list(T[] list) {
 90  8
         return Arrays.asList(list);
 91  
     }
 92  
 
 93  
     protected static Map<?, ?> map(Object[] list) {
 94  11
         assertEquals("list has even number of elements", 0, list.length % 2);
 95  11
         Map<Object, Object> map = new LinkedHashMap<Object, Object>();
 96  39
         for (int i = 0; i < list.length; i += 2) {
 97  28
             map.put(list[i], list[i + 1]);
 98  
         }
 99  11
         return map;
 100  
     }
 101  
 }