001: package com.mockrunner.test.ejb;
002:
003: import java.util.Hashtable;
004: import java.util.Properties;
005:
006: import javax.naming.Context;
007: import javax.naming.Name;
008: import javax.naming.NameParser;
009: import javax.naming.NamingEnumeration;
010: import javax.naming.NamingException;
011: import javax.naming.spi.InitialContextFactory;
012:
013: import org.mockejb.jndi.MockContext;
014: import org.mockejb.jndi.MockContextFactory;
015:
016: public class TestJNDI {
017: public static void saveProperties(Properties properties) {
018: String factory = System
019: .getProperty(Context.INITIAL_CONTEXT_FACTORY);
020: if (null != factory
021: && !factory.equals(MockContextFactory.class.getName())) {
022: properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
023: factory);
024: }
025: String urlPrefix = System.getProperty(Context.URL_PKG_PREFIXES);
026: if (null != urlPrefix && !urlPrefix.equals("org.mockejb.jndi")) {
027: properties.setProperty(Context.URL_PKG_PREFIXES, factory);
028: }
029: }
030:
031: public static void restoreProperties(Properties properties) {
032: if (null != properties
033: .getProperty(Context.INITIAL_CONTEXT_FACTORY)) {
034: System
035: .setProperty(
036: Context.INITIAL_CONTEXT_FACTORY,
037: properties
038: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
039: } else {
040: System.getProperties().remove(
041: Context.INITIAL_CONTEXT_FACTORY);
042: }
043: if (null != properties.getProperty(Context.URL_PKG_PREFIXES)) {
044: System.setProperty(Context.URL_PKG_PREFIXES, properties
045: .getProperty(Context.URL_PKG_PREFIXES));
046: } else {
047: System.getProperties().remove(Context.URL_PKG_PREFIXES);
048: }
049: }
050:
051: public static void unbind(Context context) throws Exception {
052: try {
053: context.unbind("myJNDIName");
054: } catch (NamingException exc) {
055: //ignore
056: }
057: try {
058: context.unbind("javax.transaction.UserTransaction");
059: } catch (NamingException exc) {
060: //ignore
061: }
062: try {
063: context.unbind("java:comp/UserTransaction");
064: } catch (NamingException exc) {
065: //ignore
066: }
067: }
068:
069: public static class TestContextFactory implements
070: InitialContextFactory {
071: public Context getInitialContext(Hashtable env)
072: throws NamingException {
073: return new TestContext();
074: }
075: }
076:
077: public static class TestContext implements Context {
078: public Object addToEnvironment(String propName, Object propVal)
079: throws NamingException {
080: return null;
081: }
082:
083: public void bind(Name name, Object obj) throws NamingException {
084:
085: }
086:
087: public void bind(String name, Object obj)
088: throws NamingException {
089:
090: }
091:
092: public void close() throws NamingException {
093:
094: }
095:
096: public Name composeName(Name name, Name prefix)
097: throws NamingException {
098: return null;
099: }
100:
101: public String composeName(String name, String prefix)
102: throws NamingException {
103: return null;
104: }
105:
106: public Context createSubcontext(Name name)
107: throws NamingException {
108: return null;
109: }
110:
111: public Context createSubcontext(String name)
112: throws NamingException {
113: return null;
114: }
115:
116: public void destroySubcontext(Name name) throws NamingException {
117:
118: }
119:
120: public void destroySubcontext(String name)
121: throws NamingException {
122:
123: }
124:
125: public Hashtable getEnvironment() throws NamingException {
126: return null;
127: }
128:
129: public String getNameInNamespace() throws NamingException {
130: return null;
131: }
132:
133: public NameParser getNameParser(Name name)
134: throws NamingException {
135: return null;
136: }
137:
138: public NameParser getNameParser(String name)
139: throws NamingException {
140: return null;
141: }
142:
143: public NamingEnumeration list(Name name) throws NamingException {
144: return null;
145: }
146:
147: public NamingEnumeration list(String name)
148: throws NamingException {
149: return null;
150: }
151:
152: public NamingEnumeration listBindings(Name name)
153: throws NamingException {
154: return null;
155: }
156:
157: public NamingEnumeration listBindings(String name)
158: throws NamingException {
159: return null;
160: }
161:
162: public Object lookup(Name name) throws NamingException {
163: return null;
164: }
165:
166: public Object lookup(String name) throws NamingException {
167: return "TestObject";
168: }
169:
170: public Object lookupLink(Name name) throws NamingException {
171: return null;
172: }
173:
174: public Object lookupLink(String name) throws NamingException {
175: return null;
176: }
177:
178: public void rebind(Name name, Object obj)
179: throws NamingException {
180:
181: }
182:
183: public void rebind(String name, Object obj)
184: throws NamingException {
185:
186: }
187:
188: public Object removeFromEnvironment(String propName)
189: throws NamingException {
190: return null;
191: }
192:
193: public void rename(Name oldName, Name newName)
194: throws NamingException {
195:
196: }
197:
198: public void rename(String oldName, String newName)
199: throws NamingException {
200:
201: }
202:
203: public void unbind(Name name) throws NamingException {
204:
205: }
206:
207: public void unbind(String name) throws NamingException {
208:
209: }
210: }
211:
212: public static class NullContext extends MockContext {
213: public NullContext() {
214: super(null);
215: }
216: }
217: }
|