From 78b576b5142ff225e3a600d381694460eef65262 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sat, 29 May 2021 23:31:12 +0200 Subject: [PATCH] [Fix] Nicely handle missing unit on update --- .../java/li/strolch/xmlpers/api/FileDao.java | 17 ++++++++++++++++- .../xmlpers/test/ObjectDaoResourceTest.java | 16 ---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java index fcb310cd5..b0f350edc 100644 --- a/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java +++ b/li.strolch.xmlpers/src/main/java/li/strolch/xmlpers/api/FileDao.java @@ -85,7 +85,7 @@ public class FileDao { assertIsIdRef(IoOperation.UPDATE, objectRef); File path = objectRef.getPath(this.pathBuilder); logPath(IoOperation.UPDATE, path, objectRef); - assertPathIsFileAndWritable(path, objectRef); + assertPathIsDirectoryAndWritable(path.getParentFile(), objectRef); FileIo fileIo = new FileIo(path); this.tx.getManager().getIoMode().write(ctx, fileIo); } @@ -190,6 +190,21 @@ public class FileDao { } } + private void assertPathIsDirectoryAndWritable(File path, ObjectRef objectRef) { + if (!path.exists()) { + String msg = "Persistence path does not exist for {0} at {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, objectRef.getName(), path.getAbsolutePath()); + throw new XmlPersistenceException(msg); + } + + if (!path.isDirectory() || !path.canWrite()) { + String msg; + msg = "Persistence path is not a directory or is not readable for {0} at {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, objectRef.getName(), path.getAbsolutePath()); + throw new XmlPersistenceException(msg); + } + } + private void assertPathNotExists(File path, ObjectRef objectRef) { if (path.exists()) { String msg = "Persistence unit already exists for {0} at {1}"; //$NON-NLS-1$ diff --git a/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/ObjectDaoResourceTest.java b/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/ObjectDaoResourceTest.java index 78a08acad..24e28620c 100644 --- a/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/ObjectDaoResourceTest.java +++ b/li.strolch.xmlpers/src/test/java/li/strolch/xmlpers/test/ObjectDaoResourceTest.java @@ -211,22 +211,6 @@ public class ObjectDaoResourceTest extends AbstractPersistenceTest { } } - @Test - public void shouldFailModifyNotExisting() { - setup(IoMode.SAX); - - XmlPersistenceException exception = assertThrows(XmlPersistenceException.class, () -> { - // update - try (PersistenceTransaction tx = this.persistenceManager.openTx()) { - - MyModel resource = createResource(); - tx.getObjectDao().update(resource); - } - }); - - MatcherAssert.assertThat(exception.getMessage(), containsString("Persistence unit does not exist for")); - } - @Test public void shouldFailDeleteNotExisting() { setup(IoMode.SAX);