This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
OptionsUtilTest.java
137 lines (115 loc) · 4.82 KB
/
OptionsUtilTest.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
package org.zanata.client.commands;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Question;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Warning;
import static org.zanata.client.commands.FileMappingRuleHandler.Placeholders.allHolders;
import static org.zanata.client.commands.Messages._;
import java.io.File;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.ZanataConfig;
import com.google.common.collect.Lists;
public class OptionsUtilTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private ConfigurableProjectOptions opts;
private ZanataConfig config;
@Mock
private ConsoleInteractor console;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
opts = new ConfigurableProjectOptionsImpl() {
@Override
public ZanataCommand initCommand() {
return null;
}
@Override
public String getCommandName() {
return "testCommand";
}
@Override
public String getCommandDescription() {
return "testing";
}
};
config = new ZanataConfig();
}
@Test
public void willApplyConfigFromFileIfNotSetInOptions() throws Exception {
// Given: options are not set and exists in zanata config
config.setSrcDir("a");
config.setTransDir("b");
config.setIncludes("*.properties");
config.setExcludes("a.properties");
// When:
OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);
OptionsUtil.applyIncludesAndExcludesFromProjectConfig(opts, config);
// Then:
assertThat(opts.getSrcDir(), Matchers.equalTo(new File("a")));
assertThat(opts.getTransDir(), Matchers.equalTo(new File("b")));
assertThat(opts.getIncludes(), Matchers.contains("*.properties"));
assertThat(opts.getExcludes(), Matchers.contains("a.properties"));
}
@Test
public void willSetToDefaultValueIfNeitherHasValue() {
OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);
assertThat(opts.getSrcDir(), Matchers.equalTo(new File(".")));
assertThat(opts.getTransDir(), Matchers.equalTo(new File(".")));
}
@Test
public void optionTakesPrecedenceOverConfig() {
// Given: options are set in both places
opts.setSrcDir(new File("pot"));
opts.setTransDir(new File("."));
opts.setIncludes("*.properties");
opts.setExcludes("a.properties,b.properties");
config.setSrcDir("a");
config.setTransDir("b");
config.setIncludes("b.b");
config.setExcludes("e,f");
// When:
OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);
OptionsUtil.applyIncludesAndExcludesFromProjectConfig(opts, config);
// Then:
assertThat(opts.getSrcDir(), Matchers.equalTo(new File("pot")));
assertThat(opts.getTransDir(), Matchers.equalTo(new File(".")));
assertThat(opts.getIncludes(), Matchers.contains("*.properties"));
assertThat(opts.getExcludes(),
Matchers.contains("a.properties", "b.properties"));
}
@Test
public void willWarnUserIfRuleSeemsWrong() {
opts.setInteractiveMode(false);
String rule = "{foo}/{bar}/{locale}";
opts.setFileMappingRules(Lists.newArrayList(new FileMappingRule(rule)));
OptionsUtil.checkPotentialMistakesInRules(opts, console);
verify(console).printfln(Warning, _("unrecognized.variables"),
allHolders(), rule);
}
@Test
public void willAskUserToConfirmIfRuleSeemsWrongAndInInteractiveMode() {
opts.setInteractiveMode(true);
String rule = "{foo}/{bar}/{locale}";
opts.setFileMappingRules(Lists.newArrayList(new FileMappingRule(rule)));
OptionsUtil.checkPotentialMistakesInRules(opts, console);
verify(console).printfln(Warning, _("unrecognized.variables"),
allHolders(), rule);
verify(console).printfln(Question, _("confirm.rule"));
}
@Test
public void willThrowExceptionIfRuleIsInvalid() {
expectedException.expect(IllegalStateException.class);
String rule = "{filename}/{path}";
opts.setFileMappingRules(Lists.newArrayList(new FileMappingRule(rule)));
OptionsUtil.checkPotentialMistakesInRules(opts, console);
verify(console).printfln(Warning, _("invalid.rule"), rule);
}
}