Permalink
Browse files

[jdbc-driver] Add class `Driver.Property`.

```java
import acolyte.jdbc.Driver;
import acolyte.jdbc.Property;

Driver.connection(handler,
  new Property("name1, "Foo"),
  new Property("name2", "Bar"));
```
  • Loading branch information...
cchantep
cchantep committed Apr 25, 2015
1 parent a8ed90b commit 92f72ae4bb716c1bfb853be401dc4761e8c5f251
@@ -85,6 +85,18 @@
return new acolyte.jdbc.Connection(url, info, handler);
} // end of connect
/**
* Creates a connection to specified |url| with given configuration |info|.
*
* @see #connect(java.lang.String, java.util.Properties)
*/
public acolyte.jdbc.Connection connect(final String url,
final Property... info)
throws SQLException {
return connect(url, props(info));
} // end of connect
/**
* {@inheritDoc}
*/
@@ -136,9 +148,21 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
* @throws IllegalArgumentException if handler is null
*/
public static acolyte.jdbc.Connection connection(ConnectionHandler handler) {
return connection(handler, null);
return connection(handler, (Properties) null);
} // end of connection
/**
* Direct connection, with given |handler| and random URL.
*
* @param handler Connection handler
* @param info Connection properties (optional)
* @throws IllegalArgumentException if handler is null
* @see #connection(acolyte.jdbc.ConnectionHandler, java.util.Properties)
*/
public static acolyte.jdbc.Connection connection(final ConnectionHandler handler, final Property... info) {
return connection(handler, props(info));
} // end of connnection
/**
* Direct connection, with given |handler| and random URL.
*
@@ -154,8 +178,7 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
} // end of if
final String url = String.
format("jdbc:acolyte:direct-%d",
System.identityHashCode(handler));
format("jdbc:acolyte:direct-%d", System.identityHashCode(handler));
return new acolyte.jdbc.Connection(url, info, handler);
} // end of connection
@@ -166,7 +189,7 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
* @throws IllegalArgumentException if handler is null
*/
public static acolyte.jdbc.Connection connection(StatementHandler handler) {
return connection(handler, null);
return connection(handler, (Properties) null);
} // end of connection
/**
@@ -185,6 +208,17 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return connection(new ConnectionHandler.Default(handler), info);
} // end of connection
/**
* Direct connection, with given |handler| and random URL.
*
* @param handler Statement handler
* @param info Connection properties (optional)
* @throws IllegalArgumentException if handler is null
*/
public static acolyte.jdbc.Connection connection(final StatementHandler handler, final Property... info) {
return connection(handler, props(info));
} // end of connection
// ---
/**
@@ -246,4 +280,36 @@ public static ConnectionHandler unregister(final String id) {
return handlers.remove(id);
} // end of unregister
/** Returns prepared properties. */
private static Properties props(final Property[] info) {
final Properties ps = new Properties();
for (final Property p : info) {
ps.put(p.name, p.value);
}
return ps;
} // end of props
// --- Inner classes ---
/**
* Driver (configuration) property.
*/
public static final class Property {
public final String name;
public final String value;
/**
* Bulk constructor.
*
* @param name the name of the property
* @param value the property value
*/
public Property(String name, String value) {
this.name = name;
this.value = value;
}
} // end of class Property
} // end of class Driver
@@ -56,18 +56,34 @@ object DriverSpec extends Specification with DriverUtils with DriverFixtures {
}
"accept connection properties" in {
"accept connection properties" >> {
val props = new java.util.Properties()
props.put("_test", "_val")
acolyte.jdbc.Driver.register(handlerId, defaultHandler)
"as dictionary" in {
(new acolyte.jdbc.Driver().connect(jdbcUrl, props).getProperties.
aka("connection 1 properties") mustEqual props).
and(acolyte.jdbc.Driver.connection(defaultHandler, props).
getProperties aka "connection 2 properties" mustEqual props).
and(acolyte.jdbc.Driver.connection(CompositeHandler.empty, props).
getProperties aka "connection 3 properties" mustEqual props)
}
"as list" in {
import acolyte.jdbc.Driver.Property
def prop(n: String, v: String) = new Property(n, v)
(new acolyte.jdbc.Driver().connect(jdbcUrl, props).getProperties.
aka("connection 1 properties") mustEqual props).
and(acolyte.jdbc.Driver.connection(defaultHandler, props).
getProperties aka "connection 2 properties" mustEqual props).
and(acolyte.jdbc.Driver.connection(CompositeHandler.empty, props).
getProperties aka "connection 3 properties" mustEqual props)
(new acolyte.jdbc.Driver().connect(jdbcUrl, prop("_test", "_val")).
getProperties aka "connection 1 properties" mustEqual props).
and(acolyte.jdbc.Driver.
connection(defaultHandler, prop("_test", "_val")).
getProperties aka "connection 2 properties" mustEqual props).
and(acolyte.jdbc.Driver.
connection(CompositeHandler.empty, prop("_test", "_val")).
getProperties aka "connection 3 properties" mustEqual props)
}
}
"not open connection without handler" in {
@@ -79,10 +95,12 @@ object DriverSpec extends Specification with DriverUtils with DriverFixtures {
and(acolyte.jdbc.Driver.connection(null.asInstanceOf[StatementHandler]).
aka("direct connection 2") must throwA[IllegalArgumentException]).
and(acolyte.jdbc.Driver.
connection(null.asInstanceOf[ConnectionHandler], null).
connection(null.asInstanceOf[ConnectionHandler],
null.asInstanceOf[java.util.Properties]).
aka("direct connection 3") must throwA[IllegalArgumentException]).
and(acolyte.jdbc.Driver.
connection(null.asInstanceOf[StatementHandler], null).
connection(null.asInstanceOf[StatementHandler],
null.asInstanceOf[java.util.Properties]).
aka("direct connection 4") must throwA[IllegalArgumentException])
}

0 comments on commit 92f72ae

Please sign in to comment.