Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport stable/8.1] Inject ZPT related fields into superclasses #609

Merged
2 commits merged into from
Dec 15, 2022
Merged
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 @@ -123,14 +123,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable

private void injectFields(final ExtensionContext extensionContext, final Object... objects) {
final Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
final Field[] declaredFields = requiredTestClass.getDeclaredFields();
for (final Object object : objects) {
final Optional<Field> field = getField(declaredFields, object);
final Optional<Field> field = getField(requiredTestClass, object);
field.ifPresent(value -> injectField(extensionContext, value, object));
}
}

private Optional<Field> getField(final Field[] declaredFields, final Object object) {
private Optional<Field> getField(final Class<?> requiredTestClass, final Object object) {
final Field[] declaredFields = requiredTestClass.getDeclaredFields();

final List<Field> fields =
Arrays.stream(declaredFields)
.filter(field -> field.getType().isInstance(object))
Expand All @@ -143,8 +144,12 @@ private Optional<Field> getField(final Field[] declaredFields, final Object obje
+ "found %s. Please make sure at most one field of type %s has been declared in the"
+ " test class.",
object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName()));
} else if (fields.size() == 0) {
final Class<?> superclass = requiredTestClass.getSuperclass();
return superclass == null ? Optional.empty() : getField(superclass, object);
} else {
return Optional.of(fields.get(0));
}
return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0));
}

private void injectField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable

private void injectFields(final ExtensionContext extensionContext, final Object... objects) {
final Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
final Field[] declaredFields = requiredTestClass.getDeclaredFields();
for (final Object object : objects) {
final Optional<Field> field = getField(declaredFields, object);
final Optional<Field> field = getField(requiredTestClass, object);
field.ifPresent(value -> injectField(extensionContext, value, object));
}
}

private Optional<Field> getField(final Field[] declaredFields, final Object object) {
private Optional<Field> getField(final Class<?> requiredTestClass, final Object object) {
final Field[] declaredFields = requiredTestClass.getDeclaredFields();

final List<Field> fields =
Arrays.stream(declaredFields)
.filter(field -> field.getType().isInstance(object))
Expand All @@ -119,8 +120,12 @@ private Optional<Field> getField(final Field[] declaredFields, final Object obje
+ "found %s. Please make sure at most one field of type %s has been declared in the"
+ " test class.",
object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName()));
} else if (fields.size() == 0) {
final Class<?> superclass = requiredTestClass.getSuperclass();
return superclass == null ? Optional.empty() : getField(superclass, object);
} else {
return Optional.of(fields.get(0));
}
return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0));
}

private void injectField(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2021 camunda services GmbH (info@camunda.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 io.camunda.zeebe.process.test.qa.abstracts.injection;

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

import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.process.test.api.ZeebeTestEngine;
import org.junit.jupiter.api.Test;

public abstract class AbstractInheritanceInjectionTest {
protected ZeebeClient client;
protected ZeebeTestEngine engine;

@Test
void testFieldsAreInjectedSuccessfully() {
assertThat(client).isNotNull();
assertThat(engine).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright © 2021 camunda services GmbH (info@camunda.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 io.camunda.zeebe.process.test.qa.embedded.injection;

import io.camunda.zeebe.process.test.extension.ZeebeProcessTest;
import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest;

@ZeebeProcessTest
class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright © 2021 camunda services GmbH (info@camunda.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 io.camunda.zeebe.process.test.qa.testcontainer.injection;

import io.camunda.zeebe.process.test.extension.testcontainer.ZeebeProcessTest;
import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest;

@ZeebeProcessTest
class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {}