View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jndi;
5   
6   import static org.hamcrest.MatcherAssert.assertThat;
7   
8   import java.util.Hashtable;
9   
10  import javax.naming.Binding;
11  import javax.naming.Context;
12  import javax.naming.Name;
13  import javax.naming.NameClassPair;
14  import javax.naming.NameParser;
15  import javax.naming.NamingEnumeration;
16  import javax.naming.NamingException;
17  
18  import com.github.smokestack.exception.NotYetImplementedException;
19  
20  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
21  import org.apache.commons.lang.builder.ToStringStyle;
22  import org.hamcrest.core.IsNot;
23  import org.omg.stub.java.rmi._Remote_Stub;
24  
25  /**
26   * @author gliptak
27   *
28   */
29  public class MockContext implements Context {
30  
31  	public enum ContextState {NEW, CLOSE};
32  	
33  	protected ContextState mockState=ContextState.NEW;
34  	
35  	public Hashtable environment=new Hashtable();
36  	
37  	public Hashtable<String, Object> bindings=new Hashtable<String, Object>();
38  
39  	public MockContext(Hashtable environment) {
40  		if (environment!=null){
41  			this.environment=(Hashtable) environment.clone();			
42  		}
43  	}
44  
45  	/* (non-Javadoc)
46  	 * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
47  	 */
48  	public Object addToEnvironment(String propName, Object propVal) throws NamingException {
49  		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
50          _addToEnvironment(propName, propVal);
51          return environment.put(propName, propVal);
52  	}
53  
54  	public Object _addToEnvironment(String propName, Object propVal) throws NamingException {
55  		return null;
56  	}
57  
58  	/* (non-Javadoc)
59  	 * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
60  	 */
61  	public void bind(Name name, Object obj) throws NamingException {
62  		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
63  		_bind(name, obj);
64  	}
65  
66  	public void _bind(Name name, Object obj) throws NamingException {
67  		throw new NotYetImplementedException();
68  	}
69  
70  	/* (non-Javadoc)
71  	 * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
72  	 */
73  	public void bind(String name, Object obj) throws NamingException {
74  		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
75  		_bind(name, obj);
76  		rebind(name, obj);
77  	}
78  
79  	public void _bind(String name, Object obj) throws NamingException {
80  	}
81  
82  	/* (non-Javadoc)
83  	 * @see javax.naming.Context#close()
84  	 */
85  	public void close() throws NamingException {
86  		_close();
87  		mockState=ContextState.CLOSE;
88  		MockInitialContextFactory.releaseSingleton();
89  	}
90  
91  	public void _close() throws NamingException {
92  	}
93  
94  	/* (non-Javadoc)
95  	 * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
96  	 */
97  	public Name composeName(Name name, Name prefix) throws NamingException {
98  		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
99  		return _composeName(name, prefix);
100 	}
101 
102 	public Name _composeName(Name name, Name prefix) throws NamingException {
103 		throw new NotYetImplementedException();
104 	}
105 
106 	/* (non-Javadoc)
107 	 * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
108 	 */
109 	public String composeName(String name, String prefix) throws NamingException {
110 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
111 		return _composeName(name, prefix);
112 	}
113 
114 	public String _composeName(String name, String prefix) throws NamingException {
115 		throw new NotYetImplementedException();
116 	}
117 
118 	/* (non-Javadoc)
119 	 * @see javax.naming.Context#createSubcontext(javax.naming.Name)
120 	 */
121 	public Context createSubcontext(Name name) throws NamingException {
122 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
123 		return _createSubcontext(name);
124 	}
125 
126 	public Context _createSubcontext(Name name) throws NamingException {
127 		throw new NotYetImplementedException();
128 	}
129 
130 	/* (non-Javadoc)
131 	 * @see javax.naming.Context#createSubcontext(java.lang.String)
132 	 */
133 	public Context createSubcontext(String name) throws NamingException {
134 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
135 		return _createSubcontext(name);
136 	}
137 
138 	public Context _createSubcontext(String name) throws NamingException {
139 		throw new NotYetImplementedException();
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
144 	 */
145 	public void destroySubcontext(Name name) throws NamingException {
146 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
147 		_destroySubcontext(name);
148 	}
149 
150 	public void _destroySubcontext(Name name) throws NamingException {
151 		throw new NotYetImplementedException();
152 	}
153 
154 	/* (non-Javadoc)
155 	 * @see javax.naming.Context#destroySubcontext(java.lang.String)
156 	 */
157 	public void destroySubcontext(String name) throws NamingException {
158 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
159 		_destroySubcontext(name);
160 	}
161 
162 	public void _destroySubcontext(String name) throws NamingException {
163 		throw new NotYetImplementedException();
164 	}
165 
166 	/* (non-Javadoc)
167 	 * @see javax.naming.Context#getEnvironment()
168 	 */
169 	public Hashtable<?, ?> getEnvironment() throws NamingException {
170 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
171 		_getEnvironment();
172 		return (Hashtable)environment.clone();
173 	}
174 
175 	public Hashtable<?, ?> _getEnvironment() throws NamingException {
176 		return null;
177 	}
178 
179 	/* (non-Javadoc)
180 	 * @see javax.naming.Context#getNameInNamespace()
181 	 */
182 	public String getNameInNamespace() throws NamingException {
183 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
184 		return _getNameInNamespace();
185 	}
186 
187 	public String _getNameInNamespace() throws NamingException {
188 		throw new NotYetImplementedException();
189 	}
190 
191 	/* (non-Javadoc)
192 	 * @see javax.naming.Context#getNameParser(javax.naming.Name)
193 	 */
194 	public NameParser getNameParser(Name name) throws NamingException {
195 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
196 		return _getNameParser(name);
197 	}
198 
199 	public NameParser _getNameParser(Name name) throws NamingException {
200 		throw new NotYetImplementedException();
201 	}
202 
203 	/* (non-Javadoc)
204 	 * @see javax.naming.Context#getNameParser(java.lang.String)
205 	 */
206 	public NameParser getNameParser(String name) throws NamingException {
207 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
208 		return _getNameParser(name);
209 	}
210 
211 	public NameParser _getNameParser(String name) throws NamingException {
212 		throw new NotYetImplementedException();
213 	}
214 
215 	/* (non-Javadoc)
216 	 * @see javax.naming.Context#list(javax.naming.Name)
217 	 */
218 	public NamingEnumeration<NameClassPair> list(Name name)	throws NamingException {
219 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
220 		return _list(name);
221 	}
222 
223 	public NamingEnumeration<NameClassPair> _list(Name name)	throws NamingException {
224 		throw new NotYetImplementedException();
225 	}
226 
227 	/* (non-Javadoc)
228 	 * @see javax.naming.Context#list(java.lang.String)
229 	 */
230 	public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
231 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
232 		return _list(name);
233 	}
234 
235 	public NamingEnumeration<NameClassPair> _list(String name) throws NamingException {
236 		throw new NotYetImplementedException();
237 	}
238 
239 	/* (non-Javadoc)
240 	 * @see javax.naming.Context#listBindings(javax.naming.Name)
241 	 */
242 	public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
243 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
244 		return _listBindings(name);
245 	}
246 
247 	public NamingEnumeration<Binding> _listBindings(Name name) throws NamingException {
248 		throw new NotYetImplementedException();
249 	}
250 
251 	/* (non-Javadoc)
252 	 * @see javax.naming.Context#listBindings(java.lang.String)
253 	 */
254 	public NamingEnumeration<Binding> listBindings(String name)	throws NamingException {
255 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
256 		return _listBindings(name);
257 	}
258 
259 	public NamingEnumeration<Binding> _listBindings(String name)	throws NamingException {
260 		throw new NotYetImplementedException();
261 	}
262 
263 	/* (non-Javadoc)
264 	 * @see javax.naming.Context#lookup(javax.naming.Name)
265 	 */
266 	public Object lookup(Name name) throws NamingException {
267 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
268 		return _lookup(name);
269 	}
270 
271 	public Object _lookup(Name name) throws NamingException {
272 		throw new NotYetImplementedException();
273 	}
274 
275 	/* (non-Javadoc)
276 	 * @see javax.naming.Context#lookup(java.lang.String)
277 	 */
278 	public Object lookup(String name) throws NamingException {
279 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
280 		_lookup(name);
281 		Object o=bindings.get(name);
282 		if (o==null){
283 			throw new NamingException("not bound for "+name);
284 		}
285 		return o;
286 	}
287 
288 	public Object _lookup(String name) throws NamingException {
289 		return null;
290 	}
291 
292 	/* (non-Javadoc)
293 	 * @see javax.naming.Context#lookupLink(javax.naming.Name)
294 	 */
295 	public Object lookupLink(Name name) throws NamingException {
296 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
297 		return _lookupLink(name);
298 	}
299 
300 	public Object _lookupLink(Name name) throws NamingException {
301 		throw new NotYetImplementedException();
302 	}
303 
304 	/* (non-Javadoc)
305 	 * @see javax.naming.Context#lookupLink(java.lang.String)
306 	 */
307 	public Object lookupLink(String name) throws NamingException {
308 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
309 		return _lookupLink(name);
310 	}
311 
312 	public Object _lookupLink(String name) throws NamingException {
313 		throw new NotYetImplementedException();
314 	}
315 
316 	/* (non-Javadoc)
317 	 * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
318 	 */
319 	public void rebind(Name name, Object obj) throws NamingException {
320 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
321 		_rebind(name, obj);
322 	}
323 
324 	public void _rebind(Name name, Object obj) throws NamingException {
325 		throw new NotYetImplementedException();
326 	}
327 
328 	/* (non-Javadoc)
329 	 * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
330 	 */
331 	public void rebind(String name, Object obj) throws NamingException {
332 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
333 		_rebind(name, obj);
334 		bindings.put(name, obj);
335 	}
336 
337 	public void _rebind(String name, Object obj) throws NamingException {
338 	}
339 
340 	/* (non-Javadoc)
341 	 * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
342 	 */
343 	public Object removeFromEnvironment(String propName) throws NamingException {
344 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
345 		_removeFromEnvironment(propName);
346 		return environment.remove(propName);
347 	}
348 
349 	public Object _removeFromEnvironment(String propName) throws NamingException {
350 		return null;
351 	}
352 
353 	/* (non-Javadoc)
354 	 * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
355 	 */
356 	public void rename(Name oldName, Name newName) throws NamingException {
357 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
358 		_rename(oldName, newName);
359 	}
360 
361 	public void _rename(Name oldName, Name newName) throws NamingException {
362 		throw new NotYetImplementedException();
363 	}
364 
365 	/* (non-Javadoc)
366 	 * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
367 	 */
368 	public void rename(String oldName, String newName) throws NamingException {
369 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
370 		_rename(oldName, newName);
371 		Object o=bindings.get(oldName);
372 		if (o==null){
373 			throw new NamingException("not bound for "+oldName);
374 		}
375 		unbind(oldName);
376 		rebind(newName, o);
377 	}
378 
379 	public void _rename(String oldName, String newName) throws NamingException {
380 	}
381 
382 	/* (non-Javadoc)
383 	 * @see javax.naming.Context#unbind(javax.naming.Name)
384 	 */
385 	public void unbind(Name name) throws NamingException {
386 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
387 		_unbind(name);
388 	}
389 
390 	public void _unbind(Name name) throws NamingException {
391 		throw new NotYetImplementedException();
392 	}
393 
394 	/* (non-Javadoc)
395 	 * @see javax.naming.Context#unbind(java.lang.String)
396 	 */
397 	public void unbind(String name) throws NamingException {
398 		assertThat("mockState", mockState, IsNot.not(ContextState.CLOSE));
399 		_unbind(name);
400 		bindings.remove(name);
401 	}
402 	
403 	public void _unbind(String name) throws NamingException {
404 	}
405 	
406 	@Override
407 	public String toString(){
408 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
409 	}
410 }