Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
../../../../../img/srcFileCovDistChart9.png 28% of files have more coverage
28   111   8   9.33
4   74   0.29   1.5
3     2.67  
2    
 
  BaseMonitoringTest       Line # 43 24 0% 6 2 93.3% 0.93333334
  BaseMonitoringTest.JmxRunner       Line # 100 4 0% 2 2 60% 0.6
 
No Tests
 
1    /*
2    * Copyright 2009 Ben Gidley
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   
17    package uk.co.gidley.jmxmonitor.monitoring;
18   
19    import ch.qos.logback.classic.BasicConfigurator;
20    import ch.qos.logback.classic.LoggerContext;
21    import ch.qos.logback.classic.joran.JoranConfigurator;
22    import ch.qos.logback.core.joran.spi.JoranException;
23    import ch.qos.logback.core.util.StatusPrinter;
24    import org.slf4j.Logger;
25    import org.slf4j.LoggerFactory;
26    import org.testng.annotations.AfterMethod;
27    import org.testng.annotations.BeforeMethod;
28    import uk.co.gidley.jmxmonitor.RegistryManager;
29    import uk.co.gidley.jmxmonitor.services.InitialisationException;
30    import uk.co.gidley.jmxmonitor.services.ThreadManager;
31    import uk.co.gidley.jmxmonitor.uk.co.gidley.testAppender.TestAppender;
32   
33    import javax.management.InstanceAlreadyExistsException;
34    import javax.management.MBeanRegistrationException;
35    import javax.management.MalformedObjectNameException;
36    import javax.management.NotCompliantMBeanException;
37   
38    import static org.hamcrest.MatcherAssert.assertThat;
39   
40    /**
41    * Created by IntelliJ IDEA. User: ben Date: Jan 7, 2010 Time: 4:18:00 PM
42    */
 
43    public abstract class BaseMonitoringTest {
44    private static final Logger logger = LoggerFactory.getLogger(BaseMonitoringTest.class);
45    private RegistryManager registryManager;
46    private Thread jmxMonitorThread;
47   
 
48  2 toggle @BeforeMethod
49    public void setup() throws InitialisationException, JoranException, InterruptedException, MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException {
50  2 registryManager = new RegistryManager(getConfiguration());
51   
52  2 registerTestMBeans();
53  2 jmxMonitorThread = new Thread(new JmxRunner(), "JmxRunner");
54    // Give the registry a chance to start
55  2 jmxMonitorThread.start();
56  4 while (!registryManager.isReadyToRun()) {
57  2 Thread.sleep(100);
58    }
59    // Reconfigure Logback to use the in-memory appender
60  2 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
61  2 JoranConfigurator configurator = new JoranConfigurator();
62  2 configurator.setContext(lc);
63  2 lc.reset();
64  2 configurator.doConfigure("src/test/resources/logback-inMemory.xml");
65  2 StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
66   
67    // Wait for at least 3 events to be logged
68    // THIS NUMBER SHOULD BE INCREMENTED FOR NUMBER OF OUTPUTS EXPECTED FOR THE SELECTED CONFIG
69  549 while (TestAppender.getEvents().size() < waitForEvents()) {
70  547 Thread.sleep(100);
71    }
72    }
73   
74    public abstract String getConfiguration();
75   
76    public abstract int waitForEvents();
77   
78    public abstract void registerTestMBeans() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException;
79   
 
80  2 toggle @AfterMethod
81    public void tearDown() {
82  2 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
83  2 BasicConfigurator.configure(lc);
84  2 TestAppender.reset();
85  2 try {
86  2 ThreadManager threadManager = registryManager.getRegistry().getService(ThreadManager.class);
87  2 threadManager.stop();
88    } catch (IllegalStateException e) {
89    //noop
90    }
91  2 try {
92    // Wait for jmxmonitor to stop
93  2 jmxMonitorThread.join();
94    } catch (InterruptedException e) {
95  0 logger.error("{}", e);
96  0 throw new RuntimeException(e);
97    }
98    }
99   
 
100    private class JmxRunner implements Runnable {
 
101  2 toggle @Override
102    public void run() {
103  2 try {
104  2 registryManager.start();
105    } catch (InitialisationException e) {
106  0 logger.error("{}", e);
107  0 throw new RuntimeException(e);
108    }
109    }
110    }
111    }