1   package org.riverock.dbrevision.manager.patch;
2   
3   import java.util.List;
4   import java.util.ArrayList;
5   
6   import junit.framework.TestCase;
7   
8   import org.riverock.dbrevision.annotation.schema.db.Patch;
9   import org.riverock.dbrevision.exception.TwoPatchesWithSameNameException;
10  import org.riverock.dbrevision.exception.TwoPatchesWithEmptyPreviousPatchException;
11  import org.riverock.dbrevision.exception.FirstPatchNotFoundException;
12  import org.riverock.dbrevision.exception.NoChildPatchFoundException;
13  
14  /**
15   * User: SMaslyukov
16   * Date: 02.08.2007
17   * Time: 14:44:38
18   */
19  public class TestPatchSorter extends TestCase {
20  
21      public void testWrongList_listWithOneElement() throws Exception {
22          List<Patch> list = new ArrayList<Patch>();
23          list.add( getPatch("aaa", null));
24          assertNotNull(PatchSorter.sort(list));
25      }
26  
27      public void testWrongList_firstPatchNotFound() throws Exception {
28          List<Patch> list = new ArrayList<Patch>();
29          list.add( getPatch("aaa", "zzz"));
30          list.add( getPatch("aaa", "bbb"));
31          boolean isCorrect = false;
32          try {
33              PatchSorter.sort(list);
34          }
35          catch (FirstPatchNotFoundException e) {
36              isCorrect = true;
37          }
38          assertTrue(isCorrect);
39      }
40  
41      public void testWrongList_twoFirstPatch() throws Exception {
42          List<Patch> list = new ArrayList<Patch>();
43          list.add( getPatch("aaa", null));
44          list.add( getPatch("aaa", "bbb"));
45          boolean isCorrect = false;
46          try {
47              PatchSorter.sort(list);
48          }
49          catch (TwoPatchesWithSameNameException e) {
50              isCorrect = true;
51          }
52          assertTrue(isCorrect);
53      }
54  
55      public void testWrongList_totalWrong() throws Exception {
56          List<Patch> list = new ArrayList<Patch>();
57          list.add( getPatch("aaa", null));
58          list.add( getPatch("aaa", null));
59          boolean isCorrect = false;
60          try {
61              PatchSorter.sort(list);
62          }
63          catch (TwoPatchesWithSameNameException e) {
64              isCorrect = true;
65          }
66          catch (TwoPatchesWithEmptyPreviousPatchException e) {
67              isCorrect = true;
68          }
69          assertTrue(isCorrect);
70      }
71  
72      public void testWrongList_twoPrevNameEmpty() throws Exception {
73          List<Patch> list = new ArrayList<Patch>();
74          list.add( getPatch("aaa", null));
75          list.add( getPatch("bbb", null));
76          boolean isCorrect = false;
77          try {
78              PatchSorter.sort(list);
79          }
80          catch (TwoPatchesWithEmptyPreviousPatchException e) {
81              isCorrect = true;
82          }
83          assertTrue(isCorrect);
84      }
85  
86      public void testSorter() throws Exception {
87          List<Patch> list = new ArrayList<Patch>();
88          list.add( getPatch("bbb", "zzz"));
89          list.add( getPatch("zzz", "aaa"));
90          list.add( getPatch("aaa", null));
91          list.add( getPatch("ccc", "ddd"));
92          list.add( getPatch("ddd", "bbb"));
93          list = PatchSorter.sort(list);
94          assertNotNull(list);
95          assertEquals(5, list.size());
96          assertEquals("aaa", list.get(0).getName() );
97          assertEquals("zzz", list.get(1).getName() );
98          assertEquals("bbb", list.get(2).getName() );
99          assertEquals("ddd", list.get(3).getName() );
100         assertEquals("ccc", list.get(4).getName() );
101     }
102 
103     public void testNoChildFoundSorter() throws Exception {
104         List<Patch> list = new ArrayList<Patch>();
105         list.add( getPatch("webmill_init_def_v2", null));
106         list.add( getPatch("test_0_0", "webmill_init_def_v2"));
107         list.add( getPatch("test_1_0", "test_0_0"));
108         list.add( getPatch("test_1_1", "test_1_0"));
109         list.add( getPatch("test_2_1", "test_1_1"));
110         list.add( getPatch("test_0_2", "test_0_1"));
111         boolean isCorrect = false;
112         try {
113             PatchSorter.sort(list);
114         }
115         catch (NoChildPatchFoundException e) {
116             isCorrect = true;
117         }
118         assertTrue(isCorrect);
119     }
120 
121     public void testTwoChildrenSorter() throws Exception {
122         List<Patch> list = new ArrayList<Patch>();
123         list.add( getPatch("webmill_init_def_v2", null));
124         list.add( getPatch("test_0_0", "webmill_init_def_v2"));
125         list.add( getPatch("test_1_0", "test_0_0"));
126         list.add( getPatch("test_1_1", "test_1_0"));
127         list.add( getPatch("test_2_1", "test_1_1"));
128         list.add( getPatch("test_1_2", "test_2_1"));
129         list.add( getPatch("test_0_2", "test_1_2"));
130         list = PatchSorter.sort(list);
131         assertNotNull(list);
132         assertEquals(7, list.size());
133         assertEquals("webmill_init_def_v2", list.get(0).getName() );
134         assertEquals("test_0_0", list.get(1).getName() );
135         assertEquals("test_1_0", list.get(2).getName() );
136         assertEquals("test_1_1", list.get(3).getName() );
137         assertEquals("test_2_1", list.get(4).getName() );
138         assertEquals("test_1_2", list.get(5).getName() );
139         assertEquals("test_0_2", list.get(6).getName() );
140     }
141 
142 
143     private Patch getPatch(String name, String prevName) {
144         Patch p = new Patch();
145         p.setName(name);
146         p.setPreviousName(prevName);
147         
148         return p;
149     }
150 }