/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.velocity.internal.jmx;

import org.apache.velocity.VelocityContext;
import org.junit.Assert;
import org.junit.Test;
import org.xwiki.test.AbstractComponentTestCase;
import org.xwiki.velocity.VelocityEngine;

import javax.management.openmbean.ArrayType;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import java.io.StringWriter;
import java.util.Properties;

/**
 * Unit tests for {@link JMXVelocityEngine}.
 * 
 * @version $Id$
 * @since 2.4M2
 */
public class JMXVelocityEngineTest extends AbstractComponentTestCase
{
    @Test
    public void testGetTemplates() throws Exception
    {
        VelocityEngine engine = getComponentManager().lookup(VelocityEngine.class);
        engine.initialize(new Properties());
        JMXVelocityEngine jmxBean = new JMXVelocityEngine(engine);

        TabularData data = jmxBean.getTemplates();

        ArrayType macroNameType = new ArrayType(1, SimpleType.STRING);
        String[] columnNames = new String[] {"templateName", "macroNames"};
        String[] descriptions = new String[] {"The Template Name (namespace)", "The names of registered Macros"};
        CompositeType rowType = new CompositeType("template",
            "Template management data (namespaces, macros) for a row", columnNames, descriptions,
            new OpenType[]{SimpleType.STRING, macroNameType});

        Assert.assertEquals(1, data.values().size());
        Assert.assertTrue("Found unexpected result: " + data.toString(), data.containsValue(
            new CompositeDataSupport(rowType, columnNames, new Object[]{"<global>", new String[]{}})));

        StringWriter out = new StringWriter();
        engine.evaluate(new VelocityContext(), out, "testmacronamespace", "#macro(testmacro)#end");
        data = jmxBean.getTemplates();

        Assert.assertEquals(2, data.values().size());
        Assert.assertTrue("Found unexpected result: " + data.toString(), data.containsValue(
            new CompositeDataSupport(rowType, columnNames, new Object[]{"<global>", new String[]{}})));
        Assert.assertTrue("Found unexpected result: " + data.toString(), data.containsValue(
            new CompositeDataSupport(rowType, columnNames,
                new Object[]{"testmacronamespace", new String[]{"testmacro"}})));
    }
}

@@ -1117,53 +1117,8 @@
 e;%0A%0A
-import javax.management.openmbean.ArrayType;%0A
 impo
@@ -1160,23 +1160,16 @@
 siteData
-Support
 ;%0Aimport
@@ -1200,187 +1200,125 @@
 ean.
-CompositeType;%0Aimport javax.management.openmbean.OpenType;%0Aimport javax.management.openmbean.SimpleType;%0Aimport javax.management.openmbean.TabularData;%0Aimport java.io.StringWriter
+TabularData;%0Aimport java.io.StringWriter;%0Aimport java.util.Collection;%0Aimport java.util.HashMap;%0Aimport java.util.Map
 ;%0Aim
@@ -1836,484 +1836,131 @@
    A
-rrayType macroNameType = new ArrayType(1, SimpleType.STRING);%0A        String%5B%5D columnNames = new String%5B%5D %7B%22templateName%22, %22macroNames%22%7D;%0A        String%5B%5D descriptions = new String%5B%5D %7B%22The Template Name (namespace)%22, %22The names of registered Macros%22%7D;%0A        CompositeType rowType = new CompositeType(%22template%22,%0A            %22Template management data (namespaces, macros) for a row%22, columnNames, descriptions,%0A            new OpenType%5B%5D%7BSimpleType.STRING, macroNameType%7D
+ssert.assertEquals(1, data.values().size());%0A        CompositeData cd = ((CompositeData) data.values().iterator().next()
 );%0A
-%0A
     
@@ -1987,228 +1987,115 @@
 als(
-1, data.values().size());%0A        Assert.assertTrue(%22Found unexpected result: %22 + data.toString(), data.containsValue(%0A            new CompositeDataSupport(rowType, columnNames, new Object%5B%5D%7B%22%3Cglobal%3E%22, new String%5B%5D%7B%7D%7D))
+%22%3Cglobal%3E%22, cd.get(%22templateName%22));%0A        Assert.assertEquals(0, ((String%5B%5D)cd.get(%22macroNames%22)).length
 );%0A%0A
@@ -2347,197 +2347,272 @@
     
-Assert.assertTrue(%22Found unexpected result: %22 + data.toString(), data.containsValue(%0A            new CompositeDataSupport(rowType, columnNames, new Object%5B%5D%7B%22%3Cglobal%3E%22, new String%5B%5D%7B%7D%7D)
+Map%3CString, String%5B%5D%3E retrievedData = new HashMap%3CString, String%5B%5D%3E();%0A        for (CompositeData cdata : (Collection%3CCompositeData%3E) data.values()) %7B%0A            retrievedData.put((String) cdata.get(%22templateName%22), (String%5B%5D) cdata.get(%22macroNames%22
 ));%0A
+        %7D%0A
     
@@ -2632,218 +2632,220 @@
 sert
-True(%22Found unexpected result: %22 + data.toString(), data.containsValue(%0A            new CompositeDataSupport(rowType, columnNames,%0A                new Object%5B%5D%7B%22testmacronamespace%22, new String%5B%5D%7B%22testmacro%22%7D%7D))
+Equals(0, retrievedData.get(%22%3Cglobal%3E%22).length);%0A        Assert.assertEquals(1, retrievedData.get(%22testmacronamespace%22).length);%0A        Assert.assertEquals(%22testmacro%22, retrievedData.get(%22testmacronamespace%22)%5B0%5D
 );%0A 

Add management feature to monitor Velocity Caches