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

View File

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

View File

@ -23,8 +23,6 @@ import org.hibernate.validator.constraints.NotBlank;
import org.libreccm.core.CcmObject;
import org.libreccm.security.User;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
@ -173,19 +171,20 @@ public abstract class AbstractResource extends CcmObject {
this.path = path;
}
public MimeType getMimeType() {
MimeType mimeType = null;
try {
mimeType = new MimeType(this.mimeType);
} catch (MimeTypeParseException e) {
log.error("Error on parsing the db-string for mimeType to actual" +
"MimeType", e);
}
return mimeType != null ? mimeType : null;
public String getMimeType() {
// MimeType mimeType = null;
// try {
// mimeType = new MimeType(this.mimeType);
// } catch (MimeTypeParseException e) {
// log.error("Error on parsing the db-string for mimeType to actual" +
// "MimeType", e);
// }
// return mimeType != null ? mimeType : null;
return mimeType;
}
public void setMimeType(MimeType mimeType) {
this.mimeType = mimeType.toString();
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
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.resolver.api.maven.Maven;
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.runner.RunWith;
import org.libreccm.docrepo.File;
@ -78,6 +82,11 @@ public class FilePortationTest {
private static String filePath =
"/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
public static void setUpClass() {
}
@ -179,41 +188,67 @@ public class FilePortationTest {
file = new File();
file.setName("testname");
file.setDescription("this is a text description");
file.setPath(filePath + "test2.txt");
file.setPath(filePath + "filename.txt");
file.setCreationDate(new Date());
file.setLastModifiedDate(new Date());
if (fileRepository != null && file != null) {
log.info("HELLOOOOOO!!");
log.info("A dummy for Docrepo.File has been prepared...");
fileRepository.save(file);
}
}
@Test
@InSequence(100)
public void xmlShouldBeCreated() {
fileMarshaller.prepare(Format.XML, filePath + "test1.xml");
List<File> fileList = Collections.singletonList(file);
fileMarshaller.exportList(fileList);
@InSequence(30)
public void initialCleanUp() {
java.io.File file1 = new java.io.File(filePath + f1Txt);
java.io.File file2 = new java.io.File(filePath + f2Xml);
file1.delete();
file2.delete();
assertTrue(!file1.exists()
&& !file2.exists());
}
@Test
@InSequence(200)
@InSequence(100)
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()) {
FileWriter fileWriter = null;
try {
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.flush();
fileWriter.close();
} 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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<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>
<groupId>org.libreccm</groupId>