/
MainIoCTest.java
169 lines (137 loc) · 6.08 KB
/
MainIoCTest.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.main;
import org.apache.camel.BeanInject;
import org.apache.camel.BindToRegistry;
import org.apache.camel.CamelConfiguration;
import org.apache.camel.CamelContext;
import org.apache.camel.PropertyInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.direct.DirectComponent;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.component.seda.BlockingQueueFactory;
import org.apache.camel.component.seda.PriorityBlockingQueueFactory;
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.model.ModelCamelContext;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MainIoCTest {
@Test
public void testMainIoC() throws Exception {
// use configuration class
Main main = new Main();
// add the configuration
main.configure().withConfigurations(MyConfiguration.class);
// add as class so we get IoC
main.configure().addRoutesBuilder(MyRouteBuilder.class);
// manually bind
main.bind("myBar", new MyBar());
// should be null before init
assertNull(main.getCamelContext());
// for testing that we can init camel and it has loaded configuration and routes and whatnot
main.init();
// and now its created
assertNotNull(main.getCamelContext());
// should be 1 route model
assertEquals(1, ((ModelCamelContext) main.getCamelContext()).getRouteDefinitions().size());
// and the configuration should have registered beans
assertNotNull(main.getCamelContext().getRegistry().lookupByName("MyCoolBean"));
assertEquals("Tiger", main.getCamelContext().getRegistry().lookupByName("coolStuff"));
// start it
main.start();
CamelContext camelContext = main.getCamelContext();
assertNotNull(camelContext);
MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
endpoint.expectedBodiesReceived("World");
main.getCamelTemplate().sendBody("direct:start", "<message>1</message>");
endpoint.assertIsSatisfied();
// should also auto-configure direct/seda components from application.properties
SedaComponent seda = camelContext.getComponent("seda", SedaComponent.class);
assertEquals(500, seda.getQueueSize());
assertEquals(2, seda.getConcurrentConsumers());
DirectComponent direct = camelContext.getComponent("direct", DirectComponent.class);
assertEquals(1234, direct.getTimeout());
// should have called the configure class
assertEquals("123", camelContext.getGlobalOptions().get("foo"));
// and seda should be created and use the custom queue factory
Object qf = seda.getDefaultQueueFactory();
assertNotNull(qf);
assertInstanceOf(PriorityBlockingQueueFactory.class, qf);
assertSame(camelContext, seda.getCamelContext());
MyConfiguration.MyCoolBean mcb = (MyConfiguration.MyCoolBean) camelContext.getRegistry().lookupByName("MyCoolBean");
assertNotNull(mcb);
assertEquals("Tiger", mcb.getName());
Object cool = camelContext.getRegistry().lookupByName("coolStuff");
assertNotNull(cool);
assertEquals("Tiger", cool);
main.stop();
}
public static class MyBar {
// noop
}
public static class MyConfiguration implements CamelConfiguration {
@BeanInject
private CamelContext camel;
@BindToRegistry
public static class MyCoolBean {
private String name = "Tiger";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@BindToRegistry("myQF")
public BlockingQueueFactory queueFactory(CamelContext myCamel) {
// we can optionally include camel context as parameter
assertNotNull(myCamel);
return new PriorityBlockingQueueFactory();
}
@BindToRegistry
public SedaComponent seda(@BeanInject BlockingQueueFactory qf) {
SedaComponent seda = new SedaComponent();
seda.setDefaultQueueFactory(qf);
return seda;
}
@BindToRegistry("coolStuff")
public String cool(
@BeanInject MyCoolBean cool,
@PropertyInject(value = "magic", defaultValue = "456") int num,
@BeanInject("myBar") MyBar bar) {
// should lookup MyCoolBean type from the registry and find the property
assertNotNull(cool);
assertEquals(456, num);
assertNotNull(bar);
return cool.getName();
}
@Override
public void configure(CamelContext context) {
camel.getGlobalOptions().put("foo", "123");
}
}
public static class MyRouteBuilder extends RouteBuilder {
// properties is automatic loaded from classpath:application.properties
// so we should be able to inject this field
@PropertyInject(value = "hello")
private String hello;
@Override
public void configure() {
from("direct:start").transform().constant(hello).to("mock:results");
}
}
}