1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.session.scope;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import com.google.inject.OutOfScopeException;
26 import org.apache.maven.SessionScoped;
27 import org.apache.maven.api.Session;
28 import org.apache.maven.session.scope.internal.SessionScope;
29 import org.codehaus.plexus.PlexusContainer;
30 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
31 import org.codehaus.plexus.testing.PlexusTest;
32 import org.eclipse.sisu.Typed;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37
38 import static org.junit.jupiter.api.Assertions.assertNotNull;
39 import static org.junit.jupiter.api.Assertions.assertNotSame;
40 import static org.junit.jupiter.api.Assertions.assertSame;
41 import static org.junit.jupiter.api.Assertions.assertThrows;
42 import static org.junit.jupiter.api.Assertions.assertTrue;
43
44 @PlexusTest
45 @ExtendWith(MockitoExtension.class)
46 public class SessionScopeProxyTest {
47
48 @Mock
49 Session session;
50
51 @Inject
52 SessionScope sessionScope;
53
54 @Inject
55 PlexusContainer container;
56
57 @Test
58 void testProxiedSessionScopedBean() throws ComponentLookupException {
59 ComponentLookupException e =
60 assertThrows(ComponentLookupException.class, () -> container.lookup(MySingletonBean2.class));
61 assertTrue(e.getMessage().matches("[\\s\\S]*: Can not set .* field .* to [\\s\\S]*"));
62
63 MySingletonBean bean = container.lookup(MySingletonBean.class);
64 assertNotNull(bean);
65 assertNotNull(bean.anotherBean);
66 assertSame(bean.anotherBean.getClass(), AnotherBean.class);
67 assertNotNull(bean.myBean);
68 assertNotSame(bean.myBean.getClass(), MySessionScopedBean.class);
69
70 assertThrows(OutOfScopeException.class, () -> bean.myBean.getSession());
71
72 sessionScope.enter();
73 sessionScope.seed(Session.class, this.session);
74 assertNotNull(bean.myBean.getSession());
75 assertNotNull(bean.myBean.getAnotherBean());
76 assertSame(bean.myBean.getAnotherBean().getClass(), AnotherBean.class);
77 assertThrows(TestException.class, () -> bean.myBean.throwException());
78 }
79
80 @Named
81 static class MySingletonBean {
82 @Inject
83 @Named("scoped")
84 BeanItf myBean;
85
86 @Inject
87 @Named("another")
88 BeanItf2 anotherBean;
89 }
90
91 @Named
92 static class MySingletonBean2 {
93 @Inject
94 @Named("scoped")
95 MySessionScopedBean myBean;
96
97 @Inject
98 @Named("another")
99 BeanItf2 anotherBean;
100 }
101
102 interface BeanItf {
103 Session getSession();
104
105 BeanItf2 getAnotherBean();
106
107 void throwException() throws TestException;
108 }
109
110 interface BeanItf2 {}
111
112 @Named("another")
113 @Singleton
114 static class AnotherBean implements BeanItf2 {}
115
116 @Named("scoped")
117 @SessionScoped
118 @Typed
119 static class MySessionScopedBean implements BeanItf {
120 @Inject
121 Session session;
122
123 @Inject
124 BeanItf2 anotherBean;
125
126 public Session getSession() {
127 return session;
128 }
129
130 public BeanItf2 getAnotherBean() {
131 return anotherBean;
132 }
133
134 public void throwException() throws TestException {
135 throw new TestException();
136 }
137 }
138
139 static class TestException extends Exception {}
140 }