Search in sources :

Example 1 with CASMetadata

use of de.tudarmstadt.ukp.clarin.webanno.api.type.CASMetadata in project webanno by webanno.

the class ImportExportServiceImplTest method thatExportContainsNoCasMetadata.

@Test
public void thatExportContainsNoCasMetadata() throws Exception {
    SourceDocument sd = makeSourceDocument(1l, 1l);
    // Create type system with built-in types, internal types, but without any project-specific
    // types.
    List<TypeSystemDescription> typeSystems = new ArrayList<>();
    typeSystems.add(createTypeSystemDescription());
    typeSystems.add(CasMetadataUtils.getInternalTypeSystem());
    TypeSystemDescription ts = mergeTypeSystems(typeSystems);
    // Prepare a test CAS with a CASMetadata annotation (DocumentMetaData is added as well
    // because the DKPro Core writers used by the ImportExportService expect it.
    JCas jcas = JCasFactory.createJCas(ts);
    casStorageSession.add("jcas", EXCLUSIVE_WRITE_ACCESS, jcas.getCas());
    jcas.setDocumentText("This is a test .");
    DocumentMetaData.create(jcas);
    CASMetadata cmd = new CASMetadata(jcas);
    cmd.addToIndexes(jcas);
    // Pass the CAS through the export mechanism. Write as XMI because that is one of the
    // formats which best retains the information from the CAS and is nicely human-readable
    // if the test needs to be debugged.
    File exportedXmi = sut.exportCasToFile(jcas.getCas(), sd, "testfile", sut.getFormatById(XmiFormatSupport.ID).get(), true);
    // Read the XMI back from the ZIP that was created by the exporter. This is because XMI
    // files are always serialized as XMI file + type system file.
    JCas jcas2 = JCasFactory.createJCas(ts);
    casStorageSession.add("jcas2", EXCLUSIVE_WRITE_ACCESS, jcas.getCas());
    try (ZipArchiveInputStream zipInput = new ZipArchiveInputStream(new FileInputStream(exportedXmi))) {
        ZipArchiveEntry entry;
        while ((entry = zipInput.getNextZipEntry()) != null) {
            if (entry.getName().endsWith(".xmi")) {
                XmiCasDeserializer.deserialize(zipInput, jcas2.getCas());
                break;
            }
        }
    } finally {
        exportedXmi.delete();
    }
    List<CASMetadata> result = new ArrayList<>(select(jcas2, CASMetadata.class));
    assertThat(result).hasSize(0);
}
Also used : TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) TypeSystemDescriptionFactory.createTypeSystemDescription(org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) CASMetadata(de.tudarmstadt.ukp.clarin.webanno.api.type.CASMetadata) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with CASMetadata

use of de.tudarmstadt.ukp.clarin.webanno.api.type.CASMetadata in project webanno by webanno.

the class CasStorageServiceImplTest method testCasMetadataGetsCreated.

@Test
public void testCasMetadataGetsCreated() throws Exception {
    try (CasStorageSession casStorageSession = openNested(true)) {
        List<TypeSystemDescription> typeSystems = new ArrayList<>();
        typeSystems.add(createTypeSystemDescription());
        typeSystems.add(CasMetadataUtils.getInternalTypeSystem());
        JCas cas = JCasFactory.createJCas(mergeTypeSystems(typeSystems));
        casStorageSession.add("cas", EXCLUSIVE_WRITE_ACCESS, cas.getCas());
        SourceDocument doc = makeSourceDocument(2l, 2l, "test");
        String user = "test";
        sut.writeCas(doc, cas.getCas(), user);
        JCas cas2 = sut.readCas(doc, user).getJCas();
        List<CASMetadata> cmds = new ArrayList<>(select(cas2, CASMetadata.class));
        assertThat(cmds).hasSize(1);
        assertThat(cmds.get(0).getProjectId()).isEqualTo(doc.getProject().getId());
        assertThat(cmds.get(0).getSourceDocumentId()).isEqualTo(doc.getId());
        assertThat(cmds.get(0).getLastChangedOnDisk()).isEqualTo(sut.getCasTimestamp(doc, user).get());
    }
}
Also used : TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) TypeSystemDescriptionFactory.createTypeSystemDescription(org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription) ArrayList(java.util.ArrayList) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) CASMetadata(de.tudarmstadt.ukp.clarin.webanno.api.type.CASMetadata) Test(org.junit.Test)

Aggregations

CASMetadata (de.tudarmstadt.ukp.clarin.webanno.api.type.CASMetadata)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 ArrayList (java.util.ArrayList)2 TypeSystemDescriptionFactory.createTypeSystemDescription (org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription)2 JCas (org.apache.uima.jcas.JCas)2 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)2 Test (org.junit.Test)2 CasStorageSession (de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1