/*
* (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Nuxeo - initial API and implementation
*
* $Id: TestExportedDocument.java 29029 2008-01-14 18:38:14Z ldoguin $
*/
package org.nuxeo.ecm.core.io.impl;
import static org.junit.Assert.assertEquals;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.Blobs;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.io.DocumentWriter;
import org.nuxeo.ecm.core.io.ExportedDocument;
import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.ecm.core.test.annotations.Granularity;
import org.nuxeo.ecm.core.test.annotations.RepositoryConfig;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
@RunWith(FeaturesRunner.class)
@Features(CoreFeature.class)
@RepositoryConfig(cleanup = Granularity.METHOD)
public class TestExportImportBlob {
@Inject
protected CoreSession session;
DocumentModel rootDocument;
DocumentModel workspace;
DocumentModel docToExport;
private void createDocs() throws Exception {
rootDocument = session.getRootDocument();
workspace = session.createDocumentModel(rootDocument.getPathAsString(), "ws1", "Workspace");
workspace.setProperty("dublincore", "title", "test WS");
workspace = session.createDocument(workspace);
docToExport = session.createDocumentModel(workspace.getPathAsString(), "file", "File");
docToExport.setProperty("dublincore", "title", "MyDoc");
Blob blob = Blobs.createBlob("SomeDummyContent");
blob.setFilename("dummyBlob.txt");
docToExport.setProperty("file", "content", blob);
docToExport = session.createDocument(docToExport);
session.save();
}
@Test
public void testBlobFilenamePresent() throws Exception {
createDocs();
ExportedDocument exportedDoc = new ExportedDocumentImpl(docToExport, true);
assertEquals("File", exportedDoc.getType());
session.removeDocument(docToExport.getRef());
session.save();
assertEquals(0, session.getChildren(workspace.getRef()).size());
DocumentWriter writer = new DocumentModelWriter(session, rootDocument.getPathAsString());
writer.write(exportedDoc);
DocumentModelList children = session.getChildren(workspace.getRef());
assertEquals(1, children.size());
DocumentModel importedDocument = children.get(0);
Blob blob = (Blob) importedDocument.getProperty("file", "content");
assertEquals("dummyBlob.txt", blob.getFilename());
}
}