-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate Jiffle input variable names according to grammar, escape jav…
…adocs when including Jiffle sources in output
- Loading branch information
Showing
4 changed files
with
188 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...iffle-language/src/test/java/it/geosolutions/jaiext/jiffle/runtime/JiffleRuntimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* JAI-Ext - OpenSource Java Advanced Image Extensions Library | ||
* http://www.geo-solutions.it/ | ||
* Copyright 2018 GeoSolutions | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package it.geosolutions.jaiext.jiffle.runtime; | ||
|
||
import it.geosolutions.jaiext.jiffle.Jiffle; | ||
import it.geosolutions.jaiext.jiffle.JiffleException; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class JiffleRuntimeTest { | ||
|
||
@Test | ||
public void testSkipSource() throws Exception { | ||
Jiffle jiffle = getJiffleWithComment(); | ||
String source = jiffle.getRuntimeSource(false); | ||
|
||
// by default, it does not contain the source | ||
assertFalse(source.contains("This is a comment")); | ||
} | ||
|
||
@Test | ||
public void testEscapeSource() throws Exception { | ||
Jiffle jiffle = getJiffleWithComment(); | ||
String source = jiffle.getRuntimeSource(true); | ||
|
||
// source is in the javadocs, and properly escaped | ||
assertTrue(source.contains("* /* This is a comment */")); | ||
assertTrue(source.contains("* dest=10;")); | ||
} | ||
|
||
private Jiffle getJiffleWithComment() throws JiffleException { | ||
String script = "/* This is a comment */\ndest=10;"; | ||
Jiffle jiffle = new Jiffle(); | ||
jiffle.setScript(script); | ||
Map<String, Jiffle.ImageRole> params = new HashMap<>(); | ||
params.put("dest", Jiffle.ImageRole.DEST); | ||
jiffle.setImageParams(params); | ||
|
||
jiffle.compile(); | ||
return jiffle; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...le/jt-jiffle-language/src/test/java/it/geosolutions/jaiext/jiffle/runtime/ScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* JAI-Ext - OpenSource Java Advanced Image Extensions Library | ||
* http://www.geo-solutions.it/ | ||
* Copyright 2018 GeoSolutions | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package it.geosolutions.jaiext.jiffle.runtime; | ||
|
||
import it.geosolutions.jaiext.jiffle.Jiffle; | ||
import it.geosolutions.jaiext.jiffle.JiffleException; | ||
import it.geosolutions.jaiext.jiffle.parser.JiffleParserException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ScriptTest { | ||
|
||
@Test | ||
public void testValidateSourceImages() throws Exception { | ||
checkInvalidSourceName("a b"); | ||
checkInvalidSourceName("99a"); | ||
checkInvalidSourceName("#abc"); | ||
checkInvalidSourceName("/abc"); | ||
checkInvalidSourceName("{abc"); | ||
} | ||
|
||
@Test | ||
public void testValidateDestinationImages() throws Exception { | ||
checkInvalidDestinationName("a b"); | ||
checkInvalidDestinationName("99a"); | ||
checkInvalidDestinationName("#abc"); | ||
checkInvalidDestinationName("/abc"); | ||
checkInvalidDestinationName("{abc"); | ||
} | ||
|
||
private void checkInvalidSourceName(String name) throws JiffleException { | ||
Jiffle jiffle = new Jiffle(); | ||
jiffle.setScript("dst=0;"); | ||
Map<String, Jiffle.ImageRole> params = new HashMap<>(); | ||
params.put(name, Jiffle.ImageRole.SOURCE); | ||
jiffle.setImageParams(params); | ||
|
||
JiffleParserException exception = | ||
Assert.assertThrows(JiffleParserException.class, () -> jiffle.compile()); | ||
assertEquals("Invalid source image name: " + name, exception.getMessage()); | ||
} | ||
|
||
private void checkInvalidDestinationName(String name) throws JiffleException { | ||
Jiffle jiffle = new Jiffle(); | ||
jiffle.setScript("dst=0;"); | ||
Map<String, Jiffle.ImageRole> params = new HashMap<>(); | ||
params.put(name, Jiffle.ImageRole.DEST); | ||
jiffle.setImageParams(params); | ||
|
||
JiffleParserException exception = | ||
Assert.assertThrows(JiffleParserException.class, () -> jiffle.compile()); | ||
assertEquals("Invalid dest image name: " + name, exception.getMessage()); | ||
} | ||
} |