KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > test > AbstractNavigationTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.test;
17
18 import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
19 import org.outerj.daisy.repository.testsupport.DOMBuilder;
20 import org.outerj.daisy.repository.*;
21 import org.outerj.daisy.repository.acl.*;
22 import org.outerj.daisy.repository.user.UserManager;
23 import org.outerj.daisy.repository.user.Role;
24 import org.outerj.daisy.repository.user.User;
25 import org.outerj.daisy.repository.schema.RepositorySchema;
26 import org.outerj.daisy.repository.schema.PartType;
27 import org.outerj.daisy.repository.schema.DocumentType;
28 import org.outerj.daisy.repository.schema.FieldType;
29 import org.outerj.daisy.navigation.NavigationManager;
30 import org.outerj.daisy.navigation.NavigationLookupResult;
31 import org.outerj.daisy.navigation.NavigationParams;
32 import org.outerj.daisy.navigation.LookupAlternative;
33 import org.jaxen.dom.DOMXPath;
34
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.Transformer JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38 import javax.xml.transform.dom.DOMSource JavaDoc;
39
40 /**
41  * Tests the NavigationManager. Of this test there is no
42  * distinction between Remote and Local, since there is no
43  * remote API implementation of this component (the remote
44  * usage is directly using the HTTP interface).
45  */

46 public abstract class AbstractNavigationTest extends AbstractDaisyTestCase {
47     protected boolean resetDataStores() {
48         return true;
49     }
50
51     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
52
53     public void testNavigation() throws Exception JavaDoc {
54         RepositoryManager repositoryManager = getRepositoryManager();
55         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
56         repository.switchRole(Role.ADMINISTRATOR);
57
58         RepositorySchema schema = repository.getRepositorySchema();
59
60         // Create types required by navigation stuff
61
PartType navigationDescriptionType = schema.createPartType("NavigationDescription", "text/xml");
62         navigationDescriptionType.save();
63
64         DocumentType navigationType = schema.createDocumentType("Navigation");
65         navigationType.addPartType(navigationDescriptionType, true);
66         navigationType.save();
67
68         // Create types for other documents
69
FieldType stringField = schema.createFieldType("StringField", ValueType.STRING);
70         stringField.save();
71
72         DocumentType documentType = schema.createDocumentType("doctype");
73         documentType.addFieldType(stringField, false);
74         documentType.save();
75
76         // Create a user
77
UserManager userManager = repository.getUserManager();
78         Role userRole = userManager.getRole("user", false);
79
80         User user1 = userManager.createUser("user1");
81         user1.setPassword("user1");
82         user1.addToRole(userRole);
83         user1.setDefaultRole(userRole);
84         user1.save();
85         Repository user1Repository = repositoryManager.getRepository(new Credentials("user1", "user1"));
86         NavigationManager user1NavigationManager = (NavigationManager)user1Repository.getExtension("NavigationManager");
87
88         // Create an ACL
89
AccessManager accessManager = repository.getAccessManager();
90         Acl acl = accessManager.getStagingAcl();
91         AclObject aclObject = acl.createNewObject("true");
92         AclEntry aclEntry = aclObject.createNewEntry(AclSubjectType.EVERYONE, -1);
93         aclEntry.set(AclPermission.READ_LIVE, AclActionType.GRANT);
94         aclEntry.set(AclPermission.READ, AclActionType.GRANT);
95         aclEntry.set(AclPermission.WRITE, AclActionType.GRANT);
96         aclEntry.set(AclPermission.PUBLISH, AclActionType.GRANT);
97         aclEntry.set(AclPermission.DELETE, AclActionType.GRANT);
98         aclObject.add(aclEntry);
99         acl.add(aclObject);
100         acl.save();
101         accessManager.copyStagingToLive();
102
103         // Create a collection
104
CollectionManager collectionManager = repository.getCollectionManager();
105         DocumentCollection collection1 = collectionManager.createCollection("collection1");
106         collection1.save();
107
108         // Create some documents
109
Document document1 = repository.createDocument("Document1", documentType.getId());
110         document1.setField(stringField.getId(), "X");
111         document1.setPrivate(true);
112         document1.addToCollection(collection1);
113         document1.save();
114
115         Document document2 = user1Repository.createDocument("Document2", documentType.getId());
116         document2.setField(stringField.getId(), "X");
117         document2.addToCollection(collection1);
118         document2.save();
119
120         Document document3 = user1Repository.createDocument("Document3", documentType.getId());
121         document3.setField(stringField.getId(), "Y");
122         document3.save();
123
124         // Create navigation documents
125
Document navdoc2 = repository.createDocument("navdoc2", navigationType.getId());
126         StringBuffer JavaDoc content2 = new StringBuffer JavaDoc();
127         content2.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
128         content2.append(" <d:doc id='" + document3.getId() + "'/>");
129         content2.append(" <d:query q=\"select $StringField where documentType != 'Navigation' order by $StringField\"/>");
130         content2.append("</d:navigationTree>");
131         navdoc2.setPart("NavigationDescription", "text/xml", content2.toString().getBytes("UTF-8"));
132         navdoc2.save();
133
134         Document navdoc1 = repository.createDocument("navdoc1", navigationType.getId());
135         StringBuffer JavaDoc content1 = new StringBuffer JavaDoc();
136         content1.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
137         content1.append(" <d:collections>");
138         content1.append(" <d:collection name='collection1'/>");
139         content1.append(" </d:collections>");
140         content1.append(" <d:doc id='" + document3.getId() + "'>");
141         content1.append(" <d:doc id='" + document3.getId() + "'/>");
142         content1.append(" <d:doc id='" + document1.getId() + "'/>");
143         content1.append(" </d:doc>");
144         content1.append(" <d:group label='My Group' id='mygroup'>");
145         content1.append(" <d:group label='My Second Group'>");
146         content1.append(" <d:doc id='").append(document3.getId()).append("'/>");
147         content1.append(" <d:group label='empty group' id='emptygroup'/>");
148         content1.append(" </d:group>");
149         content1.append(" <d:group label='My Third Group'>");
150         content1.append(" <d:import docId='" + navdoc2.getId() + "'/>");
151         content1.append(" </d:group>");
152         content1.append(" <d:query q='select name where true'/>");
153         content1.append(" </d:group>");
154         content1.append(" <d:link url='http://www.google.com' label='Google'>");
155         content1.append(" <d:link url='http://www.apache.org' label='Apache'/>");
156         content1.append(" </d:link>");
157         content1.append("</d:navigationTree>");
158         navdoc1.setPart("NavigationDescription", "text/xml", content1.toString().getBytes("UTF-8"));
159         navdoc1.save();
160
161         DOMBuilder domBuilder = new DOMBuilder();
162         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc1.getVariantKey(), null, false), null, false);
163         org.w3c.dom.Document JavaDoc navResult = domBuilder.getDocument();
164         DOMXPath xpath;
165         xpath = createXPath("/n:navigationTree/n:doc[1]/@id");
166         assertEquals(String.valueOf(document3.getId()), xpath.stringValueOf(navResult));
167         xpath = createXPath("/n:navigationTree/n:doc[1]/n:doc[1]/@id");
168         assertEquals(String.valueOf(document3.getId()), xpath.stringValueOf(navResult));
169         xpath = createXPath("/n:navigationTree/n:doc[1]/n:doc[2]/@id");
170         assertEquals("", xpath.stringValueOf(navResult));
171         xpath = createXPath("/n:navigationTree/n:group[1]/@label");
172         assertEquals("My Group", xpath.stringValueOf(navResult));
173         xpath = createXPath("count(/n:navigationTree/n:group[1]/n:group[2]/n:doc)");
174         assertEquals(3, xpath.numberValueOf(navResult).intValue());
175         xpath = createXPath("/n:navigationTree/n:group[1]/n:group[2]/n:doc[2]/@label");
176         assertEquals("X", xpath.stringValueOf(navResult));
177         xpath = createXPath("count(/n:navigationTree/n:group[1]/n:doc)");
178         assertEquals(1, xpath.numberValueOf(navResult).intValue());
179
180         //
181
// Check behaviour with recursive imports
182
//
183
Document navdoc3 = repository.createDocument("navdoc3", navigationType.getId());
184         navdoc3.setPart("NavigationDescription", "text/xml", new byte[0]);
185         navdoc3.save();
186
187         Document navdoc4 = repository.createDocument("navdoc4", navigationType.getId());
188         navdoc4.setPart("NavigationDescription", "text/xml", new byte[0]);
189         navdoc4.save();
190
191         Document navdoc5 = repository.createDocument("navdoc5", navigationType.getId());
192         navdoc5.setPart("NavigationDescription", "text/xml", new byte[0]);
193         navdoc5.save();
194
195         // first test 3 -> 4 -> 4
196
StringBuffer JavaDoc content3 = new StringBuffer JavaDoc();
197         content3.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
198         content3.append(" <d:import docId='" + navdoc4.getId() + "'/>");
199         content3.append("</d:navigationTree>");
200         navdoc3.setPart("NavigationDescription", "text/xml", content3.toString().getBytes("UTF-8"));
201         navdoc3.save();
202
203         StringBuffer JavaDoc content4 = new StringBuffer JavaDoc();
204         content4.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
205         content4.append(" <d:import docId='" + navdoc4.getId() + "'/>");
206         content4.append("</d:navigationTree>");
207         navdoc4.setPart("NavigationDescription", "text/xml", content4.toString().getBytes("UTF-8"));
208         navdoc4.save();
209
210         System.out.println("!!WARNING!! If the test will now hang it is because the detecting of recursive imports does not work.");
211         domBuilder = new DOMBuilder();
212         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc3.getVariantKey(), null, false), null, false);
213         navResult = domBuilder.getDocument();
214         xpath = createXPath("count(//n:error)");
215         assertEquals(1, xpath.numberValueOf(navResult).intValue());
216
217         // test 3 -> 4 -> 5 -> 3
218
content4 = new StringBuffer JavaDoc();
219         content4.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
220         content4.append(" <d:import docId='" + navdoc5.getId() + "'/>");
221         content4.append("</d:navigationTree>");
222         navdoc4.setPart("NavigationDescription", "text/xml", content4.toString().getBytes("UTF-8"));
223         navdoc4.save();
224
225         StringBuffer JavaDoc content5 = new StringBuffer JavaDoc();
226         content5.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
227         content5.append(" <d:import docId='" + navdoc3.getId() + "'/>");
228         content5.append("</d:navigationTree>");
229         navdoc5.setPart("NavigationDescription", "text/xml", content5.toString().getBytes("UTF-8"));
230         navdoc5.save();
231
232         System.out.println("!!WARNING!! If the test will now hang it is because the detecting of recursive imports does not work.");
233         domBuilder = new DOMBuilder();
234         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc3.getVariantKey(), null, false), null, false);
235         navResult = domBuilder.getDocument();
236         xpath = createXPath("count(//n:error)");
237         assertEquals(1, xpath.numberValueOf(navResult).intValue());
238
239         // test 6 -> 6
240
Document navdoc6 = repository.createDocument("navdoc6", navigationType.getId());
241         navdoc6.setPart("NavigationDescription", "text/xml", new byte[0]);
242         navdoc6.save();
243
244         StringBuffer JavaDoc content6 = new StringBuffer JavaDoc();
245         content6.append("<d:navigationTree xmlns:d='http://outerx.org/daisy/1.0#navigationspec'>");
246         content6.append(" <d:import docId='" + navdoc6.getId() + "'/>");
247         content6.append("</d:navigationTree>");
248         navdoc6.setPart("NavigationDescription", "text/xml", content6.toString().getBytes("UTF-8"));
249         navdoc6.save();
250
251         System.out.println("!!WARNING!! If the test will now hang it is because the detecting of recursive imports does not work.");
252         domBuilder = new DOMBuilder();
253         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc6.getVariantKey(), null, false), null, false);
254         navResult = domBuilder.getDocument();
255         xpath = createXPath("count(//n:error)");
256         assertEquals(1, xpath.numberValueOf(navResult).intValue());
257
258         //
259
// Test cache invalidation on updates
260
//
261

262         // update to a document
263
document3.setField(stringField.getId(), "Z");
264         document3.save();
265
266         domBuilder = new DOMBuilder();
267         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc1.getVariantKey(), null, false), null, false);
268         navResult = domBuilder.getDocument();
269
270         xpath = createXPath("/n:navigationTree/n:group[1]/n:group[2]/n:doc[3]/@label");
271         assertEquals("Z", xpath.stringValueOf(navResult));
272
273         // update to a navigation tree itself
274

275         // with current implementation it doesn't make a difference so skipped this for now
276

277         //
278
// Test navigationManager.lookup method
279
//
280

281         LookupAlternative[] lookupAlternatives = new LookupAlternative[] { new LookupAlternative("x", collection1.getId(), navdoc1.getVariantKey()) };
282
283         // a request for a group should give a redirect to its first document child
284
NavigationLookupResult lookupResult = user1NavigationManager.lookup("/mygroup", -1, -1, lookupAlternatives);
285         assertTrue(lookupResult.isRedirect());
286         assertEquals("/mygroup/g1/3", lookupResult.getNavigationPath());
287
288         // a request for a non-existing path should give a not found response
289
lookupResult = user1NavigationManager.lookup("/abc", -1, -1, lookupAlternatives);
290         assertTrue(lookupResult.isNotFound());
291
292         // a request for a document ID (does not matter whether the document really exists) that does not occur in the tree
293
lookupResult = user1NavigationManager.lookup("/2323", -1, -1, lookupAlternatives);
294         assertEquals(2323, lookupResult.getVariantKey().getDocumentId());
295         assertEquals("", lookupResult.getNavigationPath());
296
297         // a request for a document ID that does not occur at the given path but does occur
298
// at another location in the tree
299
lookupResult = user1NavigationManager.lookup("/1", -1, -1, lookupAlternatives);
300         assertTrue(lookupResult.isRedirect());
301         assertEquals("/3/1", lookupResult.getNavigationPath());
302
303         // a request for a group node which does not have any document node child
304
lookupResult = user1NavigationManager.lookup("/mygroup/g1/emptygroup", -1, -1, lookupAlternatives);
305         assertTrue(lookupResult.isNotFound());
306
307         // a request for a link node
308
lookupResult = user1NavigationManager.lookup("/l1", -1, -1, lookupAlternatives);
309         assertTrue(lookupResult.isNotFound());
310
311         // a request for a document node that exists at its given path (the most 'normal' situation)
312
lookupResult = user1NavigationManager.lookup("/3/3", -1, -1, lookupAlternatives);
313         assertEquals(3, lookupResult.getVariantKey().getDocumentId());
314         assertEquals("/3/3", lookupResult.getNavigationPath());
315
316         //
317
// Test contextualized trees
318
//
319

320         // Only activeDoc, not activePath specified
321
domBuilder = new DOMBuilder();
322         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc1.getVariantKey(), null, false), document3.getVariantKey(), false);
323         navResult = domBuilder.getDocument();
324
325         xpath = createXPath("/n:navigationTree/@selectedPath");
326         assertEquals("/" + document3.getId(), xpath.stringValueOf(navResult));
327
328         xpath = createXPath("/n:navigationTree/n:doc[1]/@selected");
329         assertEquals("true", xpath.stringValueOf(navResult));
330
331         xpath = createXPath("count(//*[@selected='true'])");
332         assertEquals(1, xpath.numberValueOf(navResult).intValue());
333
334         // activePath specified
335
String JavaDoc activePath = "/" + document3.getId() + "/" + document3.getId();
336         domBuilder = new DOMBuilder();
337         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc1.getVariantKey(), activePath, false), document3.getVariantKey(), false);
338         navResult = domBuilder.getDocument();
339
340         xpath = createXPath("/n:navigationTree/@selectedPath");
341         assertEquals(activePath, xpath.stringValueOf(navResult));
342
343         xpath = createXPath("/n:navigationTree/n:doc[1]/n:doc[1]/@selected");
344         assertEquals("true", xpath.stringValueOf(navResult));
345
346         xpath = createXPath("count(//*[@selected='true'])");
347
348         // activePath specified, with a group in the activePath
349
assertEquals(2, xpath.numberValueOf(navResult).intValue());
350         activePath = "/mygroup/g2/" + document3.getId();
351         domBuilder = new DOMBuilder();
352         user1NavigationManager.generateNavigationTree(domBuilder, new NavigationParams(navdoc1.getVariantKey(), activePath, false), document3.getVariantKey(), false);
353         navResult = domBuilder.getDocument();
354
355         xpath = createXPath("/n:navigationTree/@selectedPath");
356         assertEquals(activePath, xpath.stringValueOf(navResult));
357
358         xpath = createXPath("/n:navigationTree/n:group[1]/n:group[2]/@selected");
359         assertEquals("true", xpath.stringValueOf(navResult));
360
361         xpath = createXPath("count(//*[@selected='true'])");
362         assertEquals(3, xpath.numberValueOf(navResult).intValue());
363     }
364
365     private DOMXPath createXPath(String JavaDoc expr) throws Exception JavaDoc {
366         DOMXPath xpath = new DOMXPath(expr);
367         xpath.addNamespace("n", "http://outerx.org/daisy/1.0#navigation");
368         return xpath;
369     }
370
371     private void dumpDOM(org.w3c.dom.Document JavaDoc document) throws Exception JavaDoc {
372         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
373         Transformer JavaDoc transformer = factory.newTransformer();
374         transformer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(System.out));
375     }
376 }
377
Popular Tags