// Path: src/main/java/com/tzavellas/circuitbreaker/jmx/CircuitBreakerMBean.java
// public interface CircuitBreakerMBean {
// 	
// 	/**
// 	 * Test whether the circuit is open.
// 	 */
// 	boolean isOpen();
// 	
// 	/**
// 	 * Open the circuit, causing any subsequent calls made through the
// 	 * circuit to stop throwing an OpenCircuitException.
// 	 */
// 	void open();
// 	
// 	/**
// 	 * Test whether the circuit is closed.
// 	 */
// 	boolean isClosed();
// 	
// 	/**
// 	 * Close the circuit, allowing any method call to propagate to
// 	 * their recipients.
// 	 */
// 	void close();
// 	
// 	/**
// 	 * Test whether the circuit is open.
// 	 */
// 	boolean isHalfOpen();
// 	
// 	/**
// 	 * Get the Date the circuit was opened.
// 	 * 
// 	 * @return if the circuit is open return the open timestamp else null 
// 	 */
// 	Date getOpenTimestamp();
// 	
// 	/**
// 	 * The number of calls being made through the circuit.
// 	 */
// 	int getCalls();
// 	
// 	/**
// 	 * The number of total failures.
// 	 */
// 	int getFailures();
// 	
// 	/**
// 	 * The number of failures since the circuit was closed. 
// 	 */
// 	int getCurrentFailures();
// 	
// 	/**
// 	 * The number of times the circuit has been opened.
// 	 */
// 	int getTimesOpened();
// 	
// 	/**
// 	 * The number of failures after the circuit opens.
// 	 */
// 	int getMaxFailures();
// 	
// 	/**
// 	 * Set the number of failures after the circuit opens.
// 	 */
// 	void setMaxFailures(int n);
// 	
// 	/**
// 	 * The timeout after which the circuit closes.
// 	 */
// 	String getTimeout();
// 	
// 	/**
// 	 * Set the timeout after which the circuit closes.
// 	 * 
// 	 * @param timeout a String formated Duration
// 	 * 
// 	 * @see Duration
// 	 */
// 	void setTimeout(String timeout);
// 	
// 	/**
// 	 * Get the duration after which the number of failures tracked by
// 	 * the circuit breaker gets reset. 
// 	 */
// 	String getCurrentFailuresDuration();
// 	
// 	/**
// 	 * Specify the duration after which the number of failures tracked by
// 	 * the circuit breaker gets reset. 
// 	 * 
// 	 * @param duration a string representing a {@code Duration} object.
// 	 * 
// 	 * @see Duration
// 	 */
// 	void setCurrentFailuresDuration(String duration);
// 	
// 	/**
// 	 * Get the duration after which a method execution is considered a failure.
// 	 */
// 	String getMaxMethodDuration();
// 	
// 	/**
// 	 * Set the duration after which a method execution is considered a failure.
// 	 * 
// 	 * @param d a String formated Duration or <code>null</code> if you want to disable
// 	 *          the tracking of execution time.
// 	 *          
// 	 * @see Duration
// 	 */
// 	void setMaxMethodDuration(String duration);
// }
// 
// Path: src/main/java/com/tzavellas/circuitbreaker/jmx/JmxUtils.java
// public abstract class JmxUtils {
// 	
// 	private JmxUtils() { }
// 	
// 	private static CircuitBreakerMBean getCircuitBreaker(ObjectName name) throws JMException {
// 		return JMX.newMBeanProxy(
// 			ManagementFactory.getPlatformMBeanServer(),
// 			name,
// 			CircuitBreakerMBean.class);
// 	}
// 	
// 	public static CircuitBreakerMBean getCircuitBreaker(Object target) throws JMException {
// 		return getCircuitBreaker(new ObjectName(getObjectName(target)));
// 	}
// 	
// 	public static Set<CircuitBreakerMBean> circuitBreakersForType(Class<?> targetClass) throws JMException {
// 		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
// 		String query = String.format("com.tzavellas.circuitbreaker:type=CircuitBreaker,target=%s,code=*", targetClass.getSimpleName());
// 		Set<ObjectName> names = server.queryNames(new ObjectName(query), null);
// 		Set<CircuitBreakerMBean> mbeans = new HashSet<CircuitBreakerMBean>();
// 		for (ObjectName name: names)
// 			mbeans.add(getCircuitBreaker(name));
// 		return mbeans;
// 	}
// 
// 	public static String getObjectName(Object target) {
// 		return String.format("com.tzavellas.circuitbreaker:type=CircuitBreaker,target=%s,code=%d",
// 				target.getClass().getSimpleName(),
// 				target.hashCode());
// 	} 
// }
// 
// Path: src/test/java/com/tzavellas/test/ITimeService.java
// public interface ITimeService {
// 	
// 	Date EXPECTED = new Date(); 
// 
// 	Date networkTime();
// 
// 	Date faultyNetworkTime();
// }


import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tzavellas.circuitbreaker.jmx.CircuitBreakerMBean;
import com.tzavellas.circuitbreaker.jmx.JmxUtils;
import com.tzavellas.test.ITimeService;
package com.tzavellas.circuitbreaker.aspectj;




public class ConfigFromSpringTest {
	
	final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/aspectj-impl-context.xml");
	final ITimeService time = (ITimeService) context.getBean("timeService");
	
	@After
	public void unregisterFromJmx() {
		CircuitBreakerConfigurator.aspectOf().setEnableJmx(false);
		breaker().setEnableJmx(false);
	}
	
	@Test
	public void open_and_close_via_jmx() throws Exception {
		generateFaults(1);
		assertTrue(breaker().getCircuitInfo().isOpen());
		
// Path: src/main/java/com/tzavellas/circuitbreaker/jmx/CircuitBreakerMBean.java
// public interface CircuitBreakerMBean {
// 	
// 	/**
// 	 * Test whether the circuit is open.
// 	 */
// 	boolean isOpen();
// 	
// 	/**
// 	 * Open the circuit, causing any subsequent calls made through the
// 	 * circuit to stop throwing an OpenCircuitException.
// 	 */
// 	void open();
// 	
// 	/**
// 	 * Test whether the circuit is closed.
// 	 */
// 	boolean isClosed();
// 	
// 	/**
// 	 * Close the circuit, allowing any method call to propagate to
// 	 * their recipients.
// 	 */
// 	void close();
// 	
// 	/**
// 	 * Test whether the circuit is open.
// 	 */
// 	boolean isHalfOpen();
// 	
// 	/**
// 	 * Get the Date the circuit was opened.
// 	 * 
// 	 * @return if the circuit is open return the open timestamp else null 
// 	 */
// 	Date getOpenTimestamp();
// 	
// 	/**
// 	 * The number of calls being made through the circuit.
// 	 */
// 	int getCalls();
// 	
// 	/**
// 	 * The number of total failures.
// 	 */
// 	int getFailures();
// 	
// 	/**
// 	 * The number of failures since the circuit was closed. 
// 	 */
// 	int getCurrentFailures();
// 	
// 	/**
// 	 * The number of times the circuit has been opened.
// 	 */
// 	int getTimesOpened();
// 	
// 	/**
// 	 * The number of failures after the circuit opens.
// 	 */
// 	int getMaxFailures();
// 	
// 	/**
// 	 * Set the number of failures after the circuit opens.
// 	 */
// 	void setMaxFailures(int n);
// 	
// 	/**
// 	 * The timeout after which the circuit closes.
// 	 */
// 	String getTimeout();
// 	
// 	/**
// 	 * Set the timeout after which the circuit closes.
// 	 * 
// 	 * @param timeout a String formated Duration
// 	 * 
// 	 * @see Duration
// 	 */
// 	void setTimeout(String timeout);
// 	
// 	/**
// 	 * Get the duration after which the number of failures tracked by
// 	 * the circuit breaker gets reset. 
// 	 */
// 	String getCurrentFailuresDuration();
// 	
// 	/**
// 	 * Specify the duration after which the number of failures tracked by
// 	 * the circuit breaker gets reset. 
// 	 * 
// 	 * @param duration a string representing a {@code Duration} object.
// 	 * 
// 	 * @see Duration
// 	 */
// 	void setCurrentFailuresDuration(String duration);
// 	
// 	/**
// 	 * Get the duration after which a method execution is considered a failure.
// 	 */
// 	String getMaxMethodDuration();
// 	
// 	/**
// 	 * Set the duration after which a method execution is considered a failure.
// 	 * 
// 	 * @param d a String formated Duration or <code>null</code> if you want to disable
// 	 *          the tracking of execution time.
// 	 *          
// 	 * @see Duration
// 	 */
// 	void setMaxMethodDuration(String duration);
// }
// 
// Path: src/main/java/com/tzavellas/circuitbreaker/jmx/JmxUtils.java
// public abstract class JmxUtils {
// 	
// 	private JmxUtils() { }
// 	
// 	private static CircuitBreakerMBean getCircuitBreaker(ObjectName name) throws JMException {
// 		return JMX.newMBeanProxy(
// 			ManagementFactory.getPlatformMBeanServer(),
// 			name,
// 			CircuitBreakerMBean.class);
// 	}
// 	
// 	public static CircuitBreakerMBean getCircuitBreaker(Object target) throws JMException {
// 		return getCircuitBreaker(new ObjectName(getObjectName(target)));
// 	}
// 	
// 	public static Set<CircuitBreakerMBean> circuitBreakersForType(Class<?> targetClass) throws JMException {
// 		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
// 		String query = String.format("com.tzavellas.circuitbreaker:type=CircuitBreaker,target=%s,code=*", targetClass.getSimpleName());
// 		Set<ObjectName> names = server.queryNames(new ObjectName(query), null);
// 		Set<CircuitBreakerMBean> mbeans = new HashSet<CircuitBreakerMBean>();
// 		for (ObjectName name: names)
// 			mbeans.add(getCircuitBreaker(name));
// 		return mbeans;
// 	}
// 
// 	public static String getObjectName(Object target) {
// 		return String.format("com.tzavellas.circuitbreaker:type=CircuitBreaker,target=%s,code=%d",
// 				target.getClass().getSimpleName(),
// 				target.hashCode());
// 	} 
// }
// 
// Path: src/test/java/com/tzavellas/test/ITimeService.java
// public interface ITimeService {
// 	
// 	Date EXPECTED = new Date(); 
// 
// 	Date networkTime();
// 
// 	Date faultyNetworkTime();
// }


// Path: src/test/java/com/tzavellas/circuitbreaker/aspectj/ConfigFromSpringTest.java
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tzavellas.circuitbreaker.jmx.CircuitBreakerMBean;
import com.tzavellas.circuitbreaker.jmx.JmxUtils;
import com.tzavellas.test.ITimeService;

package com.tzavellas.circuitbreaker.aspectj;




public class ConfigFromSpringTest {
	
	final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/aspectj-impl-context.xml");
	final ITimeService time = (ITimeService) context.getBean("timeService");
	
	@After
	public void unregisterFromJmx() {
		CircuitBreakerConfigurator.aspectOf().setEnableJmx(false);
		breaker().setEnableJmx(false);
	}
	
	@Test
	public void open_and_close_via_jmx() throws Exception {
		generateFaults(1);
		assertTrue(breaker().getCircuitInfo().isOpen());
		
		CircuitBreakerMBean mbean = JmxUtils.getCircuitBreaker(time);