1 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 ; 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.stream.StreamResult ; 38 import javax.xml.transform.dom.DOMSource ; 39 40 46 public abstract class AbstractNavigationTest extends AbstractDaisyTestCase { 47 protected boolean resetDataStores() { 48 return true; 49 } 50 51 protected abstract RepositoryManager getRepositoryManager() throws Exception ; 52 53 public void testNavigation() throws Exception { 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 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 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 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 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 CollectionManager collectionManager = repository.getCollectionManager(); 105 DocumentCollection collection1 = collectionManager.createCollection("collection1"); 106 collection1.save(); 107 108 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 Document navdoc2 = repository.createDocument("navdoc2", navigationType.getId()); 126 StringBuffer content2 = new StringBuffer (); 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 content1 = new StringBuffer (); 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 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 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 StringBuffer content3 = new StringBuffer (); 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 content4 = new StringBuffer (); 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 content4 = new StringBuffer (); 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 content5 = new StringBuffer (); 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 Document navdoc6 = repository.createDocument("navdoc6", navigationType.getId()); 241 navdoc6.setPart("NavigationDescription", "text/xml", new byte[0]); 242 navdoc6.save(); 243 244 StringBuffer content6 = new StringBuffer (); 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 262 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 275 277 281 LookupAlternative[] lookupAlternatives = new LookupAlternative[] { new LookupAlternative("x", collection1.getId(), navdoc1.getVariantKey()) }; 282 283 NavigationLookupResult lookupResult = user1NavigationManager.lookup("/mygroup", -1, -1, lookupAlternatives); 285 assertTrue(lookupResult.isRedirect()); 286 assertEquals("/mygroup/g1/3", lookupResult.getNavigationPath()); 287 288 lookupResult = user1NavigationManager.lookup("/abc", -1, -1, lookupAlternatives); 290 assertTrue(lookupResult.isNotFound()); 291 292 lookupResult = user1NavigationManager.lookup("/2323", -1, -1, lookupAlternatives); 294 assertEquals(2323, lookupResult.getVariantKey().getDocumentId()); 295 assertEquals("", lookupResult.getNavigationPath()); 296 297 lookupResult = user1NavigationManager.lookup("/1", -1, -1, lookupAlternatives); 300 assertTrue(lookupResult.isRedirect()); 301 assertEquals("/3/1", lookupResult.getNavigationPath()); 302 303 lookupResult = user1NavigationManager.lookup("/mygroup/g1/emptygroup", -1, -1, lookupAlternatives); 305 assertTrue(lookupResult.isNotFound()); 306 307 lookupResult = user1NavigationManager.lookup("/l1", -1, -1, lookupAlternatives); 309 assertTrue(lookupResult.isNotFound()); 310 311 lookupResult = user1NavigationManager.lookup("/3/3", -1, -1, lookupAlternatives); 313 assertEquals(3, lookupResult.getVariantKey().getDocumentId()); 314 assertEquals("/3/3", lookupResult.getNavigationPath()); 315 316 320 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 String 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 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 expr) throws Exception { 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 document) throws Exception { 372 TransformerFactory factory = TransformerFactory.newInstance(); 373 Transformer transformer = factory.newTransformer(); 374 transformer.transform(new DOMSource (document), new StreamResult (System.out)); 375 } 376 } 377 | Popular Tags |