mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-28 15:40:20 +00:00
replace GSON from settings
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Skymatic UG (haftungsbeschränkt).
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the accompanying LICENSE file.
|
||||
*******************************************************************************/
|
||||
package org.cryptomator.common.settings;
|
||||
|
||||
import org.cryptomator.common.Environment;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SettingsJsonAdapterTest {
|
||||
|
||||
private final Environment env = Mockito.mock(Environment.class);
|
||||
private final SettingsJsonAdapter adapter = new SettingsJsonAdapter(env);
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws IOException {
|
||||
String json = """
|
||||
{
|
||||
"directories": [
|
||||
{"id": "1", "path": "/vault1", "mountName": "vault1", "winDriveLetter": "X"},
|
||||
{"id": "2", "path": "/vault2", "mountName": "vault2", "winDriveLetter": "Y"}
|
||||
],
|
||||
"autoCloseVaults" : true,
|
||||
"checkForUpdatesEnabled": true,
|
||||
"port": 8080,
|
||||
"language": "de-DE",
|
||||
"numTrayNotifications": 42
|
||||
}
|
||||
""";
|
||||
|
||||
Settings settings = adapter.fromJson(json);
|
||||
|
||||
Assertions.assertTrue(settings.checkForUpdates().get());
|
||||
Assertions.assertEquals(2, settings.getDirectories().size());
|
||||
Assertions.assertEquals(8080, settings.port().get());
|
||||
Assertions.assertEquals(true, settings.autoCloseVaults().get());
|
||||
Assertions.assertEquals("de-DE", settings.languageProperty().get());
|
||||
Assertions.assertEquals(42, settings.numTrayNotifications().get());
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
@ParameterizedTest(name = "fromJson() should throw IOException for input: {0}")
|
||||
@ValueSource(strings = { //
|
||||
"", //
|
||||
"<html>", //
|
||||
"{invalidjson}" //
|
||||
})
|
||||
public void testDeserializeMalformed(String input) {
|
||||
Assertions.assertThrows(IOException.class, () -> {
|
||||
adapter.fromJson(input);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.cryptomator.common.settings;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
public class SettingsJsonTest {
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws IOException {
|
||||
String jsonStr = """
|
||||
{
|
||||
"directories": [
|
||||
{"id": "1", "path": "/vault1", "mountName": "vault1", "winDriveLetter": "X", "shouldBeIgnored": true},
|
||||
{"id": "2", "path": "/vault2", "mountName": "vault2", "winDriveLetter": "Y", "mountFlags":"--foo --bar"}
|
||||
],
|
||||
"autoCloseVaults" : true,
|
||||
"checkForUpdatesEnabled": true,
|
||||
"port": 8080,
|
||||
"language": "de-DE",
|
||||
"numTrayNotifications": 42
|
||||
}
|
||||
""";
|
||||
|
||||
var jsonObj = new ObjectMapper().reader().readValue(jsonStr, SettingsJson.class);
|
||||
|
||||
Assertions.assertTrue(jsonObj.checkForUpdatesEnabled);
|
||||
Assertions.assertEquals(2, jsonObj.directories.size());
|
||||
Assertions.assertEquals("/vault1", jsonObj.directories.get(0).path);
|
||||
Assertions.assertEquals("/vault2", jsonObj.directories.get(1).path);
|
||||
Assertions.assertEquals("--foo --bar", jsonObj.directories.get(1).mountFlags);
|
||||
Assertions.assertEquals(8080, jsonObj.port);
|
||||
Assertions.assertTrue(jsonObj.autoCloseVaults);
|
||||
Assertions.assertEquals("de-DE", jsonObj.language);
|
||||
Assertions.assertEquals(42, jsonObj.numTrayNotifications);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
@ParameterizedTest(name = "throw JacksonException for input: {0}")
|
||||
@ValueSource(strings = { //
|
||||
"", //
|
||||
"<html>", //
|
||||
"{invalidjson}" //
|
||||
})
|
||||
public void testDeserializeMalformed(String input) {
|
||||
var objectMapper = new ObjectMapper().reader();
|
||||
|
||||
Assertions.assertThrows(JacksonException.class, () -> {
|
||||
objectMapper.readValue(input, SettingsJson.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize() throws JsonProcessingException {
|
||||
var jsonObj = new SettingsJson();
|
||||
jsonObj.directories = List.of(new VaultSettingsJson(), new VaultSettingsJson());
|
||||
jsonObj.directories.get(0).id = "test";
|
||||
jsonObj.theme = UiTheme.DARK;
|
||||
jsonObj.showTrayIcon = false;
|
||||
|
||||
var jsonStr = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);
|
||||
|
||||
MatcherAssert.assertThat(jsonStr, containsString("\"theme\" : \"DARK\""));
|
||||
MatcherAssert.assertThat(jsonStr, containsString("\"showTrayIcon\" : false"));
|
||||
MatcherAssert.assertThat(jsonStr, containsString("\"useKeychain\" : true"));
|
||||
MatcherAssert.assertThat(jsonStr, containsString("\"actionAfterUnlock\" : \"ASK\""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public class SettingsTest {
|
||||
Environment env = Mockito.mock(Environment.class);
|
||||
@SuppressWarnings("unchecked") Consumer<Settings> changeListener = Mockito.mock(Consumer.class);
|
||||
|
||||
Settings settings = new Settings(env);
|
||||
Settings settings = Settings.create(env);
|
||||
settings.setSaveCmd(changeListener);
|
||||
VaultSettings vaultSettings = VaultSettings.withRandomId();
|
||||
Mockito.verify(changeListener, Mockito.times(0)).accept(settings);
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Skymatic UG (haftungsbeschränkt).
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the accompanying LICENSE file.
|
||||
*******************************************************************************/
|
||||
package org.cryptomator.common.settings;
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class VaultSettingsJsonAdapterTest {
|
||||
|
||||
private final VaultSettingsJsonAdapter adapter = new VaultSettingsJsonAdapter();
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws IOException {
|
||||
String json = "{\"id\": \"foo\", \"path\": \"/foo/bar\", \"displayName\": \"test\", \"winDriveLetter\": \"X\", \"shouldBeIgnored\": true, \"individualMountPath\": \"/home/test/crypto\", \"mountFlags\":\"--foo --bar\"}";
|
||||
JsonReader jsonReader = new JsonReader(new StringReader(json));
|
||||
|
||||
VaultSettings vaultSettings = adapter.read(jsonReader);
|
||||
|
||||
assertAll(
|
||||
() -> assertEquals("foo", vaultSettings.getId()),
|
||||
() -> assertEquals(Paths.get("/foo/bar"), vaultSettings.path().get()),
|
||||
() -> assertEquals("test", vaultSettings.displayName().get()),
|
||||
() -> assertEquals("--foo --bar", vaultSettings.mountFlags().get())
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
@Test
|
||||
public void testSerialize() throws IOException {
|
||||
VaultSettings vaultSettings = new VaultSettings("test");
|
||||
vaultSettings.path().set(Paths.get("/foo/bar"));
|
||||
vaultSettings.displayName().set("mountyMcMountFace");
|
||||
vaultSettings.mountFlags().set("--foo --bar");
|
||||
|
||||
StringWriter buf = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(buf);
|
||||
adapter.write(jsonWriter, vaultSettings);
|
||||
String result = buf.toString();
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(result, containsString("\"id\":\"test\"")),
|
||||
() -> {
|
||||
if (System.getProperty("os.name").contains("Windows")) {
|
||||
assertThat(result, containsString("\"path\":\"\\\\foo\\\\bar\""));
|
||||
} else {
|
||||
assertThat(result, containsString("\"path\":\"/foo/bar\""));
|
||||
}
|
||||
},
|
||||
() -> assertThat(result, containsString("\"displayName\":\"mountyMcMountFace\"")),
|
||||
() -> assertThat(result, containsString("\"mountFlags\":\"--foo --bar\""))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user