Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
61aa368
chore: added unit test coverage
nathanklick Aug 3, 2023
2526362
@DisplayName("CtorService: Locator's iterator removes entry")
jeromy-cannon Aug 3, 2023
74a4e4a
testWrapperAsPrimitiveClass
jeromy-cannon Aug 4, 2023
1588bea
testWrapperAsPrimitiveClass (default)
jeromy-cannon Aug 4, 2023
a95b368
testPrimitiveAsWrapperClass
jeromy-cannon Aug 4, 2023
bed415d
testLoad
jeromy-cannon Aug 4, 2023
26aadf4
testLoadIOException
jeromy-cannon Aug 4, 2023
aaf2d8f
test load directory with no modules
jeromy-cannon Aug 7, 2023
199355b
logbackDynamicServiceRecursiveFolderLoading
jeromy-cannon Aug 7, 2023
39704b9
logbackDynamicServiceLoadingWithNoPaths
jeromy-cannon Aug 7, 2023
be45381
spotlessApply
jeromy-cannon Aug 7, 2023
284c295
call with empty parent to get more coverage
jeromy-cannon Aug 7, 2023
65ce21c
coverage for a jar with no manifest and no module descriptor
jeromy-cannon Aug 7, 2023
94d2bc6
test key value pair equals and hash code, fix equals comparison
jeromy-cannon Aug 7, 2023
3b99a6b
Merge branch 'main' into 00232-SL-Add-Coverage
jeromy-cannon Aug 7, 2023
f187743
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
3c62ada
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
1b73fc0
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
cb9921f
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
4c2d0d7
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
026cac8
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
09b3808
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
1aa284b
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
00fb85a
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
7f607a3
Update fullstack-base-api/src/test/java/com/hedera/fullstack/base/api…
jeromy-cannon Aug 7, 2023
6573c11
updated based on PR feedback
jeromy-cannon Aug 7, 2023
a5fef76
Merge branch 'main' into 00232-SL-Add-Coverage
jeromy-cannon Aug 7, 2023
ecf9bb0
removed testImplementation(gav("com.jcovalent.junit.logging"))
jeromy-cannon Aug 7, 2023
943607d
Update fullstack-service-locator/build.gradle.kts
jeromy-cannon Aug 7, 2023
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 @@ -47,7 +47,7 @@ public static <K, V> KeyValuePair<K, V> of(final K key, final V value) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof KeyValuePair<?, ?> that)) return false;
return Objects.equals(key, that.key);
return Objects.equals(key, that.key) && Objects.equals(value, that.value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.base.api.test.collections;

import static org.assertj.core.api.Assertions.assertThat;

import com.hedera.fullstack.base.api.collections.KeyValuePair;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class KeyValuePairTest {
@Test
@DisplayName("Test KeyValuePair")
void testKeyValuePair() {
KeyValuePair<String, String> kvp1 = new KeyValuePair<>("key", "value");
KeyValuePair<String, String> kvp1v2 = new KeyValuePair<>("key", "value2");
assertThat(kvp1).isNotEqualTo(kvp1v2);
assertThat(kvp1.hashCode()).isEqualTo(kvp1v2.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.base.api.test.reflect;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import com.hedera.fullstack.base.api.reflect.ReflectionUtils;
import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class ReflectionUtilsTest {
static Stream<Arguments> primitiveAndWrapperSupplier() {
return Stream.of(
arguments(named("void.class", void.class), named("Void.class", Void.class)),
arguments(named("boolean.class", boolean.class), named("Boolean.class", Boolean.class)),
arguments(named("byte.class", byte.class), named("Byte.class", Byte.class)),
arguments(named("char.class", char.class), named("Character.class", Character.class)),
arguments(named("short.class", short.class), named("Short.class", Short.class)),
arguments(named("int.class", int.class), named("Integer.class", Integer.class)),
arguments(named("long.class", long.class), named("Long.class", Long.class)),
arguments(named("float.class", float.class), named("Float.class", Float.class)),
arguments(named("double.class", double.class), named("Double.class", Double.class)),
arguments(named("String.class", String.class), named("String.class", String.class)));
}

@ParameterizedTest(name = "Validate wrapper for {0}")
@MethodSource("primitiveAndWrapperSupplier")
@DisplayName("Test primitive as wrapper class")
void testPrimitiveAsWrapperClass(Class<?> primitiveClass, Class<?> wrapperClass) {
Class<?> result = ReflectionUtils.primitiveAsWrapperClass(primitiveClass);
assertThat(result).isEqualTo(wrapperClass);
}

@ParameterizedTest(name = "Validate primitive for {1}")
@MethodSource("primitiveAndWrapperSupplier")
@DisplayName("Test wrapper as primitive class")
void testWrapperAsPrimitiveClass(Class<?> primitiveClass, Class<?> wrapperClass) {
Class<?> result = ReflectionUtils.wrapperAsPrimitiveClass(wrapperClass);
assertThat(result).isEqualTo(primitiveClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.base.api.test.resource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.hedera.fullstack.base.api.resource.ResourceLoader;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class ResourceLoaderTest {
@Test
@DisplayName("Verify load works as expected")
void testLoad() throws IOException {
ResourceLoader<ResourceLoaderTest> resourceLoader = new ResourceLoader<>(ResourceLoaderTest.class);
Path resourcePath = resourceLoader.load("resource.txt");
assertThat(resourcePath)
.exists()
.isRegularFile()
.hasFileName("resource.txt")
.isWritable()
.isReadable()
.isExecutable();
}

@Test
@DisplayName("Verify load fails when called with a non-existent file")
void testLoadIOException() {
ResourceLoader<ResourceLoaderTest> resourceLoader = new ResourceLoader<>(ResourceLoaderTest.class);
assertThatThrownBy(() -> resourceLoader.load("not-resource.txt")).isInstanceOf(IOException.class);
}
}
1 change: 1 addition & 0 deletions fullstack-base-api/src/test/resources/resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some text for testing purposes.
2 changes: 1 addition & 1 deletion fullstack-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies.constraints {
api("org.slf4j:slf4j-api:2.0.7")
api("org.slf4j:slf4j-nop:2.0.7")
api("org.slf4j:slf4j-simple:2.0.7")
api("com.jcovalent.junit:jcovalent-junit-logging:0.2.0")
api("com.jcovalent.junit:jcovalent-junit-logging:0.3.0")
api("io.github.classgraph:classgraph:4.8.161")

for (p in rootProject.childProjects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private void loadModules(final ModuleLayer parentLayer) {
final ModuleFinder finder = ModuleFinder.of(modulePath.toArray(new Path[0]));
try {
final Configuration cfg =
parentLayer.configuration().resolve(finder, ModuleFinder.of(), Collections.emptySet());
parentLayer.configuration().resolveAndBind(finder, ModuleFinder.of(), Collections.emptySet());
moduleLayer = parentLayer.defineModulesWithOneLoader(cfg, classLoader);
} catch (LayerInstantiationException | SecurityException e) {
LOGGER.atError().setCause(e).log("Failed to instantiate module layer, unable to load module path entries");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,28 @@
package com.hedera.fullstack.service.locator.test.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.hedera.fullstack.base.api.resource.ResourceLoader;
import com.hedera.fullstack.service.locator.api.ArtifactLoader;
import com.hedera.fullstack.service.locator.api.ServiceLocator;
import com.hedera.fullstack.service.locator.test.mock.MockSlf4jLocator;
import com.jcovalent.junit.logging.JCovalentLoggingSupport;
import com.jcovalent.junit.logging.LogEntryBuilder;
import com.jcovalent.junit.logging.LoggingOutput;
import com.jcovalent.junit.logging.assertj.LoggingOutputAssert;
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.event.Level;
import org.slf4j.spi.SLF4JServiceProvider;

@DisplayName("Artifact Loader")
@JCovalentLoggingSupport
class ArtifactLoaderTest {

private static final ResourceLoader<ArtifactLoaderTest> RESOURCE_LOADER =
Expand All @@ -42,7 +53,7 @@ static void setup() throws IOException {
}

@Test
@DisplayName("Logback: Artifacts dynamically loaded")
@DisplayName("Logback: Artifacts dynamically loaded successfully")
void logbackDynamicLoading() {
final ArtifactLoader artifactLoader = ArtifactLoader.from(JAR_PATH);
assertThat(artifactLoader).isNotNull();
Expand All @@ -51,4 +62,51 @@ void logbackDynamicLoading() {
assertThat(artifactLoader.classPath()).isNotEmpty();
assertThat(artifactLoader.modulePath()).isNotEmpty();
}

@Test
@DisplayName("Logback: Artifacts dynamically loaded can be used by Service Loader")
void logbackDynamicServiceLoading() {
final ArtifactLoader artifactLoader = ArtifactLoader.from(JAR_PATH);
assertServiceLocatorLoadedCorrectly(artifactLoader);
}

@Test
@DisplayName("Logback: Artifacts dynamically loaded with an empty directory")
void logbackDynamicServiceLoadingEmptyDirectory(final LoggingOutput loggingOutput) {
final ArtifactLoader artifactLoader = ArtifactLoader.from(Path.of("no-modules"));
assertThat(artifactLoader).isNotNull();
LoggingOutputAssert.assertThat(loggingOutput)
.hasAtLeastOneEntry(List.of(LogEntryBuilder.builder()
.level(Level.DEBUG)
.message("No module path entries found, skipping module layer creation")
.build()));
}

@Test
@DisplayName("Logback: Artifacts dynamically recursive folder loaded can be used by Service Loader")
void logbackDynamicServiceRecursiveFolderLoading() {
final ArtifactLoader artifactLoader = ArtifactLoader.from(true, Path.of("."));
assertServiceLocatorLoadedCorrectly(artifactLoader);
}

private void assertServiceLocatorLoadedCorrectly(ArtifactLoader artifactLoader) {
assertThat(artifactLoader).isNotNull();

final ServiceLocator<SLF4JServiceProvider> serviceLocator = MockSlf4jLocator.create(artifactLoader);
assertThat(serviceLocator).isNotNull();

final SLF4JServiceProvider serviceProvider = serviceLocator.findFirst().orElseThrow();
assertThat(serviceProvider)
.isNotNull()
.extracting(Object::getClass)
.extracting(Class::getName)
.isEqualTo("ch.qos.logback.classic.spi.LogbackServiceProvider");
}

@Test
@DisplayName("Logback: Artifacts load fails with no paths")
void logbackDynamicServiceLoadingWithNoPaths(final LoggingOutput loggingOutput) {
Path[] emptyPaths = new Path[0];
assertThatThrownBy(() -> ArtifactLoader.from(null, emptyPaths)).isInstanceOf(IllegalArgumentException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package com.hedera.fullstack.service.locator.test.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.hedera.fullstack.service.locator.api.ServiceLocator;
import com.hedera.fullstack.service.locator.api.ServiceSupplier;
import com.hedera.fullstack.service.locator.test.mock.CtorService;
import com.hedera.fullstack.service.locator.test.mock.MockCtorService;
import com.hedera.fullstack.service.locator.test.mock.MockLocator;
import java.util.Iterator;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -75,4 +78,14 @@ void locatorHasWorkingReloadSupport() {
locator.reload();
assertThat(locator.stream().count()).isEqualTo(2);
}

@Test
@DisplayName("CtorService: Locator's iterator removes entry")
void locatorIteratorRemovesEntry() {
final ServiceLocator<CtorService> locator = MockLocator.create();
assertThat(locator).isNotNull();
assertThat(locator.stream().count()).isEqualTo(2);
Iterator<ServiceSupplier<CtorService>> iterator = locator.iterator();
assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.fullstack.service.locator.test.mock;

import com.hedera.fullstack.service.locator.api.ArtifactLoader;
import com.hedera.fullstack.service.locator.api.ServiceLocator;
import java.util.ServiceLoader;
import org.slf4j.spi.SLF4JServiceProvider;
Expand All @@ -28,4 +29,8 @@ private MockSlf4jLocator(ServiceLoader<SLF4JServiceProvider> serviceLoader) {
public static ServiceLocator<SLF4JServiceProvider> create() {
return new MockSlf4jLocator(ServiceLoader.load(SLF4JServiceProvider.class));
}

public static ServiceLocator<SLF4JServiceProvider> create(final ArtifactLoader loader) {
return new MockSlf4jLocator(ServiceLoader.load(loader.moduleLayer(), SLF4JServiceProvider.class));
}
}
1 change: 1 addition & 0 deletions fullstack-service-locator/src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

requires com.hedera.fullstack.base.api;
requires com.hedera.fullstack.service.locator;
requires com.jcovalent.junit.logging;
requires org.assertj.core;
requires org.junit.jupiter.api;
requires org.slf4j;
Expand Down
Binary file not shown.
Empty file.