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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 com.iciql.Db;
  19. import com.iciql.IciqlException;
  20. import com.iciql.test.models.Product;
  21. import com.iciql.test.models.ProductAnnotationOnly;
  22. import com.iciql.test.models.ProductInheritedAnnotation;
  23. import com.iciql.test.models.ProductMixedAnnotation;
  24. import com.iciql.test.models.ProductNoCreateTable;
  25. import com.iciql.util.Utils;
  26. import org.junit.After;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import java.sql.DatabaseMetaData;
  30. import java.sql.ResultSet;
  31. import java.sql.SQLException;
  32. import java.util.List;
  33. import static org.junit.Assert.assertEquals;
  34. import static org.junit.Assert.assertTrue;
  35. /**
  36. * Test annotation processing.
  37. */
  38. public class AnnotationsTest {
  39. /**
  40. * This object represents a database (actually a connection to the
  41. * database).
  42. */
  43. private Db db;
  44. @Before
  45. public void setUp() {
  46. db = IciqlSuite.openNewDb();
  47. db.insertAll(Product.getList());
  48. db.insertAll(ProductAnnotationOnly.getList());
  49. db.insertAll(ProductMixedAnnotation.getList());
  50. }
  51. @After
  52. public void tearDown() {
  53. db.close();
  54. }
  55. @Test
  56. public void testIndexCreation() throws SQLException {
  57. // test indexes are created, and columns are in the right order
  58. DatabaseMetaData meta = db.getConnection().getMetaData();
  59. String schema = IciqlSuite.getDefaultSchema(db);
  60. boolean toUpper = meta.storesUpperCaseIdentifiers();
  61. boolean toLower = meta.storesLowerCaseIdentifiers();
  62. ResultSet rs = meta.getIndexInfo(null, prepName(schema, toUpper, toLower),
  63. prepName("ANNOTATEDPRODUCT", toUpper, toLower), false, true);
  64. List<String> list = Utils.newArrayList();
  65. while (rs.next()) {
  66. String col = rs.getString("COLUMN_NAME");
  67. String index = rs.getString("INDEX_NAME");
  68. list.add((col + ":" + index).toLowerCase());
  69. }
  70. assertTrue(list.contains("name:annotatedproduct_idx_0"));
  71. assertTrue(list.contains("cat:annotatedproduct_idx_0"));
  72. assertTrue(list.contains("name:nameidx"));
  73. }
  74. private String prepName(String name, boolean upper, boolean lower) {
  75. if (name == null) {
  76. return null;
  77. }
  78. if (upper) {
  79. return name.toUpperCase();
  80. } else if (lower) {
  81. return name.toLowerCase();
  82. }
  83. return name;
  84. }
  85. @Test
  86. public void testProductAnnotationOnly() {
  87. ProductAnnotationOnly p = new ProductAnnotationOnly();
  88. assertEquals(10, db.from(p).selectCount());
  89. // test IQColumn.name="cat"
  90. assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
  91. // test IQTable.annotationsOnly=true
  92. // public String unmappedField is ignored by iciql
  93. try {
  94. db.from(p).where(p.unmappedField).is("unmapped").selectCount();
  95. assertTrue("this should never execute", false);
  96. } catch (IciqlException e) {
  97. assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode());
  98. }
  99. // 10 objects, 10 autoIncremented unique values
  100. assertEquals(10, db.from(p).selectDistinct(p.productName).size());
  101. // test IQTable.primaryKey=id
  102. try {
  103. db.insertAll(ProductAnnotationOnly.getList());
  104. } catch (IciqlException e) {
  105. assertEquals(IciqlException.CODE_DUPLICATE_KEY, e.getIciqlCode());
  106. }
  107. }
  108. @Test
  109. public void testProductMixedAnnotation() {
  110. ProductMixedAnnotation p = new ProductMixedAnnotation();
  111. // test IQColumn.name="cat"
  112. assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
  113. // test IQTable.annotationsOnly=false
  114. // public String mappedField is reflectively mapped by iciql
  115. assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
  116. // test IQIgnore annotation
  117. assertEquals(null, db.from(p).selectFirst().productDescription);
  118. // test IQColumn.primaryKey=true
  119. try {
  120. db.insertAll(ProductMixedAnnotation.getList());
  121. } catch (IciqlException e) {
  122. assertEquals(IciqlException.CODE_DUPLICATE_KEY, e.getIciqlCode());
  123. }
  124. }
  125. @Test
  126. public void testTrimStringAnnotation() {
  127. ProductAnnotationOnly p = new ProductAnnotationOnly();
  128. ProductAnnotationOnly prod = db.from(p).selectFirst();
  129. String oldValue = prod.category;
  130. String newValue = "01234567890123456789";
  131. // 2 chars exceeds field max
  132. prod.category = newValue;
  133. db.update(prod);
  134. ProductAnnotationOnly newProd = db.from(p).where(p.productId).is(prod.productId).selectFirst();
  135. assertEquals(newValue.substring(0, 15), newProd.category);
  136. newProd.category = oldValue;
  137. db.update(newProd);
  138. }
  139. @Test
  140. public void testColumnInheritanceAnnotation() {
  141. ProductInheritedAnnotation table = new ProductInheritedAnnotation();
  142. List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
  143. db.insertAll(inserted);
  144. List<ProductInheritedAnnotation> retrieved = db.from(table).select();
  145. for (int j = 0; j < retrieved.size(); j++) {
  146. ProductInheritedAnnotation i = inserted.get(j);
  147. ProductInheritedAnnotation r = retrieved.get(j);
  148. assertEquals(i.category, r.category);
  149. assertEquals(i.mappedField, r.mappedField);
  150. assertEquals(i.unitsInStock, r.unitsInStock);
  151. assertEquals(i.unitPrice, r.unitPrice);
  152. assertEquals(i.name(), r.name());
  153. assertEquals(i.id(), r.id());
  154. }
  155. }
  156. @Test
  157. public void testCreateTableIfRequiredAnnotation() {
  158. // tests IQTable.createTableIfRequired=false
  159. try {
  160. db.insertAll(ProductNoCreateTable.getList());
  161. } catch (IciqlException e) {
  162. assertEquals(IciqlException.CODE_OBJECT_NOT_FOUND, e.getIciqlCode());
  163. }
  164. }
  165. }