1 16 package net.sf.dozer.util.mapping.stats; 17 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 import net.sf.dozer.util.mapping.DozerTestBase; 22 import net.sf.dozer.util.mapping.exception.NotFoundException; 23 24 27 public class StatisticManagerTest extends DozerTestBase { 28 private StatisticsManager statMgr; 29 30 protected void setUp() throws Exception { 31 super.setUp(); 32 statMgr = new StatisticsManager(); 33 statMgr.setStatisticsEnabled(true); 34 } 35 36 public void testAddAndGetStatistic() { 37 String type = getRandomString(); 38 Statistic stat = new Statistic(type); 39 statMgr.addStatistic(stat); 40 41 assertEquals("invalid stat size", 1, statMgr.getStatistics().size()); 42 assertEquals("invalid stat found", stat, statMgr.getStatistic(type)); 43 assertTrue("invalid stat ref found", stat == statMgr.getStatistic(type)); 44 } 45 46 public void testSetStatisticsEnabled() { 47 boolean value = true; 48 if (statMgr.isStatisticsEnabled()) { 49 value = false; 50 } 51 statMgr.setStatisticsEnabled(value); 52 assertEquals("invalid stats enabled value", value, statMgr.isStatisticsEnabled()); 53 } 54 55 public void testClearAll() { 56 String type = getRandomString(); 57 statMgr.addStatistic(new Statistic(type)); 58 statMgr.addStatistic(new Statistic(type+"2")); 59 assertEquals("invalid initial stat size", 2, statMgr.getStatistics().size()); 60 statMgr.clearAll(); 61 assertEquals("invalid stat size", 0, statMgr.getStatistics().size()); 62 } 63 64 public void testGetStatiticNotFound() { 65 try { 66 statMgr.getStatistic(getRandomString()); 67 fail("should have thrown not found exception"); 68 } catch(NotFoundException e) { 69 } 70 } 71 72 public void testGetStatisticTypes() { 73 String type = getRandomString(); 74 String type2 = type + "-2"; 75 statMgr.addStatistic(new Statistic(type)); 76 statMgr.addStatistic(new Statistic(type2)); 77 78 Set expected = new HashSet (); 79 expected.add(type); 80 expected.add(type2); 81 82 assertEquals("invalid stat types found", expected, statMgr.getStatisticTypes()); 83 } 84 85 public void testIncrementMissingParams() { 86 try { 87 statMgr.increment(null, "test", 1); 88 fail("missing type should have thrown exception"); 89 } catch (IllegalArgumentException e) { 90 } 91 92 try { 93 statMgr.increment("test2", null, 1); 94 fail("missing entry key should have thrown exception"); 95 } catch (IllegalArgumentException e) { 96 } 97 } 98 99 public void testGetStatisticValue() { 100 String type = getRandomString(); 101 Statistic stat = new Statistic(type); 102 String entryKey = getRandomString(); 103 stat.addEntry(new StatisticEntry(entryKey)); 104 105 statMgr.addStatistic(stat); 106 statMgr.increment(type, entryKey, 100); 107 108 assertEquals("invalid entry value", 100, statMgr.getStatisticValue(type)); 109 } 110 111 public void testGetStatisticValueException() { 112 String type = getRandomString(); 113 Statistic stat = new Statistic(type); 114 115 stat.addEntry(new StatisticEntry(getRandomString())); 116 stat.addEntry(new StatisticEntry(getRandomString())); 117 statMgr.addStatistic(stat); 118 119 try { 120 statMgr.getStatisticValue(type); 121 fail("trying to get stat value for type that has more than 1 entry should throw exception"); 122 } catch (IllegalArgumentException e) { 123 } 124 } 125 126 public void testAddDuplicateStatistic() { 127 String type = getRandomString(); 128 statMgr.addStatistic(new Statistic(type)); 129 130 try { 131 statMgr.addStatistic(new Statistic(type)); 132 fail("add duplicate stat should have thrown exception"); 133 } catch (IllegalArgumentException e) { 134 } 135 } 136 137 public void testIncrementUnknownTypeAndKey() { 138 String type = getRandomString(); 139 String entryKey = getRandomString(); 140 long incrementValue = 130; 141 statMgr.increment(type, entryKey, incrementValue); 142 143 assertEquals("invalid stat entry value", incrementValue, statMgr.getStatisticValue(type)); 144 } 145 146 } 147 | Popular Tags |