Author: awiner
Date: Wed Mar 11 00:59:43 2009
New Revision: 752331
URL: http://svn.apache.org/viewvc?rev=752331&view=rev
Log:
SHINDIG-748: OpenSocial Templates (in part)
- Add support for os:If and os:Repeat
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java
(with props)
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java
(with props)
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java
(with props)
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java
(with props)
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java
(with props)
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/DefaultTemplateProcessor.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TagRegistry.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateModule.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateProcessor.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/HtmlTagHandlerTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/DefaultTemplateProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/DefaultTemplateProcessor.java?rev=752331&r1=752330&r2=752331&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/DefaultTemplateProcessor.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/DefaultTemplateProcessor.java
Wed Mar 11 00:59:43 2009
@@ -204,69 +204,81 @@
}
}
-
/**
* Process repeater state, if needed, on an element.
*/
- private void processElement(Node result, Element element) {
+ private void processElement(final Node result, final Element element) {
Attr repeat = element.getAttributeNode(ATTRIBUTE_REPEAT);
if (repeat != null) {
- // TODO: Is Iterable the right interface here? The spec calls for
- // length to be available.
Iterable<?> dataList = evaluate(repeat.getValue(), Iterable.class, null);
- if (dataList != null) {
- // Compute list size
- int size = Iterables.size(dataList);
-
- // Save the initial EL state
- Map<String, ? extends Object> oldContext =
templateContext.getContext();
- Object oldCur = templateContext.getCur();
- ValueExpression oldVarExpression = null;
-
- // Set the new Context variable. Copy the old context to preserve
- // any existing "index" variable
- Map<String, Object> loopData = Maps.newHashMap(oldContext);
- loopData.put(PROPERTY_COUNT, size);
- templateContext.setContext(loopData);
-
- // TODO: This means that any loop with @var doesn't make the loop
- // variable available in the default expression context.
- // Update the specification to make this explicit.
- Attr varAttr = element.getAttributeNode(ATTRIBUTE_VAR);
- if (varAttr == null) {
- oldCur = templateContext.getCur();
- } else {
- oldVarExpression =
elContext.getVariableMapper().resolveVariable(varAttr.getValue());
- }
-
- Attr indexVarAttr = element.getAttributeNode(ATTRIBUTE_INDEX);
- String indexVar = indexVarAttr == null ? PROPERTY_INDEX :
indexVarAttr.getValue();
-
- int index = 0;
- for (Object data : dataList) {
- loopData.put(indexVar, index++);
-
- // Set up context for rendering inner node
- templateContext.setCur(data);
- if (varAttr != null) {
- ValueExpression varExpression = expressions.constant(data,
Object.class);
- elContext.getVariableMapper().setVariable(varAttr.getValue(),
varExpression);
- }
-
+ processRepeat(result, element, dataList, new Runnable() {
+ public void run() {
processElementInner(result, element);
}
+ });
+ } else {
+ processElementInner(result, element);
+ }
+ }
+
+ /**
+ * @param result
+ * @param element
+ * @param dataList
+ */
+ public void processRepeat(Node result, Element element, Iterable<?> dataList,
+ Runnable onEachLoop) {
+ // Compute list size
+ int size = Iterables.size(dataList);
+
+ if (size > 0) {
+ // Save the initial EL state
+ Map<String, ? extends Object> oldContext = templateContext.getContext();
+ Object oldCur = templateContext.getCur();
+ ValueExpression oldVarExpression = null;
+
+ // Set the new Context variable. Copy the old context to preserve
+ // any existing "index" variable
+ Map<String, Object> loopData = Maps.newHashMap(oldContext);
+ loopData.put(PROPERTY_COUNT, size);
+ templateContext.setContext(loopData);
+
+ // TODO: This means that any loop with @var doesn't make the loop
+ // variable available in the default expression context.
+ // Update the specification to make this explicit.
+ Attr varAttr = element.getAttributeNode(ATTRIBUTE_VAR);
+ if (varAttr == null) {
+ oldCur = templateContext.getCur();
+ } else {
+ oldVarExpression =
elContext.getVariableMapper().resolveVariable(varAttr.getValue());
+ }
+
+ Attr indexVarAttr = element.getAttributeNode(ATTRIBUTE_INDEX);
+ String indexVar = indexVarAttr == null ? PROPERTY_INDEX :
indexVarAttr.getValue();
- // Restore EL state
- if (varAttr == null) {
- templateContext.setCur(oldCur);
- } else {
- elContext.getVariableMapper().setVariable(varAttr.getValue(),
oldVarExpression);
+ int index = 0;
+ for (Object data : dataList) {
+ loopData.put(indexVar, index++);
+
+ // Set up context for rendering inner node
+ templateContext.setCur(data);
+ if (varAttr != null) {
+ ValueExpression varExpression = expressions.constant(data,
Object.class);
+ elContext.getVariableMapper().setVariable(varAttr.getValue(),
varExpression);
}
- templateContext.setContext(oldContext);
+ onEachLoop.run();
+
}
- } else {
- processElementInner(result, element);
+
+ // Restore EL state
+ if (varAttr == null) {
+ templateContext.setCur(oldCur);
+ } else {
+ elContext.getVariableMapper().setVariable(varAttr.getValue(),
oldVarExpression);
+ }
+
+ templateContext.setContext(oldContext);
}
}
@@ -274,15 +286,19 @@
* Process conditionals and non-repeat attributes on an element
*/
private void processElementInner(Node result, Element element) {
- Attr ifAttribute = element.getAttributeNode(ATTRIBUTE_IF);
- if (ifAttribute != null) {
- if (!evaluate(ifAttribute.getValue(), Boolean.class, false)) {
- return;
+ TagHandler handler = registry.getHandlerFor(element);
+
+ // An ugly special-case: <os:Repeat> will re-evaluate the "if" attribute
+ // (as it should) for each loop of the repeat. Don't evaluate it here.
+ if (!(handler instanceof RepeatTagHandler)) {
+ Attr ifAttribute = element.getAttributeNode(ATTRIBUTE_IF);
+ if (ifAttribute != null) {
+ if (!evaluate(ifAttribute.getValue(), Boolean.class, false)) {
+ return;
+ }
}
}
- TagHandler handler = registry.getHandlerFor(element);
-
if (handler != null) {
// TODO: We are passing in an element with all special attributes intact.
// This may be problematic. Perhaps doing a deep clone and stripping them
@@ -320,19 +336,26 @@
* Evaluates an expression within the scope of this processor's context.
* @param expression The String expression
* @param type Expected result type
- * @param defaultValue Default value to return
+ * @param defaultValue Default value to return in case of error
*/
- @SuppressWarnings("unchecked")
public <T> T evaluate(String expression, Class<T> type, T defaultValue) {
- T result = defaultValue;
- Class requestType = (type == Iterable.class) ? Object.class : type;
try {
- ValueExpression expr = expressions.parse(expression, requestType);
- result = (T) expr.getValue(elContext);
+ Object result;
+ // Coerce iterables specially
+ if (type == Iterable.class) {
+ ValueExpression expr = expressions.parse(expression, Object.class);
+ Object value = expr.getValue(elContext);
+ result = coerceToIterable(value);
+ } else {
+ ValueExpression expr = expressions.parse(expression, type);
+ result = expr.getValue(elContext);
+ }
+
+ return type.cast(result);
} catch (ELException e) {
logger.warning(e.getMessage());
+ return defaultValue;
}
- return (type == Iterable.class) ? (T) coerceToIterable(result) : result;
}
/**
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java?rev=752331&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java
Wed Mar 11 00:59:43 2009
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.shindig.gadgets.templates;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.google.inject.Inject;
+
+public class IfTagHandler extends AbstractTagHandler {
+
+ static final String TAG_IF = "If";
+ static final String CONDITION_ATTR = "condition";
+
+ @Inject
+ public IfTagHandler() {
+ super(TagHandler.OPENSOCIAL_NAMESPACE, TAG_IF);
+ }
+
+ public void process(Node result, Element tag, TemplateProcessor processor) {
+ Boolean condition = getValueFromTag(tag, CONDITION_ATTR, processor,
Boolean.class);
+ if (condition == null || !condition.booleanValue()) {
+ return;
+ }
+
+ // Condition succeeded, process all child nodes
+ processor.processChildNodes(result, tag);
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/IfTagHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java?rev=752331&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java
Wed Mar 11 00:59:43 2009
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.shindig.gadgets.templates;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.google.inject.Inject;
+
+/**
+ * Implementation of the <os:Repeat> tag.
+ */
+public class RepeatTagHandler extends AbstractTagHandler {
+
+ static final String TAG_REPEAT = "Repeat";
+ static final String EXPRESSION_ATTR = "expression";
+ static final String IF_ATTR = "if";
+
+ @Inject
+ public RepeatTagHandler() {
+ super(TagHandler.OPENSOCIAL_NAMESPACE, TAG_REPEAT);
+ }
+
+ public void process(final Node result, final Element tag, final
TemplateProcessor processor) {
+ Iterable<?> repeat = getValueFromTag(tag, EXPRESSION_ATTR, processor,
Iterable.class);
+ final Attr ifAttribute = tag.getAttributeNode(IF_ATTR);
+
+ // On each iteration, process child nodes, after checking the value of the
"if" attribute
+ processor.processRepeat(result, tag, repeat, new Runnable() {
+ public void run() {
+ if (ifAttribute != null) {
+ if (!processor.evaluate(ifAttribute.getValue(), Boolean.class,
false)) {
+ return;
+ }
+ }
+
+ processor.processChildNodes(result, tag);
+ }
+ });
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/RepeatTagHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TagRegistry.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TagRegistry.java?rev=752331&r1=752330&r2=752331&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TagRegistry.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TagRegistry.java
Wed Mar 11 00:59:43 2009
@@ -66,6 +66,11 @@
}
@Override
+ public String toString() {
+ return namespaceUri + ":" + localName;
+ }
+
+ @Override
public boolean equals(Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof NSName)) { return false; }
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateModule.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateModule.java?rev=752331&r1=752330&r2=752331&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateModule.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateModule.java
Wed Mar 11 00:59:43 2009
@@ -42,8 +42,10 @@
private final Set<TagHandler> handlers;
@Inject
- public TagHandlersProvider(HtmlTagHandler htmlHandler, NameTagHandler
nameHandler) {
- handlers = ImmutableSet.of((TagHandler) htmlHandler, nameHandler);
+ public TagHandlersProvider(HtmlTagHandler htmlHandler, NameTagHandler
nameHandler,
+ IfTagHandler ifHandler, RepeatTagHandler repeatHandler) {
+ handlers = ImmutableSet.of((TagHandler) htmlHandler, nameHandler,
ifHandler,
+ repeatHandler);
}
public Set<TagHandler> get() {
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateProcessor.java?rev=752331&r1=752330&r2=752331&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateProcessor.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateProcessor.java
Wed Mar 11 00:59:43 2009
@@ -52,7 +52,10 @@
*/
void processChildNodes(Node result, Node source);
- /**
+ void processRepeat(Node result, Element element, Iterable<?> dataList,
+ Runnable onEachLoop);
+
+ /**
* Evaluates an expression within the scope of this processor's context.
* @param expression The String expression
* @param type Expected result type
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java?rev=752331&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java
Wed Mar 11 00:59:43 2009
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.shindig.gadgets.templates;
+
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.el.ELResolver;
+
+import com.google.common.collect.Maps;
+
+/**
+ * Fake implementation of TemplateProcessor for writing TagHandler tests.
+ */
+public class FakeTemplateProcessor implements TemplateProcessor {
+ public Map<String, ? extends Object> expressionResults = Maps.newHashMap();
+
+ public final <T extends Object> T evaluate(String expression, Class<T> type,
T defaultValue) {
+ // Some quick-and-dirty mocking: put a List in the map, and
+ // you get one result per-entry
+ Object result = expressionResults.get(expression);
+ if (result instanceof List && !type.isAssignableFrom(List.class)) {
+ result = ((List<?>) result).remove(0);
+ }
+
+ return type.cast(result);
+ }
+
+ public DocumentFragment processTemplate(Element template,
+ TemplateContext templateContext, ELResolver globals) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void processChildNodes(Node result, Node source) {
+ throw new UnsupportedOperationException();
+ }
+
+ public final void processRepeat(Node result, Element element, Iterable<?>
dataList, Runnable onEachLoop) {
+ // for (Object data : dataList) produces an unused variable warning
+ Iterator<?> iterator = dataList.iterator();
+ while (iterator.hasNext()) {
+ iterator.next();
+ onEachLoop.run();
+ }
+ }
+
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/FakeTemplateProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/HtmlTagHandlerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/HtmlTagHandlerTest.java?rev=752331&r1=752330&r2=752331&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/HtmlTagHandlerTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/HtmlTagHandlerTest.java
Wed Mar 11 00:59:43 2009
@@ -28,33 +28,20 @@
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import javax.el.ELResolver;
+import com.google.common.collect.ImmutableMap;
+/**
+ * Test of the <os:Html> tag.
+ */
public class HtmlTagHandlerTest {
-
- private TemplateProcessor processor;
+ private FakeTemplateProcessor processor;
private DOMImplementation documentProvider;
private HtmlTagHandler handler;
-
+
@Before
public void setUp() throws Exception {
- processor = new TemplateProcessor() {
- public <T extends Object> T evaluate(String expression, Class<T> type, T
defaultValue) {
- // The test only "supports" String expressions
- return type.cast(expression);
- }
-
- public DocumentFragment processTemplate(Element template,
- TemplateContext templateContext, ELResolver globals) {
- return null;
- }
-
- public void processChildNodes(Node result, Node source) {
- }
- };
-
+ processor = new FakeTemplateProcessor();
documentProvider = new ParseModule.DOMImplementationProvider().get();
handler = new HtmlTagHandler(new SocialMarkupHtmlParser(documentProvider));
}
@@ -64,7 +51,9 @@
Document doc = documentProvider.createDocument(null, null, null);
// Create a mock tag; the name doesn't truly matter
Element tag = doc.createElement("test");
- tag.setAttribute("code", "Hello <b>World</b>!");
+ tag.setAttribute("code", "${code}");
+ processor.expressionResults = ImmutableMap.of(
+ "${code}", "Hello <b>World</b>!");
DocumentFragment fragment = doc.createDocumentFragment();
handler.process(fragment, tag, processor);
assertEquals(3, fragment.getChildNodes().getLength());
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java?rev=752331&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java
Wed Mar 11 00:59:43 2009
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.shindig.gadgets.templates;
+
+import static org.easymock.EasyMock.isNull;
+import static org.easymock.EasyMock.same;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+import org.apache.shindig.gadgets.parse.ParseModule;
+import org.easymock.classextension.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.google.common.collect.ImmutableMap;
+
+public class IfTagHandlerTest {
+ private FakeTemplateProcessor processor;
+ private DOMImplementation documentProvider;
+ private TagHandler handler;
+
+ @Before
+ public void setUp() throws Exception {
+ processor = EasyMock.createMock(FakeTemplateProcessor.class);
+ documentProvider = new ParseModule.DOMImplementationProvider().get();
+ handler = new IfTagHandler();
+ }
+
+ @Test
+ public void conditionIsFalse() throws Exception {
+ Document doc = documentProvider.createDocument(null, null, null);
+ // Create a mock tag; the name doesn't truly matter
+ Element tag = doc.createElement("if");
+
+ tag.setAttribute(IfTagHandler.CONDITION_ATTR, "fakeExpression");
+ processor.expressionResults = ImmutableMap.of("fakeExpression", false);
+
+ replay(processor);
+ handler.process(null, tag, processor);
+ verify(processor);
+ }
+
+ @Test
+ public void conditionIsTrue() throws Exception {
+ Document doc = documentProvider.createDocument(null, null, null);
+ // Create a mock tag; the name doesn't truly matter
+ Element tag = doc.createElement("if");
+ tag.setAttribute(IfTagHandler.CONDITION_ATTR, "fakeExpression");
+
+ processor.expressionResults = ImmutableMap.of("fakeExpression", true);
+ processor.processChildNodes((Node) isNull(), same(tag));
+
+ replay(processor);
+ handler.process(null, tag, processor);
+ verify(processor);
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/IfTagHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java?rev=752331&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java
Wed Mar 11 00:59:43 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.shindig.gadgets.templates;
+
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+import org.apache.shindig.gadgets.parse.ParseModule;
+import org.easymock.classextension.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+
+public class RepeatTagHandlerTest {
+ private FakeTemplateProcessor processor;
+ private DOMImplementation documentProvider;
+ private TagHandler handler;
+
+ @Before
+ public void setUp() throws Exception {
+ processor = EasyMock.createMock(FakeTemplateProcessor.class);
+ documentProvider = new ParseModule.DOMImplementationProvider().get();
+ handler = new RepeatTagHandler();
+ }
+
+ @Test
+ public void repeat() throws Exception {
+ Document doc = documentProvider.createDocument(null, null, null);
+ // Create a mock tag; the name doesn't truly matter
+ Element tag = doc.createElement("repeat");
+ tag.setAttribute(RepeatTagHandler.EXPRESSION_ATTR, "fakeExpression");
+
+ List<String> mockList = ImmutableList.of("a", "b", "c");
+ processor.expressionResults = ImmutableMap.of("fakeExpression", mockList);
+
+ processor.processChildNodes(null, tag);
+ EasyMock.expectLastCall().times(3);
+
+ replay(processor);
+ handler.process(null, tag, processor);
+ verify(processor);
+ }
+
+ @Test
+ public void repeatWithIf() throws Exception {
+ Document doc = documentProvider.createDocument(null, null, null);
+ // Create a mock tag; the name doesn't truly matter
+ Element tag = doc.createElement("repeat");
+ tag.setAttribute(RepeatTagHandler.EXPRESSION_ATTR, "fakeExpression");
+ tag.setAttribute(RepeatTagHandler.IF_ATTR, "fakeIf");
+
+ List<String> mockList = ImmutableList.of("a", "b", "c");
+ processor.expressionResults = ImmutableMap.of("fakeExpression", mockList,
+ // Return "false", "true", and "false" for each step
+ "fakeIf", Lists.newArrayList(false, true, false));
+
+ processor.processChildNodes(null, tag);
+ // "if" should evaluate to true only once
+ EasyMock.expectLastCall().times(1);
+
+ replay(processor);
+ handler.process(null, tag, processor);
+ verify(processor);
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/RepeatTagHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native