-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XCOMMONS-2830: Introduce a utility component to check if a string con…
…tains Velocity code * Introduce an internal VelocityDetector role and a corresponding default implementation including a test.
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...mons-velocity/src/main/java/org/xwiki/velocity/internal/util/DefaultVelocityDetector.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,41 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.velocity.internal.util; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.xwiki.component.annotation.Component; | ||
|
||
/** | ||
* Default implementation of {@link VelocityDetector}. | ||
* | ||
* @version $Id$ | ||
* @since 15.9RC1 | ||
*/ | ||
@Component | ||
@Singleton | ||
public class DefaultVelocityDetector implements VelocityDetector | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
michitux
Author
Contributor
|
||
{ | ||
@Override | ||
public boolean containsVelocityScript(String input) | ||
{ | ||
return input.contains("#") || input.contains("$"); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...iki-commons-velocity/src/main/java/org/xwiki/velocity/internal/util/VelocityDetector.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,40 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.velocity.internal.util; | ||
|
||
import org.xwiki.component.annotation.Role; | ||
|
||
/** | ||
* Utility role to detect velocity scripts. | ||
* | ||
* @version $Id$ | ||
* @since 15.9RC1 | ||
*/ | ||
@Role | ||
public interface VelocityDetector | ||
{ | ||
/** | ||
* Checks if a string contains a velocity script. | ||
* | ||
* @param input the string to check | ||
* @return true if the string contains a velocity script, false otherwise | ||
*/ | ||
boolean containsVelocityScript(String input); | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...-velocity/src/test/java/org/xwiki/velocity/internal/util/DefaultVelocityDetectorTest.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,52 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.velocity.internal.util; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.xwiki.test.junit5.mockito.ComponentTest; | ||
import org.xwiki.test.junit5.mockito.InjectMockComponents; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* Tests for {@link DefaultVelocityDetector}. | ||
* | ||
* @version $Id$ | ||
*/ | ||
@ComponentTest | ||
class DefaultVelocityDetectorTest | ||
{ | ||
@InjectMockComponents | ||
private DefaultVelocityDetector detector; | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"true, #set($foo = 'bar')", | ||
"true, Hello$foo", | ||
"true, Hello #XWiki", | ||
"false, Hello World", | ||
"false, 42'foo'" | ||
}) | ||
void containsVelocityScript(boolean isVelocity, String input) | ||
{ | ||
assertEquals(isVelocity, this.detector.containsVelocityScript(input), input); | ||
} | ||
} |
Honestly, if it's internal, I'm not sure it worth having an interface role and a class implementation.