Skip to content

Commit

Permalink
XCOMMONS-2830: Introduce a utility component to check if a string con…
Browse files Browse the repository at this point in the history
…tains Velocity code

* Introduce an internal VelocityDetector role and a corresponding
  default implementation including a test.
  • Loading branch information
michitux committed Oct 20, 2023
1 parent 2bc9a52 commit 9bcd360
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
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.

Copy link
@tmortagne

tmortagne Oct 20, 2023

Member

Honestly, if it's internal, I'm not sure it worth having an interface role and a class implementation.

This comment has been minimized.

Copy link
@michitux

michitux Oct 20, 2023

Author Contributor

Does it have any disadvantages? I just thought it would be cleaner also in case we want to make this an actual API in the future.

This comment has been minimized.

Copy link
@tmortagne

tmortagne Oct 20, 2023

Member

Does it have any disadvantages?

Just a bit of a pain to add new helpers in it.

This comment has been minimized.

Copy link
@michitux

michitux Oct 20, 2023

Author Contributor

Okay, I don't really care, I'll just remove the interface again.

This comment has been minimized.

Copy link
@michitux

michitux Oct 20, 2023

Author Contributor

Done in 245cefc.

{
@Override
public boolean containsVelocityScript(String input)
{
return input.contains("#") || input.contains("$");
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ org.xwiki.velocity.internal.DefaultVelocityContextFactory
org.xwiki.velocity.internal.DefaultVelocityManager
org.xwiki.velocity.internal.InternalVelocityEngine
org.xwiki.velocity.internal.ServicesVelocityContextInitializer
org.xwiki.velocity.internal.util.DefaultVelocityDetector
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);
}
}

0 comments on commit 9bcd360

Please sign in to comment.