/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2018 The eXist Project
 * http://exist-db.org
 *
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.exist.management;

import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.exist.test.ExistWebServer;
import org.junit.ClassRule;
import org.junit.Test;

import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.xmlunit.matchers.HasXPathMatcher.hasXPath;

public class JmxRemoteTest {

    @ClassRule
    public static final ExistWebServer existWebServer = new ExistWebServer(true, true, true, true, false);

    private static String getServerUri() {
        return "http://localhost:" + existWebServer.getPort() + "/exist/status";
    }

    @Test
    public void checkContent() throws Exception {

        // Get content
        String jmxXML = Request.Get(getServerUri()).execute().returnContent().asString();

        // Prepare XPath validation
        HashMap<String, String> prefix2Uri = new HashMap<String, String>();
        prefix2Uri.put("jmx", "http://exist-db.org/jmx");

        // Java GC
        if(SystemUtils.IS_JAVA_1_8) {
            assertThat(jmxXML, hasXPath("//jmx:GarbageCollectorImpl").withNamespaceContext(prefix2Uri));
        } else {
            assertThat(jmxXML, hasXPath("//jmx:GarbageCollectorExtImpl").withNamespaceContext(prefix2Uri));
        }

        // Jetty
        assertThat(jmxXML, hasXPath("//jmx:WebAppContext").withNamespaceContext(prefix2Uri));

        // eXist-db
        assertThat(jmxXML, hasXPath("//jmx:ProcessReport").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:Cache").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:SystemInfo").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:CacheManager").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:LockManager").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:SanityReport").withNamespaceContext(prefix2Uri));
        assertThat(jmxXML, hasXPath("//jmx:Database").withNamespaceContext(prefix2Uri));
    }

    @Test
    public void checkBasicRequest() throws Exception {

        HttpResponse response = Request.Get(getServerUri())
                .addHeader(new BasicHeader("Accept", ContentType.APPLICATION_XML.toString()))
                .execute().returnResponse();

        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
        assertEquals("application/xml", response.getEntity().getContentType().getValue());
    }

}

@@ -847,17 +847,16 @@
 USA%0A */%0A
-%0A
 package 
@@ -882,53 +882,8 @@
 t;%0A%0A
-import org.apache.commons.lang3.JavaVersion;%0A
 impo
@@ -1234,22 +1234,201 @@
 ava.
-util.HashMap;%0A
+io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport static org.exist.management.client.JMXtoXML.JMX_NAMESPACE;%0Aimport static org.exist.management.client.JMXtoXML.JMX_PREFIX;
 %0Aimp
@@ -1908,32 +1908,34 @@
 ontent() throws 
+IO
 Exception %7B%0A%0A   
@@ -1922,33 +1922,32 @@
 s IOException %7B%0A
-%0A
         // Get c
@@ -1961,16 +1961,22 @@
         
+final 
 String j
@@ -1978,18 +1978,18 @@
 ing jmxX
-ML
+ml
  = Reque
@@ -2094,20 +2094,22 @@
         
-Hash
+final 
 Map%3CStri
@@ -2145,30 +2145,16 @@
 HashMap%3C
-String, String
 %3E();%0A   
@@ -2177,40 +2177,33 @@
 put(
-%22jmx%22, %22http://exist-db.org/jmx%22
+JMX_PREFIX, JMX_NAMESPACE
 );%0A%0A
@@ -2231,16 +2231,17 @@
       if
+ 
 (SystemU
@@ -2279,34 +2279,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2401,34 +2401,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2533,34 +2533,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2648,34 +2648,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2742,34 +2742,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2828,34 +2828,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -2919,34 +2919,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -3012,34 +3012,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -3044,19 +3044,113 @@
 jmx:
-LockManager
+CollectionCache%22).withNamespaceContext(prefix2Uri));%0A        assertThat(jmxXml, hasXPath(%22//jmx:LockTable
 %22).w
@@ -3198,34 +3198,34 @@
  assertThat(jmxX
-ML
+ml
 , hasXPath(%22//jm
@@ -3299,18 +3299,18 @@
 hat(jmxX
-ML
+ml
 , hasXPa
@@ -3425,16 +3425,18 @@
  throws 
+IO
 Exceptio
@@ -3439,24 +3439,29 @@
 ption %7B%0A
-%0A
+ 
        
+final
  HttpRes
@@ -3824,11 +3824,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix the JMXRemoteTest for eXist-db 5.0.0 JMX output