/
ImplicitEventTest.java
130 lines (116 loc) · 5.95 KB
/
ImplicitEventTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* JBoss, Home of Professional Open Source
* 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.event.implicit;
import static org.jboss.cdi.tck.cdi.Sections.BUILTIN_EVENT;
import static org.jboss.cdi.tck.cdi.Sections.EVENT_TYPES_AND_QUALIFIER_TYPES;
import static org.jboss.cdi.tck.cdi.Sections.PASSIVATION_CAPABLE_DEPENDENCY;
import java.io.IOException;
import java.io.Serializable;
import javax.enterprise.context.Dependent;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Any;
import javax.enterprise.util.TypeLiteral;
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.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;
/**
* Test that there is an implicit event bean for every legal event type and verify its composition.
*
* @author Dan Allen
* @author Martin Kouba
*/
@SpecVersion(spec = "cdi", version = "2.0-PFD")
public class ImplicitEventTest extends AbstractTest {
private static final TypeLiteral<Event<StudentRegisteredEvent>> STUDENT_REGISTERED_EVENT_LITERAL = new TypeLiteral<Event<StudentRegisteredEvent>>() {
};
private static final TypeLiteral<Event<CourseFullEvent>> COURSE_FULL_EVENT_LITERAL = new TypeLiteral<Event<CourseFullEvent>>() {
};
private static final TypeLiteral<Event<AwardEvent>> AWARD_EVENT_LITERAL = new TypeLiteral<Event<AwardEvent>>() {
};
@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder().withTestClassPackage(ImplicitEventTest.class).build();
}
@Test
@SpecAssertion(section = BUILTIN_EVENT, id = "a")
public void testImplicitEventExistsForEachEventType() {
assert getBeans(STUDENT_REGISTERED_EVENT_LITERAL).size() == 1;
assert getBeans(COURSE_FULL_EVENT_LITERAL).size() == 1;
assert getBeans(AWARD_EVENT_LITERAL).size() == 1;
}
@Test
@SpecAssertion(section = BUILTIN_EVENT, id = "b")
public void testImplicitEventHasAllExplicitBindingTypes() {
assert getBeans(AWARD_EVENT_LITERAL, Any.Literal.INSTANCE, new HonorsLiteral()).size() == 1;
}
@SpecAssertion(section = EVENT_TYPES_AND_QUALIFIER_TYPES, id = "i")
public void testImplicitEventHasAnyBinding() {
assert getUniqueBean(STUDENT_REGISTERED_EVENT_LITERAL).getQualifiers().contains(Any.Literal.INSTANCE);
assert getUniqueBean(COURSE_FULL_EVENT_LITERAL).getQualifiers().contains(Any.Literal.INSTANCE);
assert getUniqueBean(AWARD_EVENT_LITERAL).getQualifiers().contains(Any.Literal.INSTANCE);
assert getUniqueBean(AWARD_EVENT_LITERAL, new HonorsLiteral()).getQualifiers().contains(Any.Literal.INSTANCE);
assert getUniqueBean(AWARD_EVENT_LITERAL, Any.Literal.INSTANCE, new HonorsLiteral()) == getUniqueBean(
AWARD_EVENT_LITERAL, new HonorsLiteral());
}
@Test
@SpecAssertion(section = BUILTIN_EVENT, id = "d")
public void testImplicitEventHasDependentScope() {
assert getUniqueBean(STUDENT_REGISTERED_EVENT_LITERAL).getScope().equals(Dependent.class);
}
@Test
@SpecAssertion(section = BUILTIN_EVENT, id = "e")
public void testImplicitEventHasNoName() {
assert getUniqueBean(STUDENT_REGISTERED_EVENT_LITERAL).getName() == null;
}
@Test
@SpecAssertion(section = BUILTIN_EVENT, id = "f")
public void testImplicitEventHasImplementation() {
StudentDirectory directory = getContextualReference(StudentDirectory.class);
directory.reset();
Registration registration = getContextualReference(Registration.class);
assert registration.getInjectedCourseFullEvent() != null;
assert registration.getInjectedStudentRegisteredEvent() != null;
Event<StudentRegisteredEvent> event = registration.getInjectedStudentRegisteredEvent();
Student student = new Student("Dan");
event.fire(new StudentRegisteredEvent(student));
assert directory.getStudents().size() == 1;
assert directory.getStudents().contains(student);
}
@Test
@SpecAssertions({ @SpecAssertion(section = BUILTIN_EVENT, id = "g"),
@SpecAssertion(section = PASSIVATION_CAPABLE_DEPENDENCY, id = "eb") })
public void testImplicitEventIsPassivationCapable() throws IOException, ClassNotFoundException {
StudentDirectory directory = getContextualReference(StudentDirectory.class);
directory.reset();
Registration registration = getContextualReference(Registration.class);
Event<StudentRegisteredEvent> event = registration.getInjectedStudentRegisteredEvent();
assert Serializable.class.isAssignableFrom(event.getClass());
byte[] serializedEvent = passivate(event);
@SuppressWarnings("unchecked")
Event<StudentRegisteredEvent> eventCopy = (Event<StudentRegisteredEvent>) activate(serializedEvent);
// make sure we can still use it
Student student = new Student("Dan");
eventCopy.fire(new StudentRegisteredEvent(student));
assert directory.getStudents().size() == 1;
assert directory.getStudents().contains(student);
}
}