1 16 package org.outerj.daisy.repository.test; 17 18 import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase; 19 import org.outerj.daisy.repository.RepositoryManager; 20 import org.outerj.daisy.repository.Credentials; 21 import org.outerj.daisy.repository.Repository; 22 import org.outerj.daisy.repository.ValueType; 23 import org.outerj.daisy.repository.user.UserManager; 24 import org.outerj.daisy.repository.user.Role; 25 import org.outerj.daisy.repository.schema.*; 26 27 import java.util.Locale ; 28 29 public abstract class AbstractSchemaTest extends AbstractDaisyTestCase { 30 protected boolean resetDataStores() { 31 return true; 32 } 33 34 public void testSchema() throws Exception { 35 RepositoryManager repositoryManager = getRepositoryManager(); 36 Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser")); 37 repository.switchRole(Role.ADMINISTRATOR); 38 RepositorySchema schema = repository.getRepositorySchema(); 39 40 UserManager userManager = repository.getUserManager(); 41 long testUserId = userManager.getUser("testuser", false).getId(); 42 43 47 PartType partType = schema.createPartType("mypart", ""); 49 partType.save(); 50 51 try { 53 partType.setName("part type name with spaces"); 54 fail("Could set an invalid part name"); 55 } catch (Exception e) {} 56 57 try { 58 partType.setName("0partNameStartingWithDigit"); 59 fail("Could set an invalid part name"); 60 } catch (Exception e) {} 61 62 partType = schema.getPartTypeById(partType.getId(), true); 64 assertNotNull(partType); 65 66 partType = schema.getPartTypeById(partType.getId(), false); 67 assertNotNull(partType); 68 69 partType = schema.getPartTypeByName("mypart", true); 70 assertNotNull(partType); 71 72 partType = schema.getPartTypeByName("mypart", false); 73 assertNotNull(partType); 74 75 try { 76 schema.getPartTypeByName("nonExistingPartType", true); 77 fail("Retrieving non existing part type should throw exception."); 78 } catch (Exception e) {} 79 80 try { 81 schema.getPartTypeByName("nonExistingPartType", false); 82 fail("Retrieving non existing part type should throw exception."); 83 } catch (Exception e) {} 84 85 assertFalse(partType.isDeprecated()); 87 88 partType = schema.getPartTypeById(partType.getId(), true); 90 partType.setMimeTypes("text/xml"); 91 partType.setLabel(new Locale ("nl"), "mijn part"); 92 partType.setDescription(new Locale ("nl"), "beschrijving van mijn part"); 93 partType.setDeprecated(true); 94 partType.save(); 95 96 partType = schema.getPartTypeById(partType.getId(), true); 97 assertEquals("text/xml", partType.getMimeTypes()); 98 assertEquals("mijn part", partType.getLabelExact(new Locale ("nl"))); 99 assertEquals("beschrijving van mijn part", partType.getDescriptionExact(new Locale ("nl"))); 100 assertEquals("mijn part", partType.getLabel(new Locale ("nl","BE"))); 101 assertNull(partType.getLabelExact(new Locale ("nl", "BE"))); 102 assertTrue(partType.isDeprecated()); 103 assertEquals(testUserId, partType.getLastModifier()); 104 105 PartType partTypeConcurrent = schema.getPartTypeById(partType.getId(), true); 107 partType.save(); 108 try { 109 partTypeConcurrent.save(); 110 fail("Expected a concurrent modification exception."); 111 } catch (Exception e) {} 112 113 PartType partType2 = schema.createPartType("mypart2", ""); 114 partType2.save(); 115 PartType partType3 = schema.createPartType("mypart3", ""); 116 partType3.save(); 117 118 assertEquals(3, schema.getAllPartTypes(true).getArray().length); 119 assertEquals(3, schema.getAllPartTypes(false).getArray().length); 120 121 PartType partTypeSameName = schema.createPartType("mypart", ""); 123 try { 124 partTypeSameName.save(); 125 fail("Expected exception when creating part with same name as existing part."); 126 } catch (Exception e) {} 127 128 partType = schema.getPartTypeByName("mypart", false); 130 try { 131 partType.save(); 132 fail("Saving read-only part should fail"); 133 } catch (Exception e) {} 134 135 PartType partTypeToBeDeleted = schema.createPartType("PartTypeToBeDeleted", ""); 137 partTypeToBeDeleted.save(); 138 schema.deletePartType(partTypeToBeDeleted.getId()); 139 try { 140 schema.getPartTypeById(partTypeToBeDeleted.getId(), false); 141 fail("Expected PartTypeNotFoundException."); 142 } catch (PartTypeNotFoundException e) { 143 } 144 try { 145 schema.getPartTypeById(partTypeToBeDeleted.getId(), true); 146 fail("Expected PartTypeNotFoundException."); 147 } catch (PartTypeNotFoundException e) { 148 } 149 try { 150 partType.save(); 151 fail("Expected exception when saving deleted part."); 152 } catch (Exception e) { 153 } 154 155 156 157 161 FieldType fieldType = schema.createFieldType("myfield", ValueType.STRING); 163 fieldType.save(); 164 165 try { 167 fieldType.setName("field type name with spaces"); 168 fail("Could set an invalid field name"); 169 } catch (Exception e) {} 170 171 fieldType = schema.getFieldTypeById(fieldType.getId(), true); 173 assertNotNull(fieldType); 174 175 fieldType = schema.getFieldTypeById(fieldType.getId(), false); 176 assertNotNull(fieldType); 177 178 fieldType = schema.getFieldTypeByName("myfield", true); 179 assertNotNull(fieldType); 180 181 fieldType = schema.getFieldTypeByName("myfield", false); 182 assertNotNull(fieldType); 183 184 try { 185 schema.getFieldTypeByName("nonExistingFieldType", true); 186 fail("Retrieving non existing field type should throw exception."); 187 } catch (Exception e) {} 188 189 try { 190 schema.getFieldTypeByName("nonExistingFieldType", false); 191 fail("Retrieving non existing field type should throw exception."); 192 } catch (Exception e) {} 193 194 assertFalse(fieldType.isDeprecated()); 196 assertFalse(fieldType.isAclAllowed()); 197 assertEquals(fieldType.getSize(), 0); 199 200 fieldType = schema.getFieldTypeById(fieldType.getId(), true); 202 fieldType.setLabel(new Locale ("nl"), "mijn veld"); 203 fieldType.setDescription(new Locale ("nl"), "beschrijving van mijn veld"); 204 fieldType.setDeprecated(true); 205 fieldType.setAclAllowed(true); 206 fieldType.setSize(10); 207 208 assertNull("no selection list set and yet not null returned", fieldType.getSelectionList()); 210 StaticSelectionList selList = fieldType.createStaticSelectionList(); 211 212 StaticListItem statListItem1 = selList.createStaticListItem("polski"); 214 statListItem1.setLabel(Locale.US, "labeltest"); 215 StaticListItem statListItem2 = selList.createStaticListItem("bez"); 216 try { 217 selList.addItem(new Integer (967)); 219 assertTrue("could add non-valuetype compliant item to selection list", false); 220 } catch (Exception e1) {} 221 StaticListItem statListItem3 = selList.createStaticListItem("trudu"); 224 225 fieldType.save(); 226 227 fieldType = schema.getFieldTypeById(fieldType.getId(), true); 228 assertEquals("mijn veld", fieldType.getLabelExact(new Locale ("nl"))); 229 assertEquals("beschrijving van mijn veld", fieldType.getDescriptionExact(new Locale ("nl"))); 230 assertEquals("mijn veld", fieldType.getLabel(new Locale ("nl","BE"))); 231 assertNull(fieldType.getLabelExact(new Locale ("nl", "BE"))); 232 assertTrue(fieldType.isDeprecated()); 233 assertTrue(fieldType.isAclAllowed()); 234 assertEquals(testUserId, fieldType.getLastModifier()); 235 assertEquals(fieldType.getSize(), 10); 236 237 StaticSelectionList fetchedSelList = (StaticSelectionList)fieldType.getSelectionList(); 240 assertNotNull(fetchedSelList); 241 ListItem[] selListItems = fetchedSelList.getItems(); 242 assertEquals(selListItems.length, 3); 243 ListItem listItem1 = selListItems[0]; 245 ListItem listItem2 = selListItems[1]; 246 ListItem listItem3 = selListItems[2]; 247 248 assertEquals(statListItem1.getValue(), listItem1.getValue()); 249 assertEquals(statListItem2.getValue(), listItem2.getValue()); 250 assertEquals(statListItem3.getValue(), listItem3.getValue()); 251 252 assertEquals("labeltest", listItem1.getLabel(Locale.US)); 253 254 fetchedSelList.addItem("DumuziAbsu"); 256 fieldType.save(); 257 258 fieldType = schema.getFieldTypeById(fieldType.getId(), true); 259 fetchedSelList = (StaticSelectionList)fieldType.getSelectionList(); 260 selListItems = fetchedSelList.getItems(); 261 assertEquals(selListItems.length, 4); 262 assertEquals(selListItems[3].getValue(), "DumuziAbsu"); 263 264 fieldType.clearSelectionList(); 266 fieldType.save(); 267 fieldType = schema.getFieldTypeById(fieldType.getId(), true); 268 assertNull(fieldType.getSelectionList()); 269 270 FieldType fieldTypeConcurrent = schema.getFieldTypeById(fieldType.getId(), true); 272 fieldType.save(); 273 try { 274 fieldTypeConcurrent.save(); 275 fail("Expected a concurrent modification exception."); 276 } catch (Exception e) {} 277 278 FieldType fieldType2 = schema.createFieldType("myfield2", ValueType.STRING); 279 fieldType2.save(); 280 FieldType fieldType3 = schema.createFieldType("myfield3", ValueType.STRING); 281 fieldType3.save(); 282 283 assertEquals(3, schema.getAllFieldTypes(true).getArray().length); 284 assertEquals(3, schema.getAllFieldTypes(false).getArray().length); 285 286 FieldType fieldTypeSameName = schema.createFieldType("myfield", ValueType.STRING); 288 try { 289 fieldTypeSameName.save(); 290 fail("Expected exception when creating field with same name as existing field."); 291 } catch (Exception e) {} 292 293 fieldType = schema.getFieldTypeByName("myfield", false); 295 try { 296 fieldType.save(); 297 fail("Saving read-only field should fail"); 298 } catch (Exception e) {} 299 300 302 FieldType otherFieldType = schema.createFieldType("datefield", ValueType.DATE); 303 otherFieldType.save(); 304 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 305 assertEquals(ValueType.DATE, otherFieldType.getValueType()); 306 307 otherFieldType = schema.createFieldType("datetimefield", ValueType.DATETIME); 308 otherFieldType.save(); 309 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 310 assertEquals(ValueType.DATETIME, otherFieldType.getValueType()); 311 312 otherFieldType = schema.createFieldType("longfield", ValueType.LONG); 313 otherFieldType.save(); 314 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 315 assertEquals(ValueType.LONG, otherFieldType.getValueType()); 316 317 otherFieldType = schema.createFieldType("doublefield", ValueType.DOUBLE); 318 otherFieldType.save(); 319 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 320 assertEquals(ValueType.DOUBLE, otherFieldType.getValueType()); 321 322 otherFieldType = schema.createFieldType("decimalfield", ValueType.DECIMAL); 323 otherFieldType.save(); 324 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 325 assertEquals(ValueType.DECIMAL, otherFieldType.getValueType()); 326 327 otherFieldType = schema.createFieldType("booleanfield", ValueType.BOOLEAN); 328 otherFieldType.save(); 329 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 330 assertEquals(ValueType.BOOLEAN, otherFieldType.getValueType()); 331 332 otherFieldType = schema.createFieldType("linkfield", ValueType.LINK); 333 otherFieldType.save(); 334 otherFieldType = schema.getFieldTypeById(otherFieldType.getId(), true); 335 assertEquals(ValueType.LINK, otherFieldType.getValueType()); 336 337 FieldType multiValueFieldType = schema.createFieldType("string_non_multi_value", ValueType.STRING, false); 339 assertEquals(false, multiValueFieldType.isMultiValue()); 340 multiValueFieldType.save(); 341 multiValueFieldType = schema.getFieldTypeById(multiValueFieldType.getId(), true); 342 assertEquals(false, multiValueFieldType.isMultiValue()); 343 344 multiValueFieldType = schema.createFieldType("string_multi_value", ValueType.STRING, true); 345 assertEquals(true, multiValueFieldType.isMultiValue()); 346 multiValueFieldType.save(); 347 multiValueFieldType = schema.getFieldTypeById(multiValueFieldType.getId(), true); 348 assertEquals(true, multiValueFieldType.isMultiValue()); 349 350 FieldType fieldTypeToBeDeleted = schema.createFieldType("FieldTypeToBeDeleted", ValueType.STRING); 352 fieldTypeToBeDeleted.save(); 353 schema.deleteFieldType(fieldTypeToBeDeleted.getId()); 354 try { 355 schema.getFieldTypeById(fieldTypeToBeDeleted.getId(), false); 356 fail("Expected FieldTypeNotFoundException."); 357 } catch (FieldTypeNotFoundException e) { 358 } 359 try { 360 schema.getFieldTypeById(fieldTypeToBeDeleted.getId(), true); 361 fail("Expected FieldTypeNotFoundException."); 362 } catch (FieldTypeNotFoundException e) { 363 } 364 try { 365 fieldType.save(); 366 fail("Expected exception when saving deleted field."); 367 } catch (Exception e) { 368 } 369 370 374 DocumentType documentType = schema.createDocumentType("mydoctype"); 375 documentType.save(); 376 377 documentType = schema.getDocumentTypeById(documentType.getId(), true); 378 documentType.addFieldType(fieldType, true); 379 documentType.addFieldType(fieldType2, false); 380 documentType.save(); 381 documentType.addPartType(partType, true); 382 documentType.addPartType(partType2, false); 383 documentType.save(); 384 385 documentType = schema.getDocumentTypeById(documentType.getId(), true); 386 assertTrue(documentType.getFieldTypeUse(fieldType.getId()).isRequired()); 387 assertFalse(documentType.getFieldTypeUse(fieldType2.getId()).isRequired()); 388 assertTrue(documentType.getFieldTypeUse(partType.getId()).isRequired()); 389 assertFalse(documentType.getFieldTypeUse(partType2.getId()).isRequired()); 390 391 assertFalse(documentType.isDeprecated()); 392 393 documentType.setDeprecated(true); 394 documentType.setName("altereddoctype"); 395 documentType.save(); 396 397 documentType = schema.getDocumentTypeById(documentType.getId(), false); 398 assertTrue(documentType.isDeprecated()); 399 assertEquals("altereddoctype", documentType.getName()); 400 401 DocumentType documentType2 = schema.createDocumentType("mydoctype2"); 402 documentType2.save(); 403 404 assertEquals(2, schema.getAllDocumentTypes(true).getArray().length); 405 assertEquals(2, schema.getAllDocumentTypes(false).getArray().length); 406 407 DocumentType documentTypeConcurrent = schema.getDocumentTypeById(documentType2.getId(), true); 408 documentType2.save(); 409 try { 410 documentTypeConcurrent.save(); 411 fail("Expected a concurrent modification exception."); 412 } catch (Exception e) {} 413 414 DocumentType documentTypeSameName = schema.createDocumentType("mydoctype2"); 416 try { 417 documentTypeSameName.save(); 418 fail("Expected exception when creating document type with same name as existing one."); 419 } catch (Exception e) {} 420 421 documentType2 = schema.getDocumentTypeByName("mydoctype2", false); 423 try { 424 documentType2.save(); 425 fail("Saving read-only document type should fail"); 426 } catch (Exception e) {} 427 428 429 DocumentType documentTypeToBeDeleted = schema.createDocumentType("DocumentTypeToBeDeleted"); 431 documentTypeToBeDeleted.addFieldType(fieldType, true); 432 documentTypeToBeDeleted.addPartType(partType, true); 433 documentTypeToBeDeleted.save(); 434 435 try { 437 schema.deleteFieldType(fieldType.getId()); 438 fail("Deleting field type associated with document type should fail."); 439 } catch (Exception e) {} 440 try { 441 schema.deletePartType(partType.getId()); 442 fail("Deleting part type associated with document type should fail."); 443 } catch (Exception e) {} 444 445 schema.deleteDocumentType(documentTypeToBeDeleted.getId()); 446 try { 447 schema.getDocumentTypeById(documentTypeToBeDeleted.getId(), false); 448 fail("Expected DocumentTypeNotFoundException."); 449 } catch (DocumentTypeNotFoundException e) { 450 } 451 try { 452 schema.getDocumentTypeById(documentTypeToBeDeleted.getId(), true); 453 fail("Expected DocumentTypeNotFoundException."); 454 } catch (DocumentTypeNotFoundException e) { 455 } 456 try { 457 documentType.save(); 458 fail("Expected exception when saving deleted document."); 459 } catch (Exception e) { 460 } 461 462 466 } 467 468 protected abstract RepositoryManager getRepositoryManager() throws Exception ; 469 } 470 | Popular Tags |