提交 164f02af authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Javadoc comment for the class was missing (./build.sh should find this)
- Make fields private where possible
- Do not comment source code (// EventLogger.activateConsoleLogger();). Remove it.
- Simplify error code assertions
上级 82ecbf33
...@@ -6,19 +6,24 @@ ...@@ -6,19 +6,24 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; import java.util.List;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.jaqu.Db; import org.h2.jaqu.Db;
import org.h2.jdbc.JdbcSQLException;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/**
* Test annotation processing.
*/
public class AnnotationsTest extends TestBase { public class AnnotationsTest extends TestBase {
/** /**
* This object represents a database (actually a connection to the database). * This object represents a database (actually a connection to the database).
*/ */
//## Java 1.5 begin ## //## Java 1.5 begin ##
Db db; private Db db;
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
...@@ -27,76 +32,82 @@ public class AnnotationsTest extends TestBase { ...@@ -27,76 +32,82 @@ public class AnnotationsTest extends TestBase {
* *
* @param args the command line parameters * @param args the command line parameters
*/ */
public static void main(String... args) { public static void main(String... args) throws SQLException {
new AnnotationsTest().test(); new AnnotationsTest().test();
} }
public void test() { public void test() throws SQLException {
//## Java 1.5 begin ## //## Java 1.5 begin ##
// EventLogger.activateConsoleLogger();
db = Db.open("jdbc:h2:mem:", "sa", "sa"); db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList()); db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList()); db.insertAll(ProductMixedAnnotation.getList());
testIndexCreation();
testProductAnnotationOnly(); testProductAnnotationOnly();
testProductMixedAnnotation(); testProductMixedAnnotation();
testTrimStringAnnotation(); testTrimStringAnnotation();
testCreateTableIfRequiredAnnotation(); testCreateTableIfRequiredAnnotation();
testColumnInheritanceAnnotation(); testColumnInheritanceAnnotation();
db.close(); db.close();
// EventLogger.deactivateConsoleLogger();
//## Java 1.5 end ## //## Java 1.5 end ##
} }
private void testIndexCreation() throws SQLException {
// test indexes are created, and columns are in the right order
DatabaseMetaData meta = db.getConnection().getMetaData();
ResultSet rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATEDPRODUCT", false, true);
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY");
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "NAME");
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "CAT");
assertFalse(rs.next());
}
private void testProductAnnotationOnly() { private void testProductAnnotationOnly() {
ProductAnnotationOnly p = new ProductAnnotationOnly(); ProductAnnotationOnly p = new ProductAnnotationOnly();
assertEquals(10, db.from(p).selectCount()); assertEquals(10, db.from(p).selectCount());
// Test JQColumn.name="cat" // test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount()); assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// Test JQTable.annotationsOnly=true // test JQTable.annotationsOnly=true
// public String unmappedField is ignored by JaQu // public String unmappedField is ignored by JaQu
assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount()); assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount());
// Test JQColumn.autoIncrement=true // test JQColumn.autoIncrement=true
// 10 objects, 10 autoIncremented unique values // 10 objects, 10 autoIncremented unique values
assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size()); assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size());
// Test JQTable.primaryKey=id // test JQTable.primaryKey=id
int errorCode = 0;
try { try {
db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductAnnotationOnly.getList());
} catch (Throwable t) { } catch (RuntimeException r) {
if (t.getCause() instanceof JdbcSQLException) { SQLException s = (SQLException) r.getCause();
JdbcSQLException s = (JdbcSQLException) t.getCause(); assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
errorCode = s.getErrorCode();
}
} }
assertEquals(errorCode, ErrorCode.DUPLICATE_KEY_1);
} }
private void testProductMixedAnnotation() { private void testProductMixedAnnotation() {
ProductMixedAnnotation p = new ProductMixedAnnotation(); ProductMixedAnnotation p = new ProductMixedAnnotation();
// Test JQColumn.name="cat" // test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount()); assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// Test JQTable.annotationsOnly=false // test JQTable.annotationsOnly=false
// public String mappedField is reflectively mapped by JaQu // public String mappedField is reflectively mapped by JaQu
assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount()); assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
// Test JQColumn.primaryKey=true // test JQColumn.primaryKey=true
int errorCode = 0;
try { try {
db.insertAll(ProductMixedAnnotation.getList()); db.insertAll(ProductMixedAnnotation.getList());
} catch (Throwable t) { } catch (RuntimeException r) {
if (t.getCause() instanceof JdbcSQLException) { SQLException s = (SQLException) r.getCause();
JdbcSQLException s = (JdbcSQLException) t.getCause(); assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
errorCode = s.getErrorCode();
}
} }
assertEquals(errorCode, ErrorCode.DUPLICATE_KEY_1);
} }
private void testTrimStringAnnotation() { private void testTrimStringAnnotation() {
...@@ -104,9 +115,10 @@ public class AnnotationsTest extends TestBase { ...@@ -104,9 +115,10 @@ public class AnnotationsTest extends TestBase {
ProductAnnotationOnly prod = db.from(p).selectFirst(); ProductAnnotationOnly prod = db.from(p).selectFirst();
String oldValue = prod.category; String oldValue = prod.category;
String newValue = "01234567890123456789"; String newValue = "01234567890123456789";
prod.category = newValue; // 2 chars exceeds field max // 2 chars exceeds field max
prod.category = newValue;
db.update(prod); db.update(prod);
ProductAnnotationOnly newProd = db.from(p) ProductAnnotationOnly newProd = db.from(p)
.where(p.productId) .where(p.productId)
.is(prod.productId) .is(prod.productId)
...@@ -116,15 +128,15 @@ public class AnnotationsTest extends TestBase { ...@@ -116,15 +128,15 @@ public class AnnotationsTest extends TestBase {
newProd.category = oldValue; newProd.category = oldValue;
db.update(newProd); db.update(newProd);
} }
private void testColumnInheritanceAnnotation() { private void testColumnInheritanceAnnotation() {
ProductInheritedAnnotation table = new ProductInheritedAnnotation(); ProductInheritedAnnotation table = new ProductInheritedAnnotation();
Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData(); List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
db.insertAll(inserted); db.insertAll(inserted);
List<ProductInheritedAnnotation> retrieved = db.from(table).select(); List<ProductInheritedAnnotation> retrieved = db.from(table).select();
for (int j = 0; j < retrieved.size(); j++) { for (int j = 0; j < retrieved.size(); j++) {
ProductInheritedAnnotation i = inserted.get(j); ProductInheritedAnnotation i = inserted.get(j);
ProductInheritedAnnotation r = retrieved.get(j); ProductInheritedAnnotation r = retrieved.get(j);
...@@ -139,19 +151,15 @@ public class AnnotationsTest extends TestBase { ...@@ -139,19 +151,15 @@ public class AnnotationsTest extends TestBase {
} }
private void testCreateTableIfRequiredAnnotation() { private void testCreateTableIfRequiredAnnotation() {
// Tests JQTable.createTableIfRequired=false // tests JQTable.createTableIfRequired=false
int errorCode = 0;
try { try {
Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa"); Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb.insertAll(ProductNoCreateTable.getList()); noCreateDb.insertAll(ProductNoCreateTable.getList());
noCreateDb.close(); noCreateDb.close();
} catch (Throwable e) { } catch (RuntimeException r) {
if (e.getCause() instanceof JdbcSQLException) { SQLException s = (SQLException) r.getCause();
JdbcSQLException error = (JdbcSQLException) e.getCause(); assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
errorCode = error.getErrorCode();
}
} }
assertTrue(errorCode == ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论