You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/09/10 08:04:28 UTC

[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705980971



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(Class<?> type, InjectConfiguration annotation, ConfigurationAsmGenerator cgen)

Review comment:
       Formatted manually




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org