EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.test]

COVERAGE SUMMARY FOR SOURCE FILE [MetricTestCase.java]

nameclass, %method, %block, %line, %
MetricTestCase.java100% (1/1)70%  (7/10)67%  (195/291)74%  (46.8/63)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MetricTestCase100% (1/1)70%  (7/10)67%  (195/291)74%  (46.8/63)
assertEquals (Object [], Object []): void 0%   (0/1)0%   (0/5)0%   (0/2)
assertEquals (String, Object [], Object []): void 0%   (0/1)0%   (0/41)0%   (0/4)
triggerOutOfMemoryError (): void 0%   (0/1)0%   (0/10)0%   (0/2)
checkRunningThreads (): boolean 100% (1/1)60%  (42/70)65%  (11.8/18)
tearDown (): void 100% (1/1)86%  (72/84)88%  (15/17)
<static initializer> 100% (1/1)100% (17/17)100% (5/5)
MetricTestCase (): void 100% (1/1)100% (11/11)100% (3/3)
getDescription (): String 100% (1/1)100% (18/18)100% (1/1)
runTest (): void 100% (1/1)100% (23/23)100% (6/6)
setUp (): void 100% (1/1)100% (12/12)100% (5/5)

1package org.jtoolkit.essence.test;
2 
3import junit.framework.TestCase;
4import org.apache.commons.logging.Log;
5import org.apache.commons.logging.LogFactory;
6import org.jtoolkit.essence.ConfigProperties;
7import org.jtoolkit.essence.app.Main;
8import org.jtoolkit.essence.concurrency.Threads;
9import org.jtoolkit.essence.data.Transaction;
10import org.jtoolkit.essence.utils.JvmMetrics;
11 
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Map;
15import java.util.Properties;
16 
17@SuppressWarnings({"JUnitTestCaseInProductSource", "UseOfSystemOutOrSystemErr"})
18public abstract class MetricTestCase extends TestCase {
19    private static final Log LOG = LogFactory.getLog(MetricTestCase.class);
20    private static final long MEMORY_THREADHOLD = Long.getLong(ConfigProperties.ESSENCE_TEST_MEMORY_THREASHOLD, 256 * 1024);
21    private static final boolean MEMORY_HEAPDUMP = Boolean.getBoolean(ConfigProperties.ESSENCE_TEST_MEMORY_HEAPDUMP);
22    private static final Properties SYSTEM_PROPERTIES = System.getProperties();
23 
24    protected static final boolean BIG_TEST = Boolean.getBoolean(ConfigProperties.ESSENCE_BIG_TEST);
25 
26    private Map<Thread, StackTraceElement[]> allStackTraces = null;
27    protected final JvmMetrics startMetrics = new JvmMetrics();
28 
29    protected void setUp() throws Exception {
30        super.setUp();
31        Properties props = new Properties();
32        //noinspection UseOfPropertiesAsHashtable
33        props.putAll(SYSTEM_PROPERTIES);
34        System.setProperties(props);
35    }
36 
37    protected void runTest() throws Throwable {
38        JvmMetrics.waitForGC();
39 
40        JvmMetrics sm = new JvmMetrics();
41        allStackTraces = Thread.getAllStackTraces();
42 
43        super.runTest();
44        System.out.println(getDescription() + sm.delta());
45    }
46 
47    protected void tearDown() throws Exception {
48        super.tearDown();
49 
50        Transaction.rollbackAll();
51        Threads.clearDefaultSES();
52        Main.closeAll();
53 
54        // check all threads started are stopped.
55        boolean threadsOkay = checkRunningThreads();
56        if (!threadsOkay)
57            LOG.error(getDescription() + "Additional threads are running after the test");
58 
59        JvmMetrics smDelta = startMetrics.delta();
60        if (smDelta.threadCount > 0)
61            LOG.warn(getDescription() + "The number of threads increased by " + smDelta.threadCount);
62        if (smDelta.mbeanCount > 0)
63            LOG.warn(getDescription() + "The number of MBeans increased by " + smDelta.mbeanCount);
64        if (smDelta.memoryUsed > MEMORY_THREADHOLD) {
65            LOG.warn(getDescription() + "Test consumed " + smDelta.memoryUsed + " bytes.");
66            // trigger OutOfMemoryError. Must be triggered to cause a Heap dump.
67            if (MEMORY_HEAPDUMP)
68                triggerOutOfMemoryError();
69        }
70    }
71 
72    public static void triggerOutOfMemoryError() {
73        //noinspection MismatchedQueryAndUpdateOfCollection
74        List<long[]> list = new ArrayList<long[]>();
75        //noinspection InfiniteLoopStatement
76        while (true)
77            list.add(new long[1024 * 1024]);
78    }
79 
80    private String getDescription() {
81        return getClass().getName() + '.' + getName() + "()" + " ~~~~ ";
82    }
83 
84    protected boolean checkRunningThreads() throws InterruptedException {
85        boolean threadsOkay = true;
86        LOOP:
87        for (int i = 0; i < 12; i++) {
88            JvmMetrics.waitForGC();
89            Thread.sleep(1000);
90            // look for left over threads.
91            Map<Thread, StackTraceElement[]> allStackTraces2 = Thread.getAllStackTraces();
92            threadsOkay = true;
93            for (Thread thread : allStackTraces2.keySet()) {
94                if (allStackTraces.containsKey(thread)) continue;
95                String name = thread.getName();
96                if (name.startsWith(Threads.CONTROL_THREAD)) continue;
97                if (i < 11) {
98                    System.out.println("tearDown(): Waiting for " + thread + " to close.");
99                    continue LOOP;
100                }
101                if (Threads.dumpStack(thread, allStackTraces2.get(thread)))
102                    threadsOkay = false;
103            }
104            break;
105        }
106        return threadsOkay;
107    }
108 
109    public static void assertEquals(Object[] array1, Object[] array2) {
110        assertEquals("", array1, array2);
111    }
112 
113    public static void assertEquals(String text, Object[] array1, Object[] array2) {
114        assertEquals(text + " size", array1.length, array2.length);
115        for (int i = 0; i < array1.length; i++)
116            assertEquals(text + "[" + i + ']', array1[i], array2[i]);
117    }
118}

[all classes][org.jtoolkit.essence.test]
EMMA 2.0.5312 (C) Vladimir Roubtsov