Skip to content

Commit be17829

Browse files
committed
misc:
* add license header * ensure singleton on redis provider * test for empty const on JvmInfo
1 parent 768990a commit be17829

4 files changed

Lines changed: 101 additions & 56 deletions

File tree

jooby-jedis/src/main/java/org/jooby/jedis/Redis.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,16 @@ public void configure(final Env env, final Config config, final Binder binder) {
218218
if (named) {
219219
binder.bind(JedisPool.class)
220220
.annotatedWith(Names.named(name))
221-
.toProvider(managed).asEagerSingleton();
221+
.toProvider(managed)
222+
.asEagerSingleton();
222223

223-
binder.bind(Jedis.class).annotatedWith(Names.named(name)).toProvider(jedis);
224+
binder.bind(Jedis.class).annotatedWith(Names.named(name)).toProvider(jedis)
225+
.asEagerSingleton();
224226
} else {
225227
binder.bind(JedisPool.class).toProvider(managed).asEagerSingleton();
226228

227-
binder.bind(Jedis.class).toProvider(jedis);
229+
binder.bind(Jedis.class).toProvider(jedis)
230+
.asEagerSingleton();
228231
}
229232
}
230233

jooby-jedis/src/test/java/org/jooby/jedis/RedisTest.java

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,48 @@ public void defaults() throws Exception {
3838
Config config = jedisConfig()
3939
.withValue("db", ConfigValueFactory.fromAnyRef("redis://localhost:6780"));
4040
new MockUnit(Env.class, Binder.class)
41-
.expect(
42-
unit -> {
43-
GenericObjectPoolConfig poolConfig = unit
44-
.mockConstructor(GenericObjectPoolConfig.class);
45-
poolConfig.setBlockWhenExhausted(true);
46-
poolConfig
47-
.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
48-
poolConfig.setJmxEnabled(false);
49-
poolConfig.setJmxNamePrefix("redis-pool");
50-
poolConfig.setLifo(true);
51-
poolConfig.setMaxIdle(10);
52-
poolConfig.setMaxTotal(128);
53-
poolConfig.setMaxWaitMillis(-1);
54-
poolConfig.setMinEvictableIdleTimeMillis(1800000L);
55-
poolConfig.setMinIdle(10);
56-
poolConfig.setNumTestsPerEvictionRun(3);
57-
poolConfig.setSoftMinEvictableIdleTimeMillis(1800000L);
58-
poolConfig.setTestOnBorrow(false);
59-
poolConfig.setTestOnReturn(false);
60-
poolConfig.setTestWhileIdle(false);
61-
poolConfig.setTimeBetweenEvictionRunsMillis(-1);
62-
63-
unit.mockConstructor(
64-
JedisPool.class,
65-
new Class[]{GenericObjectPoolConfig.class, URI.class, int.class },
66-
poolConfig, URI.create("redis://localhost:6780"), 2000);
67-
68-
ScopedBindingBuilder jpSBB = unit.mock(ScopedBindingBuilder.class);
69-
jpSBB.asEagerSingleton();
70-
71-
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
72-
expect(jpABB.toProvider(isA(RedisProvider.class))).andReturn(jpSBB);
73-
74-
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
75-
expect(jABB.toProvider(isA(Provider.class))).andReturn(null);
76-
77-
Binder binder = unit.get(Binder.class);
78-
expect(binder.bind(JedisPool.class)).andReturn(jpABB);
79-
expect(binder.bind(Jedis.class)).andReturn(jABB);
80-
})
41+
.expect(unit -> {
42+
GenericObjectPoolConfig poolConfig = unit
43+
.mockConstructor(GenericObjectPoolConfig.class);
44+
poolConfig.setBlockWhenExhausted(true);
45+
poolConfig
46+
.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
47+
poolConfig.setJmxEnabled(false);
48+
poolConfig.setJmxNamePrefix("redis-pool");
49+
poolConfig.setLifo(true);
50+
poolConfig.setMaxIdle(10);
51+
poolConfig.setMaxTotal(128);
52+
poolConfig.setMaxWaitMillis(-1);
53+
poolConfig.setMinEvictableIdleTimeMillis(1800000L);
54+
poolConfig.setMinIdle(10);
55+
poolConfig.setNumTestsPerEvictionRun(3);
56+
poolConfig.setSoftMinEvictableIdleTimeMillis(1800000L);
57+
poolConfig.setTestOnBorrow(false);
58+
poolConfig.setTestOnReturn(false);
59+
poolConfig.setTestWhileIdle(false);
60+
poolConfig.setTimeBetweenEvictionRunsMillis(-1);
61+
62+
unit.mockConstructor(
63+
JedisPool.class,
64+
new Class[]{GenericObjectPoolConfig.class, URI.class, int.class },
65+
poolConfig, URI.create("redis://localhost:6780"), 2000);
66+
67+
ScopedBindingBuilder jpSBB = unit.mock(ScopedBindingBuilder.class);
68+
jpSBB.asEagerSingleton();
69+
70+
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
71+
expect(jpABB.toProvider(isA(RedisProvider.class))).andReturn(jpSBB);
72+
73+
ScopedBindingBuilder sbbJABB = unit.mock(ScopedBindingBuilder.class);
74+
sbbJABB.asEagerSingleton();
75+
76+
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
77+
expect(jABB.toProvider(isA(Provider.class))).andReturn(sbbJABB);
78+
79+
Binder binder = unit.get(Binder.class);
80+
expect(binder.bind(JedisPool.class)).andReturn(jpABB);
81+
expect(binder.bind(Jedis.class)).andReturn(jABB);
82+
})
8183
.run(unit -> {
8284
new Redis().configure(unit.get(Env.class), config, unit.get(Binder.class));
8385
});
@@ -124,18 +126,24 @@ public void shouldGetJedisInstance() throws Exception {
124126
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
125127
expect(jpABB.toProvider(isA(RedisProvider.class))).andReturn(jpSBB);
126128

129+
ScopedBindingBuilder sbbJABB = unit.mock(ScopedBindingBuilder.class);
130+
sbbJABB.asEagerSingleton();
131+
127132
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
128-
expect(jABB.toProvider(unit.capture(Provider.class))).andReturn(null);
133+
expect(jABB.toProvider(unit.capture(Provider.class))).andReturn(sbbJABB);
129134

130135
Binder binder = unit.get(Binder.class);
131136
expect(binder.bind(JedisPool.class)).andReturn(jpABB);
132137
expect(binder.bind(Jedis.class)).andReturn(jABB);
133138
})
134-
.run(unit -> {
135-
new Redis().configure(unit.get(Env.class), config, unit.get(Binder.class));
136-
}, unit -> {
137-
assertEquals(unit.get(Jedis.class), unit.captured(Provider.class).iterator().next().get());
138-
});
139+
.run(
140+
unit -> {
141+
new Redis().configure(unit.get(Env.class), config, unit.get(Binder.class));
142+
},
143+
unit -> {
144+
assertEquals(unit.get(Jedis.class), unit.captured(Provider.class).iterator().next()
145+
.get());
146+
});
139147
}
140148

141149
@SuppressWarnings("unchecked")
@@ -180,8 +188,11 @@ public void named() throws Exception {
180188
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
181189
expect(jpABB.annotatedWith(Names.named("db"))).andReturn(jpLBB);
182190

191+
ScopedBindingBuilder sbbJABB = unit.mock(ScopedBindingBuilder.class);
192+
sbbJABB.asEagerSingleton();
193+
183194
LinkedBindingBuilder<Jedis> jLBB = unit.mock(LinkedBindingBuilder.class);
184-
expect(jLBB.toProvider(isA(Provider.class))).andReturn(null);
195+
expect(jLBB.toProvider(isA(Provider.class))).andReturn(sbbJABB);
185196

186197
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
187198
expect(jABB.annotatedWith(Names.named("db"))).andReturn(jLBB);
@@ -234,16 +245,21 @@ public void unnamed() throws Exception {
234245
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
235246
expect(jpABB.toProvider(isA(RedisProvider.class))).andReturn(jpSBB);
236247

248+
ScopedBindingBuilder sbbJABB = unit.mock(ScopedBindingBuilder.class);
249+
sbbJABB.asEagerSingleton();
250+
237251
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
238-
expect(jABB.toProvider(isA(Provider.class))).andReturn(null);
252+
expect(jABB.toProvider(isA(Provider.class))).andReturn(sbbJABB);
239253

240254
Binder binder = unit.get(Binder.class);
241255
expect(binder.bind(JedisPool.class)).andReturn(jpABB);
242256
expect(binder.bind(Jedis.class)).andReturn(jABB);
243257
})
244-
.run(unit -> {
245-
new Redis("db1").unnamed().configure(unit.get(Env.class), config, unit.get(Binder.class));
246-
});
258+
.run(
259+
unit -> {
260+
new Redis("db1").unnamed().configure(unit.get(Env.class), config,
261+
unit.get(Binder.class));
262+
});
247263
}
248264

249265
@SuppressWarnings("unchecked")
@@ -286,8 +302,11 @@ public void jedisConfigOverride() throws Exception {
286302
AnnotatedBindingBuilder<JedisPool> jpABB = unit.mock(AnnotatedBindingBuilder.class);
287303
expect(jpABB.toProvider(isA(RedisProvider.class))).andReturn(jpSBB);
288304

305+
ScopedBindingBuilder sbbJABB = unit.mock(ScopedBindingBuilder.class);
306+
sbbJABB.asEagerSingleton();
307+
289308
AnnotatedBindingBuilder<Jedis> jABB = unit.mock(AnnotatedBindingBuilder.class);
290-
expect(jABB.toProvider(isA(Provider.class))).andReturn(null);
309+
expect(jABB.toProvider(isA(Provider.class))).andReturn(sbbJABB);
291310

292311
Binder binder = unit.get(Binder.class);
293312
expect(binder.bind(JedisPool.class)).andReturn(jpABB);
@@ -300,7 +319,7 @@ public void jedisConfigOverride() throws Exception {
300319

301320
@Test
302321
public void defaultConfig() throws Exception {
303-
assertEquals(jedisConfig(), new Redis().config());
322+
assertEquals(jedisConfig(), new Redis().config());
304323
}
305324

306325
private Config jedisConfig() {

jooby/src/main/java/org/jooby/internal/JvmInfo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal;
220

321
import java.lang.management.ManagementFactory;

jooby/src/test/java/org/jooby/internal/JvmInfoTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
@PrepareForTest({JvmInfo.class, ManagementFactory.class })
1717
public class JvmInfoTest {
1818

19+
@Test
20+
public void emptyConstructor() {
21+
new JvmInfo();
22+
}
23+
1924
@Test
2025
public void pid() {
2126
assertTrue(JvmInfo.pid() > 0);

0 commit comments

Comments
 (0)