- adds functioning test for ex- and import of a docrepo.file example

- export and import are therefore, as of initial testing results, functioning as expected

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4027 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
tosmers 2016-04-25 17:27:43 +00:00
parent 62b377fbe8
commit 92c2960441
5 changed files with 98 additions and 46 deletions

View File

@ -18,8 +18,8 @@
*/ */
package org.libreccm.portation; package org.libreccm.portation;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule; import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -29,6 +29,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -57,7 +58,8 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
public void prepare(final Format format, String filename) { public void prepare(final Format format, String filename, boolean
indentation) {
this.format = format; this.format = format;
this.filename = filename; this.filename = filename;
@ -67,6 +69,9 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
JacksonXmlModule module = new JacksonXmlModule(); JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false); module.setDefaultUseWrapper(false);
xmlMapper = new XmlMapper(module); xmlMapper = new XmlMapper(module);
if (indentation) {
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
break; break;
case JSON: case JSON:
@ -87,7 +92,11 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
try { try {
fileWriter = new FileWriter(file); fileWriter = new FileWriter(file);
} catch (IOException e) {
log.error(String.format("Unable to open a fileWriter for the file" +
" with the name %s.", file.getName()));
}
if (fileWriter != null) {
for (I object : exportList) { for (I object : exportList) {
String line = null; String line = null;
@ -95,9 +104,11 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
case XML: case XML:
try { try {
line = xmlMapper.writeValueAsString(object); line = xmlMapper.writeValueAsString(object);
} catch (JsonProcessingException e) { //log.info(line);
log.error(String.format("Unable to write object %s " + } catch (IOException e) {
"as XML string.", object.getUuid())); log.error(String.format("Unable to write objetct " +
"of class %s as XML string with name %s.",
object.getClass(), file.getName()), e);
} }
break; break;
@ -122,21 +133,23 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
} }
} }
fileWriter.close(); try {
} catch (IOException e) { fileWriter.close();
log.error(String.format("Unable open a fileWriter for the file " + } catch (IOException e) {
"with the name %s.", file.getName())); log.error(String.format("Unable to close a fileWriter for the" +
} " file with the name %s.", file.getName()));
}
}
} }
protected abstract Class<I> getObjectClass(); protected abstract Class<I> getObjectClass();
protected abstract void insertIntoDb(I object); protected abstract void insertIntoDb(I object);
public void importFile() { public List<I> importFile() {
File file = new File(filename); File file = new File(filename);
List<String> lines = null;
List<String> lines = null;
try { try {
lines = Files.readAllLines(file.toPath()); lines = Files.readAllLines(file.toPath());
} catch (IOException e) { } catch (IOException e) {
@ -144,17 +157,17 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
"name %s.", file.getName())); "name %s.", file.getName()));
} }
List<I> objects = new ArrayList<I>();
if (lines != null) { if (lines != null) {
for (String line : lines) { for (String line : lines) {
I object = null; I object = null;
switch (format) { switch (format) {
case XML: case XML:
try { try {
object = xmlMapper.readValue(line, getObjectClass()); object = xmlMapper.readValue(line, getObjectClass());
} catch (IOException e) { } catch (IOException e) {
log.error(String.format("Unable to read object " + log.error(String.format("Unable to read objects " +
"from XML string:\n \"%s\"", line)); "from XML line:\n \"%s\"", line), e);
} }
break; break;
@ -168,8 +181,12 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
break; break;
} }
assert object != null;
insertIntoDb(object); insertIntoDb(object);
objects.add(object);
} }
} }
return objects;
} }
} }

View File

@ -133,7 +133,8 @@ public class Marshaller {
final AbstractMarshaller<I> marshaller = (AbstractMarshaller<I>) final AbstractMarshaller<I> marshaller = (AbstractMarshaller<I>)
iterator.next(); iterator.next();
marshaller.prepare(format, filename + "__" + type.toString()); marshaller.prepare(format, filename + "__" + type.toString(),
false);
marshaller.exportList(list); marshaller.exportList(list);
} }
} }
@ -185,7 +186,7 @@ public class Marshaller {
final AbstractMarshaller<I> marshaller = (AbstractMarshaller<I>) final AbstractMarshaller<I> marshaller = (AbstractMarshaller<I>)
iterator.next(); iterator.next();
marshaller.prepare(format, filename); marshaller.prepare(format, filename, false);
marshaller.importFile(); marshaller.importFile();
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {

View File

@ -23,8 +23,6 @@ import org.hibernate.validator.constraints.NotBlank;
import org.libreccm.core.CcmObject; import org.libreccm.core.CcmObject;
import org.libreccm.security.User; import org.libreccm.security.User;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
@ -173,19 +171,20 @@ public abstract class AbstractResource extends CcmObject {
this.path = path; this.path = path;
} }
public MimeType getMimeType() { public String getMimeType() {
MimeType mimeType = null; // MimeType mimeType = null;
try { // try {
mimeType = new MimeType(this.mimeType); // mimeType = new MimeType(this.mimeType);
} catch (MimeTypeParseException e) { // } catch (MimeTypeParseException e) {
log.error("Error on parsing the db-string for mimeType to actual" + // log.error("Error on parsing the db-string for mimeType to actual" +
"MimeType", e); // "MimeType", e);
} // }
return mimeType != null ? mimeType : null; // return mimeType != null ? mimeType : null;
return mimeType;
} }
public void setMimeType(MimeType mimeType) { public void setMimeType(String mimeType) {
this.mimeType = mimeType.toString(); this.mimeType = mimeType;
} }
public long getSize() { public long getSize() {

View File

@ -31,7 +31,11 @@ import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven; import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage; import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage;
import org.junit.*; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.libreccm.docrepo.File; import org.libreccm.docrepo.File;
@ -78,6 +82,11 @@ public class FilePortationTest {
private static String filePath = private static String filePath =
"/home/tosmers/Svn/libreccm/ccm_ng/ccm-docrepo/src/test/resources/datasets/org/libreccm/docrepo/FilePortationTest/"; "/home/tosmers/Svn/libreccm/ccm_ng/ccm-docrepo/src/test/resources/datasets/org/libreccm/docrepo/FilePortationTest/";
private static final String f1Txt = "test1.txt";
private static final String f2Xml = "test2.xml";
private static final String f3Xml = "test3.xml";
private static final String f4Xml = "test4.xml";
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
} }
@ -179,41 +188,67 @@ public class FilePortationTest {
file = new File(); file = new File();
file.setName("testname"); file.setName("testname");
file.setDescription("this is a text description"); file.setDescription("this is a text description");
file.setPath(filePath + "test2.txt"); file.setPath(filePath + "filename.txt");
file.setCreationDate(new Date()); file.setCreationDate(new Date());
file.setLastModifiedDate(new Date()); file.setLastModifiedDate(new Date());
if (fileRepository != null && file != null) { if (fileRepository != null && file != null) {
log.info("HELLOOOOOO!!"); log.info("A dummy for Docrepo.File has been prepared...");
fileRepository.save(file); fileRepository.save(file);
} }
} }
@Test @Test
@InSequence(100) @InSequence(30)
public void xmlShouldBeCreated() { public void initialCleanUp() {
fileMarshaller.prepare(Format.XML, filePath + "test1.xml"); java.io.File file1 = new java.io.File(filePath + f1Txt);
List<File> fileList = Collections.singletonList(file); java.io.File file2 = new java.io.File(filePath + f2Xml);
file1.delete();
fileMarshaller.exportList(fileList); file2.delete();
assertTrue(!file1.exists()
&& !file2.exists());
} }
@Test @Test
@InSequence(200) @InSequence(100)
public void aFileShouldBeCreated() { public void aFileShouldBeCreated() {
java.io.File file = new java.io.File(filePath + "test.txt"); java.io.File file = new java.io.File(filePath + f1Txt);
if (!file.exists()) { if (!file.exists()) {
FileWriter fileWriter = null; FileWriter fileWriter = null;
try { try {
fileWriter = new FileWriter(file); fileWriter = new FileWriter(file);
log.info("\n\n\n\n\n\n\n\n\n\n Success \n\n\n\n\n\n\n\n\n\n"); log.info(String.format("%s has successfully been created.",
f1Txt));
fileWriter.write("bloß ein test! - tosmers"); fileWriter.write("bloß ein test! - tosmers");
fileWriter.flush(); fileWriter.flush();
fileWriter.close(); fileWriter.close();
} catch (IOException e) { } catch (IOException e) {
log.error("\n\n\n\n\n\n\n\n\n\n Fehler \n\n\n\n\n\n\n\n\n\n"); log.error(String.format("%s could not be created.", f1Txt));
} }
assertTrue(file.exists());
} }
assertTrue(file.exists());
} }
@Test
@InSequence(200)
public void xmlShouldBeCreated() {
fileMarshaller.prepare(Format.XML, filePath + f2Xml, false);
List<File> fileList = Collections.singletonList(file);
fileMarshaller.exportList(fileList);
fileMarshaller.prepare(Format.XML, filePath + f3Xml, true);
fileMarshaller.exportList(fileList);
}
@Test
@InSequence(300)
public void objectShouldBeImported() {
log.info("\n\n\n" + file.toString() + "\n\n\n");
fileMarshaller.prepare(Format.XML, filePath + f2Xml, false);
List<File> objects = fileMarshaller.importFile();
objects.forEach(l -> log.info("\n\n\n" + l.toString() + "\n\n\n"));
fileMarshaller.prepare(Format.XML, filePath + f4Xml, true);
fileMarshaller.exportList(objects);
}
} }

View File

@ -9,7 +9,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<ccm.version>7.0.0-SNAPSHOT</ccm.version>--> <!--<ccm.version>7.0.0-SNAPSHOT</ccm.version>-->
<jackson-core-version>2.6.0</jackson-core-version> <jackson-core-version>2.4.5</jackson-core-version>
</properties> </properties>
<groupId>org.libreccm</groupId> <groupId>org.libreccm</groupId>