KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > webtest > ConfigReloadingTest


1 package net.sourceforge.cruisecontrol.webtest;
2
3 import junit.framework.TestCase;
4 import org.apache.tools.ant.util.FileUtils;
5
6 import javax.management.MBeanServerConnection JavaDoc;
7 import javax.management.ObjectName JavaDoc;
8 import javax.management.remote.JMXConnector JavaDoc;
9 import javax.management.remote.JMXConnectorFactory JavaDoc;
10 import javax.management.remote.JMXServiceURL JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.FileReader JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 public class ConfigReloadingTest extends TestCase {
20     private MBeanServerConnection JavaDoc server;
21
22     protected void setUp() throws Exception JavaDoc {
23         super.setUp();
24         JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost:7856/jndi/jrmp");
25
26         Map JavaDoc environment = new HashMap JavaDoc();
27         environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
28         environment.put(Context.PROVIDER_URL, "rmi://localhost:7856");
29
30         JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(address, environment);
31         server = cntor.getMBeanServerConnection();
32     }
33
34     public void testChangingConfigFileOnDisk() throws Exception JavaDoc {
35         ObjectName JavaDoc ccMgr = ObjectName.getInstance("CruiseControl Manager:id=unique");
36         ObjectName JavaDoc ccProj = ObjectName.getInstance("CruiseControl Project:name=connectfour");
37
38         //Look at the current interval value in JMX.
39
long currentJMXInterval = ((Long JavaDoc) server.getAttribute(ccProj, "BuildInterval")).longValue() / 1000;
40
41         //Look at the current interval value in the config file on disk
42
String JavaDoc pathToConfig = (String JavaDoc) server.getAttribute(ccMgr, "ConfigFileName");
43         File JavaDoc configFile = new File JavaDoc(pathToConfig);
44         String JavaDoc cruiseConfiguration = FileUtils.readFully(new FileReader JavaDoc(configFile));
45         int currentFileInterval = getInterval(cruiseConfiguration);
46         assertEquals("Build interval from JMX does not match interval from config file.", currentJMXInterval,
47                 currentFileInterval);
48
49         //Increment and set the interval value in the config file on disk
50
int newInterval = currentFileInterval + 100;
51         PrintWriter JavaDoc configWriter = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(configFile));
52         configWriter.println(cruiseConfiguration.replaceFirst("interval=\"" + currentFileInterval + "\"",
53                 "interval=\"" + newInterval + "\""));
54         configWriter.flush();
55         configWriter.close();
56
57         //Force a build, to trigger CruiseControl to reload the config file
58
server.invoke(ccProj, "build", null, null);
59
60         //Look at the current interval value in JMX
61
long timeToStopWaiting = System.currentTimeMillis() + 30000;
62         long newJMXInterval = ((Long JavaDoc) server.getAttribute(ccProj, "BuildInterval")).longValue() / 1000;
63         while (timeToStopWaiting > System.currentTimeMillis()
64                 && newJMXInterval != newInterval) {
65             Thread.sleep(1000);
66             newJMXInterval = ((Long JavaDoc) server.getAttribute(ccProj, "BuildInterval")).longValue() / 1000;
67         }
68
69         assertEquals(
70                 "CruiseControl hasn't reloaded the configuration file. The schedule interval is still the OLD value.",
71                 newInterval, newJMXInterval);
72     }
73
74
75     private int getInterval(String JavaDoc cruiseConfiguration) {
76         int startIndex = cruiseConfiguration.indexOf("interval=\"") + 10;
77         int endIndex = cruiseConfiguration.substring(startIndex).indexOf("\"") + startIndex;
78         return Integer.parseInt(cruiseConfiguration.substring(startIndex, endIndex));
79     }
80 }
81
Popular Tags