001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.tests.javax.naming;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.PrintStream;
023: import java.net.URL;
024: import java.net.URLClassLoader;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027: import java.util.Hashtable;
028:
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: import junit.framework.TestCase;
033: import org.apache.harmony.jndi.tests.javax.naming.util.Log;
034:
035: public class InitialContextAppTest extends TestCase {
036: private static final Log log = new Log(InitialContextAppTest.class);
037:
038: public void testConstructor_App() throws NamingException,
039: IOException {
040: // Comment this test case out because this test case
041: // needs complex configuration about jndi properties.
042:
043: // log.setMethod("testConstructor_App");
044: // InitialContext context = new InitialContext();
045: // Hashtable props = context.getEnvironment();
046: // // printHashtable(props);
047: // Hashtable expected = TestInitialContextLib.readAllProps(null);
048: // assertEquals(expected, props);
049: }
050:
051: /**
052: * regression: Harmony-4942
053: *
054: */
055: public void testConstructor() throws Exception {
056: final File file1 = new File("src/test/resources/test1");
057: if (!file1.exists()) {
058: file1.mkdir();
059: }
060:
061: URL url = file1.toURL();
062: URLClassLoader cltest1 = new URLClassLoader(new URL[] { url },
063: Thread.currentThread().getContextClassLoader());
064: Thread test1 = new Thread(new Runnable() {
065:
066: public void run() {
067: try {
068: File propsFile = new File(file1.toString()
069: + "/jndi.properties");
070:
071: FileOutputStream fos = new FileOutputStream(
072: propsFile);
073:
074: PrintStream ps = new PrintStream(fos);
075: ps
076: .println("java.naming.factory.initial=org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory");
077: ps.println("java.naming.provider.url=http://test1");
078: ps.close();
079:
080: InitialContext context = new InitialContext();
081: Hashtable<?, ?> env = context.getEnvironment();
082: assertEquals(
083: "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory",
084: env.get("java.naming.factory.initial"));
085: assertEquals("http://test1", env
086: .get("java.naming.provider.url"));
087:
088: propsFile.delete();
089:
090: // create new properties file with different values
091: fos = new FileOutputStream(propsFile);
092: ps = new PrintStream(fos);
093: ps
094: .println("java.naming.factory.initial=not.exist.ContextFactory");
095: ps
096: .println("java.naming.provider.url=http://test1.new");
097: ps.close();
098:
099: context = new InitialContext();
100: env = context.getEnvironment();
101: assertEquals(
102: "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory",
103: env.get("java.naming.factory.initial"));
104: assertEquals("http://test1", env
105: .get("java.naming.provider.url"));
106:
107: propsFile.delete();
108: } catch (Exception e) {
109: fail(e.getMessage());
110: }
111: }
112:
113: });
114: // use different classloader
115: test1.setContextClassLoader(cltest1);
116:
117: test1.start();
118:
119: final File file2 = new File("src/test/resources/test2");
120: if (!file2.exists()) {
121: file2.mkdir();
122: }
123: url = file2.toURL();
124: URLClassLoader cltest2 = new URLClassLoader(new URL[] { url },
125: Thread.currentThread().getContextClassLoader());
126:
127: Thread test2 = new Thread(new Runnable() {
128:
129: public void run() {
130: try {
131: File propsFile = new File(file2.toString()
132: + "/jndi.properties");
133: FileOutputStream fos = new FileOutputStream(
134: propsFile);
135: PrintStream ps = new PrintStream(fos);
136: ps
137: .println("java.naming.factory.initial=org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory");
138: ps.println("java.naming.provider.url=http://test2");
139: ps.close();
140:
141: InitialContext context = new InitialContext();
142: Hashtable<?, ?> env = context.getEnvironment();
143: assertEquals(
144: "org.apache.harmony.jndi.tests.javax.naming.spi.mock.MockContextFactory",
145: env.get("java.naming.factory.initial"));
146: assertEquals("http://test2", env
147: .get("java.naming.provider.url"));
148:
149: propsFile.delete();
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154:
155: });
156:
157: // use different classloader
158: test2.setContextClassLoader(cltest2);
159: test2.start();
160:
161: Thread.sleep(1000);
162: file1.deleteOnExit();
163: file2.deleteOnExit();
164: }
165:
166: void printHashtable(Hashtable<?, ?> env) {
167: // TO DO: Need to remove
168: Enumeration<?> keys = env.keys();
169: while (keys.hasMoreElements()) {
170: Object key = keys.nextElement();
171: log.log(key + "=" + env.get(key));
172: }
173: }
174: }
|