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 ; 7 import javax.management.ObjectName ; 8 import javax.management.remote.JMXConnector ; 9 import javax.management.remote.JMXConnectorFactory ; 10 import javax.management.remote.JMXServiceURL ; 11 import javax.naming.Context ; 12 import java.io.File ; 13 import java.io.FileOutputStream ; 14 import java.io.FileReader ; 15 import java.io.PrintWriter ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 public class ConfigReloadingTest extends TestCase { 20 private MBeanServerConnection server; 21 22 protected void setUp() throws Exception { 23 super.setUp(); 24 JMXServiceURL address = new JMXServiceURL ("service:jmx:rmi://localhost:7856/jndi/jrmp"); 25 26 Map environment = new HashMap (); 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 cntor = JMXConnectorFactory.connect(address, environment); 31 server = cntor.getMBeanServerConnection(); 32 } 33 34 public void testChangingConfigFileOnDisk() throws Exception { 35 ObjectName ccMgr = ObjectName.getInstance("CruiseControl Manager:id=unique"); 36 ObjectName ccProj = ObjectName.getInstance("CruiseControl Project:name=connectfour"); 37 38 long currentJMXInterval = ((Long ) server.getAttribute(ccProj, "BuildInterval")).longValue() / 1000; 40 41 String pathToConfig = (String ) server.getAttribute(ccMgr, "ConfigFileName"); 43 File configFile = new File (pathToConfig); 44 String cruiseConfiguration = FileUtils.readFully(new FileReader (configFile)); 45 int currentFileInterval = getInterval(cruiseConfiguration); 46 assertEquals("Build interval from JMX does not match interval from config file.", currentJMXInterval, 47 currentFileInterval); 48 49 int newInterval = currentFileInterval + 100; 51 PrintWriter configWriter = new PrintWriter (new FileOutputStream (configFile)); 52 configWriter.println(cruiseConfiguration.replaceFirst("interval=\"" + currentFileInterval + "\"", 53 "interval=\"" + newInterval + "\"")); 54 configWriter.flush(); 55 configWriter.close(); 56 57 server.invoke(ccProj, "build", null, null); 59 60 long timeToStopWaiting = System.currentTimeMillis() + 30000; 62 long newJMXInterval = ((Long ) server.getAttribute(ccProj, "BuildInterval")).longValue() / 1000; 63 while (timeToStopWaiting > System.currentTimeMillis() 64 && newJMXInterval != newInterval) { 65 Thread.sleep(1000); 66 newJMXInterval = ((Long ) 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 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 |