提交 5cdfa58c authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Source code formatting (Eclipse will find this)
- Missing Javadoc class comment
- The test didn't compare myString (Eclipse will find this)
- Missing space around '=' (Checkstyle will find this)
上级 164f02af
...@@ -92,7 +92,8 @@ public class ModelsTest extends TestBase { ...@@ -92,7 +92,8 @@ public class ModelsTest extends TestBase {
private void testModelGeneration() { private void testModelGeneration() {
DbInspector inspector = new DbInspector(db); DbInspector inspector = new DbInspector(db);
List<String> models = inspector.generateModel(null, "SupportedTypes", List<String> models = inspector.generateModel(null,
"SupportedTypes",
"org.h2.test.jaqu", true, true); "org.h2.test.jaqu", true, true);
assertEquals(1, models.size()); assertEquals(1, models.size());
// a poor test, but a start // a poor test, but a start
......
...@@ -20,26 +20,27 @@ import org.h2.jaqu.Table.JQTable; ...@@ -20,26 +20,27 @@ import org.h2.jaqu.Table.JQTable;
@JQIndex(standard = "name, cat") @JQIndex(standard = "name, cat")
public class ProductAnnotationOnly { public class ProductAnnotationOnly {
@JQColumn(autoIncrement = true)
public Integer autoIncrement;
public String unmappedField;
@JQColumn(name = "id") @JQColumn(name = "id")
Integer productId; Integer productId;
@JQColumn(name = "cat", maxLength = 15, trimString = true)
String category;
@JQColumn(name = "name") @JQColumn(name = "name")
private String productName; private String productName;
@JQColumn(name = "cat", maxLength = 15, trimString=true)
String category;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@JQColumn @JQColumn
private Double unitPrice; private Double unitPrice;
@JQColumn @JQColumn
private Integer unitsInStock; private Integer unitsInStock;
@JQColumn(autoIncrement=true)
public Integer autoIncrement;
public String unmappedField;
public ProductAnnotationOnly() { public ProductAnnotationOnly() {
// public constructor // public constructor
} }
...@@ -71,7 +72,6 @@ public class ProductAnnotationOnly { ...@@ -71,7 +72,6 @@ public class ProductAnnotationOnly {
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField), create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField), create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField),
create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), }; create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), };
return Arrays.asList(list); return Arrays.asList(list);
} }
......
...@@ -8,12 +8,11 @@ import org.h2.jaqu.Table.JQTable; ...@@ -8,12 +8,11 @@ import org.h2.jaqu.Table.JQTable;
* This class inherits all its fields from a parent class which has annotated * 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 * columns. The JQTable annotation of the parent class is ignored and only
* the JQTable annotation of this class matters. * the JQTable annotation of this class matters.
* <p>
* However, this table inherits JQColumns from its super class. * However, this table inherits JQColumns from its super class.
*/ */
@JQTable(inheritColumns = true, annotationsOnly = false) @JQTable(inheritColumns = true, annotationsOnly = false)
public class ProductInheritedAnnotation extends ProductMixedAnnotation { public class ProductInheritedAnnotation extends ProductMixedAnnotation {
public ProductInheritedAnnotation() { public ProductInheritedAnnotation() {
// public constructor // public constructor
} }
...@@ -40,7 +39,7 @@ public class ProductInheritedAnnotation extends ProductMixedAnnotation { ...@@ -40,7 +39,7 @@ public class ProductInheritedAnnotation extends ProductMixedAnnotation {
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField), create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField), create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), }; create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list); return Arrays.asList(list);
} }
} }
...@@ -17,21 +17,21 @@ import org.h2.jaqu.Table.JQTable; ...@@ -17,21 +17,21 @@ import org.h2.jaqu.Table.JQTable;
*/ */
// ## Java 1.5 begin ## // ## Java 1.5 begin ##
@JQTable(annotationsOnly = false) @JQTable(annotationsOnly = false)
@JQIndex(standard="name,cat") @JQIndex(standard = "name, cat")
public class ProductMixedAnnotation { public class ProductMixedAnnotation {
@JQColumn(name = "id", primaryKey=true) public Double unitPrice;
private Integer productId; public Integer unitsInStock;
public String mappedField;
@JQColumn(name = "name")
private String productName;
@JQColumn(name = "cat", maxLength = 255) @JQColumn(name = "cat", maxLength = 255)
String category; String category;
public Double unitPrice; @JQColumn(name = "id", primaryKey = true)
public Integer unitsInStock; private Integer productId;
public String mappedField;
@JQColumn(name = "name")
private String productName;
public ProductMixedAnnotation() { public ProductMixedAnnotation() {
// public constructor // public constructor
...@@ -64,20 +64,20 @@ public class ProductMixedAnnotation { ...@@ -64,20 +64,20 @@ public class ProductMixedAnnotation {
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField), create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField), create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), }; create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list); return Arrays.asList(list);
} }
public String toString() { public String toString() {
return productName + ": " + unitsInStock; return productName + ": " + unitsInStock;
} }
public int id() { public int id() {
return productId; return productId;
} }
public String name() { public String name() {
return productName; return productName;
} }
} }
// ## Java 1.5 end ## // ## Java 1.5 end ##
...@@ -391,10 +391,11 @@ public class SamplesTest extends TestBase { ...@@ -391,10 +391,11 @@ public class SamplesTest extends TestBase {
Set<Integer> ids = new HashSet<Integer>(); Set<Integer> ids = new HashSet<Integer>();
Product p = new Product(); Product p = new Product();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
List<Product> products = db.from(p).limit(2).offset(2*i).select(); List<Product> products = db.from(p).limit(2).offset(2 * i).select();
assertTrue(products.size() == 2); assertTrue(products.size() == 2);
for (Product prod:products) for (Product prod : products) {
assertTrue("Failed to add product id. Duplicate?", ids.add(prod.productId)); assertTrue("Failed to add product id. Duplicate?", ids.add(prod.productId));
}
} }
} }
...@@ -402,9 +403,10 @@ public class SamplesTest extends TestBase { ...@@ -402,9 +403,10 @@ public class SamplesTest extends TestBase {
List<SupportedTypes> list = SupportedTypes.createList(); List<SupportedTypes> list = SupportedTypes.createList();
List<Long> keys = db.insertAllAndGetKeys(list); List<Long> keys = db.insertAllAndGetKeys(list);
Set<Long> uniqueKeys = new HashSet<Long>(); Set<Long> uniqueKeys = new HashSet<Long>();
for (Long l:keys) for (Long l : keys) {
assertTrue("Failed to add key. Duplicate?", uniqueKeys.add(l)); assertTrue("Failed to add key. Duplicate?", uniqueKeys.add(l));
} }
}
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
......
...@@ -7,12 +7,17 @@ import org.h2.jaqu.Table.JQColumn; ...@@ -7,12 +7,17 @@ import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable; import org.h2.jaqu.Table.JQTable;
import org.h2.util.New; import org.h2.util.New;
@JQTable(strictTypeMapping=true, version=1) /**
* A data class that contains a column for each data type.
*/
@JQTable(strictTypeMapping = true, version = 1)
public class SupportedTypes { public class SupportedTypes {
@JQColumn(primaryKey=true, autoIncrement=true) static final SupportedTypes SAMPLE = new SupportedTypes();
@JQColumn(primaryKey = true, autoIncrement = true)
public Integer id; public Integer id;
@JQColumn @JQColumn
private Boolean myBool = false; private Boolean myBool = false;
...@@ -27,7 +32,7 @@ public class SupportedTypes { ...@@ -27,7 +32,7 @@ public class SupportedTypes {
@JQColumn @JQColumn
private Long myLong; private Long myLong;
@JQColumn @JQColumn
private Float myFloat = 1.0f; private Float myFloat = 1.0f;
...@@ -52,25 +57,22 @@ public class SupportedTypes { ...@@ -52,25 +57,22 @@ public class SupportedTypes {
@JQColumn @JQColumn
private java.sql.Timestamp mySqlTimestamp; private java.sql.Timestamp mySqlTimestamp;
static SupportedTypes SAMPLE = new SupportedTypes();
static List<SupportedTypes> createList() { static List<SupportedTypes> createList() {
List<SupportedTypes> list = New.arrayList(); List<SupportedTypes> list = New.arrayList();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++) {
list.add(randomValue()); list.add(randomValue());
}
return list; return list;
} }
static SupportedTypes randomValue() { static SupportedTypes randomValue() {
Random rand = new Random(); Random rand = new Random();
SupportedTypes s = new SupportedTypes(); SupportedTypes s = new SupportedTypes();
s.myBool = new Boolean(rand.nextBoolean()); s.myBool = new Boolean(rand.nextBoolean());
s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE)); s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE));
s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE)); s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE));
s.myInteger = new Integer(rand.nextInt()); s.myInteger = new Integer(rand.nextInt());
s.myLong = new Long(rand.nextLong()); s.myLong = new Long(rand.nextLong());
s.myFloat = new Float(rand.nextFloat()); s.myFloat = new Float(rand.nextFloat());
s.myDouble = new Double(rand.nextDouble()); s.myDouble = new Double(rand.nextDouble());
s.myBigDecimal = new BigDecimal(rand.nextDouble()); s.myBigDecimal = new BigDecimal(rand.nextDouble());
...@@ -81,7 +83,7 @@ public class SupportedTypes { ...@@ -81,7 +83,7 @@ public class SupportedTypes {
s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong()); s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong());
return s; return s;
} }
public boolean equivalentTo(SupportedTypes s) { public boolean equivalentTo(SupportedTypes s) {
boolean same = true; boolean same = true;
same &= myBool.equals(s.myBool); same &= myBool.equals(s.myBool);
...@@ -96,17 +98,19 @@ public class SupportedTypes { ...@@ -96,17 +98,19 @@ public class SupportedTypes {
same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime(); same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime();
same &= mySqlDate.toString().equals(s.mySqlDate.toString()); same &= mySqlDate.toString().equals(s.mySqlDate.toString());
same &= mySqlTime.toString().equals(s.mySqlTime.toString()); same &= mySqlTime.toString().equals(s.mySqlTime.toString());
same &= myString.equals(s.myString);
return same; return same;
} }
/** /**
* Class to demonstrate TableUpdater * Class to demonstrate TableUpdater
* *
*/ */
@JQTable(name="SupportedTypes", version=2, inheritColumns=true, strictTypeMapping=true) @JQTable(name = "SupportedTypes", version = 2, inheritColumns = true, strictTypeMapping = true)
public static class SupportedTypes2 extends SupportedTypes { public static class SupportedTypes2 extends SupportedTypes {
public SupportedTypes2() { public SupportedTypes2() {
// nothing to do
} }
} }
} }
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
import static java.sql.Date.valueOf;
import org.h2.jaqu.Db; import org.h2.jaqu.Db;
import org.h2.jaqu.util.StatementLogger;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import static java.sql.Date.valueOf;
/** /**
* Tests the Db.update() function. * Tests the Db.update() function.
* *
...@@ -31,8 +31,6 @@ public class UpdateTest extends TestBase { ...@@ -31,8 +31,6 @@ public class UpdateTest extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
// 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(Customer.getList()); db.insertAll(Customer.getList());
...@@ -45,7 +43,6 @@ public class UpdateTest extends TestBase { ...@@ -45,7 +43,6 @@ public class UpdateTest extends TestBase {
testSetColumns(); testSetColumns();
db.close(); db.close();
// EventLogger.deactivateConsoleLogger();
} }
private void testSimpleUpdate() { private void testSimpleUpdate() {
...@@ -120,7 +117,7 @@ public class UpdateTest extends TestBase { ...@@ -120,7 +117,7 @@ public class UpdateTest extends TestBase {
Product p = new Product(); Product p = new Product();
Product original = db.from(p).where(p.productId).is(1).selectFirst(); Product original = db.from(p).where(p.productId).is(1).selectFirst();
// SetColumn on String and Double // update string and double columns
db.from(p) db.from(p)
.set(p.productName).to("updated") .set(p.productName).to("updated")
.increment(p.unitPrice).by(3.14) .increment(p.unitPrice).by(3.14)
...@@ -129,20 +126,20 @@ public class UpdateTest extends TestBase { ...@@ -129,20 +126,20 @@ public class UpdateTest extends TestBase {
.is(1). .is(1).
update(); update();
// Confirm fields were properly updated // 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("updated", revised.productName);
assertEquals(original.unitPrice + 3.14, revised.unitPrice); assertEquals(original.unitPrice + 3.14, revised.unitPrice);
assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue()); assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue());
// Restore fields // restore the data
db.from(p) db.from(p)
.set(p.productName).to(original.productName) .set(p.productName).to(original.productName)
.set(p.unitPrice).to(original.unitPrice) .set(p.unitPrice).to(original.unitPrice)
.increment(p.unitsInStock).by(-2) .increment(p.unitsInStock).by(-2)
.where(p.productId).is(1).update(); .where(p.productId).is(1).update();
// Confirm fields were properly restored // confirm the data was properly restored
Product restored = db.from(p).where(p.productId).is(1).selectFirst(); Product restored = db.from(p).where(p.productId).is(1).selectFirst();
assertEquals(original.productName, restored.productName); assertEquals(original.productName, restored.productName);
assertEquals(original.unitPrice, restored.unitPrice); assertEquals(original.unitPrice, restored.unitPrice);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论