Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 12b7bf1

Browse files
committed
code cleanup + more
* remove .named option from some module (useless) * cleanup code around mongodb * simplify morphia integration * more unit tests * going better and better ;)
1 parent 04020ba commit 12b7bf1

File tree

28 files changed

+611
-1018
lines changed

28 files changed

+611
-1018
lines changed

jooby-akka/src/main/java/org/jooby/akka/Akka.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.jooby.Jooby.Module;
2525

2626
import com.google.inject.Binder;
27-
import com.google.inject.Key;
28-
import com.google.inject.name.Names;
2927
import com.typesafe.config.Config;
3028
import com.typesafe.config.ConfigFactory;
3129

@@ -62,8 +60,6 @@
6260
*/
6361
public class Akka implements Module {
6462

65-
private boolean named;
66-
6763
private String name;
6864

6965
/**
@@ -82,25 +78,13 @@ public Akka() {
8278
this("default");
8379
}
8480

85-
/**
86-
* Bind the {@link ActorSystem} system using it's name.
87-
*
88-
* @return This module.
89-
*/
90-
public Akka named() {
91-
named = true;
92-
return this;
93-
}
94-
9581
@Override
9682
public void configure(final Env env, final Config conf, final Binder binder) {
9783
ActorSystem sys = ActorSystem.create(name, conf);
9884

99-
Key<ActorSystem> syskey = Key.get(ActorSystem.class);
100-
if (named) {
101-
syskey = Key.get(ActorSystem.class, Names.named(name));
102-
}
103-
binder.bind(syskey).toInstance(sys);
85+
env.serviceKey().generate(ActorSystem.class, name, syskey -> {
86+
binder.bind(syskey).toInstance(sys);
87+
});
10488
}
10589

10690
@Override

jooby-akka/src/test/java/org/jooby/akka/AkkaTest.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ public void defaults() throws Exception {
3232
.andReturn(unit.get(ActorSystem.class));
3333
})
3434
.expect(unit -> {
35+
Env env = unit.get(Env.class);
36+
expect(env.serviceKey()).andReturn(new Env.ServiceKey());
37+
3538
ActorSystem sys = unit.get(ActorSystem.class);
3639

3740
LinkedBindingBuilder<ActorSystem> lbbSys = unit.mock(LinkedBindingBuilder.class);
3841
lbbSys.toInstance(sys);
42+
lbbSys.toInstance(sys);
3943

4044
Binder binder = unit.get(Binder.class);
4145

46+
expect(binder.bind(Key.get(ActorSystem.class, Names.named("default")))).andReturn(lbbSys);
4247
expect(binder.bind(Key.get(ActorSystem.class))).andReturn(lbbSys);
4348
})
4449
.run(unit -> {
@@ -47,33 +52,6 @@ public void defaults() throws Exception {
4752
});
4853
}
4954

50-
@SuppressWarnings("unchecked")
51-
@Test
52-
public void named() throws Exception {
53-
new MockUnit(Env.class, Config.class, Binder.class, ActorSystem.class)
54-
.expect(unit -> {
55-
unit.mockStatic(ActorSystem.class);
56-
expect(ActorSystem.create("default", unit.get(Config.class)))
57-
.andReturn(unit.get(ActorSystem.class));
58-
})
59-
.expect(unit -> {
60-
61-
ActorSystem sys = unit.get(ActorSystem.class);
62-
63-
LinkedBindingBuilder<ActorSystem> lbbSys = unit.mock(LinkedBindingBuilder.class);
64-
lbbSys.toInstance(sys);
65-
66-
Binder binder = unit.get(Binder.class);
67-
68-
expect(binder.bind(Key.get(ActorSystem.class, Names.named("default"))))
69-
.andReturn(lbbSys);
70-
})
71-
.run(unit -> {
72-
new Akka().named().configure(unit.get(Env.class), unit.get(Config.class),
73-
unit.get(Binder.class));
74-
});
75-
}
76-
7755
@Test
7856
public void config() {
7957
Config config = new Akka().config();

jooby-cassandra/src/main/java/org/jooby/cassandra/Cassandra.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
25-
import java.util.concurrent.atomic.AtomicInteger;
2625
import java.util.function.BiConsumer;
2726
import java.util.function.Consumer;
2827

2928
import org.jooby.Env;
29+
import org.jooby.Env.ServiceKey;
3030
import org.jooby.Jooby.Module;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
@@ -42,8 +42,6 @@
4242
import com.datastax.driver.mapping.MappingManager;
4343
import com.datastax.driver.mapping.annotations.Accessor;
4444
import com.google.inject.Binder;
45-
import com.google.inject.Key;
46-
import com.google.inject.name.Names;
4745
import com.typesafe.config.Config;
4846

4947
import javaslang.Function3;
@@ -268,8 +266,6 @@
268266
*/
269267
public class Cassandra implements Module {
270268

271-
static final AtomicInteger COUNTER = new AtomicInteger(0);
272-
273269
/** The logging system. */
274270
private final Logger log = LoggerFactory.getLogger(getClass());
275271

@@ -377,12 +373,12 @@ public void configure(final Env env, final Config conf, final Binder binder) {
377373
ConnectionString cstr = Try.of(() -> ConnectionString.parse(db))
378374
.getOrElse(() -> ConnectionString.parse(conf.getString(db)));
379375

380-
boolean first = COUNTER.getAndIncrement() == 0;
376+
ServiceKey serviceKey = env.serviceKey();
377+
381378
Function3<Class, String, Object, Void> bind = (type, name, value) -> {
382-
binder.bind(Key.get(type, Names.named(name))).toInstance(value);
383-
if (first) {
384-
binder.bind(Key.get(type)).toInstance(value);
385-
}
379+
serviceKey.generate(type, name, k -> {
380+
binder.bind(k).toInstance(value);
381+
});
386382
return null;
387383
};
388384

jooby-cassandra/src/test/java/org/jooby/cassandra/CassandraSessionStoreTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void create() throws Exception {
139139
new MockUnit(Session.class, org.jooby.Session.class)
140140
.expect(createTable)
141141
.expect(session("sid", 5, 6, 7, ImmutableMap.of("foo", "bar")))
142-
.expect(insertInto(30))
142+
.expect(insertInto(0))
143143
.expect(boundStatement)
144144
.expect(unit -> {
145145
BoundStatement statement = unit.get(BoundStatement.class);
@@ -151,8 +151,8 @@ public void create() throws Exception {
151151
expect(session.execute(statement)).andReturn(null);
152152
})
153153
.run(unit -> {
154-
new CassandraSessionStore(unit.get(Session.class), "30")
155-
.save(unit.get(org.jooby.Session.class));
154+
new CassandraSessionStore(unit.get(Session.class), 0)
155+
.create(unit.get(org.jooby.Session.class));
156156
});
157157
}
158158

@@ -307,9 +307,10 @@ private Block insertInto(final int ttl) {
307307
unit.registerMock(PreparedStatement.class, statement);
308308

309309
Session session = unit.get(Session.class);
310+
String suffix = ttl > 0 ? " USING TTL " + ttl : "";
310311
expect(session.prepare(
311-
"INSERT INTO session (id,createdAt,accessedAt,savedAt,attributes) VALUES (?,?,?,?,?) USING TTL "
312-
+ ttl + ";"))
312+
"INSERT INTO session (id,createdAt,accessedAt,savedAt,attributes) VALUES (?,?,?,?,?)"
313+
+ suffix + ";"))
313314
.andReturn(statement);
314315
};
315316
}

jooby-cassandra/src/test/java/org/jooby/cassandra/CassandraTest.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.easymock.EasyMock.expectLastCall;
55

66
import org.jooby.Env;
7+
import org.jooby.Env.ServiceKey;
78
import org.jooby.Routes;
89
import org.jooby.test.MockUnit;
910
import org.jooby.test.MockUnit.Block;
@@ -94,13 +95,13 @@ public class CassandraTest {
9495

9596
@Test
9697
public void connectViaProperty() throws Exception {
97-
Cassandra.COUNTER.set(0);
9898
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
9999
Configuration.class, Session.class)
100100
.expect(unit -> {
101101
Config conf = unit.get(Config.class);
102102
expect(conf.getString("db")).andReturn("cassandra://localhost/beers");
103103
})
104+
.expect(serviceKey(new Env.ServiceKey()))
104105
.expect(clusterBuilder)
105106
.expect(contactPoints("localhost"))
106107
.expect(port(9042))
@@ -123,12 +124,19 @@ public void connectViaProperty() throws Exception {
123124
});
124125
}
125126

127+
private Block serviceKey(final ServiceKey serviceKey) {
128+
return unit -> {
129+
Env env = unit.get(Env.class);
130+
expect(env.serviceKey()).andReturn(serviceKey);
131+
};
132+
}
133+
126134
@Test
127135
public void connectViaConnectionString() throws Exception {
128-
Cassandra.COUNTER.set(0);
129136
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
130137
Configuration.class, Session.class)
131138
.expect(clusterBuilder)
139+
.expect(serviceKey(new Env.ServiceKey()))
132140
.expect(contactPoints("localhost"))
133141
.expect(port(9042))
134142
.expect(codecRegistry)
@@ -152,10 +160,10 @@ public void connectViaConnectionString() throws Exception {
152160

153161
@Test
154162
public void onStop() throws Exception {
155-
Cassandra.COUNTER.set(0);
156163
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
157164
Configuration.class, Session.class)
158165
.expect(clusterBuilder)
166+
.expect(serviceKey(new Env.ServiceKey()))
159167
.expect(contactPoints("localhost"))
160168
.expect(port(9042))
161169
.expect(codecRegistry)
@@ -188,10 +196,10 @@ public void onStop() throws Exception {
188196

189197
@Test
190198
public void onStopSessionerr() throws Exception {
191-
Cassandra.COUNTER.set(0);
192199
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
193200
Configuration.class, Session.class)
194201
.expect(clusterBuilder)
202+
.expect(serviceKey(new Env.ServiceKey()))
195203
.expect(contactPoints("localhost"))
196204
.expect(port(9042))
197205
.expect(codecRegistry)
@@ -226,11 +234,11 @@ public void onStopSessionerr() throws Exception {
226234
@SuppressWarnings("unchecked")
227235
@Test
228236
public void withAccessor() throws Exception {
229-
Cassandra.COUNTER.set(0);
230237
Object value = new Object();
231238
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
232239
Configuration.class, Session.class)
233240
.expect(clusterBuilder)
241+
.expect(serviceKey(new Env.ServiceKey()))
234242
.expect(contactPoints("localhost"))
235243
.expect(port(9042))
236244
.expect(codecRegistry)
@@ -265,10 +273,10 @@ public void withAccessor() throws Exception {
265273

266274
@Test
267275
public void doWithCluster() throws Exception {
268-
Cassandra.COUNTER.set(0);
269276
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
270277
Configuration.class, Session.class, StateListener.class)
271278
.expect(clusterBuilder)
279+
.expect(serviceKey(new Env.ServiceKey()))
272280
.expect(contactPoints("localhost"))
273281
.expect(port(9042))
274282
.expect(codecRegistry)
@@ -297,10 +305,10 @@ public void doWithCluster() throws Exception {
297305

298306
@Test
299307
public void doWithClusterBuilder() throws Exception {
300-
Cassandra.COUNTER.set(0);
301308
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
302309
Configuration.class, Session.class)
303310
.expect(clusterBuilder)
311+
.expect(serviceKey(new Env.ServiceKey()))
304312
.expect(contactPoints("localhost"))
305313
.expect(port(9042))
306314
.expect(codecRegistry)
@@ -329,29 +337,6 @@ public void doWithClusterBuilder() throws Exception {
329337
});
330338
}
331339

332-
@Test
333-
public void secondInstance() throws Exception {
334-
Cassandra.COUNTER.set(1);
335-
new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class,
336-
Configuration.class, Session.class)
337-
.expect(clusterBuilder)
338-
.expect(contactPoints("localhost"))
339-
.expect(port(9042))
340-
.expect(codecRegistry)
341-
.expect(bind("beers", Cluster.class))
342-
.expect(bind("beers", Session.class))
343-
.expect(connect("beers"))
344-
.expect(mapper)
345-
.expect(bind("beers", MappingManager.class))
346-
.expect(datastore)
347-
.expect(bind("beers", Datastore.class))
348-
.expect(routeMapper).expect(onStop)
349-
.run(unit -> {
350-
new Cassandra("cassandra://localhost/beers")
351-
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
352-
});
353-
}
354-
355340
private Block connect(final String keyspace) {
356341
return unit -> {
357342
Cluster cluster = unit.get(Cluster.class);

jooby-commons-email/src/main/java/org/jooby/mail/CommonsEmail.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import org.apache.commons.mail.MultiPartEmail;
2727
import org.apache.commons.mail.SimpleEmail;
2828
import org.jooby.Env;
29+
import org.jooby.Env.ServiceKey;
2930
import org.jooby.Jooby;
3031
import org.jooby.internal.mail.HtmlEmailProvider;
3132
import org.jooby.internal.mail.ImageHtmlEmailProvider;
3233
import org.jooby.internal.mail.MultiPartEmailProvider;
3334
import org.jooby.internal.mail.SimpleEmailProvider;
3435

3536
import com.google.inject.Binder;
36-
import com.google.inject.Key;
37-
import com.google.inject.name.Names;
3837
import com.typesafe.config.Config;
3938
import com.typesafe.config.ConfigFactory;
4039

@@ -83,8 +82,6 @@ public class CommonsEmail implements Jooby.Module {
8382

8483
private String name;
8584

86-
private boolean named;
87-
8885
/**
8986
* Creates a {@link CommonsEmail}.
9087
*
@@ -101,29 +98,23 @@ public CommonsEmail() {
10198
this("mail");
10299
}
103100

104-
/**
105-
* Call this method if you need two or more mail configuration. This method will bind email
106-
* instances using the provided name.
107-
*
108-
* @return This module.
109-
*/
110-
public CommonsEmail named() {
111-
named = true;
112-
return this;
113-
}
114-
115101
@Override
116102
public void configure(final Env env, final Config config, final Binder binder) {
117103
Config mail = config.getConfig(name).withFallback(config.getConfig("mail"));
118104

119-
binder.bind(key(SimpleEmail.class)).toProvider(new SimpleEmailProvider(mail));
120-
binder.bind(key(HtmlEmail.class)).toProvider(new HtmlEmailProvider(mail));
121-
binder.bind(key(MultiPartEmail.class)).toProvider(new MultiPartEmailProvider(mail));
122-
binder.bind(key(ImageHtmlEmail.class)).toProvider(new ImageHtmlEmailProvider(mail));
123-
}
124-
125-
private <T> Key<T> key(final Class<T> type) {
126-
return named ? Key.get(type, Names.named(name)) : Key.get(type);
105+
ServiceKey serviceKey = env.serviceKey();
106+
serviceKey.generate(SimpleEmail.class, name, k -> {
107+
binder.bind(k).toProvider(new SimpleEmailProvider(mail));
108+
});
109+
serviceKey.generate(HtmlEmail.class, name, k -> {
110+
binder.bind(k).toProvider(new HtmlEmailProvider(mail));
111+
});
112+
serviceKey.generate(MultiPartEmail.class, name, k -> {
113+
binder.bind(k).toProvider(new MultiPartEmailProvider(mail));
114+
});
115+
serviceKey.generate(ImageHtmlEmail.class, name, k -> {
116+
binder.bind(k).toProvider(new ImageHtmlEmailProvider(mail));
117+
});
127118
}
128119

129120
@Override

0 commit comments

Comments
 (0)