1   package org.riverock.dbrevision.manager;
2   
3   import junit.framework.TestCase;
4   import org.riverock.dbrevision.db.impl.LocalDatabase;
5   import org.riverock.dbrevision.exception.*;
6   import org.riverock.dbrevision.manager.dao.InitManagerDaoFactory;
7   
8   import java.io.File;
9   import java.util.List;
10  
11  /**
12   * User: SergeMaslyukov
13   * Date: 28.07.2007
14   * Time: 20:59:49
15   */
16  public class TestDbRevisionManager extends TestCase {
17      private static final String NOT_EXISTED_PATH = "not-existed-path";
18  
19      public static final String DBREVISION_CONFIGS = "src"+ File.separatorChar+"test" + File.separatorChar+ "configs";
20  
21      public void setUp() {
22          InitManagerDaoFactory.initCompleteV570();
23      }
24  
25      public void testContructor() throws Exception {
26          boolean isCorrect = false;
27          try {
28              new DbRevisionManager(null, null);
29          }
30          catch (IllegalArgumentException e) {
31              isCorrect = true;
32          }
33          assertTrue(isCorrect);
34          isCorrect=false;
35          LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
36          try {
37              new DbRevisionManager(localDatabaseAdapter, null);
38          }
39          catch (IllegalArgumentException e) {
40              isCorrect = true;
41          }
42          assertTrue(isCorrect);
43          isCorrect=false;
44          try {
45              new DbRevisionManager(null, NOT_EXISTED_PATH);
46          }
47          catch (IllegalArgumentException e) {
48              isCorrect = true;
49          }
50          assertTrue(isCorrect);
51          isCorrect=false;
52          try {
53              new DbRevisionManager(localDatabaseAdapter, NOT_EXISTED_PATH);
54          }
55          catch (DbRevisionPathNotFoundException e) {
56              isCorrect = true;
57          }
58          assertTrue(isCorrect);
59      }
60  
61      /**
62       * test missing config.xml file
63       * 
64       * @throws Exception on error
65       */
66      public void testConfig_0() throws Exception {
67          String path = DBREVISION_CONFIGS+File.separatorChar+"config-0";
68          LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
69          boolean isCorrect=false;
70          try {
71             new DbRevisionManager(localDatabaseAdapter, path);
72          }
73          catch (ConfigFileNotFoundException e) {
74              isCorrect=true;
75          }
76          assertTrue(isCorrect);
77      }
78  
79      /**
80       * test missing directory with module
81       *
82       * @throws Exception on error
83       */
84      public void testConfig_1() throws Exception {
85          String path = DBREVISION_CONFIGS+File.separatorChar+"config-1";
86          LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
87          boolean isCorrect=false;
88          try {
89             new DbRevisionManager(localDatabaseAdapter, path);
90          }
91          catch (ModulePathNotFoundException e) {
92              isCorrect=true;
93          }
94          assertTrue(isCorrect);
95      }
96  
97      /**
98       * test missing directories with version
99       *
100      * @throws Exception on error
101      */
102     public void testConfig_2() throws Exception {
103         String path = DBREVISION_CONFIGS+File.separatorChar+"config-2";
104         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
105         boolean isCorrect=false;
106         try {
107            new DbRevisionManager(localDatabaseAdapter, path);
108         }
109         catch (VersionPathNotFoundException e) {
110             isCorrect=true;
111         }
112         assertTrue(isCorrect);
113     }
114 
115     /**
116      * test in version directory missing webmill/5.7.0/init-structure.xml file
117      * 
118      * @throws Exception on error
119      */
120     public void testConfig_3() throws Exception {
121         String path = DBREVISION_CONFIGS+File.separatorChar+"config-3";
122         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
123         boolean isCorrect=false;
124         try {
125            new DbRevisionManager(localDatabaseAdapter, path);
126         }
127         catch (InitStructureFileNotFoundException e) {
128             isCorrect=true;
129         }
130         assertTrue(isCorrect);
131     }
132 
133     /**
134      * test directory with first version must not contain patches ('patch' directory)
135      * 
136      * @throws Exception on error
137      */
138     public void testConfig_4() throws Exception {
139         String path = DBREVISION_CONFIGS+File.separatorChar+"config-4";
140         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
141         boolean isCorrect=false;
142         try {
143            new DbRevisionManager(localDatabaseAdapter, path);
144         }
145         catch (FirstVersionWithPatchdException e) {
146             isCorrect=true;
147         }
148         assertTrue(isCorrect);
149     }
150 
151     /**
152      * test not correted data in DB
153      *
154      * @throws Exception on error
155      */
156     public void testConfig_5_moduleNotConfigured() throws Exception {
157 
158         InitManagerDaoFactory.initNotExistedModule();
159 
160         String path = DBREVISION_CONFIGS+File.separatorChar+"config-5";
161         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
162         boolean isCorrect=false;
163         try {
164             new DbRevisionManager(localDatabaseAdapter, path);
165         }
166         catch (ModuleNotConfiguredException e) {
167             isCorrect=true;
168         }
169         assertTrue(isCorrect);
170     }
171 
172     /**
173      * test not correted data in DB
174      *
175      * @throws Exception on error
176      */
177     public void testConfig_5_moduleVersionNotConfigured() throws Exception {
178         
179         InitManagerDaoFactory.initNotExistedVersionModule();
180 
181         String path = DBREVISION_CONFIGS+File.separatorChar+"config-5";
182         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
183         boolean isCorrect=false;
184         try {
185             new DbRevisionManager(localDatabaseAdapter, path);
186         }
187         catch (CurrentVersionCodeNotFoundException e) {
188             isCorrect=true;
189         }
190         assertTrue(isCorrect);
191     }
192 
193     /**
194      * test correted data in DB
195      *
196      * @throws Exception on error
197      */
198     public void testConfigValid_currentVersionInMiddleOfList() throws Exception {
199 
200         InitManagerDaoFactory.initCurrentVersionInMiddleOfList();
201 
202         String path = DBREVISION_CONFIGS+File.separatorChar+"config-valid";
203         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
204         DbRevisionManager manager = new DbRevisionManager(localDatabaseAdapter, path);
205 
206         List<Module> modules = manager.getModules();
207         assertNotNull(modules);
208         assertEquals(1, modules.size());
209 
210         Module module = modules.get(0);
211         assertNotNull(module);
212 
213         List<Version> versions = module.getVersions();
214         assertNotNull(versions);
215         assertEquals(4, versions.size());
216 
217         Version currentVersion = module.getCurrentVersion();
218         assertNotNull(currentVersion);
219         assertEquals("5.7.2", currentVersion.getVersionName());
220         assertEquals("5.8.0", module.getLastVersion().getVersionName());
221         assertEquals("5.7.0", module.getFirstVersion().getVersionName());
222 
223         assertNotNull(currentVersion.getPreviousVersion());
224         assertNotNull(currentVersion.getNextVersion());
225         assertEquals("5.7.1", currentVersion.getPreviousVersion().getVersionName());
226         assertEquals("5.8.0", currentVersion.getNextVersion().getVersionName());
227 
228         Version v = currentVersion;
229         while (v!=null) {
230             assertTrue(v.isComplete());
231             v = v.getPreviousVersion();
232         }
233 
234         v = currentVersion.getNextVersion();
235         while (v!=null) {
236             assertFalse(v.isComplete());
237             v = v.getNextVersion();
238         }
239     }
240 
241     /**
242      * test correted data in DB
243      *
244      * @throws Exception on error
245      */
246     public void testConfigValid_lastVersion() throws Exception {
247 
248         InitManagerDaoFactory.initCurrentVersionIsLast();
249 
250         String path = DBREVISION_CONFIGS+File.separatorChar+"config-valid";
251         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
252         DbRevisionManager manager = new DbRevisionManager(localDatabaseAdapter, path);
253 
254         List<Module> modules = manager.getModules();
255         assertNotNull(modules);
256         assertEquals(1, modules.size());
257 
258         Module module = modules.get(0);
259         assertNotNull(module);
260         assertTrue(module.isComplete());
261 
262         List<Version> versions = module.getVersions();
263         assertNotNull(versions);
264         assertEquals(4, versions.size());
265 
266         Version currentVersion = module.getCurrentVersion();
267         assertNotNull(currentVersion);
268         assertEquals("5.8.0", currentVersion.getVersionName());
269         assertEquals("5.7.2", currentVersion.getPreviousVersion().getVersionName());
270         assertEquals("5.8.0", module.getLastVersion().getVersionName());
271         assertEquals("5.7.0", module.getFirstVersion().getVersionName());
272 
273         for (Version version : versions) {
274             assertTrue(version.isComplete());
275         }
276     }
277 
278     /**
279      * test correted data in DB
280      *
281      * @throws Exception on error
282      */
283     public void testConfigValid_1() throws Exception {
284 
285         String path = DBREVISION_CONFIGS+File.separatorChar+"config-valid-1";
286         LocalDatabase localDatabaseAdapter = new LocalDatabase(null);
287         DbRevisionManager manager = new DbRevisionManager(localDatabaseAdapter, path);
288 
289         List<Module> modules = manager.getModules();
290         assertNotNull(modules);
291         assertEquals(1, modules.size());
292 
293         Module module = modules.get(0);
294         assertNotNull(module);
295 
296         List<Version> versions = module.getVersions();
297         assertNotNull(versions);
298         assertEquals(2, versions.size());
299     }
300 }