Skip to content

Commit

Permalink
10 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahbedn committed Oct 5, 2024
1 parent cdb5cd5 commit 7690928
Show file tree
Hide file tree
Showing 10 changed files with 907 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/test/java/org/cryptomator/common/CatchingExecutorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.cryptomator.common;

import javafx.concurrent.Task;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.concurrent.*;

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

public class CatchingExecutorsTest {

private CatchingExecutors.CatchingScheduledThreadPoolExecutor scheduledExecutor;
private CatchingExecutors.CatchingThreadPoolExecutor threadPoolExecutor;

@BeforeEach
public void setUp() {
scheduledExecutor = new CatchingExecutors.CatchingScheduledThreadPoolExecutor(1, Executors.defaultThreadFactory());
threadPoolExecutor = new CatchingExecutors.CatchingThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), Executors.defaultThreadFactory());
}

@Test
public void testScheduledExecutor_ScheduleAtFixedRate() throws InterruptedException {
// Arrange
Runnable command = mock(Runnable.class);

// Act
ScheduledFuture<?> future = scheduledExecutor.scheduleAtFixedRate(command, 0, 1, TimeUnit.SECONDS);
Thread.sleep(1500); // Wait for at least one execution

// Assert
verify(command, atLeastOnce()).run();
future.cancel(true);
}

@Test
public void testScheduledExecutor_ScheduleWithFixedDelay() throws InterruptedException {
// Arrange
Runnable command = mock(Runnable.class);

// Act
ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(command, 0, 1, TimeUnit.SECONDS);
Thread.sleep(1500); // Wait for at least one execution

// Assert
verify(command, atLeastOnce()).run();
future.cancel(true);
}

@Test
public void testThreadPoolExecutor_AfterExecute() {
// Arrange
Runnable command = mock(Runnable.class);

// Act
threadPoolExecutor.execute(command);

// Assert
verify(command, timeout(1000)).run();
}

@Test
public void testAfterExecuteInternal_WithException() {
// Arrange
Runnable command = () -> { throw new RuntimeException("Test Exception"); };

// Act & Assert
assertThrows(RuntimeException.class, () -> threadPoolExecutor.execute(command));
}
}
125 changes: 125 additions & 0 deletions src/test/java/org/cryptomator/common/CommonsModuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.cryptomator.common;

import org.cryptomator.common.keychain.KeychainModule;
import org.cryptomator.common.mount.MountModule;
import org.cryptomator.common.settings.Settings;
import org.cryptomator.common.settings.SettingsProvider;
import org.cryptomator.common.vaults.VaultComponent;
import org.cryptomator.common.vaults.VaultListModule;
import org.cryptomator.cryptolib.common.MasterkeyFileAccess;
import org.cryptomator.integrations.revealpath.RevealPathService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Named;
import javax.inject.Singleton;
import java.security.SecureRandom;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

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

public class CommonsModuleTest {

private SettingsProvider settingsProvider;
private ShutdownHook shutdownHook;

@BeforeEach
public void setUp() {
settingsProvider = mock(SettingsProvider.class);
shutdownHook = mock(ShutdownHook.class);
}

@Test
public void testProvideEnvironment() {
// Act
Environment environment = CommonsModule.provideEnvironment();

// Assert
assertNotNull(environment);
}

@Test
public void testProvideLicensePublicKey() {
// Act
String publicKey = CommonsModule.provideLicensePublicKey();

// Assert
assertNotNull(publicKey);
assertFalse(publicKey.isEmpty());
}

@Test
public void testProvideCSPRNG() {
// Act
SecureRandom csprng = CommonsModule.provideCSPRNG();

// Assert
assertNotNull(csprng);
}

@Test
public void testProvideMasterkeyFileAccess() {
// Arrange
SecureRandom csprng = mock(SecureRandom.class);

// Act
MasterkeyFileAccess masterkeyFileAccess = CommonsModule.provideMasterkeyFileAccess(csprng);

// Assert
assertNotNull(masterkeyFileAccess);
}

@Test
public void testProvidesSemVerComparator() {
// Act
Comparator<String> comparator = CommonsModule.providesSemVerComparator();

// Assert
assertNotNull(comparator);
}

@Test
public void testProvideRevealPathService() {
// Act
Optional<RevealPathService> revealPathService = CommonsModule.provideRevealPathService();

// Assert
assertNotNull(revealPathService);
}

@Test
public void testProvideSettings() {
// Arrange
Settings settings = mock(Settings.class);
when(settingsProvider.get()).thenReturn(settings);

// Act
Settings providedSettings = CommonsModule.provideSettings(settingsProvider);

// Assert
assertEquals(settings, providedSettings);
}

@Test
public void testProvideScheduledExecutorService() {
// Act
ScheduledExecutorService executorService = CommonsModule.provideScheduledExecutorService(shutdownHook);

// Assert
assertNotNull(executorService);
}

@Test
public void testProvideExecutorService() {
// Act
ExecutorService executorService = CommonsModule.provideExecutorService(shutdownHook);

// Assert
assertNotNull(executorService);
}
}
85 changes: 85 additions & 0 deletions src/test/java/org/cryptomator/common/LicenseHolderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.cryptomator.common;

import com.auth0.jwt.interfaces.DecodedJWT;
import javafx.beans.property.SimpleStringProperty;
import org.cryptomator.common.settings.Settings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Optional;

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

public class LicenseHolderTest {

private LicenseChecker licenseChecker;
private Settings settings;
private LicenseHolder licenseHolder;

@BeforeEach
public void setUp() {
licenseChecker = mock(LicenseChecker.class);
settings = mock(Settings.class);
settings.licenseKey = new SimpleStringProperty();
licenseHolder = new LicenseHolder(licenseChecker, settings);
}

@Test
public void testValidateAndStoreLicense_ValidLicense() {
// Arrange
String validLicenseKey = "validKey";
DecodedJWT decodedJWT = mock(DecodedJWT.class);
when(licenseChecker.check(validLicenseKey)).thenReturn(Optional.of(decodedJWT));

// Act
boolean result = licenseHolder.validateAndStoreLicense(validLicenseKey);

// Assert
assertTrue(result);
assertEquals(validLicenseKey, settings.licenseKey.get());
assertEquals(Optional.of(decodedJWT.getToken()), licenseHolder.getLicenseKey());
}

@Test
public void testValidateAndStoreLicense_InvalidLicense() {
// Arrange
String invalidLicenseKey = "invalidKey";
when(licenseChecker.check(invalidLicenseKey)).thenReturn(Optional.empty());

// Act
boolean result = licenseHolder.validateAndStoreLicense(invalidLicenseKey);

// Assert
assertFalse(result);
assertNull(settings.licenseKey.get());
assertEquals(Optional.empty(), licenseHolder.getLicenseKey());
}

@Test
public void testLicenseSubjectProperty() {
// Arrange
DecodedJWT decodedJWT = mock(DecodedJWT.class);
when(decodedJWT.getSubject()).thenReturn("subject");
when(licenseChecker.check(anyString())).thenReturn(Optional.of(decodedJWT));
licenseHolder.validateAndStoreLicense("validKey");

// Act
String subject = licenseHolder.getLicenseSubject();

// Assert
assertEquals("subject", subject);
}

@Test
public void testValidLicenseProperty() {
// Arrange
DecodedJWT decodedJWT = mock(DecodedJWT.class);
when(licenseChecker.check(anyString())).thenReturn(Optional.of(decodedJWT));
licenseHolder.validateAndStoreLicense("validKey");

// Act & Assert
assertTrue(licenseHolder.isValidLicense());
}
}
98 changes: 98 additions & 0 deletions src/test/java/org/cryptomator/common/ObservableUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.cryptomator.common;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.function.Function;
import java.util.function.Supplier;

public class ObservableUtilTest {

@Test
public void testMapWithDefault_NullValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>(null);
Function<String, Integer> mapper = String::length;
Integer defaultValue = 0;

// Act
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValue);

// Assert
Assertions.assertEquals(defaultValue, result.getValue());
}

@Test
public void testMapWithDefault_NonNullValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>("test");
Function<String, Integer> mapper = String::length;
Integer defaultValue = 0;

// Act
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValue);

// Assert
Assertions.assertEquals(4, result.getValue());
}

@Test
public void testMapWithDefault_Supplier_NullValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>(null);
Function<String, Integer> mapper = String::length;
Supplier<Integer> defaultValueSupplier = () -> 0;

// Act
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValueSupplier);

// Assert
Assertions.assertEquals(defaultValueSupplier.get(), result.getValue());
}

@Test
public void testMapWithDefault_Supplier_NonNullValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>("test");
Function<String, Integer> mapper = String::length;
Supplier<Integer> defaultValueSupplier = () -> 0;

// Act
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValueSupplier);

// Assert
Assertions.assertEquals(4, result.getValue());
}

@Test
public void testMapWithDefault_ChangeObservableValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>("test");
Function<String, Integer> mapper = String::length;
Integer defaultValue = 0;
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValue);

// Act
observable.set("changed");

// Assert
Assertions.assertEquals(7, result.getValue());
}

@Test
public void testMapWithDefault_Supplier_ChangeObservableValue() {
// Arrange
SimpleObjectProperty<String> observable = new SimpleObjectProperty<>("test");
Function<String, Integer> mapper = String::length;
Supplier<Integer> defaultValueSupplier = () -> 0;
ObservableValue<Integer> result = ObservableUtil.mapWithDefault(observable, mapper, defaultValueSupplier);

// Act
observable.set("changed");

// Assert
Assertions.assertEquals(7, result.getValue());
}
}
Loading

0 comments on commit 7690928

Please sign in to comment.