use of org.h2.engine.User in project h2database by h2database.
the class TestDataSource method testDataSourceFactory.
private void testDataSourceFactory() throws Exception {
ObjectFactory factory = new JdbcDataSourceFactory();
assertTrue(null == factory.getObjectInstance("test", null, null, null));
Reference ref = new Reference("java.lang.String");
assertTrue(null == factory.getObjectInstance(ref, null, null, null));
ref = new Reference(JdbcDataSource.class.getName());
ref.add(new StringRefAddr("url", "jdbc:h2:mem:"));
ref.add(new StringRefAddr("user", "u"));
ref.add(new StringRefAddr("password", "p"));
ref.add(new StringRefAddr("loginTimeout", "1"));
ref.add(new StringRefAddr("description", "test"));
JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance(ref, null, null, null);
assertEquals(1, ds.getLoginTimeout());
assertEquals("test", ds.getDescription());
assertEquals("jdbc:h2:mem:", ds.getURL());
assertEquals("u", ds.getUser());
assertEquals("p", ds.getPassword());
Reference ref2 = ds.getReference();
assertEquals(ref.size(), ref2.size());
assertEquals(ref.get("url").getContent().toString(), ref2.get("url").getContent().toString());
assertEquals(ref.get("user").getContent().toString(), ref2.get("user").getContent().toString());
assertEquals(ref.get("password").getContent().toString(), ref2.get("password").getContent().toString());
assertEquals(ref.get("loginTimeout").getContent().toString(), ref2.get("loginTimeout").getContent().toString());
assertEquals(ref.get("description").getContent().toString(), ref2.get("description").getContent().toString());
ds.setPasswordChars("abc".toCharArray());
assertEquals("abc", ds.getPassword());
}
use of org.h2.engine.User in project h2database by h2database.
the class TestDataSource method testXAConnection.
private void testXAConnection(boolean userInDataSource) throws Exception {
deleteDb("dataSource");
JdbcDataSource ds = new JdbcDataSource();
String url = getURL("dataSource", true);
String user = getUser();
ds.setURL(url);
if (userInDataSource) {
ds.setUser(user);
ds.setPassword(getPassword());
}
if (userInDataSource) {
assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
} else {
assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
}
XAConnection xaConn;
if (userInDataSource) {
xaConn = ds.getXAConnection();
} else {
xaConn = ds.getXAConnection(user, getPassword());
}
int traceId = ((JdbcXAConnection) xaConn).getTraceId();
assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
xaConn.addConnectionEventListener(new ConnectionEventListener() {
@Override
public void connectionClosed(ConnectionEvent event) {
// nothing to do
}
@Override
public void connectionErrorOccurred(ConnectionEvent event) {
// nothing to do
}
});
XAResource res = xaConn.getXAResource();
assertFalse(res.setTransactionTimeout(1));
assertEquals(0, res.getTransactionTimeout());
assertTrue(res.isSameRM(res));
assertFalse(res.isSameRM(null));
Connection conn = xaConn.getConnection();
assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
assertEquals(0, list.length);
Statement stat = conn.createStatement();
stat.execute("SELECT * FROM DUAL");
conn.close();
xaConn.close();
}
use of org.h2.engine.User in project h2database by h2database.
the class TestSpatial method testInPlaceUpdate.
/**
* If the user mutate the geometry of the object, the object cache must not
* be updated.
*/
private void testInPlaceUpdate() throws SQLException {
try (Connection conn = getConnection(URL)) {
ResultSet rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
assertTrue(rs.next());
// Mutate the geometry
((Geometry) rs.getObject(1)).apply(new AffineTransformation(1, 0, 1, 1, 0, 1));
rs.close();
rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
assertTrue(rs.next());
// Check if the geometry is the one requested
assertEquals(1, ((Point) rs.getObject(1)).getX());
assertEquals(1, ((Point) rs.getObject(1)).getY());
rs.close();
}
}
use of org.h2.engine.User in project h2database by h2database.
the class MixedMode method main.
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
// start the server, allows to access the database remotely
Server server = Server.createTcpServer("-tcpPort", "9081");
server.start();
System.out.println("You can access the database remotely now, using the URL:");
System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");
// now use the database in your application in embedded mode
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
// some simple 'business usage'
Statement stat = conn.createStatement();
stat.execute("DROP TABLE TIMER IF EXISTS");
stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");
System.out.println("Execute this a few times: " + "SELECT TIME FROM TIMER");
System.out.println("To stop this application " + "(and the server), run: DROP TABLE TIMER");
try {
while (true) {
// runs forever, except if you drop the table remotely
stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
Thread.sleep(1000);
}
} catch (SQLException e) {
System.out.println("Error: " + e.toString());
}
conn.close();
// stop the server
server.stop();
}
use of org.h2.engine.User in project sql2o by aaberg.
the class H2Tests method setUp.
@Before
public void setUp() throws Exception {
driverClassName = "org.h2.Driver";
url = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
user = "sa";
pass = "";
org.h2.jdbcx.JdbcDataSource datasource = new org.h2.jdbcx.JdbcDataSource();
datasource.setURL(url);
datasource.setUser(user);
datasource.setPassword(pass);
ds = datasource;
}
Aggregations