Skip to content

Commit

Permalink
Add IT test and fix last bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jul 15, 2024
1 parent b16aa3d commit 08f07d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/main/java/org/cryptomator/linux/quickaccess/DolphinPlaces.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,18 @@ public QuickAccessService.QuickAccessEntry add(Path target, String displayName)
if (Files.size(PLACES_FILE) > MAX_FILE_SIZE) {
throw new IOException("File %s exceeds size of %d bytes".formatted(PLACES_FILE, MAX_FILE_SIZE));
}

var placesContent = Files.readString(PLACES_FILE);

//validate
xmlValidator.validate(new StreamSource(new StringReader(placesContent)));

// modify
int insertIndex = placesContent.lastIndexOf("</xbel>"); //cannot be -1 due to validation
try (var writer = Files.newBufferedWriter(TMP_FILE, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
writer.write(placesContent, 0, insertIndex);
writer.newLine();
writer.write(ENTRY_TEMPLATE.formatted(target.toUri(), displayName, id));
writer.write(ENTRY_TEMPLATE.formatted(target.toUri(), displayName, id).indent(1));
writer.newLine();
writer.write(placesContent, insertIndex, placesContent.length() - insertIndex);
}

// save
Files.move(TMP_FILE, PLACES_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
return new DolphinPlacesEntry(id);
Expand Down Expand Up @@ -113,20 +109,17 @@ public void remove() throws QuickAccessServiceException {
if (Files.size(PLACES_FILE) > MAX_FILE_SIZE) {
throw new IOException("File %s exceeds size of %d bytes".formatted(PLACES_FILE, MAX_FILE_SIZE));
}

var placesContent = Files.readString(PLACES_FILE);
int idIndex = placesContent.lastIndexOf(id);
if (idIndex == -1) {
isRemoved = true;
return; //we assume someone has removed our entry
}

//validate
xmlValidator.validate(new StreamSource(new StringReader(placesContent)));

//modify
var placesContentPart1 = placesContent.substring(0, idIndex);
int openingTagIndex = placesContentPart1.lastIndexOf("<bookmark");
int openingTagIndex = placesContentPart1.lastIndexOf("<bookmark href=");
var contentToWrite1 = placesContentPart1.substring(0, openingTagIndex).stripTrailing();

int closingTagIndex = placesContent.indexOf("</bookmark>", idIndex);
Expand All @@ -138,8 +131,8 @@ public void remove() throws QuickAccessServiceException {
writer.newLine();
writer.write(contentToWrite2);
}
// save
Files.move(TMP_FILE, PLACES_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);

isRemoved = true;
} catch (IOException | SAXException e) {
throw new QuickAccessServiceException("Removing entry from KDE places file failed.", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cryptomator.linux.quickaccess;

import org.cryptomator.integrations.quickaccess.QuickAccessServiceException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;
import java.time.Duration;

public class DolphinPlacesIT {

@Test
@DisplayName("Adds for 20s an entry to the Dolphin sidebar")
@Disabled
public void testSidebarIntegration(@TempDir Path tmpdir) throws QuickAccessServiceException, InterruptedException {
var entry = new DolphinPlaces().add(tmpdir, "integrations-linux");
Thread.sleep(Duration.ofSeconds(20));
entry.remove();
}
}

0 comments on commit 08f07d5

Please sign in to comment.