Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ModelsTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.assertEqualsIgnoreCase;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import java.sql.SQLException;
  22. import java.text.MessageFormat;
  23. import java.util.List;
  24. import org.junit.After;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ErrorCollector;
  29. import com.iciql.Db;
  30. import com.iciql.DbInspector;
  31. import com.iciql.ValidationRemark;
  32. import com.iciql.test.models.Product;
  33. import com.iciql.test.models.ProductAnnotationOnly;
  34. import com.iciql.test.models.ProductMixedAnnotation;
  35. import com.iciql.test.models.SupportedTypes;
  36. import com.iciql.util.StringUtils;
  37. /**
  38. * Test that the mapping between classes and tables is done correctly.
  39. */
  40. public class ModelsTest {
  41. /*
  42. * The ErrorCollector Rule allows execution of a test to continue after the
  43. * first problem is found and report them all at once
  44. */
  45. @Rule
  46. public ErrorCollector errorCollector = new ErrorCollector();
  47. private Db db;
  48. @Before
  49. public void setUp() {
  50. db = IciqlSuite.openNewDb();
  51. db.insertAll(Product.getList());
  52. db.insertAll(ProductAnnotationOnly.getList());
  53. db.insertAll(ProductMixedAnnotation.getList());
  54. }
  55. @After
  56. public void tearDown() {
  57. db.close();
  58. }
  59. @Test
  60. public void testValidateModels() {
  61. // SQLite metadata mapping in the JDBC driver needs improvement
  62. String schemaName = IciqlSuite.getDefaultSchema(db);
  63. DbInspector inspector = new DbInspector(db);
  64. validateModel(inspector, schemaName, new ProductAnnotationOnly(), IciqlSuite.isSQLite(db) ? 5 : 2);
  65. validateModel(inspector, schemaName, new ProductMixedAnnotation(), IciqlSuite.isSQLite(db) ? 6 : 4);
  66. }
  67. private void validateModel(DbInspector inspector, String schemaName, Object o, int expected) {
  68. List<ValidationRemark> remarks = inspector.validateModel(o, false);
  69. assertTrue("validation remarks are null for " + o.getClass().getName(), remarks != null);
  70. StringBuilder sb = new StringBuilder();
  71. sb.append("validation remarks for " + o.getClass().getName());
  72. sb.append('\n');
  73. for (ValidationRemark remark : remarks) {
  74. sb.append(remark.toString());
  75. sb.append('\n');
  76. if (remark.isError()) {
  77. errorCollector.addError(new SQLException(remark.toString()));
  78. }
  79. }
  80. if (IciqlSuite.isSQLite(db)) {
  81. assertEquals(sb.toString(), expected, remarks.size());
  82. } else if (StringUtils.isNullOrEmpty(schemaName)) {
  83. // no schema expected
  84. assertEquals(sb.toString(), expected - 1, remarks.size());
  85. } else {
  86. assertEquals(sb.toString(), expected, remarks.size());
  87. assertEqualsIgnoreCase(MessageFormat.format("@IQSchema(\"{0}\")", schemaName),
  88. remarks.get(0).message);
  89. }
  90. }
  91. @Test
  92. public void testSupportedTypes() {
  93. List<SupportedTypes> original = SupportedTypes.createList();
  94. db.insertAll(original);
  95. List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
  96. assertEquals(original.size(), retrieved.size());
  97. for (int i = 0; i < original.size(); i++) {
  98. SupportedTypes o = original.get(i);
  99. SupportedTypes r = retrieved.get(i);
  100. assertTrue(o.equivalentTo(r));
  101. }
  102. }
  103. @Test
  104. public void testModelGeneration() {
  105. List<SupportedTypes> original = SupportedTypes.createList();
  106. db.insertAll(original);
  107. DbInspector inspector = new DbInspector(db);
  108. List<String> models = inspector.generateModel(null, "SupportedTypes", "com.iciql.test.models", true,
  109. true);
  110. assertEquals(1, models.size());
  111. // a poor test, but a start
  112. String dbName = IciqlSuite.getDatabaseEngineName(db);
  113. if (dbName.equals("H2")) {
  114. assertEquals(1587, models.get(0).length());
  115. } else if (dbName.startsWith("HSQL")) {
  116. // HSQL uses Double instead of Float
  117. assertEquals(1591, models.get(0).length());
  118. } else if (dbName.equals("Apache Derby")) {
  119. // Derby uses java.sql.Timestamp not java.util.Date
  120. // Derby uses username as schema name
  121. assertEquals(1601, models.get(0).length());
  122. } else if (dbName.equals("PostgreSQL")) {
  123. assertEquals(1643, models.get(0).length());
  124. } else if (dbName.equals("MySQL")) {
  125. // MySQL uses timestamp default values like
  126. // 0000-00-00 00:00:00 and CURRENT_TIMESTAMP
  127. assertEquals(1673, models.get(0).length());
  128. } else if (dbName.equals("SQLite")) {
  129. assertEquals(1566, models.get(0).length());
  130. } else {
  131. // unknown database
  132. assertEquals(0, models.get(0).length());
  133. }
  134. }
  135. @Test
  136. public void testDiscreteUpdateStringTrimming() {
  137. List<SupportedTypes> original = SupportedTypes.createList();
  138. db.insertAll(original);
  139. SupportedTypes s1 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
  140. db.from(SupportedTypes.SAMPLE)
  141. .set(SupportedTypes.SAMPLE.myString)
  142. .to(s1.myString + s1.myString + s1.myString + s1.myString)
  143. .update();
  144. SupportedTypes s2 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
  145. assertEquals(40, s2.myString.length());
  146. }
  147. @Test
  148. public void testColumnSelection() {
  149. List<SupportedTypes> original = SupportedTypes.createList();
  150. db.insertAll(original);
  151. List<String> myStrings = db.from(SupportedTypes.SAMPLE)
  152. .select(SupportedTypes.SAMPLE.myString);
  153. assertEquals(10, myStrings.size());
  154. List<Integer> ids = db.from(SupportedTypes.SAMPLE)
  155. .orderByDesc(SupportedTypes.SAMPLE.id)
  156. .selectDistinct(SupportedTypes.SAMPLE.id);
  157. assertEquals(10, ids.size());
  158. assertEquals("[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]", ids.toString());
  159. }
  160. }