/
LoggerInjectionTest.java
141 lines (124 loc) · 4.22 KB
/
LoggerInjectionTest.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
131
132
133
134
135
136
137
138
139
140
141
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc. and/or its affiliates, 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.seam.solder.test.log;
import static org.jboss.seam.solder.test.util.Deployments.baseDeployment;
import static org.junit.Assert.assertEquals;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import junit.framework.Assert;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.seam.solder.log.LoggerExtension;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Verifies the integration with JBoss Logging, for both raw and type-safe injections.
* <p/>
* <p>
* NOTE: Some of these tests must be verified manually as we have no way to plug
* in a mock logger.
* </p>
*
* @author David Allen
* @author Dan Allen
*/
@RunWith(Arquillian.class)
public class LoggerInjectionTest
{
@Deployment
public static Archive<?> deployment()
{
return baseDeployment()
.addManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addPackage(LoggerExtension.class.getPackage())
.addPackage(LoggerInjectionTest.class.getPackage());
}
@Test
public void testLoggerInjection(Sparrow sparrow)
{
sparrow.generateLogMessage();
Assert.assertEquals(Sparrow.class.getName(), sparrow.getLogger().getName());
}
@Test
public void testLoggerInjectionWithCategory(Finch finch)
{
finch.generateLogMessage();
Assert.assertEquals("Finch", finch.getLogger().getName());
}
@Test
public void testLoggerInjectionWithTypedCategory(Wren wren)
{
wren.generateLogMessage();
Assert.assertEquals(BirdLogger.class.getName(), wren.getLogger().getName());
}
@Test
public void testLoggerInjectionWithSuffix(Raven raven)
{
raven.generateLogMessage();
Assert.assertEquals(Raven.class.getName() + ".log", raven.getLogger().getName());
}
@Test
public void testLoggerInjectionIntoNonBean(BeanManager bm)
{
NonBean nonBean = new NonBean();
InjectionTarget<NonBean> target = bm.createInjectionTarget(bm.createAnnotatedType(NonBean.class));
CreationalContext<NonBean> cc = bm.createCreationalContext(null);
try
{
target.inject(nonBean, cc);
// this will cause a NullPointerException if the injection does not occur
nonBean.logMessage();
}
finally
{
cc.release();
}
}
@Test(expected = IllegalStateException.class)
public void testMessageLoggerInjection(Instance<Owl> owlResolver)
{
owlResolver.get().generateLogMessage();
}
@Test
public void testMessageLoggerInjectionWithCategory(Hawk hawk)
{
hawk.generateLogMessage();
}
/**
* BaldEagle declares a passivating scope and therefore must not have any non-serializable dependencies. This test will fail
* deployment if the type-safe logger producer is not serializable. (see SOLDER-81)
*/
@Test
public void testMessageLoggerInjectionOnPassivatingBean(BaldEagle baldEagle)
{
baldEagle.generateLogMessage();
}
@Test
public void testMessageBundleInjection(Jay jay)
{
assertEquals("Spotted 8 jays", jay.getMessage());
}
@Test
public void testMessageBundleLocaleInjection(FrenchJay jay)
{
assertEquals("Spotted 4 geais", jay.getMessage());
}
}