Skip to content

Commit 3a52734

Browse files
Check existence of option key
1 parent 85159d8 commit 3a52734

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/main/java/pascal/taie/config/AnalysisOptions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,23 @@ void update(AnalysisOptions options) {
7474
this.options.putAll(options.options);
7575
}
7676

77+
/**
78+
* @return {@code true} if this AnalysisOptions contains value
79+
* for given option key.
80+
*/
81+
public boolean has(String key) {
82+
return options.containsKey(key);
83+
}
84+
85+
/**
86+
* @return value for given option key.
87+
* @throws ConfigException if this AnalysisOptions do not contain the key.
88+
*/
7789
public Object get(String key) {
90+
if (!has(key)) {
91+
throw new ConfigException("Cannot find option '" + key + "'," +
92+
" please check your configuration and option key");
93+
}
7894
return options.get(key);
7995
}
8096

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
5+
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package pascal.taie.config;
24+
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
public class AnalysisOptionsTest {
32+
33+
@Test
34+
public void testNullOption() {
35+
AnalysisOptions options = new AnalysisOptions(getOptionValues());
36+
Assert.assertNull(options.get("z"));
37+
}
38+
39+
@Test(expected = ConfigException.class)
40+
public void testNonExistOption() {
41+
AnalysisOptions options = new AnalysisOptions(getOptionValues());
42+
options.get("non-exist-key");
43+
}
44+
45+
private Map<String, Object> getOptionValues() {
46+
Map<String, Object> values = new HashMap<>();
47+
values.put("x", 100);
48+
values.put("y", "666");
49+
values.put("z", null);
50+
return values;
51+
}
52+
}

0 commit comments

Comments
 (0)