KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > index > FullIndexRecoveryComponentTest


1 /*
2  * Copyright (C) 2006 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.node.index;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.search.Indexer;
26 import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
27 import org.alfresco.repo.transaction.TransactionUtil;
28 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
29 import org.alfresco.service.cmr.repository.ChildAssociationRef;
30 import org.alfresco.service.cmr.repository.InvalidStoreRefException;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.service.cmr.repository.StoreRef;
34 import org.alfresco.service.namespace.NamespaceService;
35 import org.alfresco.service.namespace.QName;
36 import org.alfresco.service.transaction.TransactionService;
37 import org.alfresco.util.ApplicationContextHelper;
38 import org.springframework.context.ApplicationContext;
39
40 /**
41  * Checks that full index recovery is possible
42  *
43  * @author Derek Hulley
44  */

45 public class FullIndexRecoveryComponentTest extends TestCase
46 {
47     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
48     
49     private TransactionService transactionService;
50     private FullIndexRecoveryComponent indexRecoverer;
51     private NodeService nodeService;
52     private TransactionService txnService;
53     private Indexer indexer;
54     
55     private List JavaDoc<StoreRef> storeRefs;
56     
57     public void setUp() throws Exception JavaDoc
58     {
59         transactionService = (TransactionService) ctx.getBean("transactionComponent");
60         indexRecoverer = (FullIndexRecoveryComponent) ctx.getBean("indexRecoveryComponent");
61         txnService = (TransactionService) ctx.getBean("transactionComponent");
62         nodeService = (NodeService) ctx.getBean("nodeService");
63         indexer = (Indexer) ctx.getBean("indexerComponent");
64         
65         // create 2 stores
66
TransactionWork<List JavaDoc<StoreRef>> createStoresWork = new TransactionWork<List JavaDoc<StoreRef>>()
67         {
68             public List JavaDoc<StoreRef> doWork() throws Exception JavaDoc
69             {
70                 List JavaDoc<StoreRef> storeRefs = new ArrayList JavaDoc<StoreRef>(2);
71                 storeRefs.add(nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + System.nanoTime()));
72                 storeRefs.add(nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + System.nanoTime()));
73                 return storeRefs;
74             }
75         };
76         storeRefs = TransactionUtil.executeInUserTransaction(transactionService, createStoresWork);
77     }
78     
79     public void testNothing() throws Exception JavaDoc
80     {
81         
82     }
83     
84     public void xtestReindexing() throws Exception JavaDoc
85     {
86         // don't do anything if the component has already started
87
if (FullIndexRecoveryComponent.isStarted())
88         {
89             return;
90         }
91         // deletes a content node from the index
92
final List JavaDoc<String JavaDoc> storeRefStrings = new ArrayList JavaDoc<String JavaDoc>(2);
93         TransactionWork<String JavaDoc> dropNodeIndexWork = new TransactionWork<String JavaDoc>()
94         {
95             public String JavaDoc doWork()
96             {
97                 // create a node in each store and drop it from the index
98
for (StoreRef storeRef : storeRefs)
99                 {
100                     try
101                     {
102                         NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
103                         ChildAssociationRef assocRef = nodeService.createNode(
104                                 rootNodeRef,
105                                 ContentModel.ASSOC_CONTAINS,
106                                 QName.createQName(NamespaceService.ALFRESCO_URI, "unindexedChild" + System.currentTimeMillis()),
107                                 ContentModel.TYPE_BASE);
108                         // this will have indexed it, so remove it from the index
109
indexer.deleteNode(assocRef);
110                         // make the string version of the storeRef
111
storeRefStrings.add(storeRef.toString());
112                     }
113                     catch (InvalidStoreRefException e)
114                     {
115                         // just ignore stores that are invalid
116
}
117                 }
118                 return AlfrescoTransactionSupport.getTransactionId();
119             }
120         };
121         
122         // create un-indexed nodes
123
String JavaDoc txnId = TransactionUtil.executeInNonPropagatingUserTransaction(txnService, dropNodeIndexWork);
124         
125         indexRecoverer.setExecuteFullRecovery(true);
126         indexRecoverer.setStores(storeRefStrings);
127         // reindex
128
indexRecoverer.reindex();
129
130         // check that reindexing fails
131
try
132         {
133             indexRecoverer.reindex();
134             fail("Reindexer failed to prevent reindex from being called twice");
135         }
136         catch (RuntimeException JavaDoc e)
137         {
138             // expected
139
}
140         
141         // loop for some time, giving it a chance to do its thing
142
String JavaDoc lastProcessedTxnId = null;
143         for (int i = 0; i < 60; i++)
144         {
145             lastProcessedTxnId = FullIndexRecoveryComponent.getCurrentTransactionId();
146             if (lastProcessedTxnId.equals(txnId))
147             {
148                 break;
149             }
150             // wait for a second
151
synchronized(this)
152             {
153                 this.wait(1000L);
154             }
155         }
156         // check that the index was recovered
157
assertEquals("Index transaction not up to date", txnId, lastProcessedTxnId);
158     }
159 }
160
Popular Tags