/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jmxdatamart.Extractor.MXBean;

import java.lang.management.ManagementFactory;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.jmxdatamart.Extractor.Setting.Attribute;
import org.jmxdatamart.JMXTestServer.CarBean;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

/**
 *
 * @author Binh Tran <mynameisbinh@gmail.com>
 */
public class MultiLayeredAttributeTest {
    
    @Test
    public void testMultiLayeredAttribute() throws Exception{
        // note: test PS MarkSweep
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mS = new ObjectName("java.lang:name=CarBean");
        mbs.registerMBean(new CarBean(), mS);
        
        MultiLayeredAttribute mla = new MultiLayeredAttribute("Car.*", mbs, mS);
        Map<Attribute, Object> result = mla.getAll();
        
        assertTrue ( result.size() == 4);
        
        for (Map.Entry<Attribute, Object> entry : result.entrySet()) {
            if (entry.getKey().getAlias().equals("Car_name")) {
                assertThat(entry.getValue().toString(), equalTo(CarBean.NAME));
            } else if (entry.getKey().getAlias().equals("Car_year")) {
                assertThat(entry.getValue().toString(), equalTo(CarBean.YEAR));
            } else if (entry.getKey().getAlias().equals("Car_engine")) {
                assertThat(entry.getValue().toString(), 
                        equalTo((new Integer(CarBean.ENGINE)).toString()));
            } else if (entry.getKey().getAlias().equals("Car_power")) {
                assertThat(entry.getValue().toString(),
                        equalTo((new Integer(CarBean.POWER)).toString()));
            } else {
                fail("Unknown attribute " + entry.getKey().toString());
            }
        }
        
        mla = new MultiLayeredAttribute("Map.*.*", mbs, mS);
        result = mla.getAll();
        
        assertEquals(4, result.size());
        for (Map.Entry<Attribute, Object> entry : result.entrySet()) {
            if (entry.getKey().getAlias().equals("Map_Car_name")) {
                assertThat(entry.getValue().toString(), equalTo(CarBean.NAME));
            } else if (entry.getKey().getAlias().equals("Map_Car_year")) {
                assertThat(entry.getValue().toString(), equalTo(CarBean.YEAR));
            } else if (entry.getKey().getAlias().equals("Map_Car_engine")) {
                assertThat(entry.getValue().toString(), 
                        equalTo((new Integer(CarBean.ENGINE)).toString()));
            } else if (entry.getKey().getAlias().equals("Map_Car_power")) {
                assertThat(entry.getValue().toString(),
                        equalTo((new Integer(CarBean.POWER)).toString()));
            } else {
                fail("Unknown attribute " + entry.getKey().toString());
            }
        }
    }
}
@@ -486,16 +486,105 @@
 hers.*;%0D
+%0Aimport org.jmxdatamart.JMXTestServer.TestBean;%0D%0Aimport org.jmxdatamart.common.DataType;%0D
 %0A%0D%0A/**%0D%0A
@@ -1058,24 +1058,73 @@
 ute(
-%22Car.*%22, mbs, mS
+mbs);%0D%0A        Attribute attr = new Attribute(%22Car.*%22, null, null
 );%0D%0A
@@ -1166,32 +1166,40 @@
 lt = mla.getAll(
+mS, attr
 );%0D%0A        %0D%0A  
@@ -2168,40 +2168,19 @@
     
-%0D%0A        mla = new MultiLayered
+attr = new 
 Attr
@@ -2200,15 +2200,18 @@
 *%22, 
-mbs, mS
+null, null
 );%0D%0A
@@ -2238,16 +2238,24 @@
 .getAll(
+mS, attr
 );%0D%0A    
@@ -3205,32 +3205,32 @@
 %0A            %7D%0D%0A
-
         %7D%0D%0A    %7D
@@ -3220,20 +3220,1234 @@
 %0A        %7D%0D%0A
+        mbs.unregisterMBean(mS);%0D%0A    %7D%0D%0A    %0D%0A    @Test%0D%0A    public void testSameLevelPattern() throws Exception%7B%0D%0A        TestBean tb = new TestBean();%0D%0A        tb.setA(new Integer(42));%0D%0A        tb.setB(new Integer(8));%0D%0A        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();%0D%0A        String mbName = %22org.jmxdatamart.JMXTestServer:type=TestBean%22;%0D%0A        ObjectName mbeanName = new ObjectName(mbName);%0D%0A        mbs.registerMBean(tb, mbeanName);%0D%0A        %0D%0A        MultiLayeredAttribute mla = new MultiLayeredAttribute(mbs);%0D%0A        Attribute attr = new Attribute(null, %22*%22, null);%0D%0A        Map%3CAttribute, Object%3E result = mla.getAll(mbeanName, attr);%0D%0A        %0D%0A        assertEquals(2, result.size());%0D%0A        for (Map.Entry%3CAttribute, Object%3E entry : result.entrySet()) %7B%0D%0A            if (entry.getKey().getAlias().equals(%22A%22)) %7B%0D%0A                assertThat(entry.getValue().toString(), equalTo(%2242%22));%0D%0A            %7D else if (entry.getKey().getAlias().equals(%22B%22))%7B%0D%0A                assertThat(entry.getValue().toString(), equalTo(%228%22));%0D%0A            %7D else %7B%0D%0A                fail(%22Unknown attribute %22 + entry.getKey());%0D%0A            %7D%0D%0A        %7D%0D%0A        mbs.unregisterMBean(mbeanName);%0D%0A
     %7D%0D%0A%7D

Change format of MultiLayeredAttributeTest to fit the new format of MultiLayeredAttributeTest