Skip to content

Commit

Permalink
Adding tests to ServiceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
LikeTheSalad committed Nov 15, 2023
1 parent e46fb95 commit b7d3993
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -16,7 +17,8 @@ public final class ServiceManager implements Lifecycle {
private final Map<Class<? extends Service>, Service> services = new HashMap<>();
@Nullable private static ServiceManager instance;

private ServiceManager() {}
@VisibleForTesting
ServiceManager() {}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public static void initialize(Context appContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.internal.services;

import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ServiceManagerTest {

private ServiceManager serviceManager;

@BeforeEach
void setUp() {
serviceManager = new ServiceManager();
}

@Test
void addingServices() {
FirstService firstService = mock();
SecondService secondService = mock();

serviceManager.addService(firstService);
serviceManager.addService(secondService);

Assertions.assertEquals(firstService, serviceManager.getService(FirstService.class));
Assertions.assertEquals(secondService, serviceManager.getService(SecondService.class));
}

@Test
void allowOnlyOneServicePerType() {
FirstService firstService = mock();
FirstService anotherFirstService = mock();

serviceManager.addService(firstService);

try {
serviceManager.addService(anotherFirstService);
fail();
} catch (IllegalArgumentException ignored) {
}
}

@Test
void delegatingStartCall() {
FirstService firstService = mock();
SecondService secondService = mock();
serviceManager.addService(firstService);
serviceManager.addService(secondService);

serviceManager.start();

verify(firstService).start();
verify(secondService).start();
}

@Test
void delegatingStopCall() {
FirstService firstService = mock();
SecondService secondService = mock();
serviceManager.addService(firstService);
serviceManager.addService(secondService);

serviceManager.stop();

verify(firstService).stop();
verify(secondService).stop();
}

private static class FirstService implements Service {}

private static class SecondService implements Service {}
}

0 comments on commit b7d3993

Please sign in to comment.