summaryrefslogtreecommitdiff
path: root/java/client/src/old_test/java/org/apache/qpid/test/unit/jndi/PropertiesFileInitialContextFactoryTest.java
blob: 5ab5722146557e4585e36548cf9f404b2a5011d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 *
 * 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.qpid.test.unit.jndi;

import org.apache.qpid.client.AMQConnectionFactory;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQTopic;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import java.util.Properties;
import java.io.InputStream;


import junit.framework.TestCase;

public class PropertiesFileInitialContextFactoryTest extends TestCase
{
    InitialContextFactory contextFactory;
    Properties _properties;
    Properties _fileProperties;

    protected void setUp() throws Exception
    {
        super.setUp();

        //create simple set of hardcoded props
        _properties = new Properties();
        _properties.put("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
        _properties.put("connectionfactory.local", "amqp://guest:guest@clientid/testpath?brokerlist='vm://:1'");
        _properties.put("queue.MyQueue", "example.MyQueue");
        _properties.put("topic.ibmStocks", "stocks.nyse.ibm");
        _properties.put("destination.direct", "direct://amq.direct//directQueue");

        //create properties from file as a more realistic test
        _fileProperties = new Properties();
        ClassLoader cl = this.getClass().getClassLoader();
        InputStream is = cl.getResourceAsStream("org/apache/qpid/test/unit/jndi/example.properties");
        _fileProperties.load(is);
    }

    /**
     * Test using hardcoded properties
     */
    public void testWithoutFile()
    {
        Context ctx = null;

        try
        {
            ctx = new InitialContext(_properties);
        }
        catch (NamingException ne)
        {
            fail("Error loading context:" + ne);
        }

        checkPropertiesMatch(ctx, "Using hardcoded properties: ");
    }

    /**
     * Test using properties from example file
     */
    public void testWithFile()
    {
        Context ctx = null;

        try
        {
            ctx = new InitialContext(_fileProperties);
        }
        catch (Exception e)
        {
            fail("Error loading context:" + e);
        }

        checkPropertiesMatch(ctx, "Using properties from file: ");
    }

    public void tearDown()
    {
        _properties = null;
        _fileProperties = null;
    }

    public static junit.framework.Test suite()
    {
        return new junit.framework.TestSuite(PropertiesFileInitialContextFactoryTest.class);
    }

    private void checkPropertiesMatch(Context ctx, String errorInfo)
    {
        try
        {
            AMQConnectionFactory cf = (AMQConnectionFactory) ctx.lookup("local");
            assertEquals("amqp://guest:guest@clientid/testpath?brokerlist='vm://:1'", cf.getConnectionURL().toString());
        }
        catch (NamingException ne)
        {
            fail(errorInfo + "Unable to create Connection Factory:" + ne);
        }

        try
        {
            AMQQueue queue = (AMQQueue) ctx.lookup("MyQueue");
            assertEquals("example.MyQueue", queue.getRoutingKey().toString());
        }
        catch (NamingException ne)
        {
            fail(errorInfo + "Unable to create queue:" + ne);
        }

        try
        {
            AMQTopic topic = (AMQTopic) ctx.lookup("ibmStocks");
            assertEquals("stocks.nyse.ibm", topic.getTopicName().toString());
        }
        catch (Exception ne)
        {
            fail(errorInfo + "Unable to create topic:" + ne);
        }

        try
        {
            AMQQueue direct = (AMQQueue) ctx.lookup("direct");
            assertEquals("directQueue", direct.getRoutingKey().toString());
        }
        catch (NamingException ne)
        {
            fail(errorInfo + "Unable to create direct destination:" + ne);
        }
    }
}