/
BootiqueIT.java
113 lines (94 loc) · 3.69 KB
/
BootiqueIT.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
/*
* Licensed to ObjectStyle LLC under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ObjectStyle LLC 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 io.bootique;
import com.google.inject.Injector;
import com.google.inject.Key;
import io.bootique.annotation.Args;
import io.bootique.cli.Cli;
import io.bootique.command.Command;
import io.bootique.command.CommandOutcome;
import io.bootique.it.ItestModuleProvider;
import io.bootique.meta.application.CommandMetadata;
import org.junit.Test;
import java.util.Collection;
import static java.util.Arrays.asList;
import static org.junit.Assert.*;
public class BootiqueIT {
private String[] args = new String[]{"a", "b", "c"};
@Test
public void testExec() {
CommandOutcome outcome = Bootique.app(args).exec();
assertTrue(outcome.isSuccess());
}
@Test
public void testExec_Failure() {
CommandOutcome outcome = Bootique.app("-a").module(b ->
BQCoreModule.extend(b).addCommand(new Command() {
@Override
public CommandOutcome run(Cli cli) {
return CommandOutcome.failed(-1, "it failed");
}
@Override
public CommandMetadata getMetadata() {
return CommandMetadata.builder("acommand").build();
}
})
).exec();
assertFalse(outcome.isSuccess());
assertEquals(-1, outcome.getExitCode());
assertEquals("it failed", outcome.getMessage());
}
@Test
public void testExec_Exception() {
CommandOutcome outcome = Bootique.app("-a").module(b ->
BQCoreModule.extend(b).addCommand(new Command() {
@Override
public CommandOutcome run(Cli cli) {
throw new RuntimeException("test exception");
}
@Override
public CommandMetadata getMetadata() {
return CommandMetadata.builder("acommand").build();
}
})
).exec();
assertFalse(outcome.isSuccess());
assertEquals(1, outcome.getExitCode());
assertNotNull(outcome.getException());
assertEquals("test exception", outcome.getException().getMessage());
}
@Test
public void testAutoLoadedProviders() {
Collection<BQModuleProvider> autoLoaded = Bootique.app(args).autoLoadedProviders();
assertEquals(1, autoLoaded.size());
autoLoaded.forEach(m -> assertTrue(m instanceof ItestModuleProvider));
}
@Test
public void testCreateInjector() {
Injector i = Bootique.app(args).createInjector();
String[] args = i.getInstance(Key.get(String[].class, Args.class));
assertSame(this.args, args);
}
@Test
public void testApp_Collection() {
Injector i = Bootique.app(asList(args)).createInjector();
String[] args = i.getInstance(Key.get(String[].class, Args.class));
assertArrayEquals(this.args, args);
}
}