[Fix] Nicely handle missing unit on update
This commit is contained in:
parent
cf37ef8a2c
commit
78b576b514
|
@ -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$
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue