();
148 |
149 | for (final String fixtureName : fixtureNames) {
150 | logger.trace("Attempting to load database fixture \"" + fixtureName + "\"");
151 | final IDataSet dataSet = attemptToLoadFixture(fixtureName);
152 | if (dataSet != null) {
153 | dataSets.add(dataSet);
154 | }
155 | }
156 |
157 | if (dataSets.isEmpty()) {
158 | logger.warn("Found 0 data sets!");
159 | } else {
160 | final CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets.toArray(new IDataSet[dataSets
161 | .size()]));
162 | jdbcDatabaseTester.setDataSet(compositeDataSet);
163 | }
164 | }
165 |
166 | /**
167 | * @param fixtureName
168 | * the fixture name
169 | * @return {@link IDataSet}
170 | */
171 | private IDataSet attemptToLoadFixture(final String fixtureName) {
172 | IDataSet dataSet = null;
173 |
174 | try {
175 | final InputStream in = ClassLoader.getSystemResourceAsStream(fixtureName);
176 | try {
177 | if (in != null) {
178 | if (fixtureName.endsWith(".xml")) {
179 | dataSet = new XmlDataSet(in);
180 | }
181 | }
182 | } finally {
183 | in.close();
184 | }
185 | } catch (final Exception e) {
186 | logger.warn(e.getMessage(), e);
187 | }
188 | return dataSet;
189 | }
190 |
191 | /**
192 | * {@inheritDoc}
193 | *
194 | * @see junit.rules.TestFixture#tearDown()
195 | */
196 | @Override
197 | protected final void tearDown() throws Throwable {
198 | jdbcDatabaseTester.onTearDown();
199 | entityManagerFactory.close();
200 | }
201 |
202 | /**
203 | * @return {@link EntityManager}
204 | * @see javax.persistence.EntityManagerFactory#createEntityManager()
205 | */
206 | public final EntityManager getEntityManager() {
207 | return this.entityManager;
208 | }
209 |
210 | }
211 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | junit-rules
6 | junit-rules
7 | JUnit Rules Library
8 | 0.5.1
9 |
10 |
11 |
12 | MIT License
13 | LICENSE.txt
14 |
15 |
16 |
17 |
18 |
19 | UTF-8
20 |
21 |
22 |
23 |
24 | org.slf4j
25 | slf4j-log4j12
26 | 1.5.10
27 | test
28 |
29 |
30 | log4j
31 | log4j
32 |
33 |
34 | true
35 |
36 |
37 | log4j
38 | log4j
39 | 1.2.16
40 | jar
41 | test
42 |
43 |
44 |
45 |
46 | junit
47 | junit
48 | 4.9
49 |
50 |
51 |
52 |
53 |
54 | org.jboss.weld.se
55 | weld-se-core
56 | 1.1.0.Final
57 | true
58 |
59 |
60 |
61 |
62 | org.mortbay.jetty
63 | jetty
64 | 6.1.20
65 | provided
66 | true
67 |
68 |
69 |
70 |
71 | org.hibernate
72 | hibernate-core
73 | 3.3.2.GA
74 | compile
75 |
76 |
77 | slf4j-api
78 | org.slf4j
79 |
80 |
81 | true
82 |
83 |
84 | org.hibernate
85 | hibernate-entitymanager
86 | 3.4.0.GA
87 | compile
88 |
89 |
90 | slf4j-api
91 | org.slf4j
92 |
93 |
94 | true
95 |
96 |
97 |
98 |
99 | org.apache.derby
100 | derby
101 | 10.6.2.1
102 | compile
103 | true
104 |
105 |
106 |
107 |
108 | org.dbunit
109 | dbunit
110 | 2.4.8
111 | compile
112 |
113 |
114 | slf4j-api
115 | org.slf4j
116 |
117 |
118 | junit
119 | junit
120 |
121 |
122 | true
123 |
124 |
125 |
126 |
127 |
128 | org.slf4j
129 | slf4j-api
130 | 1.5.10
131 | compile
132 |
133 |
134 |
135 |
136 |
137 | javax.persistence
138 | persistence-api
139 | 1.0
140 | provided
141 |
142 |
143 |
144 |
145 |
146 | org.jboss.repositories.public-jboss
147 | http://repository.jboss.org/nexus/content/groups/public-jboss
148 |
149 | true
150 |
151 |
152 | false
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 | org.apache.maven.plugins
162 | maven-compiler-plugin
163 | 2.3.2
164 |
165 | 1.6
166 | 1.6
167 |
168 |
169 |
170 |
171 |
172 | org.apache.maven.plugins
173 | maven-checkstyle-plugin
174 | 2.6
175 |
176 | ${basedir}/etc/checkstyle.xml
177 | config_loc=${project.basedir}/etc
178 | true
179 | true
180 | UTF-8
181 |
182 |
183 |
184 | test
185 |
186 | check
187 |
188 |
189 | true
190 | warning
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 | org.apache.maven.plugins
199 | maven-source-plugin
200 | 2.1.2
201 |
202 |
203 | attach-sources
204 | verify
205 |
206 | jar
207 |
208 |
209 |
210 |
211 |
212 |
213 | org.apache.maven.plugins
214 | maven-surefire-plugin
215 | 2.7.1
216 |
217 |
218 |
219 | derby.system.home
220 | ${project.build.directory}
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 | org.apache.maven.plugins
234 | maven-javadoc-plugin
235 | 2.7
236 |
237 |
238 |
239 |
240 |
241 |
--------------------------------------------------------------------------------
/src/main/java/junit/rules/util/Reflection.java:
--------------------------------------------------------------------------------
1 | /**
2 | * junit-rules: JUnit Rules Library
3 | *
4 | * Copyright (c) 2009-2011 by Alistair A. Israel.
5 | * This software is made available under the terms of the MIT License.
6 | *
7 | * Created Oct 15, 2009
8 | */
9 | package junit.rules.util;
10 |
11 | import java.lang.reflect.Field;
12 | import java.lang.reflect.InvocationTargetException;
13 | import java.lang.reflect.Method;
14 | import java.util.Iterator;
15 |
16 | /**
17 | *
18 | * Provides a set of utility methods (and classes) for reflection.
19 | *
20 | *
21 | * Basically, a clever way to be able to write:
22 | *
23 | *
24 | *
25 | * Reflection.set(field).of(target).to(value);
26 | *
27 | *
28 | * @author Alistair A. Israel
29 | * @since 0.3.1
30 | */
31 | public final class Reflection {
32 |
33 | /**
34 | * Utility classes should not have a public or default constructor.
35 | */
36 | private Reflection() {
37 | // noop
38 | }
39 |
40 | /**
41 | * @param field
42 | * the Field to set
43 | * @return {@link FieldWrapper}
44 | */
45 | public static FieldWrapper set(final Field field) {
46 | return new FieldWrapper(field);
47 | }
48 |
49 | /**
50 | *
51 | * Lets us write ReflectionUtils.set(field).
52 | *
53 | */
54 | public static class FieldWrapper {
55 |
56 | private final Field field;
57 |
58 | /**
59 | * @param field
60 | * {@link Field}
61 | */
62 | public FieldWrapper(final Field field) {
63 | this.field = field;
64 | }
65 |
66 | /**
67 | * @param target
68 | * the object whose field we're setting
69 | * @return Setter
70 | */
71 | public final Setter of(final Object target) {
72 | return new Setter(target);
73 | }
74 |
75 | /**
76 | *
77 | * Lets us write ReflectionUtils.set(field).of(target).
78 | *
79 | */
80 | public class Setter {
81 |
82 | private final Object target;
83 |
84 | /**
85 | * @param target
86 | * the object to set the field value for
87 | */
88 | public Setter(final Object target) {
89 | this.target = target;
90 | }
91 |
92 | /**
93 | *
94 | * Let's us write ReflectionUtils.set(field).of(target).to(value) .
95 | *
96 | *
97 | * @param value
98 | * the value to set the field to
99 | */
100 | public final void to(final Object value) {
101 | quietlySetField(field, target, value);
102 | }
103 | }
104 | }
105 |
106 | /**
107 | * @param field
108 | * the field to set
109 | * @param target
110 | * the object whose field we're setting
111 | * @param value
112 | * the value to set it to
113 | */
114 | public static void quietlySetField(final Field field, final Object target, final Object value) {
115 | try {
116 | final boolean accessible = field.isAccessible();
117 | if (!accessible) {
118 | field.setAccessible(true);
119 | }
120 | field.set(target, value);
121 | if (!accessible) {
122 | field.setAccessible(false);
123 | }
124 | } catch (final IllegalAccessException e) {
125 | throw new RuntimeException(e.getMessage(), e);
126 | }
127 | }
128 |
129 | /**
130 | *
131 | * Allows us to cleverly write:
132 | *
133 | *
134 | *
135 | * Reflection.invoke(method).on(object);
136 | *
137 | *
138 | * @author Alistair A. Israel
139 | * @since 0.3.1
140 | */
141 | public static class MethodInvoker {
142 |
143 | private final Method method;
144 |
145 | /**
146 | * @param method
147 | * the method to invoke
148 | */
149 | public MethodInvoker(final Method method) {
150 | this.method = method;
151 | }
152 |
153 | /**
154 | * @param object
155 | * the ojbect to invoke the method on
156 | * @param params
157 | * any parameters to the method invocation
158 | * @return any return value
159 | */
160 | public final Object on(final Object object, final Object... params) {
161 | return quietlyInvokeMethod(object, method, params);
162 | }
163 | }
164 |
165 | /**
166 | * @param method
167 | * the method to invoke
168 | * @return {@link MethodInvoker}
169 | */
170 | public static MethodInvoker invoke(final Method method) {
171 | return new MethodInvoker(method);
172 | }
173 |
174 |
175 | /**
176 | * @param clazz
177 | * the {@link Class}
178 | * @param methodName
179 | * the name of the method we're interested in
180 | * @param parameterTypes
181 | * the parameter types
182 | * @return the {@link Method} found, or {@code null} if no matching method is found
183 | * @since 0.5.1
184 | */
185 | public static Method quietlyGetMethod(final Class> clazz, final String methodName,
186 | final Class>... parameterTypes) {
187 | try {
188 | return clazz.getMethod(methodName, parameterTypes);
189 | } catch (final SecurityException e) {
190 | throw new RuntimeException("SecurityException attempting to retrieve method \"" + methodName
191 | + "\" from class " + clazz.getName(), e);
192 | } catch (final NoSuchMethodException e) {
193 | return null;
194 | }
195 | }
196 |
197 | /**
198 | * @param object
199 | * the object to invoke the method on
200 | * @param method
201 | * the method to invoke
202 | * @param params
203 | * any parameters to the method
204 | * @return any return value
205 | */
206 | public static Object quietlyInvokeMethod(final Object object, final Method method, final Object... params) {
207 | try {
208 | final boolean accessible = method.isAccessible();
209 | if (!accessible) {
210 | method.setAccessible(true);
211 | }
212 | try {
213 | return method.invoke(object, params);
214 | } finally {
215 | if (!accessible) {
216 | method.setAccessible(false);
217 | }
218 | }
219 | } catch (final IllegalArgumentException e) {
220 | throw new RuntimeException(e.getMessage(), e);
221 | } catch (final IllegalAccessException e) {
222 | throw new RuntimeException(e.getMessage(), e);
223 | } catch (final InvocationTargetException e) {
224 | throw new RuntimeException(e.getMessage(), e);
225 | }
226 | }
227 |
228 | /**
229 | * @param clazz
230 | * the class
231 | * @return an iterable for the class hierarchy
232 | */
233 | public static Iterable> iterateClassHierarchy(final Class> clazz) {
234 | return new Iterable>() {
235 | @Override
236 | public Iterator> iterator() {
237 | return new ReadOnlyIterator>() {
238 |
239 | private Class> cl = clazz;
240 |
241 | @Override
242 | public boolean hasNext() {
243 | return cl != null && cl != Class.class;
244 | }
245 |
246 | @Override
247 | public Class> next() {
248 | final Class> next = cl;
249 | cl = cl.getSuperclass();
250 | return next;
251 | }
252 | };
253 | }
254 | };
255 | }
256 | }
257 |
--------------------------------------------------------------------------------
/src/main/java/junit/rules/jndi/StubJndiContext.java:
--------------------------------------------------------------------------------
1 | /**
2 | * junit-rules: JUnit Rules Library
3 | *
4 | * Copyright (c) 2009-2011 by Alistair A. Israel.
5 | * This software is made available under the terms of the MIT License.
6 | *
7 | * Created Jun 2, 2011
8 | */
9 | package junit.rules.jndi;
10 |
11 | import java.util.HashMap;
12 | import java.util.Hashtable;
13 | import java.util.Map;
14 | import java.util.Map.Entry;
15 | import java.util.logging.Logger;
16 |
17 | import javax.naming.Binding;
18 | import javax.naming.Context;
19 | import javax.naming.Name;
20 | import javax.naming.NameClassPair;
21 | import javax.naming.NameParser;
22 | import javax.naming.NamingEnumeration;
23 | import javax.naming.NamingException;
24 | import javax.naming.spi.InitialContextFactory;
25 | import javax.naming.spi.InitialContextFactoryBuilder;
26 | import javax.naming.spi.NamingManager;
27 |
28 | import junit.rules.TestFixture;
29 |
30 | /**
31 | * A 'stub' JNDI Context backed by a simple {@link Map}, useful only for unit testing. This implementation is
32 | * not thread-safe, and should not be used in production. Consider using Spring's SimpleNamingContext
33 | * for a more robust implementation with more features.
34 | *
35 | * @author Alistair.Israel
36 | */
37 | public class StubJndiContext extends TestFixture {
38 |
39 | private static final Logger logger = Logger.getLogger(StubJndiContext.class.getCanonicalName());
40 |
41 | private final Map boundObjects = new HashMap();
42 |
43 | private boolean closed;
44 |
45 | /**
46 | * The {@link InitialContextFactoryBuilder} for our {@link StubContext}
47 | *
48 | * @author Alistair A. Israel
49 | */
50 | private class StubContextFactoryBuilder implements InitialContextFactoryBuilder {
51 |
52 | /**
53 | * {@inheritDoc}
54 | *
55 | * @see javax.naming.spi.InitialContextFactoryBuilder#createInitialContextFactory(java.util.Hashtable)
56 | */
57 | @Override
58 | public InitialContextFactory createInitialContextFactory(final Hashtable, ?> environment)
59 | throws NamingException {
60 | return new InitialContextFactory() {
61 | @Override
62 | public Context getInitialContext(final Hashtable, ?> environment) throws NamingException {
63 | return new StubContext(environment);
64 | }
65 | };
66 | }
67 | }
68 |
69 | /**
70 | * @param name
71 | * the name to bind to
72 | * @param obj
73 | * the object to bind
74 | */
75 | public final void bind(final String name, final Object obj) {
76 | boundObjects.put(name, obj);
77 | logger.finest("Bound " + obj.getClass().getCanonicalName() + "@" + System.identityHashCode(obj) + " to \""
78 | + name + "\"");
79 | }
80 |
81 | /**
82 | * {@inheritDoc}
83 | *
84 | * @see junit.rules.TestFixture#setUp()
85 | */
86 | @Override
87 | protected final void setUp() throws Throwable {
88 | logger.info("Activating stub JNDI context");
89 | if (!NamingManager.hasInitialContextFactoryBuilder()) {
90 | try {
91 | NamingManager.setInitialContextFactoryBuilder(new StubContextFactoryBuilder());
92 | } catch (final NamingException e) {
93 | throw new RuntimeException(e.getClass().getCanonicalName()
94 | + " attempting to activate StubJndiContextBuilder", e);
95 | }
96 | }
97 | }
98 |
99 | /**
100 | * @return if {@link StubContext#close()} was called
101 | */
102 | public final boolean wasClosed() {
103 | return closed;
104 | }
105 |
106 | /**
107 | * Our internal, stub JNDI context.
108 | */
109 | private class StubContext implements Context {
110 |
111 | private final Hashtable environment;
112 |
113 | /**
114 | * @param environment
115 | * this context's environment
116 | */
117 | public StubContext(final Hashtable, ?> environment) {
118 | this.environment = new Hashtable();
119 | if (environment != null && !environment.isEmpty()) {
120 | for (final Entry, ?> entry : environment.entrySet()) {
121 | this.environment.put(entry.getKey().toString(), entry.getValue());
122 | }
123 | }
124 | }
125 |
126 | /**
127 | * {@inheritDoc}
128 | *
129 | * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
130 | */
131 | @Override
132 | public Object addToEnvironment(final String propName, final Object propVal) throws NamingException {
133 | return environment.put(propName, propVal);
134 | }
135 |
136 | /**
137 | * {@inheritDoc}
138 | *
139 | * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
140 | */
141 | @Override
142 | public void bind(final String name, final Object obj) throws NamingException {
143 | boundObjects.put(name, obj);
144 | logger.finest("Bound \"" + name + "\" to " + obj.getClass().getCanonicalName() + "@"
145 | + System.identityHashCode(obj));
146 | }
147 |
148 | /**
149 | * {@inheritDoc}
150 | *
151 | * @see javax.naming.Context#close()
152 | */
153 | @Override
154 | public void close() throws NamingException {
155 | closed = true;
156 | }
157 |
158 | /**
159 | * {@inheritDoc}
160 | *
161 | * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
162 | */
163 | @Override
164 | public String composeName(final String name, final String prefix) throws NamingException {
165 | return prefix + name;
166 | }
167 |
168 | /**
169 | * {@inheritDoc}
170 | *
171 | * @see javax.naming.Context#createSubcontext(java.lang.String)
172 | */
173 | @Override
174 | public Context createSubcontext(final String name) throws NamingException {
175 | throw notSupported();
176 | }
177 |
178 | /**
179 | * {@inheritDoc}
180 | *
181 | * @see javax.naming.Context#destroySubcontext(java.lang.String)
182 | */
183 | @Override
184 | public void destroySubcontext(final String name) throws NamingException {
185 | // TODO Auto-generated method stub
186 |
187 | }
188 |
189 | /**
190 | * {@inheritDoc}
191 | *
192 | * @see javax.naming.Context#getEnvironment()
193 | */
194 | @Override
195 | public Hashtable, ?> getEnvironment() throws NamingException {
196 | return this.environment;
197 | }
198 |
199 | /**
200 | * {@inheritDoc}
201 | *
202 | * @see javax.naming.Context#list(java.lang.String)
203 | */
204 | @Override
205 | public NamingEnumeration list(final String name) throws NamingException {
206 | // TODO Auto-generated method stub
207 | return null;
208 | }
209 |
210 | /**
211 | * {@inheritDoc}
212 | *
213 | * @see javax.naming.Context#listBindings(java.lang.String)
214 | */
215 | @Override
216 | public NamingEnumeration listBindings(final String name) throws NamingException {
217 | throw notSupported();
218 | }
219 |
220 | /**
221 | * {@inheritDoc}
222 | *
223 | * @see javax.naming.Context#lookup(java.lang.String)
224 | */
225 | @Override
226 | public Object lookup(final String name) throws NamingException {
227 | final Object o = boundObjects.get(name);
228 | if (o != null) {
229 | logger.finest("lookup(\"" + name + "\") returning " + o.getClass().getCanonicalName() + "@"
230 | + System.identityHashCode(o));
231 | } else {
232 | logger.finest("lookup(\"" + name + "\") returning null");
233 | }
234 | return o;
235 | }
236 |
237 | /**
238 | * {@inheritDoc}
239 | *
240 | * @see javax.naming.Context#lookupLink(java.lang.String)
241 | */
242 | @Override
243 | public Object lookupLink(final String name) throws NamingException {
244 | throw notSupported();
245 | }
246 |
247 | /**
248 | * {@inheritDoc}
249 | *
250 | * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
251 | */
252 | @Override
253 | public void rebind(final String name, final Object obj) throws NamingException {
254 | boundObjects.put(name, obj);
255 | }
256 |
257 | /**
258 | * {@inheritDoc}
259 | *
260 | * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
261 | */
262 | @Override
263 | public Object removeFromEnvironment(final String propName) throws NamingException {
264 | return environment.remove(propName);
265 | }
266 |
267 | /**
268 | * {@inheritDoc}
269 | *
270 | * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
271 | */
272 | @Override
273 | public void rename(final String oldName, final String newName) throws NamingException {
274 | if (!boundObjects.containsKey(oldName)) {
275 | throw new NamingException("StubJndiContext namme \"" + oldName + "\" not bound!");
276 | }
277 | if (boundObjects.containsKey(newName)) {
278 | throw new NamingException("StubJndiContext name \"" + oldName + "\" already bound to object of type: "
279 | + boundObjects.get(newName).getClass().getCanonicalName());
280 | }
281 |
282 | final Object obj = boundObjects.remove(oldName);
283 | boundObjects.put(newName, obj);
284 | }
285 |
286 | /**
287 | * {@inheritDoc}
288 | *
289 | * @see javax.naming.Context#unbind(java.lang.String)
290 | */
291 | @Override
292 | public void unbind(final String name) throws NamingException {
293 | throw notSupported();
294 | }
295 |
296 | /**
297 | * {@inheritDoc}
298 | *
299 | * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
300 | */
301 | @Override
302 | public void bind(final Name name, final Object obj) throws NamingException {
303 | throw notSupported();
304 | }
305 |
306 | /**
307 | * {@inheritDoc}
308 | *
309 | * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
310 | */
311 | @Override
312 | public Name composeName(final Name name, final Name prefix) throws NamingException {
313 | throw notSupported();
314 | }
315 |
316 | /**
317 | * {@inheritDoc}
318 | *
319 | * @see javax.naming.Context#createSubcontext(javax.naming.Name)
320 | */
321 | @Override
322 | public Context createSubcontext(final Name name) throws NamingException {
323 | throw notSupported();
324 | }
325 |
326 | /**
327 | * {@inheritDoc}
328 | *
329 | * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
330 | */
331 | @Override
332 | public void destroySubcontext(final Name name) throws NamingException {
333 | throw notSupported();
334 | }
335 |
336 | /**
337 | * {@inheritDoc}
338 | *
339 | * @see javax.naming.Context#getNameInNamespace()
340 | */
341 | @Override
342 | public String getNameInNamespace() throws NamingException {
343 | throw notSupported();
344 | }
345 |
346 | /**
347 | * {@inheritDoc}
348 | *
349 | * @see javax.naming.Context#getNameParser(javax.naming.Name)
350 | */
351 | @Override
352 | public NameParser getNameParser(final Name name) throws NamingException {
353 | throw notSupported();
354 | }
355 |
356 | /**
357 | * {@inheritDoc}
358 | *
359 | * @see javax.naming.Context#getNameParser(java.lang.String)
360 | */
361 | @Override
362 | public NameParser getNameParser(final String name) throws NamingException {
363 | throw notSupported();
364 | }
365 |
366 | /**
367 | * {@inheritDoc}
368 | *
369 | * @see javax.naming.Context#list(javax.naming.Name)
370 | */
371 | @Override
372 | public NamingEnumeration list(final Name name) throws NamingException {
373 | throw notSupported();
374 | }
375 |
376 | /**
377 | * {@inheritDoc}
378 | *
379 | * @see javax.naming.Context#listBindings(javax.naming.Name)
380 | */
381 | @Override
382 | public NamingEnumeration listBindings(final Name name) throws NamingException {
383 | throw notSupported();
384 | }
385 |
386 | /**
387 | * {@inheritDoc}
388 | *
389 | * @see javax.naming.Context#lookup(javax.naming.Name)
390 | */
391 | @Override
392 | public Object lookup(final Name name) throws NamingException {
393 | throw notSupported();
394 | }
395 |
396 | /**
397 | * {@inheritDoc}
398 | *
399 | * @see javax.naming.Context#lookupLink(javax.naming.Name)
400 | */
401 | @Override
402 | public Object lookupLink(final Name name) throws NamingException {
403 | throw notSupported();
404 | }
405 |
406 | /**
407 | * {@inheritDoc}
408 | *
409 | * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
410 | */
411 | @Override
412 | public void rebind(final Name name, final Object obj) throws NamingException {
413 | throw notSupported();
414 | }
415 |
416 | /**
417 | * {@inheritDoc}
418 | *
419 | * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
420 | */
421 | @Override
422 | public void rename(final Name oldName, final Name newName) throws NamingException {
423 | throw notSupported();
424 | }
425 |
426 | /**
427 | * {@inheritDoc}
428 | *
429 | * @see javax.naming.Context#unbind(javax.naming.Name)
430 | */
431 | @Override
432 | public void unbind(final Name name) throws NamingException {
433 | throw notSupported();
434 | }
435 |
436 | }
437 |
438 | /**
439 | * Equivalent to:
440 | *
441 | *
442 | * new UnsupportedOperationException("ClassName.MethodName is not supported!");
443 | *
444 | *
445 | * @return {@link UnsupportedOperationException}
446 | */
447 | public static UnsupportedOperationException notSupported() {
448 | final StackTraceElement[] unwound = unwindStackTrace(Thread.currentThread().getStackTrace());
449 | final UnsupportedOperationException e = new UnsupportedOperationException("StubJndiContext."
450 | + unwound[0].getMethodName() + " is not supported!");
451 | e.setStackTrace(unwound);
452 | return e;
453 | }
454 |
455 | /**
456 | * @param st
457 | * the {@link StackTraceElement}[]
458 | * @return the remaining {@link StackTraceElement}[]
459 | */
460 | private static StackTraceElement[] unwindStackTrace(final StackTraceElement[] st) {
461 | final int len = st.length;
462 |
463 | int i = 0;
464 | // Unwind up to StubJndiContext
465 | final String stubJndiContext = StubJndiContext.class.getName();
466 | while (i < len && !st[i].getClassName().equals(stubJndiContext)) {
467 | ++i;
468 | }
469 | // Unwind past unwindStackTrace() and notSupported()
470 | i += 2;
471 |
472 | final int rest = len - i;
473 | final StackTraceElement[] unwound = new StackTraceElement[rest];
474 | System.arraycopy(st, i, unwound, 0, rest);
475 | return unwound;
476 | }
477 | }
478 |
--------------------------------------------------------------------------------