/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.lookup;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.logging.log4j.ThreadContext;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockejb.jndi.MockContextFactory;

import static org.junit.Assert.*;

/**
 *
 */
public class InterpolatorTest {

    private static final String TESTKEY = "TestKey";
    private static final String TESTVAL = "TestValue";

    private static final String TEST_CONTEXT_RESOURCE_NAME = "logging/context-name";
    private static final String TEST_CONTEXT_NAME = "app-1";

    private static Context context;

    @BeforeClass
    public static void before() throws NamingException {
        System.setProperty(TESTKEY, TESTVAL);

        MockContextFactory.setAsInitial();
        context = new InitialContext();
        context.bind(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME);
    }

    @AfterClass
    public static void after() {
        try {
            context.close();
        } catch (final NamingException ignored) {
        }
        MockContextFactory.revertSetAsInitial();

        System.clearProperty(TESTKEY);
    }

    @Test
    public void testLookup() {
        final Map<String, String> map = new HashMap<>();
        map.put(TESTKEY, TESTVAL);
        final StrLookup lookup = new Interpolator(new MapLookup(map));
        ThreadContext.put(TESTKEY, TESTVAL);
        String value = lookup.lookup(TESTKEY);
        assertEquals(TESTVAL, value);
        value = lookup.lookup("ctx:" + TESTKEY);
        assertEquals(TESTVAL, value);
        value = lookup.lookup("sys:" + TESTKEY);
        assertEquals(TESTVAL, value);
        value = lookup.lookup("BadKey");
        assertNull(value);
        ThreadContext.clearMap();
        value = lookup.lookup("ctx:" + TESTKEY);
        assertEquals(TESTVAL, value);
        value = lookup.lookup("jndi:" + TEST_CONTEXT_RESOURCE_NAME);
        assertEquals(TEST_CONTEXT_NAME, value);
    }

    private void assertLookupNotEmpty(final StrLookup lookup, final String key) {
        final String value = lookup.lookup(key);
        assertNotNull(value);
        assertFalse(value.isEmpty());
        System.out.println(key + " = " + value);
    }
    
    @Test
    public void testLookupWithDefaultInterpolator() {
        final StrLookup lookup = new Interpolator();
        String value = lookup.lookup("sys:" + TESTKEY);
        assertEquals(TESTVAL, value);
        value = lookup.lookup("env:PATH");
        assertNotNull(value);
        value = lookup.lookup("jndi:" + TEST_CONTEXT_RESOURCE_NAME);
        assertEquals(TEST_CONTEXT_NAME, value);
        value = lookup.lookup("date:yyyy-MM-dd");
        assertNotNull("No Date", value);
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final String today = format.format(new Date());
        assertEquals(value, today);
        assertLookupNotEmpty(lookup, "java:version");
        assertLookupNotEmpty(lookup, "java:runtime");
        assertLookupNotEmpty(lookup, "java:vm");
        assertLookupNotEmpty(lookup, "java:os");
        assertLookupNotEmpty(lookup, "java:locale");
        assertLookupNotEmpty(lookup, "java:hw");
    }
}

@@ -959,103 +959,47 @@
 ort 
-javax.naming.Context;%0Aimport javax.naming.InitialContext;%0Aimport javax.naming.NamingException;%0A
+org.apache.logging.log4j.ThreadContext;
 %0Aimp
@@ -1023,37 +1023,38 @@
 gging.log4j.
-ThreadContext
+junit.JndiRule
 ;%0Aimport org
@@ -1060,26 +1060,25 @@
 g.junit.
-After
 Class
+Rule
 ;%0Aimport
@@ -1088,27 +1088,20 @@
 g.junit.
-BeforeClass
+Test
 ;%0Aimport
@@ -1115,56 +1115,64 @@
 nit.
-Test;%0Aimport org.mockejb.jndi.MockContextFactory
+rules.ExternalResource;%0Aimport org.junit.rules.RuleChain
 ;%0A%0Ai
@@ -1513,75 +1513,135 @@
     
-private static Context context;%0A%0A    @BeforeClass%0A    public static
+@ClassRule%0A    public static RuleChain rules = RuleChain.outerRule(new ExternalResource() %7B%0A        @Override%0A        protected
  voi
@@ -1662,26 +1662,24 @@
 ows 
-NamingException %7B%0A
+Throwable %7B%0A    
     
@@ -1724,463 +1724,256 @@
 L);%0A
-%0A        MockContextFactory.setAsInitial();%0A        context = new InitialContext();%0A        context.bind(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME);%0A    %7D%0A%0A    @AfterClass%0A    public static void after() %7B%0A        try %7B%0A            context.close();%0A        %7D catch (final NamingException ignored) %7B%0A        %7D%0A        MockContextFactory.revertSetAsInitial();%0A%0A        System.clearProperty(TESTKEY);%0A    %7D
+        %7D%0A%0A        @Override%0A        protected void after() %7B%0A            System.clearProperty(TESTKEY);%0A        %7D%0A    %7D).around(new JndiRule(%0A        JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME));
 %0A%0A  
@@ -3045,20 +3045,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te

Refactor InterpolatorTest to use JndiRule