Skip to content

Commit

Permalink
UP-4427: Added a unit test for processText()
Browse files Browse the repository at this point in the history
  • Loading branch information
drewwills committed Mar 27, 2015
1 parent c1bd8df commit 53dee7f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -16,4 +16,5 @@
uportal-portlets-overlay/*/overlays
uportal-platform-api/target/
**/node_modules/
npm-debug.log
npm-debug.log
**/.DS_Store
Expand Up @@ -74,15 +74,15 @@ public Source processTemplates(Document data, String filename) {
case org.w3c.dom.Node.ATTRIBUTE_NODE:
Attribute a = (Attribute) n;
inpt = a.getValue();
otpt = processText(inpt, ctx);
otpt = processText(inpt);
if (!otpt.equals(inpt)) {
a.setValue(otpt);
}
break;
case org.w3c.dom.Node.TEXT_NODE:
Text t = (Text) n;
inpt = t.getText();
otpt = processText(inpt, ctx);
otpt = processText(inpt);
if (!otpt.equals(inpt)) {
t.setText(otpt);
}
Expand All @@ -104,7 +104,7 @@ public Source processTemplates(Document data, String filename) {
* Implementation
*/

private String processText(String text, EvaluationContext ctx) {
private String processText(String text) {
String rslt = text; // default
Expression x = portalSpELService.parseExpression(text, PortalSpELServiceImpl.TemplateParserContext.INSTANCE);
rslt = x.getValue(ctx, String.class);
Expand Down
@@ -0,0 +1,84 @@
/*
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you 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 org.jasig.portal.io.xml;

import org.jasig.portal.spring.spel.IPortalSpELService;
import org.jasig.portal.spring.spel.PortalSpELServiceImpl;
import org.jasig.portal.tenants.ITenant;
import org.jasig.portal.tenants.TemplateDataTenantOperationsListener;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.expression.spel.support.StandardEvaluationContext;

class SpELDataTemplatingStrategyTest {

private IPortalSpELService portalSpELService = new PortalSpELServiceImpl();

@Test
void testProcessText() {
ITenant tenant = [
getName: { 'Mordor' },
getFname: { 'mordor'}
] as ITenant;

/*
new ITenant() {
private static final long serialVersionUID = 1L;
@Override
public int compareTo(ITenant o) { return 0; }
@Override
public long getId() { return 1L; }
@Override
public String getName() { return "Mordor"; }
@Override
public void setName(String name) {}
@Override
public String getFname() { return "mordor"; }
@Override
public void setFname(String fname) {}
@Override
public String getAttribute(String name) { return null; }
@Override
public void setAttribute(String name, String value) {}
@Override
public Map<String, String> getAttributesMap() { return Collections.emptyMap(); }
}; */
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setRootObject(new TemplateDataTenantOperationsListener.RootObjectImpl(tenant));
IDataTemplatingStrategy templating = new SpELDataTemplatingStrategy(portalSpELService, ctx);

def inputs = [
'foobar': 'foobar',
'${tenant.name}': 'Mordor',
'Something ${tenant.name}': 'Something Mordor',
'${tenant.name} Something': 'Mordor Something',
'${tenant.fname}': 'mordor',
'Something ${tenant.fname}': 'Something mordor',
'${tenant.fname} Something': 'mordor Something'
];

inputs.each { k,v ->
String output = templating.processText(k);
assertEquals('Unexpected output from processText() -- ', v, output);
};
}

}

0 comments on commit 53dee7f

Please sign in to comment.