KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > adapter > MockedXADataSource


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.adapter;
23
24 import javax.sql.XADataSource JavaDoc;
25 import javax.sql.XAConnection JavaDoc;
26 import javax.sql.ConnectionEventListener JavaDoc;
27 import javax.transaction.xa.XAResource JavaDoc;
28 import javax.transaction.xa.XAException JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.DatabaseMetaData JavaDoc;
37 import java.sql.SQLWarning JavaDoc;
38 import java.sql.Savepoint JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import java.sql.CallableStatement JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.lang.reflect.Proxy JavaDoc;
43 import java.lang.reflect.InvocationHandler JavaDoc;
44 import java.lang.reflect.Method JavaDoc;
45
46 /**
47  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
48  * @version <tt>$Revision: 37406 $</tt>
49  */

50 public class MockedXADataSource
51    implements XADataSource JavaDoc
52 {
53    private static final Map JavaDoc instances = new HashMap JavaDoc();
54
55    public static MockedXADataSource getInstance(String JavaDoc url)
56    {
57       return (MockedXADataSource)instances.get(url);
58    }
59
60    public static void stop(String JavaDoc url)
61    {
62       getInstance(url).stopped = true;
63    }
64
65    public static void start(String JavaDoc url)
66    {
67       getInstance(url).stopped = false;
68    }
69
70    public static String JavaDoc[] getUrls()
71    {
72       return (String JavaDoc[])instances.keySet().toArray(new String JavaDoc[instances.size()]);
73    }
74
75    private String JavaDoc url;
76    private boolean stopped;
77    private int loginTimeout;
78    private PrintWriter JavaDoc logWriter;
79
80    public String JavaDoc getURL()
81    {
82       return url;
83    }
84
85    public void setURL(String JavaDoc url)
86    {
87       this.url = url;
88       instances.put(url, this);
89    }
90
91    public int getLoginTimeout() throws SQLException JavaDoc
92    {
93       return loginTimeout;
94    }
95
96    public void setLoginTimeout(int seconds) throws SQLException JavaDoc
97    {
98       this.loginTimeout = seconds;
99    }
100
101    public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
102    {
103       return logWriter;
104    }
105
106    public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc
107    {
108       this.logWriter = out;
109    }
110
111    public XAConnection JavaDoc getXAConnection() throws SQLException JavaDoc
112    {
113       return new MockedXAConnection();
114    }
115
116    public XAConnection JavaDoc getXAConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc
117    {
118       return new MockedXAConnection();
119    }
120
121    // Inner
122

123    public class MockedXAConnection
124       implements XAConnection JavaDoc
125    {
126       private boolean closed;
127       private Connection JavaDoc con = new MockedConnection();
128       private XAResource JavaDoc xaResource = new MockedXAResource();
129
130       public XAResource JavaDoc getXAResource() throws SQLException JavaDoc
131       {
132          return xaResource;
133       }
134
135       public void close() throws SQLException JavaDoc
136       {
137          closed = true;
138       }
139
140       public Connection JavaDoc getConnection() throws SQLException JavaDoc
141       {
142          return con;
143       }
144
145       public void addConnectionEventListener(ConnectionEventListener JavaDoc listener)
146       {
147       }
148
149       public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener)
150       {
151       }
152
153       class MockedConnection
154          implements Connection JavaDoc
155       {
156          private int holdability;
157          private int txIsolation;
158          private boolean autoCommit;
159          private boolean readOnly;
160          private String JavaDoc catalog;
161
162          public String JavaDoc getUrl()
163          {
164             return url;
165          }
166
167          public int getHoldability() throws SQLException JavaDoc
168          {
169             check();
170             return holdability;
171          }
172
173          public int getTransactionIsolation() throws SQLException JavaDoc
174          {
175             check();
176             return txIsolation;
177          }
178
179          public void clearWarnings() throws SQLException JavaDoc
180          {
181             check();
182          }
183
184          public void close() throws SQLException JavaDoc
185          {
186             check();
187             closed = true;
188          }
189
190          public void commit() throws SQLException JavaDoc
191          {
192             check();
193          }
194
195          public void rollback() throws SQLException JavaDoc
196          {
197             check();
198          }
199
200          public boolean getAutoCommit() throws SQLException JavaDoc
201          {
202             check();
203             return autoCommit;
204          }
205
206          public boolean isClosed() throws SQLException JavaDoc
207          {
208             check();
209             return closed;
210          }
211
212          public boolean isReadOnly() throws SQLException JavaDoc
213          {
214             check();
215             return readOnly;
216          }
217
218          public void setHoldability(int holdability) throws SQLException JavaDoc
219          {
220             check();
221             this.holdability = holdability;
222          }
223
224          public void setTransactionIsolation(int level) throws SQLException JavaDoc
225          {
226             check();
227             this.txIsolation = level;
228          }
229
230          public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc
231          {
232             check();
233             this.autoCommit = autoCommit;
234          }
235
236          public void setReadOnly(boolean readOnly) throws SQLException JavaDoc
237          {
238             check();
239             this.readOnly = readOnly;
240          }
241
242          public String JavaDoc getCatalog() throws SQLException JavaDoc
243          {
244             check();
245             return catalog;
246          }
247
248          public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc
249          {
250             check();
251             this.catalog = catalog;
252          }
253
254          public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
255          {
256             check();
257             return (DatabaseMetaData JavaDoc)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
258                new Class JavaDoc[]{DatabaseMetaData JavaDoc.class},
259                new InvocationHandler JavaDoc()
260                {
261                   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
262                   {
263                      if("getURL".equals(method.getName()))
264                      {
265                         return url;
266                      }
267
268                      return new UnsupportedOperationException JavaDoc(
269                         "Not implemented: method=" +
270                         method.getName() +
271                         ", args=" +
272                         (args == null ? (Object JavaDoc)"null" : Arrays.asList(args))
273                      );
274                   }
275                }
276             );
277          }
278
279          public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
280          {
281             check();
282             return null;
283          }
284
285          public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc
286          {
287             check();
288             throw new UnsupportedOperationException JavaDoc("setSavepoint() is not implemented.");
289          }
290
291          public void releaseSavepoint(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
292          {
293             check();
294             throw new UnsupportedOperationException JavaDoc("releaseSavepoint() is not implemented.");
295          }
296
297          public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc
298          {
299             check();
300          }
301
302          public Statement JavaDoc createStatement() throws SQLException JavaDoc
303          {
304             check();
305             return (Statement JavaDoc)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
306                new Class JavaDoc[]{Statement JavaDoc.class},
307                new InvocationHandler JavaDoc()
308                {
309                   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
310                   {
311                      String JavaDoc methodName = method.getName();
312                      if("execute".equals(methodName))
313                      {
314                         // let's suppose it went well!
315
return Boolean.FALSE;
316                      }
317
318                      return new UnsupportedOperationException JavaDoc(
319                         "Not implemented: method=" +
320                         methodName +
321                         ", args=" +
322                         (args == null ? (Object JavaDoc)"null" : Arrays.asList(args))
323                      );
324                   }
325                }
326             );
327          }
328
329          public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc
330          {
331             check();
332             throw new UnsupportedOperationException JavaDoc("Not implemented.");
333          }
334
335          public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
336             throws SQLException JavaDoc
337          {
338             check();
339             throw new UnsupportedOperationException JavaDoc("Not implemented.");
340          }
341
342          public Map JavaDoc getTypeMap() throws SQLException JavaDoc
343          {
344             check();
345             throw new UnsupportedOperationException JavaDoc("Not implemented.");
346          }
347
348          public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc
349          {
350             check();
351             throw new UnsupportedOperationException JavaDoc("Not implemented.");
352          }
353
354          public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
355          {
356             check();
357             throw new UnsupportedOperationException JavaDoc("Not implemented.");
358          }
359
360          public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc
361          {
362             check();
363             throw new UnsupportedOperationException JavaDoc("Not implemented.");
364          }
365
366          public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
367             throws SQLException JavaDoc
368          {
369             check();
370             throw new UnsupportedOperationException JavaDoc("Not implemented.");
371          }
372
373          public CallableStatement JavaDoc prepareCall(String JavaDoc sql,
374                                               int resultSetType,
375                                               int resultSetConcurrency,
376                                               int resultSetHoldability) throws SQLException JavaDoc
377          {
378             check();
379             throw new UnsupportedOperationException JavaDoc("Not implemented.");
380          }
381
382          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc
383          {
384             check();
385             throw new UnsupportedOperationException JavaDoc("Not implemented.");
386          }
387
388          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
389          {
390             check();
391             throw new UnsupportedOperationException JavaDoc("Not implemented.");
392          }
393
394          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
395             throws SQLException JavaDoc
396          {
397             check();
398             throw new UnsupportedOperationException JavaDoc("Not implemented.");
399          }
400
401          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
402                                                    int resultSetType,
403                                                    int resultSetConcurrency,
404                                                    int resultSetHoldability) throws SQLException JavaDoc
405          {
406             check();
407             throw new UnsupportedOperationException JavaDoc("Not implemented.");
408          }
409
410          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc
411          {
412             check();
413             throw new UnsupportedOperationException JavaDoc("Not implemented.");
414          }
415
416          public Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc
417          {
418             check();
419             throw new UnsupportedOperationException JavaDoc("Not implemented.");
420          }
421
422          public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc
423          {
424             check();
425             throw new UnsupportedOperationException JavaDoc("Not implemented.");
426          }
427
428          // Private
429

430          private void check() throws SQLException JavaDoc
431          {
432             if(stopped)
433             {
434                throw new SQLException JavaDoc("The database is not available: " + url);
435             }
436          }
437       }
438    }
439
440    private static class MockedXAResource
441       implements XAResource JavaDoc
442    {
443       private int txTimeOut;
444
445       public int getTransactionTimeout() throws XAException JavaDoc
446       {
447          return txTimeOut;
448       }
449
450       public boolean setTransactionTimeout(int i) throws XAException JavaDoc
451       {
452          this.txTimeOut = i;
453          return true;
454       }
455
456       public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc
457       {
458          return xaResource instanceof MockedXAResource;
459       }
460
461       public Xid JavaDoc[] recover(int i) throws XAException JavaDoc
462       {
463          throw new UnsupportedOperationException JavaDoc("recover is not implemented.");
464       }
465
466       public int prepare(Xid JavaDoc xid) throws XAException JavaDoc
467       {
468          return XAResource.XA_OK;
469       }
470
471       public void forget(Xid JavaDoc xid) throws XAException JavaDoc
472       {
473       }
474
475       public void rollback(Xid JavaDoc xid) throws XAException JavaDoc
476       {
477       }
478
479       public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc
480       {
481       }
482
483       public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc
484       {
485       }
486
487       public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc
488       {
489       }
490    }
491 }
492
Popular Tags