|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.api.di.testing; |
| 20 | + |
| 21 | +import java.io.*; |
| 22 | + |
| 23 | +import org.apache.maven.di.Injector; |
| 24 | +import org.apache.maven.di.Key; |
| 25 | +import org.apache.maven.di.impl.DIException; |
| 26 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 27 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 28 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 29 | + |
| 30 | +/** |
| 31 | + * This is a slightly modified version of the original plexus class |
| 32 | + * available at https://raw.githubusercontent.com/codehaus-plexus/plexus-containers/master/plexus-container-default/ |
| 33 | + * src/main/java/org/codehaus/plexus/PlexusTestCase.java |
| 34 | + * in order to migrate the tests to JUnit 4. |
| 35 | + * |
| 36 | + * @author Jason van Zyl |
| 37 | + * @author <a href="mailto:[email protected]">Trygve Laugstøl</a> |
| 38 | + * @author <a href="mailto:[email protected]">Michal Maczka</a> |
| 39 | + * @author Guillaume Nodet |
| 40 | + */ |
| 41 | +public class MavenDIExtension implements BeforeEachCallback, AfterEachCallback { |
| 42 | + protected static ExtensionContext context; |
| 43 | + protected Injector injector; |
| 44 | + protected static String basedir; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void beforeEach(ExtensionContext context) throws Exception { |
| 48 | + basedir = getBasedir(); |
| 49 | + |
| 50 | + setContext(context); |
| 51 | + |
| 52 | + getInjector().bindInstance((Class<Object>) context.getRequiredTestClass(), context.getRequiredTestInstance()); |
| 53 | + getInjector().injectInstance(context.getRequiredTestInstance()); |
| 54 | + } |
| 55 | + |
| 56 | + protected void setContext(ExtensionContext context) { |
| 57 | + this.context = context; |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + protected void setupContainer() { |
| 62 | + try { |
| 63 | + injector = Injector.create(); |
| 64 | + injector.bindInstance(ExtensionContext.class, this.context); |
| 65 | + injector.discover(this.context.getRequiredTestClass().getClassLoader()); |
| 66 | + injector.bindInstance(Injector.class, injector); |
| 67 | + injector.bindInstance((Class) this.context.getRequiredTestClass(), this.context.getRequiredTestInstance()); |
| 68 | + } catch (Exception e) { |
| 69 | + throw new IllegalArgumentException("Failed to create DI injector.", e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void afterEach(ExtensionContext context) throws Exception { |
| 75 | + if (injector != null) { |
| 76 | + // TODO: implement |
| 77 | + // injector.dispose(); |
| 78 | + injector = null; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public Injector getInjector() { |
| 83 | + if (injector == null) { |
| 84 | + setupContainer(); |
| 85 | + } |
| 86 | + |
| 87 | + return injector; |
| 88 | + } |
| 89 | + |
| 90 | + // ---------------------------------------------------------------------- |
| 91 | + // Container access |
| 92 | + // ---------------------------------------------------------------------- |
| 93 | + |
| 94 | + protected <T> T lookup(Class<T> componentClass) throws DIException { |
| 95 | + return getInjector().getInstance(componentClass); |
| 96 | + } |
| 97 | + |
| 98 | + protected <T> T lookup(Class<T> componentClass, String roleHint) throws DIException { |
| 99 | + return getInjector().getInstance(Key.ofType(componentClass, roleHint)); |
| 100 | + } |
| 101 | + |
| 102 | + protected <T> T lookup(Class<T> componentClass, Object qualifier) throws DIException { |
| 103 | + return getInjector().getInstance(Key.ofType(componentClass, qualifier)); |
| 104 | + } |
| 105 | + |
| 106 | + protected void release(Object component) throws DIException { |
| 107 | + // TODO: implement |
| 108 | + // getInjector().release(component); |
| 109 | + } |
| 110 | + |
| 111 | + // ---------------------------------------------------------------------- |
| 112 | + // Helper methods for sub classes |
| 113 | + // ---------------------------------------------------------------------- |
| 114 | + |
| 115 | + public static File getTestFile(String path) { |
| 116 | + return new File(getBasedir(), path); |
| 117 | + } |
| 118 | + |
| 119 | + public static File getTestFile(String basedir, String path) { |
| 120 | + File basedirFile = new File(basedir); |
| 121 | + |
| 122 | + if (!basedirFile.isAbsolute()) { |
| 123 | + basedirFile = getTestFile(basedir); |
| 124 | + } |
| 125 | + |
| 126 | + return new File(basedirFile, path); |
| 127 | + } |
| 128 | + |
| 129 | + public static String getTestPath(String path) { |
| 130 | + return getTestFile(path).getAbsolutePath(); |
| 131 | + } |
| 132 | + |
| 133 | + public static String getTestPath(String basedir, String path) { |
| 134 | + return getTestFile(basedir, path).getAbsolutePath(); |
| 135 | + } |
| 136 | + |
| 137 | + public static String getBasedir() { |
| 138 | + if (basedir != null) { |
| 139 | + return basedir; |
| 140 | + } |
| 141 | + |
| 142 | + basedir = System.getProperty("basedir"); |
| 143 | + |
| 144 | + if (basedir == null) { |
| 145 | + basedir = new File("").getAbsolutePath(); |
| 146 | + } |
| 147 | + |
| 148 | + return basedir; |
| 149 | + } |
| 150 | +} |
0 commit comments