// ========================================================================
// Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================

package org.eclipse.jetty.spring;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jetty.spring.Server;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class SpringXmlConfigurationTest
{
    protected String _configure="org/mortbay/jetty/spring/configure.xml";

    @Before
    public void init() throws Exception
    {
        // Jetty's XML configuration will make use of java.util.ServiceLoader
        // to load the proper ConfigurationProcessorFactory, so these tests
        // will always fail in JDK 5.

        String javaVersion = System.getProperty("java.version");
        Pattern regexp = Pattern.compile("1\\.(\\d{1})\\..*");
        Matcher matcher = regexp.matcher(javaVersion);
        if (matcher.matches())
        {
            String minor = matcher.group(1);
            Assume.assumeTrue(Integer.parseInt(minor) > 5);
        }
    }

    @Test
    public void testPassedObject() throws Exception
    {
        TestConfiguration.VALUE=77;

        URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure);
        XmlConfiguration configuration = new XmlConfiguration(url);

        Map<String,String> properties = new HashMap<String,String>();
        properties.put("test", "xxx");

        TestConfiguration nested = new TestConfiguration();
        nested.setTestString0("nested");
        configuration.getIdMap().put("nested",nested);

        TestConfiguration tc = new TestConfiguration();
        tc.setTestString0("preconfig");
        tc.setTestInt0(42);
        configuration.getProperties().putAll(properties);

        tc=(TestConfiguration)configuration.configure(tc);

        assertEquals("preconfig",tc.getTestString0());
        assertEquals(42,tc.getTestInt0());
        assertEquals("SetValue",tc.getTestString1());
        assertEquals(1,tc.getTestInt1());

        assertEquals("nested",tc.getNested().getTestString0());
        assertEquals("nested",tc.getNested().getTestString1());
        assertEquals("default",tc.getNested().getNested().getTestString0());
        assertEquals("deep",tc.getNested().getNested().getTestString1());

        assertEquals("deep",((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
        assertEquals(2,((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());

        assertEquals("xxx",tc.getTestString2());
    }

    @Test
    public void testNewObject() throws Exception
    {
        TestConfiguration.VALUE=71;

        URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure);
        XmlConfiguration configuration = new XmlConfiguration(url);

        Map<String,String> properties = new HashMap<String,String>();
        properties.put("test", "xxx");

        TestConfiguration nested = new TestConfiguration();
        nested.setTestString0("nested");
        configuration.getIdMap().put("nested",nested);

        configuration.getProperties().putAll(properties);
        TestConfiguration tc = (TestConfiguration)configuration.configure();

        assertEquals("default",tc.getTestString0());
        assertEquals(-1,tc.getTestInt0());
        assertEquals("SetValue",tc.getTestString1());
        assertEquals(1,tc.getTestInt1());

        assertEquals("nested",tc.getNested().getTestString0());
        assertEquals("nested",tc.getNested().getTestString1());
        assertEquals("default",tc.getNested().getNested().getTestString0());
        assertEquals("deep",tc.getNested().getNested().getTestString1());

        assertEquals("deep",((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1());
        assertEquals(2,((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2());

        assertEquals("xxx",tc.getTestString2());
    }

    @Test
    public void testJettyXml() throws Exception
    {
        URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource("org/mortbay/jetty/spring/jetty.xml");
        XmlConfiguration configuration = new XmlConfiguration(url);

        Server server = (Server)configuration.configure();

        server.dumpStdErr();

    }

    @Test
    public void XmlConfigurationMain() throws Exception
    {
        XmlConfiguration.main(new String[]{"src/test/resources/org/mortbay/jetty/spring/jetty.xml"});

    }
}

@@ -1,30 +1,34 @@
 //
+%0A// 
  ===========================
@@ -75,16 +75,17 @@
 =====%0A//
+ 
  Copyrig
@@ -95,17 +95,17 @@
 (c) 
-2006-2009
+1995-2012
  Mor
@@ -133,16 +133,17 @@
  Ltd.%0A//
+ 
  -------
@@ -210,16 +210,17 @@
 -----%0A//
+ 
  All rig
@@ -279,16 +279,17 @@
 rials%0A//
+ 
  are mad
@@ -353,16 +353,17 @@
  v1.0%0A//
+ 
  and Apa
@@ -418,16 +418,24 @@
 tion.%0A//
+%0A//     
  The Ecl
@@ -464,32 +464,37 @@
  available at%0A//
+     
  http://www.ecli
@@ -522,16 +522,24 @@
 .html%0A//
+%0A//     
  The Apa
@@ -573,16 +573,21 @@
 le at%0A//
+     
  http://
@@ -630,16 +630,20 @@
 0.php%0A//
+%0A// 
  You may
@@ -709,16 +709,17 @@
 nses.%0A//
+ 
  =======
@@ -783,16 +783,19 @@
 ========
+%0A//
 %0A%0Apackag

fix license block