提交 6216db17 authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Fix missing or incorrectly formatted license headers
- Line comments are lowercase except for complete sentences
- Remove trailing whitespace (the IDE should fix this)
上级 8a7312b8
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.h2.constant.ErrorCode;
import org.h2.jaqu.Db;
import org.h2.test.TestBase;
/**
* Test annotation processing.
*/
public class AnnotationsTest extends TestBase {
/**
* This object represents a database (actually a connection to the database).
*/
//## Java 1.5 begin ##
private Db db;
//## Java 1.5 end ##
/**
* This method is called when executing this application from the command
* line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws SQLException {
new AnnotationsTest().test();
}
public void test() throws SQLException {
//## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList());
testIndexCreation();
testProductAnnotationOnly();
testProductMixedAnnotation();
testTrimStringAnnotation();
testCreateTableIfRequiredAnnotation();
testColumnInheritanceAnnotation();
db.close();
//## 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() {
ProductAnnotationOnly p = new ProductAnnotationOnly();
assertEquals(10, db.from(p).selectCount());
// test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=true
// public String unmappedField is ignored by JaQu
assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount());
// test JQColumn.autoIncrement=true
// 10 objects, 10 autoIncremented unique values
assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size());
// test JQTable.primaryKey=id
try {
db.insertAll(ProductAnnotationOnly.getList());
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
}
}
private void testProductMixedAnnotation() {
ProductMixedAnnotation p = new ProductMixedAnnotation();
// test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=false
// public String mappedField is reflectively mapped by JaQu
assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
// test JQColumn.primaryKey=true
try {
db.insertAll(ProductMixedAnnotation.getList());
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
}
}
private void testTrimStringAnnotation() {
ProductAnnotationOnly p = new ProductAnnotationOnly();
ProductAnnotationOnly prod = db.from(p).selectFirst();
String oldValue = prod.category;
String newValue = "01234567890123456789";
// 2 chars exceeds field max
prod.category = newValue;
db.update(prod);
ProductAnnotationOnly newProd = db.from(p)
.where(p.productId)
.is(prod.productId)
.selectFirst();
assertEquals(newValue.substring(0, 15), newProd.category);
newProd.category = oldValue;
db.update(newProd);
}
private void testColumnInheritanceAnnotation() {
ProductInheritedAnnotation table = new ProductInheritedAnnotation();
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
db.insertAll(inserted);
List<ProductInheritedAnnotation> retrieved = db.from(table).select();
for (int j = 0; j < retrieved.size(); j++) {
ProductInheritedAnnotation i = inserted.get(j);
ProductInheritedAnnotation r = retrieved.get(j);
assertEquals(i.category, r.category);
assertEquals(i.mappedField, r.mappedField);
assertEquals(i.unitsInStock, r.unitsInStock);
assertEquals(i.unitPrice, r.unitPrice);
assertEquals(i.name(), r.name());
assertEquals(i.id(), r.id());
}
db.close();
}
private void testCreateTableIfRequiredAnnotation() {
// tests JQTable.createTableIfRequired=false
try {
Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb.insertAll(ProductNoCreateTable.getList());
noCreateDb.close();
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
}
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.h2.constant.ErrorCode;
import org.h2.jaqu.Db;
import org.h2.test.TestBase;
/**
* Test annotation processing.
*/
public class AnnotationsTest extends TestBase {
/**
* This object represents a database (actually a connection to the database).
*/
//## Java 1.5 begin ##
private Db db;
//## Java 1.5 end ##
/**
* This method is called when executing this application from the command
* line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws SQLException {
new AnnotationsTest().test();
}
public void test() throws SQLException {
//## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList());
testIndexCreation();
testProductAnnotationOnly();
testProductMixedAnnotation();
testTrimStringAnnotation();
testCreateTableIfRequiredAnnotation();
testColumnInheritanceAnnotation();
db.close();
//## 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", "ANNOTATED" + "PRODUCT", false, true);
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY");
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "NAME");
assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "CAT");
assertFalse(rs.next());
}
private void testProductAnnotationOnly() {
ProductAnnotationOnly p = new ProductAnnotationOnly();
assertEquals(10, db.from(p).selectCount());
// test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=true
// public String unmappedField is ignored by JaQu
assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount());
// test JQColumn.autoIncrement=true
// 10 objects, 10 autoIncremented unique values
assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size());
// test JQTable.primaryKey=id
try {
db.insertAll(ProductAnnotationOnly.getList());
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
}
}
private void testProductMixedAnnotation() {
ProductMixedAnnotation p = new ProductMixedAnnotation();
// test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=false
// public String mappedField is reflectively mapped by JaQu
assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
// test JQColumn.primaryKey=true
try {
db.insertAll(ProductMixedAnnotation.getList());
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
}
}
private void testTrimStringAnnotation() {
ProductAnnotationOnly p = new ProductAnnotationOnly();
ProductAnnotationOnly prod = db.from(p).selectFirst();
String oldValue = prod.category;
String newValue = "01234567890123456789";
// 2 chars exceeds field max
prod.category = newValue;
db.update(prod);
ProductAnnotationOnly newProd = db.from(p)
.where(p.productId)
.is(prod.productId)
.selectFirst();
assertEquals(newValue.substring(0, 15), newProd.category);
newProd.category = oldValue;
db.update(newProd);
}
private void testColumnInheritanceAnnotation() {
ProductInheritedAnnotation table = new ProductInheritedAnnotation();
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
db.insertAll(inserted);
List<ProductInheritedAnnotation> retrieved = db.from(table).select();
for (int j = 0; j < retrieved.size(); j++) {
ProductInheritedAnnotation i = inserted.get(j);
ProductInheritedAnnotation r = retrieved.get(j);
assertEquals(i.category, r.category);
assertEquals(i.mappedField, r.mappedField);
assertEquals(i.unitsInStock, r.unitsInStock);
assertEquals(i.unitPrice, r.unitPrice);
assertEquals(i.name(), r.name());
assertEquals(i.id(), r.id());
}
db.close();
}
private void testCreateTableIfRequiredAnnotation() {
// tests JQTable.createTableIfRequired=false
try {
Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb.insertAll(ProductNoCreateTable.getList());
noCreateDb.close();
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
}
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.h2.jaqu.Db;
import org.h2.jaqu.DbInspector;
import org.h2.jaqu.DbUpgrader;
import org.h2.jaqu.DbVersion;
import org.h2.jaqu.Table.JQDatabase;
import org.h2.jaqu.Validation;
import org.h2.test.TestBase;
import org.h2.test.jaqu.SupportedTypes.SupportedTypes2;
/**
* Test that the mapping between classes and tables is done correctly.
*/
public class ModelsTest extends TestBase {
/**
* This object represents a database (actually a connection to the database).
*/
//## Java 1.5 begin ##
private Db db;
//## Java 1.5 end ##
/**
* This method is called when executing this application from the command
* line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
ModelsTest test = new ModelsTest();
test.init();
test.config.traceTest = true;
test.test();
}
public void test() {
//## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList());
testValidateModels();
testSupportedTypes();
testModelGeneration();
testDatabaseUpgrade();
testTableUpgrade();
db.close();
//## Java 1.5 end ##
}
private void testValidateModels() {
DbInspector inspector = new DbInspector(db);
validateModel(inspector, new Product());
validateModel(inspector, new ProductAnnotationOnly());
validateModel(inspector, new ProductMixedAnnotation());
}
private void validateModel(DbInspector inspector, Object o) {
List<Validation> remarks = inspector.validateModel(o, false);
if (config.traceTest && remarks.size() > 0) {
trace("Validation remarks for " + o.getClass().getName());
for (Validation remark : remarks) {
trace(remark.toString());
}
trace("");
}
for (Validation remark : remarks) {
assertFalse(remark.toString(), remark.isError());
}
}
private void testSupportedTypes() {
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
assertEquals(original.size(), retrieved.size());
for (int i = 0; i < original.size(); i++) {
SupportedTypes o = original.get(i);
SupportedTypes r = retrieved.get(i);
assertTrue(o.equivalentTo(r));
}
}
private void testModelGeneration() {
DbInspector inspector = new DbInspector(db);
List<String> models = inspector.generateModel(null,
"SupportedTypes",
"org.h2.test.jaqu", true, true);
assertEquals(1, models.size());
// a poor test, but a start
assertEquals(1364, models.get(0).length());
}
private void testDatabaseUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// Insert a Database version record
db.insert(new DbVersion(1));
TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader);
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get());
db.close();
}
private void testTableUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// Insert first, this will create version record automatically
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
// Reset the dbUpgrader (clears updatecheck cache)
TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader);
SupportedTypes2 s2 = new SupportedTypes2();
List<SupportedTypes2> types = db.from(s2).select();
assertEquals(10, types.size());
assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get());
db.close();
}
/**
* A sample database upgrader class.
*/
@JQDatabase(version = 2)
class TestDbUpgrader implements DbUpgrader {
final AtomicInteger oldVersion = new AtomicInteger(0);
final AtomicInteger newVersion = new AtomicInteger(0);
public boolean upgradeTable(Db db, String schema, String table,
int fromVersion, int toVersion) {
// just claims success on upgrade request
oldVersion.set(fromVersion);
newVersion.set(toVersion);
return true;
}
public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
// just claims success on upgrade request
oldVersion.set(fromVersion);
newVersion.set(toVersion);
return true;
}
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.h2.jaqu.Db;
import org.h2.jaqu.DbInspector;
import org.h2.jaqu.DbUpgrader;
import org.h2.jaqu.DbVersion;
import org.h2.jaqu.Table.JQDatabase;
import org.h2.jaqu.Validation;
import org.h2.test.TestBase;
import org.h2.test.jaqu.SupportedTypes.SupportedTypes2;
/**
* Test that the mapping between classes and tables is done correctly.
*/
public class ModelsTest extends TestBase {
/**
* This object represents a database (actually a connection to the database).
*/
//## Java 1.5 begin ##
private Db db;
//## Java 1.5 end ##
/**
* This method is called when executing this application from the command
* line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
ModelsTest test = new ModelsTest();
test.init();
test.config.traceTest = true;
test.test();
}
public void test() {
//## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList());
testValidateModels();
testSupportedTypes();
testModelGeneration();
testDatabaseUpgrade();
testTableUpgrade();
db.close();
//## Java 1.5 end ##
}
private void testValidateModels() {
DbInspector inspector = new DbInspector(db);
validateModel(inspector, new Product());
validateModel(inspector, new ProductAnnotationOnly());
validateModel(inspector, new ProductMixedAnnotation());
}
private void validateModel(DbInspector inspector, Object o) {
List<Validation> remarks = inspector.validateModel(o, false);
if (config.traceTest && remarks.size() > 0) {
trace("Validation remarks for " + o.getClass().getName());
for (Validation remark : remarks) {
trace(remark.toString());
}
trace("");
}
for (Validation remark : remarks) {
assertFalse(remark.toString(), remark.isError());
}
}
private void testSupportedTypes() {
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
assertEquals(original.size(), retrieved.size());
for (int i = 0; i < original.size(); i++) {
SupportedTypes o = original.get(i);
SupportedTypes r = retrieved.get(i);
assertTrue(o.equivalentTo(r));
}
}
private void testModelGeneration() {
DbInspector inspector = new DbInspector(db);
List<String> models = inspector.generateModel(null,
"SupportedTypes",
"org.h2.test.jaqu", true, true);
assertEquals(1, models.size());
// a poor test, but a start
assertEquals(1364, models.get(0).length());
}
private void testDatabaseUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// insert a database version record
db.insert(new DbVersion(1));
TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader);
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get());
db.close();
}
private void testTableUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// insert first, this will create version record automatically
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
// reset the dbUpgrader (clears the update check cache)
TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader);
SupportedTypes2 s2 = new SupportedTypes2();
List<SupportedTypes2> types = db.from(s2).select();
assertEquals(10, types.size());
assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get());
db.close();
}
/**
* A sample database upgrader class.
*/
@JQDatabase(version = 2)
class TestDbUpgrader implements DbUpgrader {
final AtomicInteger oldVersion = new AtomicInteger(0);
final AtomicInteger newVersion = new AtomicInteger(0);
public boolean upgradeTable(Db db, String schema, String table,
int fromVersion, int toVersion) {
// just claims success on upgrade request
oldVersion.set(fromVersion);
newVersion.set(toVersion);
return true;
}
public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
// just claims success on upgrade request
oldVersion.set(fromVersion);
newVersion.set(toVersion);
return true;
}
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data.
*/
// ## Java 1.5 begin ##
@JQTable(name = "AnnotatedProduct", primaryKey = "id")
@JQIndex(standard = "name, cat")
public class ProductAnnotationOnly {
@JQColumn(autoIncrement = true)
public Integer autoIncrement;
public String unmappedField;
@JQColumn(name = "id")
Integer productId;
@JQColumn(name = "cat", maxLength = 15, trimString = true)
String category;
@JQColumn(name = "name")
private String productName;
@SuppressWarnings("unused")
@JQColumn
private Double unitPrice;
@JQColumn
private Integer unitsInStock;
public ProductAnnotationOnly() {
// public constructor
}
private ProductAnnotationOnly(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) {
this.productId = productId;
this.productName = productName;
this.category = category;
this.unitPrice = unitPrice;
this.unitsInStock = unitsInStock;
this.unmappedField = unmappedField;
}
private static ProductAnnotationOnly create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) {
return new ProductAnnotationOnly(productId, productName, category, unitPrice, unitsInStock, unmappedField);
}
public static List<ProductAnnotationOnly> getList() {
String unmappedField = "unmapped";
ProductAnnotationOnly[] list = { create(1, "Chai", "Beverages", 18, 39, unmappedField),
create(2, "Chang", "Beverages", 19.0, 17, unmappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, unmappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, unmappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, unmappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, unmappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, unmappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField),
create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), };
return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock;
}
}
// ## Java 1.5 end ##
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
//## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data.
*/
//## Java 1.5 begin ##
@JQTable(name = "AnnotatedProduct", primaryKey = "id")
@JQIndex(standard = "name, cat")
public class ProductAnnotationOnly {
@JQColumn(autoIncrement = true)
public Integer autoIncrement;
public String unmappedField;
@JQColumn(name = "id")
Integer productId;
@JQColumn(name = "cat", maxLength = 15, trimString = true)
String category;
@JQColumn(name = "name")
private String productName;
@SuppressWarnings("unused")
@JQColumn
private Double unitPrice;
@JQColumn
private Integer unitsInStock;
public ProductAnnotationOnly() {
// public constructor
}
private ProductAnnotationOnly(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) {
this.productId = productId;
this.productName = productName;
this.category = category;
this.unitPrice = unitPrice;
this.unitsInStock = unitsInStock;
this.unmappedField = unmappedField;
}
private static ProductAnnotationOnly create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) {
return new ProductAnnotationOnly(productId, productName, category, unitPrice, unitsInStock, unmappedField);
}
public static List<ProductAnnotationOnly> getList() {
String unmappedField = "unmapped";
ProductAnnotationOnly[] list = { create(1, "Chai", "Beverages", 18, 39, unmappedField),
create(2, "Chang", "Beverages", 19.0, 17, unmappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, unmappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, unmappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, unmappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, unmappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, unmappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField),
create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), };
return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock;
}
}
//## Java 1.5 end ##
package org.h2.test.jaqu;
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQTable;
/**
* This class inherits all its fields from a parent class which has annotated
* columns. The JQTable annotation of the parent class is ignored and only
* the JQTable annotation of this class matters.
* However, this table inherits JQColumns from its super class.
*/
@JQTable(inheritColumns = true, annotationsOnly = false)
public class ProductInheritedAnnotation extends ProductMixedAnnotation {
public ProductInheritedAnnotation() {
// public constructor
}
private ProductInheritedAnnotation(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
super(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
private static ProductInheritedAnnotation create(int productId, String productName, String category,
double unitPrice, int unitsInStock, String mappedField) {
return new ProductInheritedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductInheritedAnnotation> getData() {
String mappedField = "mapped";
ProductInheritedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list);
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQTable;
/**
* This class inherits all its fields from a parent class which has annotated
* columns. The JQTable annotation of the parent class is ignored and only
* the JQTable annotation of this class matters.
* However, this table inherits JQColumns from its super class.
*/
@JQTable(inheritColumns = true, annotationsOnly = false)
public class ProductInheritedAnnotation extends ProductMixedAnnotation {
public ProductInheritedAnnotation() {
// public constructor
}
private ProductInheritedAnnotation(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
super(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
private static ProductInheritedAnnotation create(int productId, String productName, String category,
double unitPrice, int unitsInStock, String mappedField) {
return new ProductInheritedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductInheritedAnnotation> getData() {
String mappedField = "mapped";
ProductInheritedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list);
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data.
*/
// ## Java 1.5 begin ##
@JQTable(annotationsOnly = false)
@JQIndex(standard = "name, cat")
public class ProductMixedAnnotation {
public Double unitPrice;
public Integer unitsInStock;
public String mappedField;
@JQColumn(name = "cat", maxLength = 255)
String category;
@JQColumn(name = "id", primaryKey = true)
private Integer productId;
@JQColumn(name = "name")
private String productName;
public ProductMixedAnnotation() {
// public constructor
}
protected ProductMixedAnnotation(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
this.productId = productId;
this.productName = productName;
this.category = category;
this.unitPrice = unitPrice;
this.unitsInStock = unitsInStock;
this.mappedField = mappedField;
}
private static ProductMixedAnnotation create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
return new ProductMixedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductMixedAnnotation> getList() {
String mappedField = "mapped";
ProductMixedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock;
}
public int id() {
return productId;
}
public String name() {
return productName;
}
}
// ## Java 1.5 end ##
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
//## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data.
*/
//## Java 1.5 begin ##
@JQTable(annotationsOnly = false)
@JQIndex(standard = "name, cat")
public class ProductMixedAnnotation {
public Double unitPrice;
public Integer unitsInStock;
public String mappedField;
@JQColumn(name = "cat", maxLength = 255)
String category;
@JQColumn(name = "id", primaryKey = true)
private Integer productId;
@JQColumn(name = "name")
private String productName;
public ProductMixedAnnotation() {
// public constructor
}
protected ProductMixedAnnotation(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
this.productId = productId;
this.productName = productName;
this.category = category;
this.unitPrice = unitPrice;
this.unitsInStock = unitsInStock;
this.mappedField = mappedField;
}
private static ProductMixedAnnotation create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) {
return new ProductMixedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductMixedAnnotation> getList() {
String mappedField = "mapped";
ProductMixedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock;
}
public int id() {
return productId;
}
public String name() {
return productName;
}
}
//## Java 1.5 end ##
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data.
*/
// ## Java 1.5 begin ##
@JQTable(createIfRequired = false)
public class ProductNoCreateTable {
@SuppressWarnings("unused")
@JQColumn(name = "id")
private Integer productId;
@SuppressWarnings("unused")
@JQColumn(name = "name")
private String productName;
public ProductNoCreateTable() {
// public constructor
}
private ProductNoCreateTable(int productId, String productName) {
this.productId = productId;
this.productName = productName;
}
private static ProductNoCreateTable create(int productId, String productName) {
return new ProductNoCreateTable(productId, productName);
}
public static List<ProductNoCreateTable> getList() {
ProductNoCreateTable[] list = { create(1, "Chai"), create(2, "Chang") };
return Arrays.asList(list);
}
}
// ## Java 1.5 end ##
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
//## Java 1.5 begin ##
import java.util.Arrays;
import java.util.List;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable;
//## Java 1.5 end ##
/**
* A table containing product data.
*/
//## Java 1.5 begin ##
@JQTable(createIfRequired = false)
public class ProductNoCreateTable {
@SuppressWarnings("unused")
@JQColumn(name = "id")
private Integer productId;
@SuppressWarnings("unused")
@JQColumn(name = "name")
private String productName;
public ProductNoCreateTable() {
// public constructor
}
private ProductNoCreateTable(int productId, String productName) {
this.productId = productId;
this.productName = productName;
}
private static ProductNoCreateTable create(int productId, String productName) {
return new ProductNoCreateTable(productId, productName);
}
public static List<ProductNoCreateTable> getList() {
ProductNoCreateTable[] list = { create(1, "Chai"), create(2, "Chang") };
return Arrays.asList(list);
}
}
//## Java 1.5 end ##
......@@ -386,7 +386,7 @@ public class SamplesTest extends TestBase {
assertEquals(1, count);
}
private void testLimitOffset() {
Set<Integer> ids = new HashSet<Integer>();
Product p = new Product();
......@@ -398,7 +398,7 @@ public class SamplesTest extends TestBase {
}
}
}
private void testKeyRetrieval() {
List<SupportedTypes> list = SupportedTypes.createList();
List<Long> keys = db.insertAllAndGetKeys(list);
......@@ -406,7 +406,7 @@ public class SamplesTest extends TestBase {
for (Long l : keys) {
assertTrue("Failed to add key. Duplicate?", uniqueKeys.add(l));
}
}
}
//## Java 1.5 end ##
/**
......
package org.h2.test.jaqu;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable;
import org.h2.util.New;
/**
* A data class that contains a column for each data type.
*/
@JQTable(strictTypeMapping = true, version = 1)
public class SupportedTypes {
static final SupportedTypes SAMPLE = new SupportedTypes();
@JQColumn(primaryKey = true, autoIncrement = true)
public Integer id;
@JQColumn
private Boolean myBool = false;
@JQColumn
private Byte myByte = 2;
@JQColumn
private Short myShort;
@JQColumn
private Integer myInteger;
@JQColumn
private Long myLong;
@JQColumn
private Float myFloat = 1.0f;
@JQColumn
private Double myDouble;
@JQColumn
private BigDecimal myBigDecimal;
@JQColumn
private String myString;
@JQColumn
private java.util.Date myUtilDate;
@JQColumn
private java.sql.Date mySqlDate;
@JQColumn
private java.sql.Time mySqlTime;
@JQColumn
private java.sql.Timestamp mySqlTimestamp;
static List<SupportedTypes> createList() {
List<SupportedTypes> list = New.arrayList();
for (int i = 0; i < 10; i++) {
list.add(randomValue());
}
return list;
}
static SupportedTypes randomValue() {
Random rand = new Random();
SupportedTypes s = new SupportedTypes();
s.myBool = new Boolean(rand.nextBoolean());
s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE));
s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE));
s.myInteger = new Integer(rand.nextInt());
s.myLong = new Long(rand.nextLong());
s.myFloat = new Float(rand.nextFloat());
s.myDouble = new Double(rand.nextDouble());
s.myBigDecimal = new BigDecimal(rand.nextDouble());
s.myString = Long.toHexString(rand.nextLong());
s.myUtilDate = new java.util.Date(rand.nextLong());
s.mySqlDate = new java.sql.Date(rand.nextLong());
s.mySqlTime = new java.sql.Time(rand.nextLong());
s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong());
return s;
}
public boolean equivalentTo(SupportedTypes s) {
boolean same = true;
same &= myBool.equals(s.myBool);
same &= myByte.equals(s.myByte);
same &= myShort.equals(s.myShort);
same &= myInteger.equals(s.myInteger);
same &= myLong.equals(s.myLong);
same &= myFloat.equals(s.myFloat);
same &= myDouble.equals(s.myDouble);
same &= myBigDecimal.equals(s.myBigDecimal);
same &= myUtilDate.getTime() == s.myUtilDate.getTime();
same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime();
same &= mySqlDate.toString().equals(s.mySqlDate.toString());
same &= mySqlTime.toString().equals(s.mySqlTime.toString());
same &= myString.equals(s.myString);
return same;
}
/**
* Class to demonstrate TableUpdater
*
*/
@JQTable(name = "SupportedTypes", version = 2, inheritColumns = true, strictTypeMapping = true)
public static class SupportedTypes2 extends SupportedTypes {
public SupportedTypes2() {
// nothing to do
}
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: James Moger
*/
package org.h2.test.jaqu;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable;
import org.h2.util.New;
/**
* A data class that contains a column for each data type.
*/
@JQTable(strictTypeMapping = true, version = 1)
public class SupportedTypes {
static final SupportedTypes SAMPLE = new SupportedTypes();
@JQColumn(primaryKey = true, autoIncrement = true)
public Integer id;
@JQColumn
private Boolean myBool = false;
@JQColumn
private Byte myByte = 2;
@JQColumn
private Short myShort;
@JQColumn
private Integer myInteger;
@JQColumn
private Long myLong;
@JQColumn
private Float myFloat = 1.0f;
@JQColumn
private Double myDouble;
@JQColumn
private BigDecimal myBigDecimal;
@JQColumn
private String myString;
@JQColumn
private java.util.Date myUtilDate;
@JQColumn
private java.sql.Date mySqlDate;
@JQColumn
private java.sql.Time mySqlTime;
@JQColumn
private java.sql.Timestamp mySqlTimestamp;
static List<SupportedTypes> createList() {
List<SupportedTypes> list = New.arrayList();
for (int i = 0; i < 10; i++) {
list.add(randomValue());
}
return list;
}
static SupportedTypes randomValue() {
Random rand = new Random();
SupportedTypes s = new SupportedTypes();
s.myBool = new Boolean(rand.nextBoolean());
s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE));
s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE));
s.myInteger = new Integer(rand.nextInt());
s.myLong = new Long(rand.nextLong());
s.myFloat = new Float(rand.nextFloat());
s.myDouble = new Double(rand.nextDouble());
s.myBigDecimal = new BigDecimal(rand.nextDouble());
s.myString = Long.toHexString(rand.nextLong());
s.myUtilDate = new java.util.Date(rand.nextLong());
s.mySqlDate = new java.sql.Date(rand.nextLong());
s.mySqlTime = new java.sql.Time(rand.nextLong());
s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong());
return s;
}
public boolean equivalentTo(SupportedTypes s) {
boolean same = true;
same &= myBool.equals(s.myBool);
same &= myByte.equals(s.myByte);
same &= myShort.equals(s.myShort);
same &= myInteger.equals(s.myInteger);
same &= myLong.equals(s.myLong);
same &= myFloat.equals(s.myFloat);
same &= myDouble.equals(s.myDouble);
same &= myBigDecimal.equals(s.myBigDecimal);
same &= myUtilDate.getTime() == s.myUtilDate.getTime();
same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime();
same &= mySqlDate.toString().equals(s.mySqlDate.toString());
same &= mySqlTime.toString().equals(s.mySqlTime.toString());
same &= myString.equals(s.myString);
return same;
}
/**
* Class to demonstrate TableUpdater
*
*/
@JQTable(name = "SupportedTypes", version = 2, inheritColumns = true, strictTypeMapping = true)
public static class SupportedTypes2 extends SupportedTypes {
public SupportedTypes2() {
// nothing to do
}
}
}
......@@ -18,7 +18,7 @@ import static java.sql.Date.valueOf;
*/
public class UpdateTest extends TestBase {
Db db;
private Db db;
/**
* This method is called when executing this application from the command
......@@ -112,11 +112,11 @@ public class UpdateTest extends TestBase {
ourOrder.orderDate = valueOf("2007-01-02");
db.merge(ourOrder);
}
private void testSetColumns() {
Product p = new Product();
Product original = db.from(p).where(p.productId).is(1).selectFirst();
// update string and double columns
db.from(p)
.set(p.productName).to("updated")
......@@ -125,9 +125,9 @@ public class UpdateTest extends TestBase {
.where(p.productId)
.is(1).
update();
// confirm the data was properly updated
Product revised = db.from(p).where(p.productId).is(1).selectFirst();
Product revised = db.from(p).where(p.productId).is(1).selectFirst();
assertEquals("updated", revised.productName);
assertEquals(original.unitPrice + 3.14, revised.unitPrice);
assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue());
......@@ -138,12 +138,12 @@ public class UpdateTest extends TestBase {
.set(p.unitPrice).to(original.unitPrice)
.increment(p.unitsInStock).by(-2)
.where(p.productId).is(1).update();
// confirm the data was properly restored
Product restored = db.from(p).where(p.productId).is(1).selectFirst();
assertEquals(original.productName, restored.productName);
assertEquals(original.unitPrice, restored.unitPrice);
assertEquals(original.unitsInStock, restored.unitsInStock);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论