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.

ModelsTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertTrue;
  20. import java.sql.SQLException;
  21. import java.util.List;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import org.junit.After;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ErrorCollector;
  28. import com.iciql.Db;
  29. import com.iciql.DbInspector;
  30. import com.iciql.DbUpgrader;
  31. import com.iciql.DbVersion;
  32. import com.iciql.Iciql.IQDatabase;
  33. import com.iciql.ValidationRemark;
  34. import com.iciql.test.models.Product;
  35. import com.iciql.test.models.ProductAnnotationOnly;
  36. import com.iciql.test.models.ProductMixedAnnotation;
  37. import com.iciql.test.models.SupportedTypes;
  38. import com.iciql.test.models.SupportedTypes.SupportedTypes2;
  39. /**
  40. * Test that the mapping between classes and tables is done correctly.
  41. */
  42. public class ModelsTest {
  43. /*
  44. * The ErrorCollector Rule allows execution of a test to continue after the
  45. * first problem is found and report them all at once
  46. */
  47. @Rule
  48. public ErrorCollector errorCollector = new ErrorCollector();
  49. private Db db;
  50. private void log(String text) {
  51. System.out.println(text);
  52. }
  53. @Before
  54. public void setUp() {
  55. db = Db.open("jdbc:h2:mem:", "sa", "sa");
  56. db.insertAll(Product.getList());
  57. db.insertAll(ProductAnnotationOnly.getList());
  58. db.insertAll(ProductMixedAnnotation.getList());
  59. }
  60. @After
  61. public void tearDown() {
  62. db.close();
  63. }
  64. @Test
  65. public void testValidateModels() {
  66. DbInspector inspector = new DbInspector(db);
  67. validateModel(inspector, new Product());
  68. validateModel(inspector, new ProductAnnotationOnly());
  69. validateModel(inspector, new ProductMixedAnnotation());
  70. }
  71. private void validateModel(DbInspector inspector, Object o) {
  72. List<ValidationRemark> remarks = inspector.validateModel(o, false);
  73. assertTrue("Validation remarks are null for " + o.getClass().getName(), remarks != null);
  74. log("Validation remarks for " + o.getClass().getName());
  75. for (ValidationRemark remark : remarks) {
  76. log(remark.toString());
  77. if (remark.isError()) {
  78. errorCollector.addError(new SQLException(remark.toString()));
  79. }
  80. }
  81. }
  82. @Test
  83. public void testSupportedTypes() {
  84. List<SupportedTypes> original = SupportedTypes.createList();
  85. db.insertAll(original);
  86. List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
  87. assertEquals(original.size(), retrieved.size());
  88. for (int i = 0; i < original.size(); i++) {
  89. SupportedTypes o = original.get(i);
  90. SupportedTypes r = retrieved.get(i);
  91. assertTrue(o.equivalentTo(r));
  92. }
  93. }
  94. @Test
  95. public void testModelGeneration() {
  96. List<SupportedTypes> original = SupportedTypes.createList();
  97. db.insertAll(original);
  98. DbInspector inspector = new DbInspector(db);
  99. List<String> models = inspector.generateModel(null, "SupportedTypes", "com.iciql.test.models", true,
  100. true);
  101. assertEquals(1, models.size());
  102. // a poor test, but a start
  103. assertEquals(1361, models.get(0).length());
  104. }
  105. @Test
  106. public void testDatabaseUpgrade() {
  107. // insert a database version record
  108. db.insert(new DbVersion(1));
  109. TestDbUpgrader dbUpgrader = new TestDbUpgrader();
  110. db.setDbUpgrader(dbUpgrader);
  111. List<SupportedTypes> original = SupportedTypes.createList();
  112. db.insertAll(original);
  113. assertEquals(1, dbUpgrader.oldVersion.get());
  114. assertEquals(2, dbUpgrader.newVersion.get());
  115. }
  116. @Test
  117. public void testTableUpgrade() {
  118. Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
  119. // insert first, this will create version record automatically
  120. List<SupportedTypes> original = SupportedTypes.createList();
  121. db.insertAll(original);
  122. // reset the dbUpgrader (clears the update check cache)
  123. TestDbUpgrader dbUpgrader = new TestDbUpgrader();
  124. db.setDbUpgrader(dbUpgrader);
  125. SupportedTypes2 s2 = new SupportedTypes2();
  126. List<SupportedTypes2> types = db.from(s2).select();
  127. assertEquals(10, types.size());
  128. assertEquals(1, dbUpgrader.oldVersion.get());
  129. assertEquals(2, dbUpgrader.newVersion.get());
  130. db.close();
  131. }
  132. /**
  133. * A sample database upgrader class.
  134. */
  135. @IQDatabase(version = 2)
  136. class TestDbUpgrader implements DbUpgrader {
  137. final AtomicInteger oldVersion = new AtomicInteger(0);
  138. final AtomicInteger newVersion = new AtomicInteger(0);
  139. public boolean upgradeTable(Db db, String schema, String table, int fromVersion, int toVersion) {
  140. // just claims success on upgrade request
  141. oldVersion.set(fromVersion);
  142. newVersion.set(toVersion);
  143. return true;
  144. }
  145. public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
  146. // just claims success on upgrade request
  147. oldVersion.set(fromVersion);
  148. newVersion.set(toVersion);
  149. return true;
  150. }
  151. }
  152. }