Created
January 15, 2016 20:30
-
-
Save vmassol/83db5df84a4041ec018f to your computer and use it in GitHub Desktop.
ExceptionCatchingUberspectorTest
This file contains hidden or 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
| /* | |
| * 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.introspection; | |
| import java.io.StringReader; | |
| import java.io.StringWriter; | |
| import java.util.Properties; | |
| import org.apache.velocity.VelocityContext; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.xwiki.context.Execution; | |
| import org.xwiki.context.ExecutionContext; | |
| import org.xwiki.test.annotation.BeforeComponent; | |
| import org.xwiki.test.annotation.ComponentList; | |
| import org.xwiki.test.mockito.MockitoComponentManagerRule; | |
| import org.xwiki.text.StringUtils; | |
| import org.xwiki.velocity.VelocityEngine; | |
| import org.xwiki.velocity.internal.DefaultVelocityConfiguration; | |
| import org.xwiki.velocity.internal.DefaultVelocityContextFactory; | |
| import org.xwiki.velocity.internal.DefaultVelocityEngine; | |
| import org.xwiki.velocity.internal.VelocityExecutionContextInitializer; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.mockito.Mockito.when; | |
| /** | |
| * Unit tests for {@link ExceptionCatchingUberspector}. | |
| * | |
| * @version $Id$ | |
| * @since 8.0M1 | |
| */ | |
| @ComponentList({ | |
| DefaultVelocityEngine.class, | |
| DefaultVelocityContextFactory.class, | |
| DefaultVelocityConfiguration.class | |
| }) | |
| public class ExceptionCatchingUberspectorTest | |
| { | |
| @Rule | |
| public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); | |
| private VelocityEngine velocityEngine; | |
| private VelocityContext vcontext; | |
| public class ExceptionThrowingClass | |
| { | |
| public void throwException1() throws Exception | |
| { | |
| throw new Exception("exception1"); | |
| } | |
| public void throwException2() throws Exception | |
| { | |
| throw new Exception("exception2"); | |
| } | |
| } | |
| @BeforeComponent | |
| public void setUpComponents() throws Exception | |
| { | |
| this.componentManager.registerMemoryConfigurationSource(); | |
| } | |
| public void setUp(boolean withExecutionComponent) throws Exception | |
| { | |
| this.velocityEngine = this.componentManager.getInstance(VelocityEngine.class); | |
| Properties properties = new Properties(); | |
| // Override default value to not execute all other uberspectors defined in the default configuration | |
| properties.setProperty("runtime.introspector.uberspect.chainClasses", StringUtils.join( | |
| new String[] { SecureUberspector.class.getName(), ExceptionCatchingUberspector.class.getName() }, ',')); | |
| this.vcontext = new VelocityContext(); | |
| this.vcontext.put("var", new ExceptionThrowingClass()); | |
| if (withExecutionComponent) { | |
| Execution execution = this.componentManager.registerMockComponent(Execution.class); | |
| ExecutionContext ec = new ExecutionContext(); | |
| ec.setProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID, this.vcontext); | |
| when(execution.getContext()).thenReturn(ec); | |
| } | |
| this.velocityEngine.initialize(properties); | |
| } | |
| @Test | |
| public void catchExceptionOk() throws Exception | |
| { | |
| setUp(true); | |
| StringWriter writer = new StringWriter(); | |
| this.velocityEngine.evaluate(this.vcontext, writer, "template", | |
| new StringReader("#set ($discard = $var.throwException1())$exception.message")); | |
| assertEquals("exception1", writer.toString()); | |
| } | |
| /** | |
| * Verify that the Exception Catching Uberspector doesn't interfere with the #try() directive. For example with | |
| * the following input the try# directive will save in the {@code $exception} binding the first exception raised, | |
| * whereas if nothing was done to support this in the Exception Catching Uberspector, the exception stored in | |
| * {@code $exception} would be the one raised by the second call! | |
| * <code><pre> | |
| * #try() | |
| * $var.throwException1() | |
| * $var.throwException2() | |
| * #end | |
| * $exception.message | |
| * </pre></code> | |
| */ | |
| @Test | |
| public void catchExceptionWhenInTryDirective() throws Exception | |
| { | |
| setUp(true); | |
| StringWriter writer = new StringWriter(); | |
| this.velocityEngine.evaluate(this.vcontext, writer, "template", | |
| new StringReader("#try()\n" | |
| + "#set ($discard = $var.throwException1())\n" | |
| + "#set ($discard = $var.throwException2())\n" | |
| + "#end\n" | |
| + "$exception.message")); | |
| assertEquals("exception1", writer.toString()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment