Revision 32:39fb4be45035

View differences:

src/main/java/cz/agents/alite/configreader/ConfigReader.java
15 15
import java.net.MalformedURLException;
16 16
import java.net.URI;
17 17
import java.net.URL;
18
import java.nio.file.Files;
19
import java.nio.file.Paths;
18 20
import java.util.List;
19 21
import java.util.Map;
20 22

  
......
28 30
    private final ConfigSlurper slurper;
29 31
    private boolean flatten;
30 32
    private ConfigMap configMap;
33
    private String source = "";
31 34

  
32 35
    public ConfigReader() {
33 36
        config = new ConfigObject();
......
46 49
        }
47 50
    }
48 51

  
52
    public String getSourceConfig() {
53
        return source;
54
    }
55

  
49 56
    @Override
50 57
    public int getInt(String key) {
51 58
        ensureConfigMap();
......
191 198

  
192 199
        GroovySystem.getMetaClassRegistry().removeMetaClass(script.getClass());
193 200
        classLoader.clearCache();
201

  
202
        appendToSource(scriptText);
194 203
    }
195 204

  
196 205
    /**
......
218 227
        //See http://groovy.329449.n5.nabble.com/ConfigSlurper-GroovyClassLoader-memory-leak-td364279.html
219 228
        // for details
220 229

  
221
        URL url = null;
230
        URL url;
222 231
        try {
223 232
            url = file.toURL();
224 233
        } catch (MalformedURLException e) {
......
226 235
        }
227 236

  
228 237
        GroovyClassLoader classLoader = new GroovyClassLoader();
229
        GroovyCodeSource codeSource = null;
230
        Script script = null;
238
        GroovyCodeSource codeSource;
239
        Script script;
231 240
        try {
232 241
            codeSource = new GroovyCodeSource(url);
233 242
            script = (Script) classLoader.parseClass(codeSource).newInstance();
234
        } catch (IOException e) {
235
            throw new ConfigurationLoadException(e);
236
        } catch (InstantiationException e) {
237
            throw new ConfigurationLoadException(e);
238
        } catch (IllegalAccessException e) {
243
        } catch (IOException | InstantiationException | IllegalAccessException e) {
239 244
            throw new ConfigurationLoadException(e);
240 245
        }
241 246

  
......
245 250

  
246 251
        GroovySystem.getMetaClassRegistry().removeMetaClass(script.getClass());
247 252
        classLoader.clearCache();
253

  
254
        try {
255
            List<String> data = Files.readAllLines(Paths.get(file));
256
            appendToSource(String.join("\n", data));
257
        } catch (IOException e) {
258
            throw new ConfigurationLoadException(e);
259
        }
260
    }
261

  
262
    private void appendToSource(String toAppend) {
263
        if (source.isEmpty()) {
264
            source = toAppend;
265
        } else {
266
            source = source + "\n" + toAppend;
267
        }
248 268
    }
249 269

  
250 270
    private void ensureConfigMap() {
src/test/java/cz/agents/alite/configreader/ConfigTest.java
6 6
import org.junit.Test;
7 7

  
8 8
import java.io.ByteArrayOutputStream;
9
import java.io.IOException;
9 10
import java.io.OutputStreamWriter;
10 11
import java.math.BigDecimal;
11 12
import java.net.URI;
13
import java.net.URISyntaxException;
14
import java.nio.file.Files;
15
import java.nio.file.Paths;
12 16
import java.util.*;
13 17

  
14 18
import static org.junit.Assert.*;
......
26 30
    public static final float SOME_FLOAT = 3.14f;
27 31
    public static final int INT_DEFAULT = 21;
28 32
    public static final int SOME_INT = 42;
33
    public static final String TEST_CONFIG_FILE = "/TestConfig.groovy";
29 34

  
30 35
    private ConfigReader testConfig;
31 36
    private final String[] shortKeysArray = new String[]{"someInt", "someIntDoubleFormat", "someFloat", "someDouble", "someBoolean",
32
            "someObject", "someString", "someMap", "mapStringString", "someList"};
37
            "someObject", "someString", "someMap", "mapStringString", "someList", "anotherObject"};
33 38

  
34 39
    @Before
35 40
    public void setUp() throws Exception {
36
        URI file = ConfigTest.class.getResource("/TestConfig.groovy").toURI();
41
        URI file = ConfigTest.class.getResource(TEST_CONFIG_FILE).toURI();
37 42
        testConfig = new ConfigReader();
38 43
        testConfig.loadAndMerge(file);
39 44
    }
......
54 59
    }
55 60

  
56 61
    @Test
62
    public void getSourceConfig() throws IOException, URISyntaxException {
63
        byte[] expected = Files.readAllBytes(Paths.get(ConfigTest.class.getResource(TEST_CONFIG_FILE).toURI()));
64
        byte[] actual = testConfig.getSourceConfig().getBytes();
65
        assertArrayEquals(expected, actual);
66
    }
67

  
68
    @Test
69
    public void getSourceConfigWithLoadAndMerge() throws IOException, URISyntaxException {
70
        URI uri = ConfigTest.class.getResource(TEST_CONFIG_FILE).toURI();
71
        String scriptText = "a.b.c=4;";
72

  
73

  
74
        List<String> expected = new ArrayList<>(Files.readAllLines(Paths.get(uri)));
75
        expected.add(scriptText);
76

  
77
        testConfig.loadAndMergeScriptText(scriptText);
78

  
79
        String actual = testConfig.getSourceConfig();
80
        assertEquals(String.join("\n", expected), actual);
81
    }
82

  
83
    @Test
57 84
    public void testGetInt() throws Exception {
58 85
        int testInt = testConfig.getInt(getWholeKey("someInt"));
59 86
        assertEquals(SOME_INT, testInt);
src/test/resources/TestConfig.groovy
10 10
        someMap = [key1:12, key2:24, key3:36];
11 11
        someList = ["alpha", "beta", "gama"];
12 12
        mapStringString = ["key1String":"value1", "key2String":"value2", "key3String":"value3"];
13
        anotherObject = String.valueOf("object")
13 14
    }
14 15
}

Also available in: Unified diff