1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.lifecycle;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assume.assumeNotNull;
22 import static org.junit.Assume.assumeNoException;
23 import static org.mockito.Mockito.mock;
24
25 import java.io.FileNotFoundException;
26 import java.io.StringWriter;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.Method;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import org.apache.log4j.Logger;
34 import org.junit.AfterClass;
35 import org.junit.Assume;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
39 import org.kuali.rice.krad.uif.UifConstants;
40 import org.kuali.rice.krad.uif.element.Message;
41 import org.kuali.rice.krad.uif.util.ComponentFactory;
42 import org.kuali.rice.krad.uif.util.CopyUtils;
43 import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest;
44 import org.kuali.rice.krad.uif.util.UifUnitTestUtils;
45 import org.kuali.rice.krad.uif.view.View;
46 import org.springframework.mock.web.MockHttpServletRequest;
47 import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
48 import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
49
50 import freemarker.core.Environment;
51 import freemarker.core.Expression;
52 import freemarker.core.Macro;
53 import freemarker.core.TemplateElement;
54 import freemarker.template.Template;
55
56 public class ComponentFreemarkerTest extends ProcessLoggingUnitTest {
57
58 private static final Logger LOG = Logger.getLogger(ComponentFreemarkerTest.class);
59
60 @BeforeClass
61 public static void setUpClass() throws Throwable {
62 UifUnitTestUtils.establishMockConfig("KRAD-ComponentFreemarkerTest");
63 try {
64 ComponentFactory.getMessage();
65 } catch (Throwable t) {
66 LOG.error("Skipping tests, message component is not available", t);
67 Assume.assumeNoException("Skipping tests, message component is not available", t);
68 }
69 }
70
71 @AfterClass
72 public static void tearDownClass() throws Throwable {
73 UifUnitTestUtils.tearDownMockConfig();
74 }
75
76 @Test
77 public void testMessage() throws Throwable {
78 Message m = ComponentFactory.getMessage();
79 m.setMessageText("foobar");
80 m.setWrapperTag("span");
81 m.setId("_span");
82
83 FreeMarkerViewResolver viewResolver = (FreeMarkerViewResolver)
84 UifUnitTestUtils.getWebApplicationContext().getBean("viewResolver");
85 assertNotNull(viewResolver);
86
87 assert m.getTemplate().endsWith(".ftl");
88 FreeMarkerView v = (FreeMarkerView) viewResolver.resolveViewName(
89 m.getTemplate().substring(0, m.getTemplate().length() - 4),
90 Locale.getDefault());
91 assumeNotNull(v);
92
93 Method getTemplate = FreeMarkerView.class.getDeclaredMethod("getTemplate", Locale.class);
94 getTemplate.setAccessible(true);
95 Template template = (Template) getTemplate.invoke(v, Locale.getDefault());
96 assertNotNull(template);
97
98 Macro macro = (Macro) template.getMacros().get(m.getTemplateName());
99 assertNotNull(macro);
100
101 Map<String, Object> rootMap = new java.util.HashMap<String, Object>();
102 rootMap.put("component", m);
103 StringWriter out = new StringWriter();
104 Environment env = template.createProcessingEnvironment(rootMap, out);
105
106 env.importLib("/krad/WEB-INF/ftl/lib/krad.ftl", "krad");
107 env.importLib("/krad/WEB-INF/ftl/lib/spring.ftl", "spring");
108
109 Class<?> identifier = Class.forName("freemarker.core.Identifier");
110 Constructor<?> newIdentifier = identifier.getDeclaredConstructor(String.class);
111 newIdentifier.setAccessible(true);
112 Map<String, Expression> args = new java.util.HashMap<String, Expression>();
113 args.put(m.getComponentTypeName(), (Expression) newIdentifier.newInstance("component"));
114
115 Method visit = Environment.class.getDeclaredMethod("visit", Macro.class, Map.class, List.class, List.class,
116 TemplateElement.class);
117 visit.setAccessible(true);
118 visit.invoke(env, macro, args, null, null, null);
119
120 assertEquals("<span id=\"_span\" class=\"uif-message\" >\r\n" +
121 "foobar </span>", out.toString().trim());
122 }
123
124 @Test
125 public void testMessageNoReflection() throws Throwable {
126 MockHttpServletRequest request = new MockHttpServletRequest();
127 View view = mock(View.class);
128 ViewLifecycle.encapsulateLifecycle(view, new Object(), request, new Runnable() {
129 @Override
130 public void run() {
131 Message msg = CopyUtils.copy(ComponentFactory.getMessage());
132 msg.setMessageText("foobar");
133 msg.setId("_naps");
134 msg.setWrapperTag("pans");
135
136 msg.setViewStatus(UifConstants.ViewStatus.FINAL);
137
138 RenderComponentPhase renderPhase = (RenderComponentPhase) KRADServiceLocatorWeb
139 .getViewLifecyclePhaseBuilder().buildPhase(UifConstants.ViewPhases.RENDER, msg, null, "", null);
140
141 try {
142 ViewLifecycle.getProcessor().performPhase(renderPhase);
143 } catch (IllegalStateException e) {
144 if (e.getCause() instanceof FileNotFoundException) {
145 assumeNoException(e.getCause());
146 } else {
147 throw e;
148 }
149 }
150
151 assertTrue(msg.isSelfRendered());
152 assertEquals("<pans id=\"_naps\" class=\"uif-message\" >\r\n" +
153 "foobar </pans>", msg.getRenderedHtmlOutput().trim());
154 }
155 });
156 }
157
158 }