Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
import org.apache.wicket.Application;
import org.apache.wicket.cdi.testapp.ModelWithInjectedDependency;
import org.apache.wicket.cdi.testapp.TestConversationPage;
import org.apache.wicket.cdi.testapp.TestFilteredApplication;
import org.apache.wicket.cdi.testapp.TestFilteredPage;
import org.apache.wicket.cdi.testapp.TestFilteredSession;
import org.apache.wicket.cdi.testapp.TestPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author jsarman
Expand All @@ -35,6 +41,24 @@ class CdiConfigurationTest extends WicketCdiTestCase
@Inject
BeanManager beanManager;

/**
* The filter an application would realistically write: only its own classes are candidates.
*/
private static final java.util.function.Predicate<Class<?>> ONLY_TEST_APP = clazz -> clazz
.getName()
.startsWith("org.apache.wicket.cdi.testapp");

@Override
protected WebApplication newApplication()
{
return new TestFilteredApplication();
}

private TestFilteredApplication application()
{
return (TestFilteredApplication)tester.getApplication();
}

@Test
void testApplicationScope()
{
Expand All @@ -55,6 +79,24 @@ void testUsesCdiJUnitConfiguration()
void testConversationScope()
{
configure(new CdiConfiguration());
assertConversationCounterWorks();
}

/**
* The conversation only propagates when wicket-cdi's own listeners got injected, so this asserts
* that they are injected even though the filter rejects every class outside the test application.
*/
@Test
void testCdiTypesAreInjectedRegardlessOfFilter()
{
assertFalse(ONLY_TEST_APP.test(ConversationPropagator.class),
"the filter under test has to reject wicket-cdi's own types");
configure(new CdiConfiguration().setInjectionCandidateFilter(ONLY_TEST_APP));
assertConversationCounterWorks();
}

private void assertConversationCounterWorks()
{
tester.startPage(TestConversationPage.class);
for (int i = 0; i < 20; i++)
{
Expand All @@ -63,6 +105,70 @@ void testConversationScope()
}
}

@Test
void testDefaultFilterInjectsComponentBehaviorAndSession()
{
configure(new CdiConfiguration());

TestFilteredPage page = startPageWithNewSession();

assertTrue(page.isInjected(), "component should have been injected");
assertTrue(page.getBehavior().isInjected(), "behavior should have been injected");
assertTrue(session().isInjected(), "session should have been injected");
tester.assertLabel("appscope", "Test ok");
}

@Test
void testFilterIsAppliedToComponentBehaviorAndSession()
{
configure(new CdiConfiguration().setInjectionCandidateFilter(clazz -> false));

TestFilteredPage page = startPageWithNewSession();

assertFalse(page.isInjected(), "component should not have been injected");
assertFalse(page.getBehavior().isInjected(), "behavior should not have been injected");
assertFalse(session().isInjected(), "session should not have been injected");
tester.assertLabel("appscope", "not injected");
}

/**
* The session is already created when the tester is built, ie before the configuration under test
* is applied, so a new one is forced to have {@link SessionInjector} see it.
*/
private TestFilteredPage startPageWithNewSession()
{
tester.getSession().invalidateNow();
return tester.startPage(TestFilteredPage.class);
}

private TestFilteredSession session()
{
return (TestFilteredSession)tester.getSession();
}

/**
* The application is injected by {@link CdiConfiguration#configure(Application)} itself, not
* through an injector, so the filter must not be able to suppress it.
*/
@Test
void testApplicationPostConstructIsNotAffectedByFilter()
{
configure(new CdiConfiguration().setInjectionCandidateFilter(clazz -> false));

assertTrue(application().isInjected(), "application should have been injected");
assertTrue(application().isPostConstructed(), "@PostConstruct should have run on the application");
}

@Test
void testShutdownCleanerIsNotAffectedByFilter()
{
configure(new CdiConfiguration().setInjectionCandidateFilter(clazz -> false));

new CdiShutdownCleaner().onBeforeDestroyed(tester.getApplication());

assertTrue(application().isPreDestroyed(), "@PreDestroy should have run on the application");
}

@Test
void testNotConfigured()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.wicket.cdi.testapp;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;

import org.apache.wicket.Session;
import org.apache.wicket.mock.MockApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;

/**
* An application that reports whether CDI was applied to it.
*/
public class TestFilteredApplication extends MockApplication
{
@Inject
TestAppScope appScope;

private boolean postConstructed;

private boolean preDestroyed;

@PostConstruct
void onPostConstruct()
{
postConstructed = true;
}

@PreDestroy
void onPreDestroy()
{
preDestroyed = true;
}

@Override
public Session newSession(Request request, Response response)
{
return new TestFilteredSession(request);
}

public boolean isInjected()
{
return appScope != null;
}

public boolean isPostConstructed()
{
return postConstructed;
}

public boolean isPreDestroyed()
{
return preDestroyed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.wicket.cdi.testapp;

import jakarta.inject.Inject;

import org.apache.wicket.behavior.Behavior;

/**
* A behavior that reports whether CDI was applied to it.
*/
public class TestFilteredBehavior extends Behavior
{
private static final long serialVersionUID = 1L;

@Inject
TestAppScope appScope;

public boolean isInjected()
{
return appScope != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns:wicket>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div wicket:id="appscope">Fail</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.wicket.cdi.testapp;

import jakarta.inject.Inject;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;

/**
* A page that renders and reports whether CDI was applied to it, without requiring it.
*/
public class TestFilteredPage extends WebPage
{
private static final long serialVersionUID = 1L;

@Inject
TestAppScope appScope;

private final TestFilteredBehavior behavior = new TestFilteredBehavior();

public TestFilteredPage()
{
Label label = new Label("appscope", isInjected() ? appScope.test() : "not injected");
label.add(behavior);
add(label);
}

public boolean isInjected()
{
return appScope != null;
}

public TestFilteredBehavior getBehavior()
{
return behavior;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.wicket.cdi.testapp;

import jakarta.inject.Inject;

import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Request;

/**
* A session that reports whether CDI was applied to it.
*/
public class TestFilteredSession extends WebSession
{
private static final long serialVersionUID = 1L;

@Inject
TestAppScope appScope;

public TestFilteredSession(Request request)
{
super(request);
}

public boolean isInjected()
{
return appScope != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@
*/
class AbstractInjector
{
public AbstractInjector()
private final CdiConfiguration configuration;

public AbstractInjector(CdiConfiguration configuration)
{
this.configuration = configuration;
}

protected <T> void postConstruct(T instance)
{
NonContextual.of(instance).postConstruct(instance);
if (configuration.isInjectionCandidate(instance.getClass()))
{
NonContextual.of(instance).postConstruct(instance);
}
}

protected <T> void inject(T instance)
{
NonContextual.of(instance).inject(instance);
if (configuration.isInjectionCandidate(instance.getClass()))
{
NonContextual.of(instance).inject(instance);
}
}
}
Loading
Loading