|
| 1 | +package org.openstreetmap.josm.plugins.scripting.graalvm.api |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach |
| 4 | +import org.junit.jupiter.api.BeforeEach |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.openstreetmap.josm.plugins.scripting.graalvm.AbstractGraalVMBasedTest |
| 7 | +import org.openstreetmap.josm.plugins.scripting.graalvm.GraalVMFacadeFactory |
| 8 | +import org.openstreetmap.josm.plugins.scripting.graalvm.IGraalVMFacade |
| 9 | + |
| 10 | +class DataSetInputOutputTest extends AbstractGraalVMBasedTest{ |
| 11 | + private IGraalVMFacade facade |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + void initGraalVMFacade() { |
| 15 | + facade = GraalVMFacadeFactory.getOrCreateGraalVMFacade() |
| 16 | + } |
| 17 | + |
| 18 | + @AfterEach |
| 19 | + void resetGraalVMFacade() { |
| 20 | + if (facade != null) { |
| 21 | + facade.resetContext() |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void "can load an osm file - default options"() { |
| 27 | + final file = new File( |
| 28 | + getProjectHome(), |
| 29 | + "src/test/resources/sample-data-files/test-josm-open.osm") |
| 30 | + |
| 31 | + final src = """ |
| 32 | + const josm = require('josm') |
| 33 | + const {DataSetUtil} = require('josm/ds') |
| 34 | + |
| 35 | + DataSetUtil.load('$file.absolutePath') |
| 36 | + """ |
| 37 | + facade.eval(graalJSDescriptor, src) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void "can load an osm file, with File object - default options"() { |
| 42 | + final file = new File( |
| 43 | + getProjectHome(), |
| 44 | + "src/test/resources/sample-data-files/test-josm-open.osm") |
| 45 | + |
| 46 | + final src = """ |
| 47 | + const josm = require('josm') |
| 48 | + const {DataSetUtil} = require('josm/ds') |
| 49 | + const File = Java.type('java.io.File') |
| 50 | + const file = new File('$file.absolutePath') |
| 51 | + DataSetUtil.load(file) |
| 52 | + """ |
| 53 | + facade.eval(graalJSDescriptor, src) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void "can load an osm file - with format option"() { |
| 58 | + final file = new File( |
| 59 | + getProjectHome(), |
| 60 | + "src/test/resources/sample-data-files/test-josm-open.osm") |
| 61 | + |
| 62 | + final src = """ |
| 63 | + const josm = require('josm') |
| 64 | + const {DataSetUtil} = require('josm/ds') |
| 65 | + |
| 66 | + DataSetUtil.load('$file.absolutePath', {format: 'osm'}) |
| 67 | + """ |
| 68 | + facade.eval(graalJSDescriptor, src) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void "should fail if format isn't matching with content"() { |
| 73 | + final file = new File( |
| 74 | + getProjectHome(), |
| 75 | + "src/test/resources/sample-data-files/test-josm-open.osm") |
| 76 | + |
| 77 | + final src = """ |
| 78 | + const josm = require('josm') |
| 79 | + const {DataSetUtil} = require('josm/ds') |
| 80 | + |
| 81 | + DataSetUtil.load('$file.absolutePath', {format: 'osm.gz'}) |
| 82 | + """ |
| 83 | + shouldFail(Throwable) { |
| 84 | + facade.eval(graalJSDescriptor, src) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void "can load a file, save it, and load it again"() { |
| 90 | + final file = new File( |
| 91 | + getProjectHome(), |
| 92 | + "src/test/resources/sample-data-files/test-josm-open.osm") |
| 93 | + final tempOutputFile = File.createTempFile("test-josm-open", ".tmp") |
| 94 | + |
| 95 | + final src = """ |
| 96 | + const josm = require('josm') |
| 97 | + const {DataSetUtil} = require('josm/ds') |
| 98 | + |
| 99 | + const loadedData = DataSetUtil.load('$file.absolutePath') |
| 100 | + loadedData.save('$tempOutputFile.absolutePath') |
| 101 | + const loadedAgain = DataSetUtil.load('$tempOutputFile.absolutePath', {format: 'osm'}) |
| 102 | + """ |
| 103 | + facade.eval(graalJSDescriptor, src) |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments