KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > PatchTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.admin.patch;
18
19 import java.util.Date JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.alfresco.repo.domain.AppliedPatch;
26 import org.alfresco.service.cmr.admin.PatchException;
27 import org.alfresco.service.transaction.TransactionService;
28 import org.alfresco.util.ApplicationContextHelper;
29 import org.springframework.context.ApplicationContext;
30
31 /**
32  * @see org.alfresco.repo.admin.patch.Patch
33  * @see org.alfresco.repo.admin.patch.AbstractPatch
34  * @see org.alfresco.repo.admin.patch.PatchService
35  *
36  * @author Derek Hulley
37  */

38 public class PatchTest extends TestCase
39 {
40     private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
41     
42     private TransactionService transactionService;
43     private PatchService patchService;
44     private PatchDaoService patchDaoComponent;
45     
46     public PatchTest(String JavaDoc name)
47     {
48         super(name);
49     }
50     
51     public void setUp() throws Exception JavaDoc
52     {
53         transactionService = (TransactionService) ctx.getBean("transactionComponent");
54         patchService = (PatchService) ctx.getBean("PatchService");
55         patchDaoComponent = (PatchDaoService) ctx.getBean("patchDaoComponent");
56         
57         // get the patches to play with
58
patchService.registerPatch((Patch)ctx.getBean("patch.sample.02"));
59         patchService.registerPatch((Patch)ctx.getBean("patch.sample.01"));
60     }
61     
62     public void testSetup() throws Exception JavaDoc
63     {
64         assertNotNull(transactionService);
65         assertNotNull(patchService);
66         assertNotNull(patchDaoComponent);
67     }
68     
69     public void testSimplePatchSuccess() throws Exception JavaDoc
70     {
71         Patch patch = new SamplePatch(false, transactionService);
72         String JavaDoc report = patch.apply();
73         // check that the report was generated
74
assertEquals("Patch report incorrect", SamplePatch.MSG_SUCCESS, report);
75     }
76     
77     public void testPatchReapplication()
78     {
79         // successfully apply a patch
80
Patch patch = new SamplePatch(false, transactionService);
81         patch.apply();
82         // check that the patch cannot be reapplied
83
try
84         {
85             patch.apply();
86             fail("AbstractPatch failed to prevent reapplication");
87         }
88         catch (AlfrescoRuntimeException e)
89         {
90             // expected
91
}
92         
93         // apply an unsuccessful patch
94
patch = new SamplePatch(true, transactionService);
95         try
96         {
97             patch.apply();
98             fail("Failed patch didn't throw PatchException");
99         }
100         catch (PatchException e)
101         {
102             // expected
103
}
104         // repeat
105
try
106         {
107             patch.apply();
108             fail("Reapplication of failed patch didn't throw PatchException");
109         }
110         catch (PatchException e)
111         {
112             // expected
113
}
114     }
115     
116     public void testApplyOutstandingPatches() throws Exception JavaDoc
117     {
118         // apply outstanding patches
119
boolean success = patchService.applyOutstandingPatches();
120         assertTrue(success);
121         // get applied patches
122
List JavaDoc<AppliedPatch> appliedPatches = patchDaoComponent.getAppliedPatches();
123         // check that the patch application was recorded
124
boolean found01 = false;
125         boolean found02 = false;
126         for (AppliedPatch appliedPatch : appliedPatches)
127         {
128             if (appliedPatch.getId().equals("Sample01"))
129             {
130                 found01 = true;
131                 assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
132             }
133             else if (appliedPatch.getId().equals("Sample02"))
134             {
135                 found02 = true;
136                 assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
137             }
138         }
139         assertTrue("Sample 01 not in list of applied patches", found01);
140         assertTrue("Sample 02 not in list of applied patches", found02);
141     }
142     
143     public void testGetPatchesByDate() throws Exception JavaDoc
144     {
145         // ensure that there are some applied patches
146
testApplyOutstandingPatches();
147         // get the number of applied patches
148
List JavaDoc<AppliedPatch> appliedPatches = patchDaoComponent.getAppliedPatches();
149         assertTrue("Expected at least 2 applied patches", appliedPatches.size() >= 2);
150         
151         // now requery using null dates
152
List JavaDoc<PatchInfo> appliedPatchesAllDates = patchService.getPatches(null, null);
153         assertEquals("Applied patches by all dates doesn't match all applied patches",
154                 appliedPatches.size(), appliedPatchesAllDates.size());
155         
156         // make sure that the objects are not connected to the persistence layer
157
PatchInfo disconnectedObject = appliedPatchesAllDates.get(0);
158         AppliedPatch persistedObject = patchDaoComponent.getAppliedPatch(disconnectedObject.getId());
159         assertNotSame("Instances should not be shared between evicted and cached objects",
160                 disconnectedObject, persistedObject);
161         
162         // perform another query with dates that should return no results
163
List JavaDoc<PatchInfo> appliedPatchesFutureDates = patchService.getPatches(new Date JavaDoc(), new Date JavaDoc());
164         assertEquals("Query returned results for dates when no patches should exist", 0, appliedPatchesFutureDates.size());
165     }
166 }
167
Popular Tags