package io.pcp.parfait.spring;

import io.pcp.parfait.Monitorable;
import io.pcp.parfait.MonitorableRegistry;
import io.pcp.parfait.MonitoredCounter;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

import java.util.Collection;


public class MonitorTest extends TestCase {
	public void testThing() {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"test.xml"});
		DelayingBean test = ((DelayingBean) context.getBean("test"));
		DelayingBean other = ((DelayingBean) context.getBean("other"));

        MonitorableRegistry registry = (MonitorableRegistry) context.getBean("registry");

        assertNotNull("registry should not be null", registry);

        assertTrue("should have located counter", counterExists(registry, "xx.other.count"));
        assertTrue("should have located counter", counterExists(registry, "xx.test.count"));

        MonitoredCounter otherCounter = (MonitoredCounter) findCounter(registry, "xx.other.count");
        MonitoredCounter testCounter = (MonitoredCounter) findCounter(registry, "xx.test.count");

        assertNotNull("should have located the basic counter for 'other'", otherCounter);
        assertEquals(0L, otherCounter.get().longValue());

        assertNotNull("should have located the basic counter for 'test'", testCounter);
        assertEquals(0L, testCounter.get().longValue());


		test.doThing();
		other.doThing();

        assertEquals(1L, otherCounter.get().longValue());
        assertEquals(1L, testCounter.get().longValue());


	}

    // TODO some Hamcrest matcher goodness on assertions that specific monitorable exists?
    private Monitorable<?> findCounter(MonitorableRegistry registry, final String name) {
        Collection<Monitorable<?>> monitorables = registry.getMonitorables();
        Monitorable<?> monitorable = Iterators.find(monitorables.iterator(), new Predicate<Monitorable<?>>() {
            @Override
            public boolean apply(Monitorable<?> monitorable) {
                return monitorable.getName().equals(name);
            }
        });

        if (monitorable == null) {
            throw new IllegalArgumentException(name + " could not be located in the MonitorableRegistry");
        }
        return monitorable;

    }

    private boolean counterExists(MonitorableRegistry registry, String name) {
        for (Monitorable<?> monitorable: registry.getMonitorables()) {
            if (monitorable.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }


}

@@ -232,63 +232,8 @@
 rs;%0A
-import org.springframework.context.ApplicationContext;%0A
 impo
@@ -413,17 +413,20 @@
 tCase %7B%0A
-%09
+    
 public v
@@ -443,18 +443,36 @@
 ing() %7B%0A
-%09%09
+        ClassPathXml
 Applicat
@@ -554,18 +554,42 @@
 xml%22%7D);%0A
-%09%09
+        try %7B%0A            
 Delaying
@@ -642,18 +642,28 @@
 est%22));%0A
-%09%09
+            
 Delaying
@@ -719,32 +719,36 @@
 er%22));%0A%0A        
+   
+ 
 MonitorableRegis
@@ -814,32 +814,36 @@
 try%22);%0A%0A        
+    
 assertNotNull(%22r
@@ -883,32 +883,36 @@
 stry);%0A%0A        
+ 
+   
 assertTrue(%22shou
@@ -977,24 +977,28 @@
 r.count%22));%0A
+    
         asse
@@ -1079,32 +1079,36 @@
 nt%22));%0A%0A        
+    
 MonitoredCounter
@@ -1183,32 +1183,36 @@
 ount%22);%0A        
+  
+  
 MonitoredCounter
@@ -1286,32 +1286,36 @@
 unt%22);%0A%0A        
+    
 assertNotNull(%22s
@@ -1380,32 +1380,36 @@
 unter);%0A        
+ 
+   
 assertEquals(0L,
@@ -1443,32 +1443,36 @@
 ue());%0A%0A        
+   
+ 
 assertNotNull(%22s
@@ -1535,32 +1535,36 @@
 unter);%0A        
+    
 assertEquals(0L,
@@ -1598,18 +1598,28 @@
 e());%0A%0A%0A
-%09%09
+            
 test.doT
@@ -1630,10 +1630,20 @@
 ();%0A
-%09%09
+            
 othe
@@ -1652,24 +1652,28 @@
 doThing();%0A%0A
+    
         asse
@@ -1718,32 +1718,36 @@
 lue());%0A        
+  
+  
 assertEquals(1L,
@@ -1783,11 +1783,71 @@
 ));%0A
-%0A%0A%09
+        %7D finally %7B%0A            context.close();%0A        %7D%0A    
 %7D%0A%0A 
@@ -2566,17 +2566,16 @@
 orable;%0A
-%0A
     %7D%0A%0A 
@@ -2858,12 +2858,10 @@
 ;%0A    %7D%0A
-%0A%0A
 %7D%0A

Fix partfait-spring resource leak on application XML class path load.