Changeset 9524 in josm


Ignore:
Timestamp:
2016-01-18T01:46:48+01:00 (9 years ago)
Author:
Don-vip
Message:

CustomConfigurator - add unit test, javadoc, and use JavaScript engine instead of rhino, gone with Java 8 (replaced by Nashorn)

Location:
trunk
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r9498 r9524  
    9797    }
    9898
     99    /**
     100     * Resets the log.
     101     */
     102    public static void resetLog() {
     103        summary = new StringBuilder();
     104    }
     105
     106    /**
     107     * Read configuration script from XML file, modifying main preferences
     108     * @param dir - directory
     109     * @param fileName - XML file name
     110     */
    99111    public static void readXML(String dir, String fileName) {
    100112        readXML(new File(dir, fileName));
     
    474486            try {
    475487                this.mainPrefs = mainPrefs;
    476                 summary = new StringBuilder();
    477                 engine = new ScriptEngineManager().getEngineByName("rhino");
     488                resetLog();
     489                engine = new ScriptEngineManager().getEngineByName("JavaScript");
    478490                engine.eval("API={}; API.pref={}; API.fragments={};");
    479491
     
    493505            } catch (Exception ex) {
    494506                log("Error: initializing script engine: "+ex.getMessage());
     507                Main.error(ex);
    495508            }
    496509        }
  • trunk/test/unit/org/openstreetmap/josm/data/CustomConfiguratorTest.java

    r9498 r9524  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertTrue;
    67
     
    1011import java.nio.file.Files;
    1112import java.util.Arrays;
     13import java.util.Collections;
    1214
     15import org.junit.Before;
    1316import org.junit.BeforeClass;
    1417import org.junit.Test;
    1518import org.openstreetmap.josm.JOSMFixture;
    1619import org.openstreetmap.josm.Main;
     20import org.openstreetmap.josm.TestUtils;
    1721import org.openstreetmap.josm.tools.Utils;
    1822
     
    2630     */
    2731    @BeforeClass
    28     public static void setUp() {
     32    public static void setUpBeforeClass() {
    2933        JOSMFixture.createUnitTestFixture().init();
     34    }
     35
     36    /**
     37     * Setup test.
     38     */
     39    @Before
     40    public void setUp() {
     41        CustomConfigurator.resetLog();
    3042    }
    3143
     
    7991        Utils.deleteFile(tmp);
    8092    }
     93
     94    /**
     95     * Test method for {@link CustomConfigurator#readXML}.
     96     * @throws IOException if any I/O error occurs
     97     */
     98    @Test
     99    public void testReadXML() throws IOException {
     100        // Test 1 - read(dir, file) + append
     101        Main.pref.putCollection("test", Collections.<String>emptyList());
     102        assertTrue(Main.pref.getCollection("test").isEmpty());
     103        CustomConfigurator.readXML(TestUtils.getTestDataRoot() + "customconfigurator", "append.xml");
     104        String log = CustomConfigurator.getLog();
     105        System.out.println(log);
     106        assertFalse(log.contains("Error"));
     107        assertFalse(Main.pref.getCollection("test").isEmpty());
     108
     109        // Test 1 - read(file, pref) + replace
     110        Preferences pref = new Preferences();
     111        pref.putCollection("lorem_ipsum", Arrays.asList("only 1 string"));
     112        assertEquals(1, pref.getCollection("lorem_ipsum").size());
     113        CustomConfigurator.readXML(new File(TestUtils.getTestDataRoot() + "customconfigurator", "replace.xml"), pref);
     114        log = CustomConfigurator.getLog();
     115        System.out.println(log);
     116        assertFalse(log.contains("Error"));
     117        assertEquals(9, pref.getCollection("lorem_ipsum").size());
     118    }
    81119}
Note: See TracChangeset for help on using the changeset viewer.