Skip to content

Commit

Permalink
merge: #608
Browse files Browse the repository at this point in the history
608: [Backport stable/8.0] Inject ZPT related fields into superclasses r=remcowesterhoud a=github-actions[bot]

# Description
Backport of #605 to `stable/8.0`.

relates to #604

Co-authored-by: Remco Westerhoud <remco@westerhoud.nl>
  • Loading branch information
zeebe-bors-camunda[bot] and remcowesterhoud authored Dec 15, 2022
2 parents 1d9c1dc + 55144f5 commit 2955b60
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
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 {}

0 comments on commit 2955b60

Please sign in to comment.