1 17 18 package org.sape.carbon.services.instrumentation.statistics.test; 19 20 21 import java.util.Random ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import org.sape.carbon.core.component.Lookup; 26 import org.sape.carbon.services.instrumentation.statistics.StatisticsInterceptor; 27 import org.sape.carbon.services.instrumentation.statistics.DefaultStatisticsInterceptor; 28 29 import junit.extensions.ActiveTestSuite; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 43 public class DefaultStatisticsInterceptorTest extends TestCase { 44 45 private static int THREAD_COUNT = 50; 46 47 private static int ITERATION_COUNT = 1000; 48 49 private static final String TEST_COMPONENT_NAME = 50 "/stats/TestStatisticsComponent"; 51 52 53 public DefaultStatisticsInterceptorTest(String name) { 54 super(name); 55 } 56 57 58 public void testStatisticsLog() { 59 TestStatisticsComponent component = 60 (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 61 62 ((StatisticsInterceptor) component).logStats(); 63 } 64 65 public void testStatisticsReset() { 66 TestStatisticsComponent component = 67 (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 68 69 ((StatisticsInterceptor) component).resetStats(); 70 } 71 72 public void testStatisticsInThreads() { 73 TestStatisticsComponent component = 74 (TestStatisticsComponent) 75 Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 76 77 for (int i = 0; i < ITERATION_COUNT; i++) { 78 component.succeed(); 79 } 80 for (int i = 0; i < ITERATION_COUNT; i++) { 81 try { 82 component.fail(); 83 } catch (UnsupportedOperationException uoe) { } 84 } 85 ((StatisticsInterceptor) component).logStats(); 86 } 87 88 public void testCallCount() { 89 TestStatisticsComponent component = 90 (TestStatisticsComponent) 91 Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 92 93 long calls = ((StatisticsInterceptor) component).getCallCounts(); 94 assertEquals("Total call count was not as expected", 95 calls, 2 * (THREAD_COUNT * ITERATION_COUNT)); 96 } 97 98 public void testStatisticsInThreadsWithFailures() { 99 TestStatisticsComponent TestStatisticsComponent = 100 (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 101 Random rand = new Random (); 102 for (int i = 0; i < ITERATION_COUNT; i++) { 103 try { 104 TestStatisticsComponent.fail(); 105 } catch (Exception e) { 106 } 108 } 109 110 ((StatisticsInterceptor) TestStatisticsComponent).logStats(); 111 } 112 113 public void testFailureCount() { 114 TestStatisticsComponent TestStatisticsComponent = 115 (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 116 Collection methodStats = ((StatisticsInterceptor)TestStatisticsComponent).getMethodStats(); 117 for (Iterator iterator = methodStats.iterator(); iterator.hasNext();) { 118 DefaultStatisticsInterceptor.MethodStats stat = (DefaultStatisticsInterceptor.MethodStats) iterator.next(); 119 if (stat.getMethod().getName().equals("fail")) { 120 long failFailures = stat.getFailures(); 121 assertEquals("The number of failures counted does not equal the number expected", 122 failFailures, THREAD_COUNT * ITERATION_COUNT); 123 } 124 } 125 } 126 127 131 public static Test suite() { 132 TestSuite masterSuite = new TestSuite("DefaultStatisticsInterceptorTest"); 133 134 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsReset")); 136 137 TestSuite activeSuite = new ActiveTestSuite(); 139 for (int i = 0; i < THREAD_COUNT; i++) { 140 activeSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsInThreads")); 141 } 142 masterSuite.addTest(activeSuite); 143 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testCallCount")); 144 145 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsLog")); 147 148 149 150 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsReset")); 152 153 TestSuite activeSuite2 = new ActiveTestSuite(); 155 for (int i = 0; i < THREAD_COUNT; i++) { 156 activeSuite2.addTest(new DefaultStatisticsInterceptorTest("testStatisticsInThreadsWithFailures")); 157 } 158 masterSuite.addTest(activeSuite2); 159 160 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testFailureCount")); 161 162 masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsLog")); 164 165 166 return masterSuite; 167 } 168 169 } 170 | Popular Tags |