-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12b8666
commit 866e2a3
Showing
4 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
core/test/org/tangram/view/velocity/test/IncludeDirectiveTest.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,101 @@ | ||
/* | ||
* | ||
* Copyright 2016 Martin Goellnitz | ||
* | ||
* This program 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 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.tangram.view.velocity.test; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.apache.commons.io.output.StringBuilderWriter; | ||
import org.apache.velocity.context.Context; | ||
import org.apache.velocity.context.InternalContextAdapter; | ||
import org.apache.velocity.context.InternalContextAdapterImpl; | ||
import org.apache.velocity.runtime.RuntimeInstance; | ||
import org.apache.velocity.runtime.directive.DirectiveConstants; | ||
import org.apache.velocity.runtime.parser.node.Node; | ||
import org.mockito.Mockito; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.mock.web.MockServletContext; | ||
import org.tangram.Constants; | ||
import org.tangram.view.DefaultViewContextFactory; | ||
import org.tangram.view.ViewContextFactory; | ||
import org.tangram.view.ViewUtilities; | ||
import org.tangram.view.velocity.IncludeDirective; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
/** | ||
* Test aspects of the velocity directive to include views for beans. | ||
*/ | ||
public class IncludeDirectiveTest { | ||
|
||
private final ViewContextFactory contextFactory = new DefaultViewContextFactory(); | ||
|
||
|
||
@Test | ||
public void testIncludeDirective() { | ||
String bean = "linkit"; | ||
String view = "default"; | ||
String url = "/testapp/id_RootTopic:1"; | ||
|
||
IncludeDirective includeDirective = new IncludeDirective(); | ||
Assert.assertEquals(includeDirective.getName(), "include", "Unexpected name for link directive."); | ||
Assert.assertEquals(includeDirective.getType(), DirectiveConstants.LINE, "Unexpected type for include directive."); | ||
StringBuilder output = new StringBuilder(); | ||
StringBuilderWriter writer = new StringBuilderWriter(output); | ||
|
||
Context c = Mockito.mock(Context.class); | ||
InternalContextAdapter context = new InternalContextAdapterImpl(c); | ||
|
||
ServletContext app = new MockServletContext(); | ||
HttpServletRequest request = new MockHttpServletRequest(app, "GET", url); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
ViewUtilities viewUtilities = Mockito.mock(ViewUtilities.class); | ||
Mockito.when(viewUtilities.getViewContextFactory()).thenReturn(contextFactory); | ||
|
||
String[] keys = {Constants.ATTRIBUTE_REQUEST, Constants.ATTRIBUTE_RESPONSE, Constants.ATTRIBUTE_VIEW_UTILITIES}; | ||
Mockito.when(context.get(Constants.ATTRIBUTE_REQUEST)).thenReturn(request); | ||
Mockito.when(context.get(Constants.ATTRIBUTE_RESPONSE)).thenReturn(response); | ||
Mockito.when(context.get(Constants.ATTRIBUTE_VIEW_UTILITIES)).thenReturn(viewUtilities); | ||
Mockito.when(context.getKeys()).thenReturn(keys); | ||
|
||
Node node = Mockito.mock(Node.class); | ||
Node beanNode = Mockito.mock(Node.class); | ||
Node viewNode = Mockito.mock(Node.class); | ||
Mockito.when(beanNode.value(context)).thenReturn(bean); | ||
Mockito.when(viewNode.value(context)).thenReturn(view); | ||
Mockito.when(node.jjtGetChild(0)).thenReturn(beanNode); | ||
Mockito.when(node.jjtGetChild(1)).thenReturn(viewNode); | ||
Mockito.when(node.jjtGetNumChildren()).thenReturn(2); | ||
|
||
RuntimeInstance runtime = new RuntimeInstance(); | ||
runtime.addDirective(includeDirective); | ||
includeDirective.init(runtime, context, node); | ||
|
||
boolean result = true; | ||
try { | ||
result = includeDirective.render(context, writer, node); | ||
} catch (Throwable t) { | ||
Assert.fail("Cannot handle request.", t); | ||
} // try/catch | ||
Assert.assertFalse(result, "Include directive should return false."); | ||
Assert.assertEquals(writer.toString(), "", "Unexpected result on writer."); | ||
} // testIncludeDirective() | ||
|
||
} // IncludeDirectiveTest |
108 changes: 108 additions & 0 deletions
108
core/test/org/tangram/view/velocity/test/LinkDirectiveTest.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,108 @@ | ||
/* | ||
* | ||
* Copyright 2016 Martin Goellnitz | ||
* | ||
* This program 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 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.tangram.view.velocity.test; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.apache.commons.io.output.StringBuilderWriter; | ||
import org.apache.velocity.context.Context; | ||
import org.apache.velocity.context.InternalContextAdapter; | ||
import org.apache.velocity.context.InternalContextAdapterImpl; | ||
import org.apache.velocity.runtime.RuntimeInstance; | ||
import org.apache.velocity.runtime.directive.DirectiveConstants; | ||
import org.apache.velocity.runtime.parser.node.Node; | ||
import org.mockito.Mockito; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.mock.web.MockServletContext; | ||
import org.tangram.Constants; | ||
import org.tangram.link.Link; | ||
import org.tangram.link.LinkFactoryAggregator; | ||
import org.tangram.view.velocity.LinkDirective; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
/** | ||
* Test aspects of the velocity directive to generate and output links. | ||
*/ | ||
public class LinkDirectiveTest { | ||
|
||
@Test | ||
public void testLinkDirective() { | ||
String bean = "linkit"; | ||
String view = "default"; | ||
String action = "none"; | ||
String url = "/testapp/id_RootTopic:1"; | ||
|
||
LinkDirective linkDirective = new LinkDirective(); | ||
|
||
Assert.assertEquals(linkDirective.getName(), "link", "Unexpected name for link directive."); | ||
Assert.assertEquals(linkDirective.getType(), DirectiveConstants.LINE, "Unexpected type for link directive."); | ||
StringBuilder output = new StringBuilder(); | ||
StringBuilderWriter writer = new StringBuilderWriter(output); | ||
|
||
Context c = Mockito.mock(Context.class); | ||
InternalContextAdapter context = new InternalContextAdapterImpl(c); | ||
|
||
ServletContext app = new MockServletContext(); | ||
HttpServletRequest request = new MockHttpServletRequest(app, "GET", url); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
LinkFactoryAggregator aggregator = Mockito.mock(LinkFactoryAggregator.class); | ||
Mockito.when(aggregator.createLink(request, response, bean, action, view)).thenReturn(new Link(url)); | ||
|
||
Mockito.when(context.get(Constants.ATTRIBUTE_REQUEST)).thenReturn(request); | ||
Mockito.when(context.get(Constants.ATTRIBUTE_RESPONSE)).thenReturn(response); | ||
Mockito.when(context.get(Constants.ATTRIBUTE_LINK_FACTORY_AGGREGATOR)).thenReturn(aggregator); | ||
|
||
Node node = Mockito.mock(Node.class); | ||
Node beanNode = Mockito.mock(Node.class); | ||
Node viewNode = Mockito.mock(Node.class); | ||
Node actionNode = Mockito.mock(Node.class); | ||
Node hrefNode = Mockito.mock(Node.class); | ||
Node targetNode = Mockito.mock(Node.class); | ||
Node handlersNode = Mockito.mock(Node.class); | ||
Mockito.when(beanNode.value(context)).thenReturn(bean); | ||
Mockito.when(viewNode.value(context)).thenReturn(view); | ||
Mockito.when(actionNode.value(context)).thenReturn(action); | ||
Mockito.when(hrefNode.value(context)).thenReturn(true); | ||
Mockito.when(targetNode.value(context)).thenReturn(false); | ||
Mockito.when(handlersNode.value(context)).thenReturn(false); | ||
Mockito.when(node.jjtGetChild(0)).thenReturn(beanNode); | ||
Mockito.when(node.jjtGetChild(1)).thenReturn(viewNode); | ||
Mockito.when(node.jjtGetChild(2)).thenReturn(actionNode); | ||
Mockito.when(node.jjtGetChild(3)).thenReturn(hrefNode); | ||
Mockito.when(node.jjtGetChild(4)).thenReturn(targetNode); | ||
Mockito.when(node.jjtGetChild(5)).thenReturn(handlersNode); | ||
Mockito.when(node.jjtGetNumChildren()).thenReturn(6); | ||
|
||
RuntimeInstance runtime = new RuntimeInstance(); | ||
runtime.addDirective(linkDirective); | ||
linkDirective.init(runtime, context, node); | ||
boolean result = true; | ||
try { | ||
result = linkDirective.render(context, writer, node); | ||
} catch (Throwable t) { | ||
Assert.fail("Cannot handle request.", t); | ||
} // try/catch | ||
Assert.assertFalse(result, "Link directive should return false."); | ||
Assert.assertEquals(writer.toString(), "href=\""+url+"\" ", "Unexpected result on writer."); | ||
} // testLinkDirective() | ||
|
||
} // LinkDirectiveTest |
54 changes: 54 additions & 0 deletions
54
core/test/org/tangram/view/velocity/test/VelocityLogTest.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,54 @@ | ||
/* | ||
* | ||
* Copyright 2016 Martin Goellnitz | ||
* | ||
* This program 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 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.tangram.view.velocity.test; | ||
|
||
import org.tangram.view.velocity.VelocityLog; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
/** | ||
* Just trigger the log somehow. | ||
*/ | ||
public class VelocityLogTest { | ||
|
||
@Test | ||
public void testVelocityLog() { | ||
VelocityLog log = new VelocityLog(); | ||
|
||
log.log(VelocityLog.DEBUG_ID, "just trigger log"); | ||
log.log(VelocityLog.INFO_ID, "just trigger log"); | ||
log.log(VelocityLog.WARN_ID, "just trigger log"); | ||
log.log(VelocityLog.ERROR_ID, "just trigger log"); | ||
|
||
Throwable t = new Throwable("Test"); | ||
log.log(VelocityLog.DEBUG_ID, "just trigger log", t); | ||
log.log(VelocityLog.INFO_ID, "just trigger log", t); | ||
log.log(VelocityLog.WARN_ID, "just trigger log", t); | ||
log.log(VelocityLog.ERROR_ID, "just trigger log", t); | ||
|
||
Assert.assertTrue(log.isLevelEnabled(VelocityLog.DEBUG_ID), "Level debug should be enabled during test."); | ||
Assert.assertTrue(log.isLevelEnabled(VelocityLog.ERROR_ID), "Level error should be enabled during test."); | ||
Assert.assertTrue(log.isLevelEnabled(VelocityLog.INFO_ID), "Level info should be enabled during test."); | ||
Assert.assertFalse(log.isLevelEnabled(VelocityLog.TRACE_ID), "Level trace should be disabled during test."); | ||
Assert.assertTrue(log.isLevelEnabled(VelocityLog.WARN_ID), "Level warn should be enabled during test."); | ||
|
||
} // testVelocityLog() | ||
|
||
} // VelocityLogTest |
68 changes: 68 additions & 0 deletions
68
core/test/org/tangram/view/velocity/test/VelocityResourceLoaderTest.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,68 @@ | ||
/* | ||
* | ||
* Copyright 2016 Martin Goellnitz | ||
* | ||
* This program 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 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.tangram.view.velocity.test; | ||
|
||
import org.apache.velocity.runtime.resource.Resource; | ||
import org.mockito.Mockito; | ||
import org.tangram.content.CodeResourceCache; | ||
import org.tangram.content.TransientCode; | ||
import org.tangram.view.velocity.VelocityPatchBean; | ||
import org.tangram.view.velocity.VelocityResourceLoader; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
/** | ||
* Test some aspects of the velocity template loading from the code resource cache. | ||
*/ | ||
public class VelocityResourceLoaderTest { | ||
|
||
@Test | ||
public void testVelocityResourceLoader() { | ||
CodeResourceCache codeCache = Mockito.mock(CodeResourceCache.class); | ||
TransientCode code = new TransientCode("org.tangram.example.Topic", "text/html", "Code:42", "", 12345); | ||
Mockito.when(codeCache.get("org.tangram.example.Topic")).thenReturn(code); | ||
|
||
VelocityPatchBean bean = new VelocityPatchBean(); | ||
bean.setCodeResourceCache(codeCache); | ||
|
||
VelocityResourceLoader resourceLoader = new VelocityResourceLoader(); | ||
resourceLoader.init(null); | ||
|
||
boolean resourceExists = resourceLoader.resourceExists("org.tangram.example.Topic.name"); | ||
Assert.assertFalse(resourceExists, "The given resource should not exist."); | ||
resourceExists = resourceLoader.resourceExists("org.tangram.example.Topic"); | ||
Assert.assertTrue(resourceExists, "The given resource should exist."); | ||
|
||
Resource resource = Mockito.mock(Resource.class); | ||
Mockito.when(resource.getName()).thenReturn("org.tangram.example.Topic"); | ||
Mockito.when(resource.getLastModified()).thenReturn(1234L); | ||
boolean sourceModified = resourceLoader.isSourceModified(resource); | ||
Assert.assertTrue(sourceModified, "The given source should be modified."); | ||
|
||
resource = Mockito.mock(Resource.class); | ||
Mockito.when(resource.getName()).thenReturn("org.tangram.example.Topic"); | ||
Mockito.when(resource.getLastModified()).thenReturn(12345L); | ||
sourceModified = resourceLoader.isSourceModified(resource); | ||
Assert.assertFalse(sourceModified, "The given source should not be modified."); | ||
|
||
Assert.assertNotNull(resourceLoader.getResourceStream("org.tangram.example.Topic"), "Stream should not be null for existing resources."); | ||
} // testVelocityResourceLoader() | ||
|
||
} // VelocityResourceLoaderTest |