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.spi.mock;
018:
019: import java.util.Hashtable;
020:
021: import javax.naming.Binding;
022: import javax.naming.Context;
023: import javax.naming.Name;
024: import javax.naming.NameClassPair;
025: import javax.naming.NameParser;
026: import javax.naming.NamingEnumeration;
027: import javax.naming.NamingException;
028:
029: public class MockContext implements Context {
030: protected Hashtable<String, Object> props;
031:
032: static protected DazzleActionController actions;
033:
034: public MockContext() {
035: this .props = new Hashtable<String, Object>();
036: }
037:
038: @SuppressWarnings("unchecked")
039: public MockContext(Hashtable<?, ?> props) {
040: if (null != props) {
041: this .props = (Hashtable<String, Object>) props.clone();
042: }
043: }
044:
045: @Override
046: public boolean equals(Object obj) {
047: if (obj instanceof MockContext) {
048: MockContext theOther = (MockContext) obj;
049: boolean envmtEqual = (null == props ? null == theOther.props
050: : props.equals(theOther.props));
051: if (!envmtEqual) {
052: return false;
053: }
054:
055: return true;
056: }
057: return false;
058: }
059:
060: public static void setActionController(
061: DazzleActionController actionController) {
062: MockContext.actions = actionController;
063: }
064:
065: Object takeActions() throws NamingException {
066: if (actions == null) {
067: return DazzleActionController.RETURN_NORMAL;
068: }
069: Object obj = actions.doActions();
070: return obj;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see javax.naming.Context#addToEnvironment(java.lang.String,
077: * java.lang.Object)
078: */
079: public Object addToEnvironment(String s, Object o)
080: throws NamingException {
081: return this .props.put(s, o);
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
088: */
089: public void bind(Name n, Object o) throws NamingException {
090: InvokeRecord.set((String) this .props.get("url.schema"), n, o);
091: takeActions();
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
098: */
099: public void bind(String s, Object o) throws NamingException {
100: InvokeRecord.set((String) this .props.get("url.schema"), s, o);
101: takeActions();
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see javax.naming.Context#close()
108: */
109: public void close() throws NamingException {
110: InvokeRecord.set(null, "close");
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see javax.naming.Context#composeName(javax.naming.Name,
117: * javax.naming.Name)
118: */
119: public Name composeName(Name n, Name pfx) throws NamingException {
120: InvokeRecord.set((String) this .props.get("url.schema"), n, pfx);
121: takeActions();
122: return null;
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
129: */
130: public String composeName(String s, String pfx)
131: throws NamingException {
132: InvokeRecord.set((String) this .props.get("url.schema"), s, pfx);
133: takeActions();
134: return null;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see javax.naming.Context#createSubcontext(javax.naming.Name)
141: */
142: public Context createSubcontext(Name n) throws NamingException {
143: InvokeRecord.set((String) this .props.get("url.schema"), n);
144: takeActions();
145: return null;
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see javax.naming.Context#createSubcontext(java.lang.String)
152: */
153: public Context createSubcontext(String s) throws NamingException {
154: InvokeRecord.set((String) this .props.get("url.schema"), s);
155: takeActions();
156: return null;
157: }
158:
159: /*
160: * (non-Javadoc)
161: *
162: * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
163: */
164: public void destroySubcontext(Name n) throws NamingException {
165: InvokeRecord.set((String) this .props.get("url.schema"), n);
166: takeActions();
167:
168: }
169:
170: /*
171: * (non-Javadoc)
172: *
173: * @see javax.naming.Context#destroySubcontext(java.lang.String)
174: */
175: public void destroySubcontext(String s) throws NamingException {
176: InvokeRecord.set((String) this .props.get("url.schema"), s);
177: takeActions();
178:
179: }
180:
181: /*
182: * (non-Javadoc)
183: *
184: * @see javax.naming.Context#getEnvironment()
185: */
186: public Hashtable<String, Object> getEnvironment()
187: throws NamingException {
188: return this .props;
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see javax.naming.Context#getNameInNamespace()
195: */
196: public String getNameInNamespace() throws NamingException {
197: return (String) takeActions();
198: }
199:
200: /*
201: * (non-Javadoc)
202: *
203: * @see javax.naming.Context#getNameParser(javax.naming.Name)
204: */
205: public NameParser getNameParser(Name n) throws NamingException {
206: InvokeRecord.set((String) this .props.get("url.schema"), n);
207: takeActions();
208: return null;
209: }
210:
211: /*
212: * (non-Javadoc)
213: *
214: * @see javax.naming.Context#getNameParser(java.lang.String)
215: */
216: public NameParser getNameParser(String s) throws NamingException {
217: InvokeRecord.set((String) this .props.get("url.schema"), s);
218: takeActions();
219: return null;
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see javax.naming.Context#list(javax.naming.Name)
226: */
227: public NamingEnumeration<NameClassPair> list(Name n)
228: throws NamingException {
229: InvokeRecord.set((String) this .props.get("url.schema"), n);
230: return null;
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see javax.naming.Context#list(java.lang.String)
237: */
238: public NamingEnumeration<NameClassPair> list(String s)
239: throws NamingException {
240: InvokeRecord.set((String) this .props.get("url.schema"), s);
241: return null;
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see javax.naming.Context#listBindings(javax.naming.Name)
248: */
249: public NamingEnumeration<Binding> listBindings(Name n)
250: throws NamingException {
251: InvokeRecord.set((String) this .props.get("url.schema"), n);
252: return null;
253: }
254:
255: /*
256: * (non-Javadoc)
257: *
258: * @see javax.naming.Context#listBindings(java.lang.String)
259: */
260: public NamingEnumeration<Binding> listBindings(String s)
261: throws NamingException {
262: InvokeRecord.set((String) this .props.get("url.schema"), s);
263: return null;
264: }
265:
266: /*
267: * (non-Javadoc)
268: *
269: * @see javax.naming.Context#lookup(javax.naming.Name)
270: */
271: public Object lookup(Name n) throws NamingException {
272: InvokeRecord.set((String) this .props.get("url.schema"), n);
273: return null;
274: }
275:
276: /*
277: * (non-Javadoc)
278: *
279: * @see javax.naming.Context#lookup(java.lang.String)
280: */
281: public Object lookup(String s) throws NamingException {
282: InvokeRecord.set((String) this .props.get("url.schema"), s);
283: return null;
284: }
285:
286: /*
287: * (non-Javadoc)
288: *
289: * @see javax.naming.Context#lookupLink(javax.naming.Name)
290: */
291: public Object lookupLink(Name n) throws NamingException {
292: InvokeRecord.set((String) this .props.get("url.schema"), n);
293: return null;
294: }
295:
296: /*
297: * (non-Javadoc)
298: *
299: * @see javax.naming.Context#lookupLink(java.lang.String)
300: */
301: public Object lookupLink(String s) throws NamingException {
302: InvokeRecord.set((String) this .props.get("url.schema"), s);
303: return null;
304: }
305:
306: /*
307: * (non-Javadoc)
308: *
309: * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
310: */
311: public void rebind(Name n, Object o) throws NamingException {
312: InvokeRecord.set((String) this .props.get("url.schema"), n, o);
313:
314: }
315:
316: /*
317: * (non-Javadoc)
318: *
319: * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
320: */
321: public void rebind(String s, Object o) throws NamingException {
322: InvokeRecord.set((String) this .props.get("url.schema"), s, o);
323:
324: }
325:
326: /*
327: * (non-Javadoc)
328: *
329: * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
330: */
331: public Object removeFromEnvironment(String s)
332: throws NamingException {
333: InvokeRecord.set((String) this .props.get("url.schema"), s);
334: return this .props.remove(s);
335: }
336:
337: /*
338: * (non-Javadoc)
339: *
340: * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
341: */
342: public void rename(Name nOld, Name nNew) throws NamingException {
343: InvokeRecord.set((String) this .props.get("url.schema"), nOld,
344: nNew);
345:
346: }
347:
348: /*
349: * (non-Javadoc)
350: *
351: * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
352: */
353: public void rename(String sOld, String sNew) throws NamingException {
354: InvokeRecord.set((String) this .props.get("url.schema"), sOld,
355: sNew);
356:
357: }
358:
359: /*
360: * (non-Javadoc)
361: *
362: * @see javax.naming.Context#unbind(javax.naming.Name)
363: */
364: public void unbind(Name n) throws NamingException {
365: InvokeRecord.set((String) this .props.get("url.schema"), n);
366:
367: }
368:
369: /*
370: * (non-Javadoc)
371: *
372: * @see javax.naming.Context#unbind(java.lang.String)
373: */
374: public void unbind(String s) throws NamingException {
375: InvokeRecord.set((String) this .props.get("url.schema"), s);
376:
377: }
378:
379: }
|