001/** 002 * Copyright (C) 2006-2019 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.junit5; 017 018import static java.util.Optional.ofNullable; 019 020import java.lang.annotation.Annotation; 021import java.lang.reflect.AnnotatedElement; 022import java.util.Optional; 023 024import org.junit.jupiter.api.extension.AfterAllCallback; 025import org.junit.jupiter.api.extension.AfterEachCallback; 026import org.junit.jupiter.api.extension.BeforeAllCallback; 027import org.junit.jupiter.api.extension.BeforeEachCallback; 028import org.junit.jupiter.api.extension.ExtensionContext; 029import org.talend.sdk.component.junit.BaseComponentsHandler; 030import org.talend.sdk.component.junit.base.junit5.JUnit5InjectionSupport; 031import org.talend.sdk.component.junit.environment.Environment; 032 033/** 034 * Extension allowing the test to use a {@link org.talend.sdk.component.junit.ComponentsHandler} 035 * and auto register components from current project. 036 */ 037public class ComponentExtension extends BaseComponentsHandler 038 implements BeforeAllCallback, AfterAllCallback, JUnit5InjectionSupport, BeforeEachCallback, AfterEachCallback { 039 040 public static final ExtensionContext.Namespace NAMESPACE = 041 ExtensionContext.Namespace.create(ComponentExtension.class.getName()); 042 043 private static final String USE_EACH_KEY = ComponentExtension.class.getName() + ".useEach"; 044 045 private static final String SHARED_INSTANCE = ComponentExtension.class.getName() + ".instance"; 046 047 @Override 048 public void beforeAll(final ExtensionContext extensionContext) { 049 final WithComponents element = extensionContext 050 .getElement() 051 .map(e -> e.getAnnotation(WithComponents.class)) 052 .orElseThrow(() -> new IllegalArgumentException( 053 "No annotation @WithComponents on " + extensionContext.getRequiredTestClass())); 054 this.packageName = element.value(); 055 if (element.isolatedPackages().length > 0) { 056 withIsolatedPackage(null, element.isolatedPackages()); 057 } 058 059 final boolean shouldUseEach = shouldIgnore(extensionContext.getElement()); 060 if (!shouldUseEach) { 061 doStart(extensionContext); 062 } else if (!extensionContext.getElement().map(AnnotatedElement::getAnnotations).map(annotations -> { 063 int componentIndex = -1; 064 for (int i = 0; i < annotations.length; i++) { 065 final Class<? extends Annotation> type = annotations[i].annotationType(); 066 if (type == WithComponents.class) { 067 componentIndex = i; 068 } else if (type == Environment.class && componentIndex >= 0) { 069 return false; 070 } 071 } 072 return true; 073 }).orElse(false)) { 074 // check the ordering, if environments are put after this then the context is likely wrong 075 // this condition is a simple heuristic but enough for most cases 076 throw new IllegalArgumentException("If you combine @WithComponents and @Environment, you must ensure " 077 + "environment annotations are becoming before the component one otherwise you will run in an " 078 + "unexpected context and will not reproduce real execution."); 079 } 080 extensionContext.getStore(NAMESPACE).put(USE_EACH_KEY, shouldUseEach); 081 extensionContext.getStore(NAMESPACE).put(SHARED_INSTANCE, this); 082 } 083 084 @Override 085 public void afterAll(final ExtensionContext extensionContext) { 086 if (!shouldUseEach(extensionContext)) { 087 doStop(extensionContext); 088 } 089 } 090 091 @Override 092 public Class<? extends Annotation> injectionMarker() { 093 return Injected.class; 094 } 095 096 @Override 097 public void beforeEach(final ExtensionContext extensionContext) { 098 if (!shouldUseEach(extensionContext)) { 099 doInject(extensionContext); 100 } 101 } 102 103 @Override 104 public void afterEach(final ExtensionContext extensionContext) { 105 if (!shouldUseEach(extensionContext)) { 106 resetState(); 107 } 108 } 109 110 public void doStart(final ExtensionContext extensionContext) { 111 extensionContext.getStore(NAMESPACE).put(EmbeddedComponentManager.class.getName(), start()); 112 } 113 114 public void doStop(final ExtensionContext extensionContext) { 115 ofNullable(EmbeddedComponentManager.class 116 .cast(extensionContext.getStore(NAMESPACE).get(EmbeddedComponentManager.class.getName()))) 117 .ifPresent(EmbeddedComponentManager::close); 118 } 119 120 public void doInject(final ExtensionContext extensionContext) { 121 extensionContext.getTestInstance().ifPresent(this::injectServices); 122 } 123 124 private Boolean shouldUseEach(final ExtensionContext extensionContext) { 125 return extensionContext.getStore(NAMESPACE).get(USE_EACH_KEY, boolean.class); 126 } 127 128 private boolean shouldIgnore(final Optional<AnnotatedElement> element) { 129 return element.map(e -> e.getDeclaredAnnotationsByType(Environment.class).length > 0).orElse(false); 130 } 131}