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

avoid custom CreationalContext and Contextual implementations in the TCK #452

Merged
merged 8 commits into from
Sep 21, 2023
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
24 changes: 20 additions & 4 deletions api/src/main/java/org/jboss/cdi/tck/api/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.jboss.cdi.tck.spi.Beans;
import org.jboss.cdi.tck.spi.Contexts;
import org.jboss.cdi.tck.spi.Contextuals;
import org.jboss.cdi.tck.spi.CreationalContexts;
import org.jboss.cdi.tck.spi.EL;

/**
Expand Down Expand Up @@ -79,17 +81,31 @@ public interface Configuration {
*/
public <T extends Context> Contexts<T> getContexts();

public void setBeans(Beans beans);

public <T extends Context> void setContexts(Contexts<T> contexts);
/**
* The implementation of {@link Contextuals} in use.
*/
public Contextuals getContextuals();

public void setEl(EL el);
/**
* The implementation of {@link CreationalContexts} in use.
*/
public CreationalContexts getCreationalContexts();

/**
* The implementation of {@link EL} in use.
*/
public EL getEl();

public void setBeans(Beans beans);

public <T extends Context> void setContexts(Contexts<T> contexts);

public void setContextuals(Contextuals contextuals);

public void setCreationalContexts(CreationalContexts creationalContexts);

public void setEl(EL el);

/**
* The TCK allows additional libraries to be put in the deployed test artifacts (for example the porting package for the implementation). Any jars in this
* directory will be added to the deployed artifact.
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/jboss/cdi/tck/spi/Contexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface Contexts<T extends Context> {
public T getDependentContext();

/**
* Destroy the context. This operation is defined by the Web Beans specification but has no API.
* Destroy the context. This operation is defined by the CDI specification but has no API.
*
* @param context the context to destroy
*/
Expand Down
80 changes: 80 additions & 0 deletions api/src/main/java/org/jboss/cdi/tck/spi/Contextuals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2023, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.cdi.tck.spi;

import jakarta.enterprise.context.spi.Context;
import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;

/**
* Provides Contextual related operations.
*
* The TCK porting package must provide an implementation of this interface which is suitable for the target implementation.
*/
public interface Contextuals {

public static final String PROPERTY_NAME = Contextuals.class.getName();

/**
* Creates a dummy {@linkplain Inspectable inspectable} Contextual that will be used
* with given Context. The result does not necessarily fulfil the entire {@link Contextual}
* contract; the TCK only requires it to:
*
* <ul>
* <li>return the given {@code instance} from {@link Contextual#create(CreationalContext)};</li>
* <li>capture all the parameters passed to {@link Contextual} methods in order to fulfil
* the {@link Inspectable} contract.</li>
* </ul>
*
* @param instance the instance to be returned by {@link Contextual#create(CreationalContext)}
* @param context the Context that the returned Contextual can be used with
* @return a Contextual that can be inspected
* @param <T> type of the instance
*/
public <T> Inspectable<T> create(T instance, Context context);

/**
* A Contextual that can be inspected.
*
* @param <T> type of the instances
*/
public static interface Inspectable<T> extends Contextual<T> {
/**
* Returns the {@link CreationalContext} passed to {@link Contextual#create(CreationalContext)}.
* Returns {@code null} if {@code create} was not called yet.
*
* @return the {@link CreationalContext} passed to {@link Contextual#create(CreationalContext)}
*/
public CreationalContext<T> getCreationalContextPassedToCreate();

/**
* Returns the instance passed to {@link Contextual#destroy(Object, CreationalContext)}.
* Returns {@code null} if {@code destroy} was not called yet.
*
* @return the instance passed to {@link Contextual#destroy(Object, CreationalContext)}
*/
public T getInstancePassedToDestroy();

/**
* Returns the {@link CreationalContext} passed to {@link Contextual#destroy(Object, CreationalContext)}.
* Returns {@code null} if {@code destroy} was not called yet.
*
* @return the {@link CreationalContext} passed to {@link Contextual#destroy(Object, CreationalContext)}
*/
public CreationalContext<T> getCreationalContextPassedToDestroy();
}

}
70 changes: 70 additions & 0 deletions api/src/main/java/org/jboss/cdi/tck/spi/CreationalContexts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2023, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.cdi.tck.spi;

import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;

/**
* Provides CreationalContext related operations.
*
* The TCK porting package must provide an implementation of this interface which is suitable for the target implementation.
*/
public interface CreationalContexts {

public static final String PROPERTY_NAME = CreationalContexts.class.getName();

/**
* Creates an {@linkplain Inspectable inspectable} CreationalContext for given Contextual.
* This operation is identical to {@link jakarta.enterprise.inject.spi.BeanContainer#createCreationalContext(Contextual)},
* except it returns a specialized variant of CreationalContext that can be inspected.
*
* @param contextual a Contextual for which a CreationalContext should be created
* @return a CreationalContext that can be inspected
* @param <T> type of the instance
*/
public <T> Inspectable<T> create(Contextual<T> contextual);

/**
* A CreationalContext that can be inspected.
*
* @param <T> type of the instances
*/
public static interface Inspectable<T> extends CreationalContext<T> {
/**
* Returns whether {@link #push(Object)} was called on this CreationalContext.
*
* @return whether {@link #push(Object)} was called on this CreationalContext.
*/
public boolean isPushCalled();

/**
* If {@code push} {@linkplain #isPushCalled() was called} on this CreationalContext, returns the pushed object.
* Returns {@code null} otherwise.
*
* @return the pushed object if {@code push} was called, otherwise {@code null}
*/
public Object getLastBeanPushed();

/**
* Returns whether {@link #release()} was called on this CreationalContext.
*
* @return whether {@link #release()} was called on this CreationalContext.
*/
public boolean isReleaseCalled();
}

}
3 changes: 1 addition & 2 deletions api/src/main/java/org/jboss/cdi/tck/spi/EL.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
*
* For the purpose of TCK, EL function and variable mapping is not required and therefore may be disabled.
*
* The TCK porting package must provide an implementation of this interface which is suitable for the target Web Beans
* implementation.
* The TCK porting package must provide an implementation of this interface which is suitable for the target implementation.
*
* @author Pete Muir
*/
Expand Down
2 changes: 2 additions & 0 deletions doc/reference/src/main/asciidoc/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ The SPI classes in the CDI TCK are as follows:

* +org.jboss.cdi.tck.spi.Beans+
* +org.jboss.cdi.tck.spi.Contexts+
* +org.jboss.cdi.tck.spi.Contextuals+
* +org.jboss.cdi.tck.spi.CreationalContexts+
* +org.jboss.cdi.tck.spi.EL+

Please consult the JavaDoc for these interfaces for the implementation requirements.
Expand Down
2 changes: 2 additions & 0 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
<!-- Dummy porting package impls -->
<org.jboss.cdi.tck.spi.Beans>org.jboss.cdi.tck.test.porting.DummyBeans</org.jboss.cdi.tck.spi.Beans>
<org.jboss.cdi.tck.spi.Contexts>org.jboss.cdi.tck.test.porting.DummyContexts</org.jboss.cdi.tck.spi.Contexts>
<org.jboss.cdi.tck.spi.Contextuals>org.jboss.cdi.tck.test.porting.DummyContextuals</org.jboss.cdi.tck.spi.Contextuals>
<org.jboss.cdi.tck.spi.CreationalContexts>org.jboss.cdi.tck.test.porting.DummyCreationalContexts</org.jboss.cdi.tck.spi.CreationalContexts>
<org.jboss.cdi.tck.spi.EL>org.jboss.cdi.tck.test.porting.DummyEL</org.jboss.cdi.tck.spi.EL>
</systemPropertyVariables>
</configuration>
Expand Down
11 changes: 11 additions & 0 deletions impl/src/main/java/org/jboss/cdi/tck/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Set;

import jakarta.enterprise.context.spi.Context;
import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.inject.AmbiguousResolutionException;
import jakarta.enterprise.inject.UnsatisfiedResolutionException;
import jakarta.enterprise.inject.spi.Bean;
Expand All @@ -39,6 +40,8 @@
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.cdi.tck.api.Configuration;
import org.jboss.cdi.tck.impl.ConfigurationFactory;
import org.jboss.cdi.tck.spi.Contextuals;
import org.jboss.cdi.tck.spi.CreationalContexts;
import org.jboss.cdi.tck.util.BeanLookupUtils;
import org.jboss.cdi.tck.util.DependentInstance;

Expand Down Expand Up @@ -94,6 +97,14 @@ protected void destroyContext(Context context) {
getCurrentConfiguration().getContexts().destroyContext(context);
}

protected <T> CreationalContexts.Inspectable<T> createInspectableCreationalContext(Contextual<T> contextual) {
return getCurrentConfiguration().getCreationalContexts().create(contextual);
}

protected <T> Contextuals.Inspectable<T> createInspectableContextual(T instance, Context context) {
return getCurrentConfiguration().getContextuals().create(instance, context);
}

protected Configuration getCurrentConfiguration() {
return ConfigurationFactory.get();
}
Expand Down
22 changes: 22 additions & 0 deletions impl/src/main/java/org/jboss/cdi/tck/impl/ConfigurationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.jboss.cdi.tck.api.Configuration;
import org.jboss.cdi.tck.spi.Beans;
import org.jboss.cdi.tck.spi.Contexts;
import org.jboss.cdi.tck.spi.Contextuals;
import org.jboss.cdi.tck.spi.CreationalContexts;
import org.jboss.cdi.tck.spi.EL;

/**
Expand All @@ -35,6 +37,8 @@ public class ConfigurationImpl implements Configuration {

private Beans beans;
private Contexts<? extends Context> contexts;
private Contextuals contextuals;
private CreationalContexts creationalContexts;
private EL el;

private String libraryDirectory;
Expand Down Expand Up @@ -74,6 +78,22 @@ public <T extends Context> void setContexts(Contexts<T> contexts) {
this.contexts = contexts;
}

public Contextuals getContextuals() {
return contextuals;
}

public void setContextuals(Contextuals contextuals) {
this.contextuals = contextuals;
}

public CreationalContexts getCreationalContexts() {
return creationalContexts;
}

public void setCreationalContexts(CreationalContexts creationalContexts) {
this.creationalContexts = creationalContexts;
}

public EL getEl() {
return el;
}
Expand Down Expand Up @@ -149,6 +169,8 @@ public String toString() {
configuration.append("\tCDI Lite mode: ").append(getCdiLiteMode()).append("\n");
configuration.append("\tBeans: ").append(getBeans()).append("\n");
configuration.append("\tContexts: ").append(getContexts()).append("\n");
configuration.append("\tContextuals: ").append(getContextuals()).append("\n");
configuration.append("\tCreationalContexts: ").append(getCreationalContexts()).append("\n");
configuration.append("\tEL: ").append(getEl()).append("\n");
configuration.append("\tLibrary dir: ").append(getLibraryDirectory()).append("\n");
configuration.append("\tTest DS: ").append(getTestDataSource()).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.jboss.cdi.tck.api.Configuration;
import org.jboss.cdi.tck.spi.Beans;
import org.jboss.cdi.tck.spi.Contexts;
import org.jboss.cdi.tck.spi.Contextuals;
import org.jboss.cdi.tck.spi.CreationalContexts;
import org.jboss.cdi.tck.spi.EL;

import java.io.IOException;
Expand Down Expand Up @@ -66,6 +68,8 @@ public PropertiesBasedConfigurationBuilder init(boolean deploymentPhase) {
configuration.setBeans(getInstanceValue(Beans.PROPERTY_NAME, Beans.class, !deploymentPhase));
configuration.setEl(getInstanceValue(EL.PROPERTY_NAME, EL.class, !deploymentPhase));
configuration.setContexts((Contexts<?>)getInstanceValue(Contexts.PROPERTY_NAME, Contexts.class, !deploymentPhase));
configuration.setContextuals(getInstanceValue(Contextuals.PROPERTY_NAME, Contextuals.class, !deploymentPhase));
configuration.setCreationalContexts(getInstanceValue(CreationalContexts.PROPERTY_NAME, CreationalContexts.class, !deploymentPhase));

configuration.setLibraryDirectory(getStringValue(Configuration.LIBRARY_DIRECTORY_PROPERTY_NAME, null, deploymentPhase));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.cdi.tck.tests.context;

import static org.jboss.cdi.tck.cdi.Sections.CONTEXT;

import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.spi.Context;

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

/**
*
* @author Nicklas Karlsson
* @author Pete Muir
* @author David Allen
* @author Martin Kouba
*/
@SpecVersion(spec = "cdi", version = "2.0")
public class DestroyForSameCreationalContext2Test extends AbstractTest {

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder().withTestClassPackage(DestroyForSameCreationalContext2Test.class).build();
}

@Test
@SpecAssertion(section = CONTEXT, id = "r")
public void testDestroyForSameCreationalContextOnly() {
// Check that the cc is called (via cc.release()) when we request a context destroyed
// Note that this is an indirect effect
Context requestContext = getCurrentManager().getContext(RequestScoped.class);

// We also test this directly using an inspectable contextual, and ensuring that the same creational context is passed to both methods
Contextuals.Inspectable<String> contextual = createInspectableContextual("123", requestContext);
requestContext.get(contextual, getCurrentManager().createCreationalContext(contextual));
destroyContext(requestContext);
assert contextual.getCreationalContextPassedToCreate() == contextual.getCreationalContextPassedToDestroy();
}

}
Loading
Loading