Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.flexdock.docking.state.PersistenceException;
import org.flexdock.perspective.persist.xml.XMLPersister;
Expand Down Expand Up @@ -73,11 +77,29 @@ public boolean store(String persistenceKey, PerspectiveModel perspectiveModel) t

// XMLDebugger.println(perspectiveModel);

FileOutputStream fos = new FileOutputStream(file);
// Write to a temporary file and replace the layout file only once it
// is complete, so that a failure keeps the previous layout
Path target = file.toPath();
Path temp = Files.createTempFile(file.getAbsoluteFile().getParentFile().toPath(), file.getName(), ".tmp");
try {
return this.persister.store(fos, perspectiveModel);
boolean stored;
OutputStream os = Files.newOutputStream(temp);
try {
stored = this.persister.store(os, perspectiveModel);
} finally {
os.close();
}

if(stored) {
try {
Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch(AtomicMoveNotSupportedException e) {
Files.move(temp, target, StandardCopyOption.REPLACE_EXISTING);
}
}
return stored;
} finally {
fos.close();
Files.deleteIfExists(temp);
}
}

Expand Down Expand Up @@ -110,10 +132,6 @@ protected void validatePerspectiveFile(File file) throws IOException {
if(!dir.exists()) {
dir.mkdirs();
}

if(!file.exists()) {
file.createNewFile();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -16,6 +20,22 @@ private static PerspectiveModel createModel() {
return new PerspectiveModel("one", "one", new Perspective[] {perspective});
}

// Writes part of a layout, then fails, as a serializer error would
private static Persister createFailingPersister() {
return new Persister() {
@Override
public boolean store(OutputStream os, PerspectiveModel model) throws IOException {
os.write("<PerspectiveModel".getBytes());
throw new IOException("store failed");
}

@Override
public PerspectiveModel load(InputStream is) {
return null;
}
};
}

@Test
void testNullKeyUsesDefaultFile(@TempDir File dir) throws Exception {
File defaultFile = new File(dir, "default.xml");
Expand Down Expand Up @@ -78,4 +98,28 @@ void testStoredModelIsLoadedBack(@TempDir File dir) throws Exception {
assertEquals("First perspective", result.getPerspectives()[0].getName());
}

@Test
void testFailedStoreKeepsPreviousFile(@TempDir File dir) throws Exception {
File file = new File(dir, "default.xml");
new FilePersistenceHandler(file).store(null, createModel());
byte[] stored = Files.readAllBytes(file.toPath());
FilePersistenceHandler handler = new FilePersistenceHandler(file, createFailingPersister());

assertThrows(IOException.class, () -> handler.store(null, createModel()));

assertArrayEquals(stored, Files.readAllBytes(file.toPath()));
// No temporary file is left behind
assertArrayEquals(new String[] {"default.xml"}, dir.list());
}

@Test
void testFailedFirstStoreLeavesNoFile(@TempDir File dir) {
FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml"),
createFailingPersister());

assertThrows(IOException.class, () -> handler.store(null, createModel()));

assertEquals(0, dir.list().length);
}

}
1 change: 1 addition & 0 deletions release-notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Updates:

Fixes:
* DockingPathSerializer: read all split nodes of a stored docking path
* FilePersistenceHandler: keep the previous layout file if saving fails
* PerspectiveManager: ignore detached docking ports when finding the main one
* DefaultDockingPort: clear() also clears the nested docking ports
* DockingStateListener: validate pending layout before capturing docking state
Expand Down
Loading