almira.weblogic.JndiInjectorTest.java Source code

Java tutorial

Introduction

Here is the source code for almira.weblogic.JndiInjectorTest.java

Source

/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed 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 almira.weblogic;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map.Entry;
import java.util.Properties;

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

import junit.framework.Assert;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;

import almira.weblogic.JndiInjector;

public final class JndiInjectorTest {

    private Context ic = null;
    private JndiInjector injector = null;

    private static final String[] ARGUMENTS = { "p1=v1,p2=v2", "p3=v3" };
    private static final Properties PROPERTIES = new Properties();

    @BeforeClass
    public static void init() throws IOException {
        InputStreamReader reader = new InputStreamReader(
                new ByteArrayInputStream("p1=v1\np2=v2\np3=v3\n".getBytes()));

        PROPERTIES.load(reader);
    }

    @Before
    public void setUp() throws NamingException, IOException {
        SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        ic = new InitialContext();
        injector = new JndiInjector(ic);
    }

    @Test
    public void bindsPairToJndiContext() throws NamingException {
        injector.bind(PROPERTIES);
        Assert.assertEquals("v1", ic.lookup(JndiInjector.ROOT + "/p1"));
        Assert.assertEquals("v2", ic.lookup(JndiInjector.ROOT + "/p2"));
        Assert.assertEquals("v3", ic.lookup(JndiInjector.ROOT + "/p3"));
    }

    @Test
    public void tokenizesPropertyPairs() {
        Properties parsedProperties = injector.getProperties(ARGUMENTS);
        for (Entry<Object, Object> entry : parsedProperties.entrySet()) {
            Object key = entry.getKey();
            Assert.assertTrue(PROPERTIES.containsKey(key));
            Assert.assertEquals(PROPERTIES.get(key), entry.getValue());
        }
    }
}