Search in sources :

Example 41 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class CreateCollectionsTest method storeSamplesShakespeare.

@Test
public void storeSamplesShakespeare() throws XMLDBException, IOException, URISyntaxException {
    final Collection colTest = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    final CollectionManagementService service = (CollectionManagementService) colTest.getService("CollectionManagementService", "1.0");
    final Collection testCollection = service.createCollection("test");
    UserManagementService ums = (UserManagementService) testCollection.getService("UserManagementService", "1.0");
    ums.chmod("rwxr-xr-x");
    final List<String> storedResourceNames = new ArrayList<>();
    final List<String> filenames = new ArrayList<>();
    // store the samples
    for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
        final Resource res = storeResourceFromFile(SAMPLES.getShakespeareSample(sampleName), testCollection, sampleName);
        storedResourceNames.add(res.getId());
        filenames.add(sampleName);
    }
    assertEquals(filenames, storedResourceNames);
    // get a list from the database of stored resource names
    final List<String> retrievedStoredResourceNames = Arrays.asList(testCollection.listResources());
    // order of names from database may not be the order in which the files were loaded!
    Collections.sort(filenames);
    Collections.sort(retrievedStoredResourceNames);
    assertEquals(filenames, retrievedStoredResourceNames);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) ArrayList(java.util.ArrayList) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection)

Example 42 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class CreateCollectionsTest method storeRemoveStoreResource.

@Test
public void storeRemoveStoreResource() throws XMLDBException, IOException, URISyntaxException {
    final Collection colTest = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    final CollectionManagementService service = (CollectionManagementService) colTest.getService("CollectionManagementService", "1.0");
    final Collection testCollection = service.createCollection("test");
    UserManagementService ums = (UserManagementService) testCollection.getService("UserManagementService", "1.0");
    ums.chmod("rwxr-xr-x");
    final String testFile = "macbeth.xml";
    try (final InputStream is = SAMPLES.getMacbethSample()) {
        storeResourceFromFile(is, testCollection, testFile);
    }
    Resource resMacbeth = testCollection.getResource(testFile);
    assertNotNull("getResource(" + testFile + "\")", resMacbeth);
    final int resourceCount = testCollection.getResourceCount();
    testCollection.removeResource(resMacbeth);
    assertEquals("After removal resource count must decrease", resourceCount - 1, testCollection.getResourceCount());
    resMacbeth = testCollection.getResource(testFile);
    assertNull(resMacbeth);
    // restore the resource just removed
    try (final InputStream is = SAMPLES.getMacbethSample()) {
        storeResourceFromFile(is, testCollection, testFile);
    }
    assertEquals("After re-store resource count must increase", resourceCount, testCollection.getResourceCount());
    resMacbeth = testCollection.getResource(testFile);
    assertNotNull("getResource(" + testFile + "\")", resMacbeth);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) InputStream(java.io.InputStream) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection)

Example 43 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class BinaryResourceUpdateTest method updateBinary_windows.

// with same docname test fails for windows
@Test
public void updateBinary_windows() throws XMLDBException, URISyntaxException {
    for (int i = 0; i < REPEAT; i++) {
        BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test.xml", "BinaryResource");
        binaryResource.setContent(Paths.get(binFile.toURI()));
        testCollection.storeResource(binaryResource);
        Resource resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
        XMLResource xmlResource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
        xmlResource.setContent(Paths.get(xmlFile.toURI()));
        testCollection.storeResource(xmlResource);
        resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 44 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class CollectionConfigurationTest method collectionConfigurationService10.

/**
 * Remove config document
 */
@Test
public void collectionConfigurationService10() throws XMLDBException {
    Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    CollectionManagementService cms = (CollectionManagementService) testCollection.getService("CollectionManagementService", "1.0");
    Collection sub2 = cms.createCollection(COLLECTION_SUB2.toString());
    UserManagementService ums = (UserManagementService) sub2.getService("UserManagementService", "1.0");
    ums.chmod("rwxr-xr-x");
    IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(CONFIG1);
    // ... then index document
    XMLResource doc = (XMLResource) sub2.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource");
    doc.setContent(DOCUMENT_CONTENT);
    sub2.storeResource(doc);
    XPathQueryService service = (XPathQueryService) sub2.getService("XPathQueryService", "1.0");
    // 3 numeric values
    ResourceSet result = service.query("util:index-key-occurrences(/test/a, 1)");
    assertEquals("3", result.getResource(0).getContent());
    // ... but 1 string value
    result = service.query("util:index-key-occurrences(/test/b, \"1\")");
    assertEquals("1", result.getResource(0).getContent());
    // 3 numeric values
    result = service.query("util:qname-index-lookup(xs:QName(\"a\"), 1 ) ");
    assertEquals(3, result.getSize());
    // ... but 1 string value
    result = service.query("util:qname-index-lookup(xs:QName(\"b\"), \"1\" ) ");
    assertEquals(1, result.getSize());
    // remove config document thus dropping the configuration
    Collection confCol = DatabaseManager.getCollection("xmldb:exist://" + CONF_COLL_URI.toString(), ADMIN_DB_USER, ADMIN_DB_PWD);
    Resource confDoc = confCol.getResource(DEFAULT_COLLECTION_CONFIG_FILE);
    assertNotNull(confDoc);
    confCol.removeResource(confDoc);
    // cms = (CollectionManagementService) confCol.getService("CollectionManagementService", "1.0");
    // cms.removeCollection(".");
    idxConf.reindexCollection();
    // index should be empty since configuration was removed
    result = service.query("util:index-key-occurrences(/test/a, 1)");
    assertEquals(0, result.getSize());
    result = service.query("util:index-key-occurrences(/test/b, \"1\")");
    assertEquals(0, result.getSize());
    result = service.query("util:qname-index-lookup(xs:QName(\"a\"), 1 ) ");
    assertEquals(0, result.getSize());
    result = service.query("util:qname-index-lookup(xs:QName(\"b\"), \"1\" ) ");
    assertEquals(0, result.getSize());
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 45 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class ContentAsDOMTest method setUp.

@Before
public void setUp() throws Exception {
    CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    Collection testCollection = service.createCollection(TEST_COLLECTION);
    UserManagementService ums = (UserManagementService) testCollection.getService("UserManagementService", "1.0");
    // change ownership to guest
    Account guest = ums.getAccount(GUEST_DB_USER);
    ums.chown(guest, guest.getPrimaryGroup());
    ums.chmod(Permission.DEFAULT_COLLECTION_PERM);
    Resource resource = testCollection.createResource("test.xml", "XMLResource");
    resource.setContent(XML);
    testCollection.storeResource(resource);
    // change resource ownership to guest
    ums.chown(resource, guest, GUEST_DB_USER);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Account(org.exist.security.Account) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Before(org.junit.Before)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9