Skip to content

Commit

Permalink
Fixing javadoc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Sep 5, 2024
1 parent 67dced1 commit 368cc3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
5 changes: 0 additions & 5 deletions modules/test/portal-core-unit-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
weld-junit testing
</description>
<properties>
<!-- Fix for
/home/oliver/git/cui-portal-core/modules/test/portal-core-unit-testing/src/main/java/de/cuioss/portal/core/test/junit5/mockwebserver/MockWebServerExtension.java:26: error: package org.junit.platform.commons.logging is not visible
import org.junit.platform.commons.logging.Logger;
-->
<maven.javadoc.plugin.failOnError>false</maven.javadoc.plugin.failOnError>
<maven.jar.plugin.automatic.module.name>
de.cuioss.portal.core.test
</maven.jar.plugin.automatic.module.name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
package de.cuioss.portal.core.test.junit5;

import de.cuioss.portal.core.test.mocks.configuration.PortalTestConfiguration;
import de.cuioss.tools.logging.CuiLogger;
import de.cuioss.tools.string.Splitter;
import jakarta.enterprise.inject.spi.CDI;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.support.AnnotationSupport;

import static de.cuioss.tools.base.Preconditions.checkArgument;
Expand All @@ -40,40 +39,40 @@
*/
public class PortalTestConfigurationExtension implements BeforeEachCallback {

private static final Logger log = LoggerFactory.getLogger(PortalTestConfigurationExtension.class);
private static final CuiLogger LOGGER = new CuiLogger(PortalTestConfigurationExtension.class);

@Override
public void beforeEach(ExtensionContext context) {
Class<?> testClass = context.getTestClass()
.orElseThrow(() -> new IllegalStateException("Unable to determine Test-class"));
log.debug(() -> "Processing test-class " + testClass);
.orElseThrow(() -> new IllegalStateException("Unable to determine Test-class"));
LOGGER.debug(() -> "Processing test-class " + testClass);

CDI<Object> cdi;
try {
cdi = CDI.current();
} catch (IllegalStateException e) {
throw new IllegalStateException("""
CDI not present, change the order of annotation and put @EnableAutoWeld above \
@EnablePortalConfiguration\
""", e);
CDI not present, change the order of annotation and put @EnableAutoWeld above \
@EnablePortalConfiguration\
""", e);
}

var configuration = cdi.select(PortalTestConfiguration.class).get();
log.debug(() -> "Resolved " + configuration);
LOGGER.debug(() -> "Resolved " + configuration);

configuration.clear();

var annotation = AnnotationSupport.findAnnotation(testClass, EnablePortalConfiguration.class);
if (annotation.isPresent()) {
log.debug(() -> "Resolved annotation " + annotation.get());
LOGGER.debug(() -> "Resolved annotation " + annotation.get());
for (String element : annotation.get().configuration()) {
var splitted = Splitter.on(':').splitToList(element);
checkArgument(2 <= splitted.size(), "Expected element in the form key:value, but was " + element);
log.debug(() -> "Adding configuration entry: " + element);
LOGGER.debug(() -> "Adding configuration entry: " + element);
configuration.update(splitted.get(0), element.substring(element.indexOf(':') + 1));
}
}

log.debug(() -> "Finished processing instance " + testClass);
LOGGER.debug(() -> "Finished processing instance " + testClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
*/
package de.cuioss.portal.core.test.junit5.mockwebserver;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.util.Optional;

import de.cuioss.tools.logging.CuiLogger;
import mockwebserver3.MockWebServer;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.support.AnnotationSupport;

import mockwebserver3.MockWebServer;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

/**
* Handle the lifetime of an instance of {@link MockWebServer}, see
Expand All @@ -37,30 +35,30 @@
*/
public class MockWebServerExtension implements TestInstancePostProcessor, AfterEachCallback {

private static final Logger log = LoggerFactory.getLogger(MockWebServerExtension.class);
private static final CuiLogger LOGGER = new CuiLogger(MockWebServerExtension.class);

/**
* Identifies the {@link Namespace} under which the concrete instance of
* {@link MockWebServer} is stored.
*/
public static final Namespace NAMESPACE = Namespace.create("test", "portal", "MockWebServer");

@SuppressWarnings({ "squid:S2095" }) // owolff: Will be closed after all tests
@SuppressWarnings({"squid:S2095"}) // owolff: Will be closed after all tests
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {

var mockWebServer = new MockWebServer();

assertInstanceOf(MockWebServerHolder.class, testInstance, "In order to use within a test the test-class must implement de.cuioss.portal.core.test.junit5.mockwebserver.MockWebServerHolder "
+ testInstance);
+ testInstance);

var holder = (MockWebServerHolder) testInstance;
holder.setMockWebServer(mockWebServer);
Optional.ofNullable(holder.getDispatcher()).ifPresent(mockWebServer::setDispatcher);
if (extractAnnotation(testInstance.getClass()).map(annotation -> !annotation.manualStart())
.orElse(Boolean.FALSE)) {
mockWebServer.start();
log.info(() -> "Started MockWebServer at " + mockWebServer.url("/"));
LOGGER.info(() -> "Started MockWebServer at " + mockWebServer.url("/"));
}
put(mockWebServer, context);
}
Expand All @@ -69,10 +67,10 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
public void afterEach(ExtensionContext context) throws Exception {
var server = get(context);
if (server.isPresent()) {
log.info(() -> "Shutting down MockWebServer at " + server.get().url("/"));
LOGGER.info(() -> "Shutting down MockWebServer at " + server.get().url("/"));
server.get().shutdown();
} else {
log.error(() -> "Server not present, therefore can not be shutdown");
LOGGER.error(() -> "Server not present, therefore can not be shutdown");
}

}
Expand Down

0 comments on commit 368cc3f

Please sign in to comment.