KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > stats > StatisticManagerTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 package net.sf.dozer.util.mapping.stats;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import net.sf.dozer.util.mapping.DozerTestBase;
22 import net.sf.dozer.util.mapping.exception.NotFoundException;
23
24 /**
25  * @author tierney.matt
26  */

27 public class StatisticManagerTest extends DozerTestBase {
28   private StatisticsManager statMgr;
29   
30   protected void setUp() throws Exception JavaDoc {
31     super.setUp();
32       statMgr = new StatisticsManager();
33       statMgr.setStatisticsEnabled(true);
34   }
35   
36   public void testAddAndGetStatistic() {
37     String JavaDoc 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 JavaDoc 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 JavaDoc type = getRandomString();
74     String JavaDoc type2 = type + "-2";
75     statMgr.addStatistic(new Statistic(type));
76     statMgr.addStatistic(new Statistic(type2));
77     
78     Set JavaDoc expected = new HashSet JavaDoc();
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 JavaDoc e) {
90     }
91     
92     try {
93       statMgr.increment("test2", null, 1);
94       fail("missing entry key should have thrown exception");
95     } catch (IllegalArgumentException JavaDoc e) {
96     }
97   }
98   
99   public void testGetStatisticValue() {
100     String JavaDoc type = getRandomString();
101     Statistic stat = new Statistic(type);
102     String JavaDoc 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 JavaDoc 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 JavaDoc e) {
123     }
124   }
125   
126   public void testAddDuplicateStatistic() {
127     String JavaDoc 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 JavaDoc e) {
134     }
135   }
136   
137   public void testIncrementUnknownTypeAndKey() {
138     String JavaDoc type = getRandomString();
139     String JavaDoc 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