Skip to content

Commit

Permalink
Upgrade to alpha-13 (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 9, 2024
1 parent 9e643f6 commit 99af984
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 227 deletions.
7 changes: 6 additions & 1 deletion maven-plugin-testing-harness/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ under the License.
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.0</version>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -74,6 +74,11 @@ under the License.
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-xml-impl</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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.maven.api.di.testing;

import java.io.*;

import org.apache.maven.di.Injector;
import org.apache.maven.di.Key;
import org.apache.maven.di.impl.DIException;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* This is a slightly modified version of the original plexus class
* available at https://raw.githubusercontent.com/codehaus-plexus/plexus-containers/master/plexus-container-default/
* src/main/java/org/codehaus/plexus/PlexusTestCase.java
* in order to migrate the tests to JUnit 4.
*
* @author Jason van Zyl
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
* @author Guillaume Nodet
*/
public class MavenDIExtension implements BeforeEachCallback, AfterEachCallback {
protected static ExtensionContext context;
protected Injector injector;
protected static String basedir;

@Override
public void beforeEach(ExtensionContext context) throws Exception {
basedir = getBasedir();

setContext(context);

getInjector().bindInstance((Class<Object>) context.getRequiredTestClass(), context.getRequiredTestInstance());
getInjector().injectInstance(context.getRequiredTestInstance());
}

protected void setContext(ExtensionContext context) {
this.context = context;
}

@SuppressWarnings("unchecked")
protected void setupContainer() {
try {
injector = Injector.create();
injector.bindInstance(ExtensionContext.class, this.context);
injector.discover(this.context.getRequiredTestClass().getClassLoader());
injector.bindInstance(Injector.class, injector);
injector.bindInstance((Class) this.context.getRequiredTestClass(), this.context.getRequiredTestInstance());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to create DI injector.", e);
}
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (injector != null) {
// TODO: implement
// injector.dispose();
injector = null;
}
}

public Injector getInjector() {
if (injector == null) {
setupContainer();
}

return injector;
}

// ----------------------------------------------------------------------
// Container access
// ----------------------------------------------------------------------

protected <T> T lookup(Class<T> componentClass) throws DIException {
return getInjector().getInstance(componentClass);
}

protected <T> T lookup(Class<T> componentClass, String roleHint) throws DIException {
return getInjector().getInstance(Key.ofType(componentClass, roleHint));
}

protected <T> T lookup(Class<T> componentClass, Object qualifier) throws DIException {
return getInjector().getInstance(Key.ofType(componentClass, qualifier));
}

protected void release(Object component) throws DIException {
// TODO: implement
// getInjector().release(component);
}

// ----------------------------------------------------------------------
// Helper methods for sub classes
// ----------------------------------------------------------------------

public static File getTestFile(String path) {
return new File(getBasedir(), path);
}

public static File getTestFile(String basedir, String path) {
File basedirFile = new File(basedir);

if (!basedirFile.isAbsolute()) {
basedirFile = getTestFile(basedir);
}

return new File(basedirFile, path);
}

public static String getTestPath(String path) {
return getTestFile(path).getAbsolutePath();
}

public static String getTestPath(String basedir, String path) {
return getTestFile(basedir, path).getAbsolutePath();
}

public static String getBasedir() {
if (basedir != null) {
return basedir;
}

basedir = System.getProperty("basedir");

if (basedir == null) {
basedir = new File("").getAbsolutePath();
}

return basedir;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.maven.api.di.testing;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* Plexus test
*/
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(MavenDIExtension.class)
@Target(ElementType.TYPE)
public @interface MavenDITest {}
Loading

0 comments on commit 99af984

Please sign in to comment.