Skip to content

Commit 01b75ab

Browse files
committed
add unit tests for load()/save() of DataSets
1 parent de9f811 commit 01b75ab

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

src/main/javascript/v2/josm/ds.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ class DataSetUtil {
500500
* <dd class="param-desc">one of the strings <code>osm</code> (Open Street Map XML data),
501501
* <code>osc</code> (Open Street Map change format), or
502502
* <code>osm.gz</code> (Open Street Map XML data,
503-
* compressed with gzip). format is normalized by removing leading and
504-
* trailing whitespace and conversion to lower case.</dd>
503+
* compressed with gzip). The format is normalized: white space is removed and it is
504+
* converted to lower case.</dd>
505505
* </dl>
506506
*
507507
* @example
@@ -515,6 +515,8 @@ class DataSetUtil {
515515
*
516516
* @param {string|java.io.File} source the data source
517517
* @param {object} [options] optional named parameters
518+
*
519+
* @return {module:josm/ds~DataSetUtil} the data set util with the loaded data set
518520
*/
519521
static load (source, options) {
520522
function normalizeFile (source) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)