Skip to content

Commit

Permalink
4.x: Global Service Registry support in tests (#9656)
Browse files Browse the repository at this point in the history
* Refactoring of global service registry to use Helidon Context to store the value.

* Testing support for Service registry.
This change ensures that all tests are run in a specific "common Context" that holds the current global `ServiceRegistry`.
This way each test class has its own global `ServiceRegistry` instance that does not interfere with other test classes.
  • Loading branch information
tomas-langer authored Jan 22, 2025
1 parent c09a47a commit 643a742
Show file tree
Hide file tree
Showing 42 changed files with 1,980 additions and 318 deletions.
8 changes: 8 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@
<groupId>io.helidon.common.processor</groupId>
<artifactId>helidon-common-processor-class-model</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.testing</groupId>
<artifactId>helidon-testing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.testing</groupId>
<artifactId>helidon-testing-junit5</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common.testing</groupId>
<artifactId>helidon-common-testing-junit5</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@
<artifactId>helidon-common-processor-class-model</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.testing</groupId>
<artifactId>helidon-testing</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.testing</groupId>
<artifactId>helidon-testing-junit5</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common.testing</groupId>
<artifactId>helidon-common-testing-junit5</artifactId>
Expand Down
266 changes: 266 additions & 0 deletions common/common/src/main/java/io/helidon/common/Functions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* 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 io.helidon.common;

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

/**
* Functional interfaces required to complement the {@link java.lang.Runnable}, {@link java.util.concurrent.Callable},
* and {@link java.util.function.Supplier} to help with checked exceptions in lambdas.
*/
public final class Functions {
private Functions() {
}

/**
* Wrap a {@link CheckedSupplier} into a {@link java.util.function.Supplier}.
*
* @param supplier checked supplier
* @param <T> supplier type
* @param <E> checked exception type
* @return Supplier
*/
public static <T, E extends Throwable> Supplier<T> unchecked(CheckedSupplier<T, E> supplier) {
return () -> {
try {
return supplier.get();
} catch (RuntimeException ex) {
throw ex;
} catch (Throwable ex) {
throw new UncheckedException(ex);
}
};
}

/**
* Wrap a {@link CheckedRunnable} into a {@link Runnable}.
*
* @param runnable checked runnable
* @param <E> checked exception type
* @return Consumer
*/
public static <E extends Throwable> Runnable unchecked(CheckedRunnable<E> runnable) {
return () -> {
try {
runnable.run();
} catch (RuntimeException | Error ex) {
throw ex;
} catch (Throwable ex) {
throw new UncheckedException(ex);
}
};
}

/**
* Wrap a {@link CheckedConsumer} into a {@link java.util.function.Consumer}.
*
* @param consumer checked consumer
* @param <T> item type
* @param <E> checked exception type
* @return Consumer
*/
public static <T, E extends Throwable> Consumer<T> unchecked(CheckedConsumer<T, E> consumer) {
return t -> {
try {
consumer.accept(t);
} catch (RuntimeException | Error ex) {
throw ex;
} catch (Throwable ex) {
throw new UncheckedException(ex);
}
};
}

/**
* Wrap a {@link CheckedBiConsumer} into a {@link java.util.function.BiConsumer}.
*
* @param consumer checked consumer
* @param <T> 1st item type
* @param <U> 2nd item type
* @param <E> checked exception type
* @return BiConsumer
*/
public static <T, U, E extends Throwable> BiConsumer<T, U> unchecked(CheckedBiConsumer<T, U, E> consumer) {
return (t, u) -> {
try {
consumer.accept(t, u);
} catch (RuntimeException | Error ex) {
throw ex;
} catch (Throwable ex) {
throw new UncheckedException(ex);
}
};
}

/**
* Wrap a {@link CheckedBiConsumer} into a {@link BiConsumer}.
*
* @param function checked function
* @param <T> 1st item type
* @param <U> 2nd item type
* @param <E> checked exception type
* @return Function
*/
public static <T, U, E extends Throwable> Function<T, U> unchecked(CheckedFunction<T, U, E> function) {
return t -> {
try {
return function.apply(t);
} catch (RuntimeException | Error ex) {
throw ex;
} catch (Throwable ex) {
throw new UncheckedException(ex);
}
};
}

/**
* Wrap an exception wrapped with {@link UncheckedException} if checked.
*
* @param ex exception to wrap
* @return exception
*/
public static RuntimeException wrap(Throwable ex) {
if (ex instanceof RuntimeException runtimeException) {
return runtimeException;
}
return new UncheckedException(ex);
}

/**
* Unwrap a checked exception wrapped with {@link UncheckedException}.
*
* @param ex exception to unwrap
* @return exception
*/
public static Throwable unwrap(Throwable ex) {
if (ex instanceof UncheckedException) {
return ex.getCause();
}
return ex;
}

/**
* Checked consumer.
*
* @param <T> item type
* @param <E> checked exception type
*/
@FunctionalInterface
public interface CheckedConsumer<T, E extends Throwable> {
/**
* Accept an item.
*
* @param item item
* @throws E if an error occurs
*/
void accept(T item) throws E;
}

/**
* Checked bi-consumer.
*
* @param <T> 1st item type
* @param <U> 2nd item type
* @param <E> checked exception type
*/
@FunctionalInterface
public interface CheckedBiConsumer<T, U, E extends Throwable> {

/**
* Accept an item.
*
* @param item1 1st item
* @param item2 2nd item
* @throws E if an error occurs
*/
void accept(T item1, U item2) throws E;
}

/**
* Checked consumer.
*
* @param <T> input type
* @param <U> output type
* @param <E> checked exception type
*/
@FunctionalInterface
public interface CheckedFunction<T, U, E extends Throwable> {

/**
* Accept an item.
*
* @param item input item
* @return U
* @throws E if an error occurs
*/
U apply(T item) throws E;
}

/**
* Checked runnable.
*
* @param <E> checked exception type
*/
@FunctionalInterface
public interface CheckedRunnable<E extends Throwable> {

/**
* Run.
*
* @throws E if an error occurs
*/
void run() throws E;
}

/**
* Checked supplier.
*
* @param <T> supplier type
* @param <E> checked exception type
*/
@FunctionalInterface
public interface CheckedSupplier<T, E extends Throwable> {

/**
* Get the value.
*
* @return T value
* @throws E if an error occurs
*/
T get() throws E;
}

/**
* Unchecked exception.
*
* @see #unwrap(Throwable)
*/
public static class UncheckedException extends RuntimeException {

/**
* Create a new unchecked exception.
*
* @param cause cause
*/
public UncheckedException(Throwable cause) {
super(cause);
}
}
}
Loading

0 comments on commit 643a742

Please sign in to comment.