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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.name.Names;
import com.yahoo.jdisc.test.TestDriver;
import org.junit.Test;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class ContainerBuilderTestCase {
@Test
public void requireThatAccessorsWork() throws URISyntaxException {
final Object obj = new Object();
TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() {
@Override
protected void configure() {
bind(Object.class).toInstance(obj);
bind(String.class).annotatedWith(Names.named("foo")).toInstance("foo");
}
});
ContainerBuilder builder = driver.newContainerBuilder();
assertSame(obj, builder.getInstance(Object.class));
assertEquals("foo", builder.getInstance(Key.get(String.class, Names.named("foo"))));
Object ctx = new Object();
builder.setAppContext(ctx);
assertSame(ctx, builder.appContext());
assertTrue(driver.close());
}
@Test
public void requireThatContainerThreadFactoryIsBound() {
TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
ContainerBuilder builder = driver.newContainerBuilder();
assertSame(ContainerThread.Factory.class, builder.getInstance(ThreadFactory.class).getClass());
assertTrue(driver.close());
}
@Test
public void requireThatThreadFactoryCanBeReconfigured() {
final ThreadFactory factory = Executors.defaultThreadFactory();
TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
ContainerBuilder builder = driver.newContainerBuilder();
builder.guiceModules().install(new AbstractModule() {
@Override
protected void configure() {
bind(ThreadFactory.class).toInstance(factory);
}
});
assertSame(factory, builder.getInstance(ThreadFactory.class));
assertTrue(driver.close());
}
@Test
public void requireThatBindingSetsAreCreatedOnDemand() {
TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
ContainerBuilder builder = driver.newContainerBuilder();
BindingRepository<?> repo = builder.serverBindings("foo");
assertNotNull(repo);
assertSame(repo, builder.serverBindings("foo"));
assertNotNull(repo = builder.serverBindings("bar"));
assertSame(repo, builder.serverBindings("bar"));
assertNotNull(repo = builder.clientBindings("baz"));
assertSame(repo, builder.clientBindings("baz"));
assertNotNull(repo = builder.clientBindings("cox"));
assertSame(repo, builder.clientBindings("cox"));
driver.close();
}
@Test
public void requireThatSafeClassCastWorks() {
ContainerBuilder.safeClassCast(Integer.class, Integer.class);
}
@Test
public void requireThatSafeClassCastThrowsIllegalArgument() {
try {
ContainerBuilder.safeClassCast(Integer.class, Double.class);
fail();
} catch (IllegalArgumentException e) {
}
}
@Test
public void requireThatSafeStringSplitWorks() {
assertTrue(ContainerBuilder.safeStringSplit(new Object(), ",").isEmpty());
assertTrue(ContainerBuilder.safeStringSplit("", ",").isEmpty());
assertTrue(ContainerBuilder.safeStringSplit(" \f\n\r\t", ",").isEmpty());
assertEquals(Arrays.asList("foo"), ContainerBuilder.safeStringSplit("foo", ","));
assertEquals(Arrays.asList("foo"), ContainerBuilder.safeStringSplit(" foo", ","));
assertEquals(Arrays.asList("foo"), ContainerBuilder.safeStringSplit("foo ", ","));
assertEquals(Arrays.asList("foo"), ContainerBuilder.safeStringSplit("foo, ", ","));
assertEquals(Arrays.asList("foo"), ContainerBuilder.safeStringSplit("foo ,", ","));
assertEquals(Arrays.asList("foo", "bar"), ContainerBuilder.safeStringSplit("foo, bar", ","));
}
}
|