View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jdbc;
5   
6   import java.sql.Connection;
7   import java.sql.Driver;
8   import java.sql.DriverManager;
9   import java.sql.DriverPropertyInfo;
10  import java.sql.SQLException;
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.Properties;
14  
15  import com.github.smokestack.exception.NotYetImplementedException;
16  
17  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
18  import org.apache.commons.lang.builder.ToStringStyle;
19  import org.hamcrest.core.IsNull;
20  
21  import static org.hamcrest.MatcherAssert.assertThat; 
22  
23  /**
24   * @author gliptak
25   *
26   */
27  public class MockDriver implements Driver {
28  
29  	/**
30  	 * Public access to instance
31  	 */
32      public static final MockDriver instance = new MockDriver();
33      
34      private static volatile boolean registered;
35  
36      static {
37          load();
38      }
39  
40  	private List<MockConnection> mockConnections=new ArrayList<MockConnection>();
41  
42  	private String url;
43  
44  	/* (non-Javadoc)
45  	 * @see java.sql.Driver#acceptsURL(java.lang.String)
46  	 */
47  	public boolean acceptsURL(String url) throws SQLException {
48  		this.url = url;
49  		_acceptsURL(url);
50  		return true;
51  	}
52  
53  	public boolean _acceptsURL(String url) throws SQLException {
54  		return true;
55  	}
56  
57  	/* (non-Javadoc)
58  	 * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
59  	 */
60  	public Connection connect(String url, Properties info) throws SQLException {
61  		_connect(url, info);
62  		MockConnection c=new MockConnection(url, info);
63  		mockConnections.add(c);
64  		return c;
65  	}
66  
67  	public MockConnection _connect(String url, Properties info) throws SQLException {
68  		return null;
69  	}
70  
71  	/* (non-Javadoc)
72  	 * @see java.sql.Driver#getMajorVersion()
73  	 */
74  	public int getMajorVersion() {
75  		throw new NotYetImplementedException();
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see java.sql.Driver#getMinorVersion()
80  	 */
81  	public int getMinorVersion() {
82  		throw new NotYetImplementedException();
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
87  	 */
88  	public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
89  		throw new NotYetImplementedException();
90  	}
91  
92  	/* (non-Javadoc)
93  	 * @see java.sql.Driver#jdbcCompliant()
94  	 */
95  	public boolean jdbcCompliant() {
96  		_jdbcCompliant();
97  		return true;
98  	}
99  
100     public boolean _jdbcCompliant() {
101     	return true;
102 	}
103 
104 	public static synchronized Driver load() {
105         try {
106             if (!registered) {
107                 registered = true;
108                 DriverManager.registerDriver(instance);
109             }
110         } catch (SQLException e) {
111         	// TODO: is there a better way to fail?
112             assertThat("not expecting an exception", e, IsNull.nullValue());
113         }
114         return instance;
115     }
116 
117     /**
118      * For use with ClassLoader based environments
119      */
120     public static synchronized void unload() {
121         try {
122             if (registered) {
123                 registered = false;
124                 DriverManager.deregisterDriver(instance);
125             }
126         } catch (SQLException e) {
127         	// TODO: is there a better way to fail?
128             assertThat("not expecting an exception", e, IsNull.nullValue());
129         }
130     }
131 
132 	/**
133 	 * @return the mockConnections
134 	 */
135 	public List<MockConnection> getMockConnections() {
136 		return mockConnections;
137 	}
138 	
139 	/**
140 	 * Reset to original state
141 	 */
142 	public void reset(){
143 		instance.mockConnections=new ArrayList<MockConnection>();
144 	}
145 	
146 	@Override
147 	public String toString(){
148 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
149 	}
150 }