Skip to content

Commit 0bfb017

Browse files
author
Simone Bordet
committed
Added Guice tests.
1 parent c551c60 commit 0bfb017

4 files changed

Lines changed: 151 additions & 0 deletions

File tree

cometd-java/cometd-java-annotations/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
<version>2.2</version>
8080
<scope>test</scope>
8181
</dependency>
82+
<!-- Guice dependencies -->
83+
<dependency>
84+
<groupId>com.google.inject</groupId>
85+
<artifactId>guice</artifactId>
86+
<version>3.0</version>
87+
<scope>test</scope>
88+
</dependency>
8289
</dependencies>
8390

8491
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.cometd.java.annotation.guice;
2+
3+
import javax.inject.Singleton;
4+
5+
import com.google.inject.AbstractModule;
6+
import org.cometd.bayeux.server.BayeuxServer;
7+
import org.cometd.server.BayeuxServerImpl;
8+
9+
public class CometDModule extends AbstractModule
10+
{
11+
@Override
12+
protected void configure()
13+
{
14+
// Binds but does not call start() yet, since it's not good practice in modules
15+
// (modules do not have a lifecycle: cannot be stopped)
16+
17+
// Specifies that the implementation class must be a singleton.
18+
// This is needed in case a dependency declares to depend on
19+
// BayeuxServerImpl instead of BayeuxServer so that Guice does
20+
// not create one instance for BayeuxServer dependencies and
21+
// one instance for BayeuxServerImpl dependencies.
22+
bind(BayeuxServerImpl.class).in(Singleton.class);
23+
// Binds the interface to the implementation class
24+
bind(BayeuxServer.class).to(BayeuxServerImpl.class);
25+
}
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.cometd.java.annotation.guice;
2+
3+
import com.google.inject.Guice;
4+
import com.google.inject.Injector;
5+
import org.cometd.java.annotation.ServerAnnotationProcessor;
6+
import org.cometd.server.BayeuxServerImpl;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertNotNull;
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class GuiceAnnotationTest
16+
{
17+
@Test
18+
public void testGuiceWiringOfCometDServices() throws Exception
19+
{
20+
// Configure Guice
21+
Injector injector = Guice.createInjector(new CometDModule());
22+
// Manually start BayeuxServer
23+
BayeuxServerImpl bayeuxServer = injector.getInstance(BayeuxServerImpl.class);
24+
bayeuxServer.setOption("logLevel", "3");
25+
bayeuxServer.start();
26+
27+
// Configure services
28+
// Guice does not handle @PostConstruct and @PreDestroy, so we need to handle them
29+
ServerAnnotationProcessor processor = new ServerAnnotationProcessor(bayeuxServer);
30+
GuiceBayeuxService service = injector.getInstance(GuiceBayeuxService.class);
31+
Assert.assertTrue(processor.process(service));
32+
33+
// At this point we're configured properly
34+
// The code above should be put into a ServletContextListener.contextInitialized()
35+
// method, so that it is triggered by the web application lifecycle handling
36+
// and the BayeuxServer instance can be put into the ServletContext
37+
38+
// Test that we're configured properly
39+
Assert.assertNotNull(service);
40+
assertNotNull(service.dependency);
41+
assertNotNull(service.bayeuxServer);
42+
assertNotNull(service.serverSession);
43+
assertTrue(service.active);
44+
assertEquals(1, service.bayeuxServer.getChannel(GuiceBayeuxService.CHANNEL).getSubscribers().size());
45+
46+
// Deconfigure services
47+
// The code below should be put into a ServletContextListener.contextDestroyed()
48+
// method, so that it is triggered by the web application lifecycle handling
49+
Assert.assertTrue(processor.deprocess(service));
50+
// Manually stop the BayeuxServer
51+
bayeuxServer.stop();
52+
53+
assertFalse(service.active);
54+
}
55+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.cometd.java.annotation.guice;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.annotation.PreDestroy;
5+
import javax.inject.Inject;
6+
7+
import org.cometd.bayeux.Message;
8+
import org.cometd.bayeux.server.BayeuxServer;
9+
import org.cometd.bayeux.server.ConfigurableServerChannel;
10+
import org.cometd.bayeux.server.ServerSession;
11+
import org.cometd.java.annotation.Configure;
12+
import org.cometd.java.annotation.Service;
13+
import org.cometd.java.annotation.Session;
14+
import org.cometd.java.annotation.Subscription;
15+
16+
@Service // CometD annotation that marks the class as a CometD service
17+
public class GuiceBayeuxService
18+
{
19+
public static final String CHANNEL = "/foo";
20+
21+
public final Dependency dependency; // Injected by Guice via constructor
22+
@Inject
23+
public BayeuxServer bayeuxServer; // Injected by Guice
24+
public boolean configured;
25+
public boolean active;
26+
@Session
27+
public ServerSession serverSession; // Injected by CometD's annotation processor
28+
29+
@Inject
30+
public GuiceBayeuxService(Dependency dependency)
31+
{
32+
this.dependency = dependency;
33+
}
34+
35+
@PostConstruct
36+
public void start() // Initialization method invoked by Guice
37+
{
38+
if (!configured)
39+
throw new IllegalStateException();
40+
active = true;
41+
}
42+
43+
@PreDestroy
44+
public void stop() // Destruction method invoked by Guice
45+
{
46+
active = false;
47+
}
48+
49+
@Configure(CHANNEL)
50+
private void configureFoo(ConfigurableServerChannel channel)
51+
{
52+
configured = true;
53+
}
54+
55+
@Subscription(CHANNEL)
56+
public void foo(Message message) // Subscription method detected by CometD's annotation processor
57+
{
58+
}
59+
60+
public static class Dependency // Another bean
61+
{
62+
}
63+
}

0 commit comments

Comments
 (0)