You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnnotationsTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.test;
  18. import static com.iciql.test.IciqlSuite.assertStartsWith;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertTrue;
  22. import java.sql.DatabaseMetaData;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.List;
  26. import org.h2.constant.ErrorCode;
  27. import org.junit.After;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import com.iciql.Db;
  31. import com.iciql.IciqlException;
  32. import com.iciql.test.models.Product;
  33. import com.iciql.test.models.ProductAnnotationOnly;
  34. import com.iciql.test.models.ProductInheritedAnnotation;
  35. import com.iciql.test.models.ProductMixedAnnotation;
  36. import com.iciql.test.models.ProductNoCreateTable;
  37. /**
  38. * Test annotation processing.
  39. */
  40. public class AnnotationsTest {
  41. /**
  42. * This object represents a database (actually a connection to the
  43. * database).
  44. */
  45. private Db db;
  46. @Before
  47. public void setUp() {
  48. db = Db.open("jdbc:h2:mem:", "sa", "sa");
  49. db.insertAll(Product.getList());
  50. db.insertAll(ProductAnnotationOnly.getList());
  51. db.insertAll(ProductMixedAnnotation.getList());
  52. }
  53. @After
  54. public void tearDown() {
  55. db.close();
  56. }
  57. @Test
  58. public void testIndexCreation() throws SQLException {
  59. // test indexes are created, and columns are in the right order
  60. DatabaseMetaData meta = db.getConnection().getMetaData();
  61. ResultSet rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATED" + "PRODUCT", false, true);
  62. assertTrue(rs.next());
  63. assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY");
  64. assertTrue(rs.next());
  65. assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
  66. assertStartsWith(rs.getString("COLUMN_NAME"), "NAME");
  67. assertTrue(rs.next());
  68. assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
  69. assertStartsWith(rs.getString("COLUMN_NAME"), "CAT");
  70. assertFalse(rs.next());
  71. }
  72. @Test
  73. public void testProductAnnotationOnly() {
  74. ProductAnnotationOnly p = new ProductAnnotationOnly();
  75. assertEquals(10, db.from(p).selectCount());
  76. // test IQColumn.name="cat"
  77. assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
  78. // test IQTable.annotationsOnly=true
  79. // public String unmappedField is ignored by iciql
  80. assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount());
  81. // test IQColumn.autoIncrement=true
  82. // 10 objects, 10 autoIncremented unique values
  83. assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size());
  84. // test IQTable.primaryKey=id
  85. try {
  86. db.insertAll(ProductAnnotationOnly.getList());
  87. } catch (IciqlException r) {
  88. SQLException s = (SQLException) r.getCause();
  89. assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
  90. }
  91. }
  92. @Test
  93. public void testProductMixedAnnotation() {
  94. ProductMixedAnnotation p = new ProductMixedAnnotation();
  95. // test IQColumn.name="cat"
  96. assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
  97. // test IQTable.annotationsOnly=false
  98. // public String mappedField is reflectively mapped by iciql
  99. assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
  100. // test IQColumn.primaryKey=true
  101. try {
  102. db.insertAll(ProductMixedAnnotation.getList());
  103. } catch (IciqlException r) {
  104. SQLException s = (SQLException) r.getCause();
  105. assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
  106. }
  107. }
  108. @Test
  109. public void testTrimStringAnnotation() {
  110. ProductAnnotationOnly p = new ProductAnnotationOnly();
  111. ProductAnnotationOnly prod = db.from(p).selectFirst();
  112. String oldValue = prod.category;
  113. String newValue = "01234567890123456789";
  114. // 2 chars exceeds field max
  115. prod.category = newValue;
  116. db.update(prod);
  117. ProductAnnotationOnly newProd = db.from(p).where(p.productId).is(prod.productId).selectFirst();
  118. assertEquals(newValue.substring(0, 15), newProd.category);
  119. newProd.category = oldValue;
  120. db.update(newProd);
  121. }
  122. @Test
  123. public void testColumnInheritanceAnnotation() {
  124. ProductInheritedAnnotation table = new ProductInheritedAnnotation();
  125. List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
  126. db.insertAll(inserted);
  127. List<ProductInheritedAnnotation> retrieved = db.from(table).select();
  128. for (int j = 0; j < retrieved.size(); j++) {
  129. ProductInheritedAnnotation i = inserted.get(j);
  130. ProductInheritedAnnotation r = retrieved.get(j);
  131. assertEquals(i.category, r.category);
  132. assertEquals(i.mappedField, r.mappedField);
  133. assertEquals(i.unitsInStock, r.unitsInStock);
  134. assertEquals(i.unitPrice, r.unitPrice);
  135. assertEquals(i.name(), r.name());
  136. assertEquals(i.id(), r.id());
  137. }
  138. }
  139. @Test
  140. public void testCreateTableIfRequiredAnnotation() {
  141. // tests IQTable.createTableIfRequired=false
  142. try {
  143. db.insertAll(ProductNoCreateTable.getList());
  144. } catch (IciqlException r) {
  145. SQLException s = (SQLException) r.getCause();
  146. assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
  147. }
  148. }
  149. }