From 92c296044104e912019d2c688e12e68f2fd76395 Mon Sep 17 00:00:00 2001 From: tosmers Date: Mon, 25 Apr 2016 17:27:43 +0000 Subject: [PATCH] - 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 --- .../portation/AbstractMarshaller.java | 49 ++++++++++----- .../org/libreccm/portation/Marshaller.java | 5 +- .../libreccm/docrepo/AbstractResource.java | 25 ++++---- .../docrepo/portation/FilePortationTest.java | 63 ++++++++++++++----- pom.xml | 2 +- 5 files changed, 98 insertions(+), 46 deletions(-) diff --git a/ccm-core/src/main/java/org/libreccm/portation/AbstractMarshaller.java b/ccm-core/src/main/java/org/libreccm/portation/AbstractMarshaller.java index 4cc5848d1..800f4d3dd 100644 --- a/ccm-core/src/main/java/org/libreccm/portation/AbstractMarshaller.java +++ b/ccm-core/src/main/java/org/libreccm/portation/AbstractMarshaller.java @@ -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 { - 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 { 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 { 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 { 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 { } } - 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 getObjectClass(); protected abstract void insertIntoDb(I object); - public void importFile() { + public List importFile() { File file = new File(filename); - List lines = null; + List lines = null; try { lines = Files.readAllLines(file.toPath()); } catch (IOException e) { @@ -144,17 +157,17 @@ public abstract class AbstractMarshaller { "name %s.", file.getName())); } + List objects = new ArrayList(); 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 { break; } + assert object != null; insertIntoDb(object); + objects.add(object); } } + return objects; } + } diff --git a/ccm-core/src/main/java/org/libreccm/portation/Marshaller.java b/ccm-core/src/main/java/org/libreccm/portation/Marshaller.java index ed82939cc..761698646 100644 --- a/ccm-core/src/main/java/org/libreccm/portation/Marshaller.java +++ b/ccm-core/src/main/java/org/libreccm/portation/Marshaller.java @@ -133,7 +133,8 @@ public class Marshaller { final AbstractMarshaller marshaller = (AbstractMarshaller) 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 marshaller = (AbstractMarshaller) iterator.next(); - marshaller.prepare(format, filename); + marshaller.prepare(format, filename, false); marshaller.importFile(); } } catch (ClassNotFoundException e) { diff --git a/ccm-docrepo/src/main/java/org/libreccm/docrepo/AbstractResource.java b/ccm-docrepo/src/main/java/org/libreccm/docrepo/AbstractResource.java index 98a87c7f4..ef944d778 100644 --- a/ccm-docrepo/src/main/java/org/libreccm/docrepo/AbstractResource.java +++ b/ccm-docrepo/src/main/java/org/libreccm/docrepo/AbstractResource.java @@ -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() { diff --git a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java index 6e2e109ef..f33749557 100644 --- a/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java +++ b/ccm-docrepo/src/test/java/org/libreccm/docrepo/portation/FilePortationTest.java @@ -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 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 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 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); + } } diff --git a/pom.xml b/pom.xml index 83c16004f..de0435dd1 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ UTF-8 - 2.6.0 + 2.4.5 org.libreccm