From 36a8350b532d15a183b2509f135e14c13ccc01ab Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 28 Feb 2018 17:58:44 +0100 Subject: [PATCH] Fixed database export with unitary test. Added XML export in file. --- src/fr/devinsy/sikevadb/core/XMLSikevaDB.java | 11 +++--- .../sikevadb/filetree/FileTreeSikevaDB.java | 6 ++-- .../filetree/TreeFileSikevaDBTest.java | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java index de0cdd6..6cc00a3 100644 --- a/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java @@ -297,11 +297,11 @@ public class XMLSikevaDB if (out == null) { - throw new IllegalArgumentException("out is null."); + throw new IllegalArgumentException("Parameter out is null."); } else if (source == null) { - throw new IllegalArgumentException("element is null."); + throw new IllegalArgumentException("Parameter element is null."); } else { @@ -371,12 +371,15 @@ public class XMLSikevaDB { // Element topElement = source.getElement(topkey); - write(out, topElement); + if (topElement != null) + { + write(out, topElement); + } // for (String subkey : source.getSubkeys(topkey).sort()) { - Element subElement = source.getElement(subkey); + Element subElement = source.getElement(topkey, subkey); write(out, subElement); } } diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index aababf4..7f15e75 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -64,8 +64,9 @@ public class FileTreeSikevaDB implements SikevaDB CLOSED }; + private static final Logger logger = LoggerFactory.getLogger(FileTreeSikevaDB.class); + private static final DateTimeFormatter ISOFormatter = ISODateTimeFormat.dateHourMinuteSecondMillis(); - private final Logger logger = LoggerFactory.getLogger(FileTreeSikevaDB.class); private Status status; private String login; @@ -787,9 +788,10 @@ public class FileTreeSikevaDB implements SikevaDB result = new StringList(); File targetDirectory = new File(this.dataDirectory, key); - if (targetDirectory.exists()) + if ((targetDirectory.exists()) && (targetDirectory.isDirectory())) { File[] subfiles = targetDirectory.listFiles(); + for (File subfile : subfiles) { result.add(subfile.getName()); diff --git a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java index 33b2806..5008a9c 100644 --- a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java @@ -34,6 +34,7 @@ import org.junit.Test; import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; import fr.devinsy.sikevadb.core.SikevaDBException; +import fr.devinsy.sikevadb.core.XMLSikevaDB; import fr.devinsy.util.strings.StringList; /** @@ -46,6 +47,38 @@ public class TreeFileSikevaDBTest private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TreeFileSikevaDBTest.class); private static FileTreeSikevaDB database; + @Test + public void testExport01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.put("alpha01", "qlskjfmlqja"); + database.put("alpha01", "qlskjfmlqjb"); + database.put("alpha02", "qlskjfmlqj"); + database.put("alpha03", "qlskjfmlqj"); + database.put("alpha04", "qlskjfmlqj"); + database.put("alpha05", "qlskjfmlqj"); + database.delete("alpha03"); + database.put("alpha01s", "bravo1", "qlskjfmlqja"); + database.put("alpha01s", "bravo1", "qlskjfmlqjb"); + database.put("alpha01s", "bravo2", "qlskjfmlqj"); + database.put("alpha01s", "bravo3", "qlskjfmlqj"); + database.put("alpha01s", "bravo4", "qlskjfmlqj"); + database.put("alpha01s", "bravo5", "qlskjfmlqj"); + database.delete("alpha01s", "bravo3"); + + File file = File.createTempFile("sikevadb-", ".tmp"); + XMLSikevaDB.export(file, database); + Assert.assertTrue(file.exists()); + file.delete(); + + // + logger.debug("===== test done."); + } + /** * Test get keyse 01. * @@ -671,6 +704,7 @@ public class TreeFileSikevaDBTest if (database != null) { database.close(); + database.destroy(); } }