Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial TCK tests for build compatible extensions #312

Merged
merged 3 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,19 @@
<artifactId>jakarta.inject-api</artifactId>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -424,20 +421,6 @@
</build>
</profile>

<profile>
<!-- auto-activated profile for any JDK 9+ -->
<id>jdk9+</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>3.0.0-M3</version>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.cdi.tck.shrinkwrap;

import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.spi.Extension;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
Expand Down Expand Up @@ -118,6 +119,8 @@ public abstract class ArchiveBuilder<T extends ArchiveBuilder<T, A>, A extends A

protected BeansXml beansDescriptor = null;

protected boolean includeBeansXml = true;

protected ResourceDescriptor webXml = null;

protected WebAppDescriptor webXmlDescriptor = null;
Expand Down Expand Up @@ -190,6 +193,18 @@ public T withBeansXml(BeansXml beansDescriptor) {
return self();
}

/**
* Suppresses adding the {@code beans.xml} descriptor to the archive.
* If this method is called, any previous or following calls to
* {@link #withBeansXml(String)} or {@link #withBeansXml(BeansXml)} are ignored.
*
* @return self
*/
public T withoutBeansXml() {
this.includeBeansXml = false;
return self();
}

/**
* Add CDI extension. This method does not add the specified extension class to the archive.
*
Expand All @@ -210,6 +225,16 @@ public T withExtensions(Class<? extends Extension>... extensionClasses) {
return withServiceProvider(new ServiceProviderDescriptor(Extension.class, extensionClasses));
}

/**
* Add CDI build compatible extension. This method does not add the specified extension class to the archive.
*
* @param extensionClass
* @return self
*/
public T withBuildCompatibleExtension(Class<? extends BuildCompatibleExtension> extensionClass) {
return withServiceProvider(new ServiceProviderDescriptor(BuildCompatibleExtension.class, extensionClass));
}

/**
* Add service provider.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ protected WebArchive buildInternal() {
processResources(webArchive);

// Deployment descriptors
webArchive.addAsWebInfResource(getBeansDescriptorAsset(), buildBeansDescriptorTargetPath(getBeansDescriptorTarget()));
if (includeBeansXml) {
webArchive.addAsWebInfResource(getBeansDescriptorAsset(), buildBeansDescriptorTargetPath(getBeansDescriptorTarget()));
}

if (webXmlDescriptor != null) {
webArchive.setWebXML(new StringAsset(webXmlDescriptor.exportAsString()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.ClassConfig;
import jakarta.enterprise.inject.build.compatible.spi.Discovery;
import jakarta.enterprise.inject.build.compatible.spi.Enhancement;
import jakarta.enterprise.inject.build.compatible.spi.FieldConfig;
import jakarta.enterprise.inject.build.compatible.spi.ScannedClasses;

public class ChangeBeanQualifierExtension implements BuildCompatibleExtension {
@Discovery
public void discovery(ScannedClasses scan) {
scan.add(MyServiceFoo.class.getName());
scan.add(MyServiceBar.class.getName());
scan.add(MyServiceBaz.class.getName());
}

@Enhancement(types = MyServiceFoo.class)
public void foo(ClassConfig clazz) {
clazz.removeAnnotation(ann -> ann.name().equals(MyQualifier.class.getName()));
}

@Enhancement(types = MyServiceBar.class)
public void bar(ClassConfig clazz) {
clazz.addAnnotation(MyQualifier.class);
}

@Enhancement(types = MyOtherService.class)
public void service(FieldConfig field) {
if ("myService".equals(field.info().name())) {
field.addAnnotation(MyQualifier.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

@SpecVersion(spec = "cdi", version = "4.0")
public class ChangeBeanQualifierTest extends AbstractTest {
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClassPackage(ChangeBeanQualifierTest.class)
.withBuildCompatibleExtension(ChangeBeanQualifierExtension.class)
.build();
}

@Test
//@SpecAssertion(section = TODO, id = "TODO")
public void test() {
manovotn marked this conversation as resolved.
Show resolved Hide resolved
MyOtherService bean = getContextualReference(MyOtherService.class);
// all beans are dependent, so there's no client proxy and direct field access and `instanceof` are OK
assertTrue(bean.myService instanceof MyServiceBar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

@Dependent
public class MyOtherService {
manovotn marked this conversation as resolved.
Show resolved Hide resolved
@Inject
MyService myService;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

import jakarta.inject.Qualifier;

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

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface MyQualifier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

public interface MyService {
String hello();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

public class MyServiceBar implements MyService {
private static final String VALUE = "bar";

@Override
public String hello() {
return VALUE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

public class MyServiceBaz implements MyService {
@Override
public String hello() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeBeanQualifier;

@MyQualifier
public class MyServiceFoo implements MyService {
private final String value = "foo";

@Override
public String hello() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import jakarta.enterprise.inject.build.compatible.spi.AnnotationBuilder;
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.ClassConfig;
import jakarta.enterprise.inject.build.compatible.spi.Enhancement;

public class ChangeInjectionPointExtension implements BuildCompatibleExtension {
@Enhancement(types = MyOtherService.class)
public void service(ClassConfig clazz) {
clazz.fields()
.stream()
.filter(it -> "myService".equals(it.info().name()))
.forEach(field -> field.addAnnotation(AnnotationBuilder.of(MyQualifier.class).build()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

@SpecVersion(spec = "cdi", version = "4.0")
public class ChangeInjectionPointTest extends AbstractTest {
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClassPackage(ChangeInjectionPointTest.class)
.withBuildCompatibleExtension(ChangeInjectionPointExtension.class)
.build();
}

@Test
//@SpecAssertion(section = TODO, id = "TODO")
public void test() {
MyOtherService bean = getContextualReference(MyOtherService.class);
// all beans are dependent, so there's no client proxy and direct field access and `instanceof` are OK
assertTrue(bean.myService instanceof MyServiceBar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

@Dependent
public class MyOtherService {
@Inject
MyService myService;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import jakarta.inject.Qualifier;

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

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface MyQualifier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

public interface MyService {
String hello();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import jakarta.enterprise.context.Dependent;

@Dependent
@MyQualifier
public class MyServiceBar implements MyService {
@Override
public String hello() {
return "bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInjectionPoint;

import jakarta.enterprise.context.Dependent;

@Dependent
public class MyServiceFoo implements MyService {
@Override
public String hello() {
return "foo";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeObserverQualifier;

import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.Discovery;
import jakarta.enterprise.inject.build.compatible.spi.Enhancement;
import jakarta.enterprise.inject.build.compatible.spi.MethodConfig;
import jakarta.enterprise.inject.build.compatible.spi.ScannedClasses;

public class ChangeObserverQualifierExtension implements BuildCompatibleExtension {
@Discovery
public void discovery(ScannedClasses scan) {
scan.add(MyConsumer.class.getName());
scan.add(MyProducer.class.getName());
}

@Enhancement(types = MyConsumer.class)
public void consumer(MethodConfig method) {
switch (method.info().name()) {
case "consume":
method.parameters().get(0).addAnnotation(MyQualifier.class);
break;
case "noConsume":
method.parameters().get(0).removeAllAnnotations();
break;
}
}
}
Loading