@Override public int insert(String statement, Object[] args, FieldType[] argfieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { if (proxy == null) { return 0; } else { return proxy.insert(statement, args, argfieldTypes, keyHolder); } }
@Override public int insert(String statement, Object[] args, FieldType[] argfieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { if (proxy == null) { return 0; } else { return proxy.insert(statement, args, argfieldTypes, keyHolder); } }
@Test public void testInsert() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "insert bar"; int result = 13712321; expect(conn.insert(statement, null, null, null)).andReturn(result); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(result, proxy.insert(statement, null, null, null)); proxy.close(); verify(conn); }
@Test public void testQueryKeyHolderNoKeys() throws Exception { DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); try { createDao(Foo.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); keyHolder.addKey(0L); replay(keyHolder); databaseConnection.insert("insert into foo (id) values (2)", new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
@Test public void testIdColumnChangedFromStringToNumber() throws Exception { // NOTE: trying to get the database to return a string as a result but could not figure it out DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOINT_TABLE_NAME); try { createDao(FooString.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); keyHolder.addKey(0L); replay(keyHolder); databaseConnection.insert("insert into fooint (id, stuff) values ('12', 'zipper')", new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
@Test public void testIdColumnInteger() throws Exception { // NOTE: this doesn't seem to generate an INTEGER type, oh well DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOINT_TABLE_NAME); try { createDao(FooInt.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); keyHolder.addKey(1L); replay(keyHolder); databaseConnection.insert("insert into fooint (stuff) values (2)", new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
@Test public void testIdColumnInvalid() throws Exception { // NOTE: this doesn't seem to generate an INTEGER type, oh well DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOINT_TABLE_NAME); try { createDao(FooInt.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); keyHolder.addKey(1L); replay(keyHolder); databaseConnection.insert("insert into fooint (stuff) values ('zipper')", new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
@Test(expected = SQLException.class) public void testArgumentHolderNotSet() throws Exception { TableInfo<Foo, Integer> tableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class); Dao<Foo, Integer> dao = createDao(Foo.class, false); MappedCreate<Foo, Integer> mappedCreate = MappedCreate.build(dao, tableInfo); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), isA(GeneratedKeyHolder.class))).andReturn(1); replay(conn); mappedCreate.insert(databaseType, conn, new Foo(), null); }
@Test public void testGeneratedIdSequenceLong() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); Dao<GeneratedIdLong, Long> dao = createDao(GeneratedIdLong.class, false); StatementExecutor<GeneratedIdLong, Long> se = new StatementExecutor<GeneratedIdLong, Long>(databaseType, new TableInfo<GeneratedIdLong, Long>(databaseType, GeneratedIdLong.class), dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedIdLong genIdSeq = new GeneratedIdLong(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
@Test public void testGeneratedIdSequence() throws Exception { DatabaseType databaseType = new NeedsSequenceDatabaseType(); connectionSource.setDatabaseType(databaseType); TableInfo<GeneratedId, Integer> tableInfo = new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class); Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, false); StatementExecutor<GeneratedId, Integer> se = new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L); expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), (GeneratedKeyHolder) isNull())).andReturn(1); replay(databaseConnection); GeneratedId genIdSeq = new GeneratedId(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
@Test(expected = SQLException.class) public void testArgumentHolderSetZero() throws Exception { TableInfo<Foo, Integer> tableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class); Dao<Foo, Integer> dao = createDao(Foo.class, false); MappedCreate<Foo, Integer> mappedCreate = MappedCreate.build(dao, tableInfo); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), isA(GeneratedKeyHolder.class))).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { GeneratedKeyHolder holder = (GeneratedKeyHolder) getCurrentArguments()[3]; holder.addKey((Integer) 0); return 1; } }); replay(conn); mappedCreate.insert(databaseType, conn, new Foo(), null); }
@Test(expected = SQLException.class) public void testArgumentHolderDoubleSet() throws Exception { TableInfo<Foo, Integer> tableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class); Dao<Foo, Integer> dao = createDao(Foo.class, false); MappedCreate<Foo, Integer> mappedCreate = MappedCreate.build(dao, tableInfo); DatabaseConnection conn = createMock(DatabaseConnection.class); expect(conn.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), isA(GeneratedKeyHolder.class))).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { GeneratedKeyHolder holder = (GeneratedKeyHolder) getCurrentArguments()[3]; holder.addKey((Integer) 1); holder.addKey((Integer) 2); return 1; } }); replay(conn); mappedCreate.insert(databaseType, conn, new Foo(), null); }
@Test public void testGeneratedId() throws Exception { TableInfo<GeneratedId, Integer> tableInfo = new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class); Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, false); StatementExecutor<GeneratedId, Integer> se = new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, dao); DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class), isA(GeneratedKeyHolder.class)); expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Integer answer() throws Throwable { GeneratedKeyHolder keyHolder = (GeneratedKeyHolder) (getCurrentArguments()[3]); keyHolder.addKey(2); return 1; } }); replay(databaseConnection); GeneratedId genIdSeq = new GeneratedId(); se.create(databaseConnection, genIdSeq, null); verify(databaseConnection); }
rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) {
rowC = databaseConnection.insert(statement, args, argFieldTypes, keyHolder); } catch (SQLException e) {