Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -87,12 +87,10 @@ public class BaseComponentsHandler implements ComponentsHandler {
protected static final Local<State> STATE = loadStateHolder();

private static Local<State> loadStateHolder() {
switch (System.getProperty("talend.component.junit.handler.state", "thread").toLowerCase(ROOT)) {
case "static":
return new Local.StaticImpl<>();
default:
return new Local.ThreadLocalImpl<>();
}
final String handlerState = System.getProperty("talend.component.junit.handler.state", "thread");
return "static".equals(handlerState.toLowerCase(ROOT))
? new Local.StaticImpl<>()
: new Local.ThreadLocalImpl<>();
}

private final ThreadLocal<PreState> initState = ThreadLocal.withInitial(PreState::new);
Expand All @@ -111,13 +109,14 @@ public <T> T injectServices(final T instance) {
.orElseThrow(() -> new IllegalArgumentException("cant find plugin '" + plugin + "'"))
.get(ComponentManager.AllServices.class)
.getServices();
Injector.class.cast(services.get(Injector.class)).inject(instance);
((Injector) services.get(Injector.class)).inject(instance);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intellij asked me to do so

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless change

return instance;
}

public BaseComponentsHandler withIsolatedPackage(final String packageName, final String... packages) {
isolatedPackages =
Stream.concat(Stream.of(packageName), Stream.of(packages)).filter(Objects::nonNull).collect(toList());
isolatedPackages = Stream.concat(Stream.of(packageName), Stream.of(packages))
.filter(Objects::nonNull)
.collect(toList());
if (isolatedPackages.isEmpty()) {
isolatedPackages = null;
}
Expand All @@ -140,14 +139,10 @@ protected boolean isContainerClass(final Filter filter, final String name) {
public void close() {
try {
final State state = STATE.get();
if (state.jsonb != null) {
try {
state.jsonb.close();
} catch (final Exception e) {
// no-op: not important
}
if (state != null) {
state.cleanUp();
STATE.remove();
}
STATE.remove();
initState.remove();
} finally {
super.close();
Expand All @@ -170,7 +165,7 @@ public Outputs collect(final Processor processor, final ControllableInputFactory
* Collects all outputs of a processor.
*
* @param processor the processor to run while there are inputs.
* @param inputs the input factory, when an input will return null it will stop the
* @param inputs The input factory, when an input returns null, it will stop the
* processing.
* @param bundleSize the bundle size to use.
* @return a map where the key is the output name and the value a stream of the
Expand Down Expand Up @@ -203,10 +198,10 @@ public <T> Stream<T> collect(final Class<T> recordType, final Mapper mapper, fin

/**
* Collects data emitted from this mapper. If the split creates more than one
* mapper, it will create as much threads as mappers otherwise it will use the
* mapper, it will create as many threads as mappers, otherwise it will use the
* caller thread.
*
* IMPORTANT: don't forget to consume all the stream to ensure the underlying
* IMPORTANT: don't forget to consume the stream entirely to ensure the underlying
* { @see org.talend.sdk.component.runtime.input.Input} is closed.
*
* @param recordType the record type to use to type the returned type.
Expand Down Expand Up @@ -268,7 +263,7 @@ public <T> Stream<T> collect(final Class<T> recordType, final Mapper mapper, fin
latch.countDown();
}
}))
.collect(toList());
.toList();
es.shutdown();

final int timeout = Integer.getInteger("talend.component.junit.timeout", 5);
Expand Down Expand Up @@ -601,6 +596,16 @@ synchronized RecordBuilderFactory recordBuilderFactory() {
}
return recordBuilderFactory;
}

synchronized void cleanUp() {
if (jsonb != null) {
try {
jsonb.close();
} catch (final Exception e) {
// no-op: not important
}
}
}
}

public static class EmbeddedComponentManager extends ComponentManager {
Expand Down Expand Up @@ -653,7 +658,7 @@ public <T> List<T> get(final Class<T> type, final String name) {
}
}

interface Local<T> {
protected interface Local<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intellij detected that the interface Local is used with a protected variable. Hence it's better to make this class protected as well

protected static final Local<State> STATE = loadStateHolder();

Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local was changed from package-private to protected, which expands the API surface of BaseComponentsHandler to subclasses in other packages. Since there are no in-repo usages of BaseComponentsHandler.Local, consider keeping it package-private unless an external use-case requires this visibility change.

Suggested change
protected interface Local<T> {
interface Local<T> {

Copilot uses AI. Check for mistakes.

void set(T value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/**
* Extension allowing the test to use a {@link org.talend.sdk.component.junit.ComponentsHandler}
* and auto register components from current project.
* and auto register components from the current project.
*/
public class ComponentExtension extends BaseComponentsHandler
implements BeforeAllCallback, AfterAllCallback, JUnit5InjectionSupport, BeforeEachCallback, AfterEachCallback {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2006-2026 Talend Inc. - www.talend.com
*
* 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 org.talend.sdk.component.junit;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

class BaseComponentHandlerTest {

@Test
void canCloseTheEmbeddedManagerTwice() {
final MyBaseComponentsHandler myBaseComponentsHandler = new MyBaseComponentsHandler("org.talend.sdk.component.junit");
final BaseComponentsHandler.EmbeddedComponentManager start = myBaseComponentsHandler.start();
try {
assertNotNull(BaseComponentsHandler.STATE.get());
start.close();
assertNull(BaseComponentsHandler.STATE.get());
start.close();
assertNull(BaseComponentsHandler.STATE.get());
} finally {
start.close();
}
}

private static class MyBaseComponentsHandler extends BaseComponentsHandler {
public MyBaseComponentsHandler(final String packageName) {
this.packageName = packageName;
}
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
<geronimo-openapi.version>1.0.12</geronimo-openapi.version>
<geronimo-metrics.version>1.0.3</geronimo-metrics.version>
<geronimo-config.version>1.2.2</geronimo-config.version>
<junit5.version>5.10.0</junit5.version>
<junit5.version>5.14.3</junit5.version>
<mockito4.version>4.8.1</mockito4.version>
<hamcrest.version>1.3</hamcrest.version>
<jackson.version>2.16.0</jackson.version>
Expand Down
Loading