Skip to content

Commit 0837982

Browse files
marcelabcbrianchandotcom
authored andcommitted
LPD-56406 Setup method with common logic
1 parent 0114bb6 commit 0837982

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

modules/apps/dynamic-data-mapping/dynamic-data-mapping-web-test/src/test/java/com/liferay/dynamic/data/mapping/web/internal/portlet/action/RenderStructureFieldMVCResourceCommandTest.java

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collections;
2525

2626
import org.junit.Assert;
27+
import org.junit.Before;
2728
import org.junit.ClassRule;
2829
import org.junit.Test;
2930

@@ -38,11 +39,8 @@ public class RenderStructureFieldMVCResourceCommandTest {
3839
public static final LiferayUnitTestRule liferayUnitTestRule =
3940
LiferayUnitTestRule.INSTANCE;
4041

41-
@Test
42-
public void testCreateDDMFormFieldRenderingContext() {
43-
HttpServletRequest httpServletRequest = Mockito.mock(
44-
HttpServletRequest.class);
45-
42+
@Before
43+
public void setUp() throws Exception {
4644
ThemeDisplay themeDisplay = Mockito.mock(ThemeDisplay.class);
4745

4846
Mockito.when(
@@ -52,23 +50,24 @@ public void testCreateDDMFormFieldRenderingContext() {
5250
);
5351

5452
Mockito.when(
55-
httpServletRequest.getAttribute(WebKeys.THEME_DISPLAY)
53+
_httpServletRequest.getAttribute(WebKeys.THEME_DISPLAY)
5654
).thenReturn(
5755
themeDisplay
5856
);
57+
}
5958

60-
String script = "'\"></option><img onerror=alert(123) src=x>";
61-
59+
@Test
60+
public void testCreateDDMFormFieldRenderingContext() {
6261
Mockito.when(
63-
httpServletRequest.getParameter("namespace")
62+
_httpServletRequest.getParameter("namespace")
6463
).thenReturn(
65-
script
64+
_SCRIPT
6665
);
6766

6867
Mockito.when(
69-
httpServletRequest.getParameter("portletNamespace")
68+
_httpServletRequest.getParameter("portletNamespace")
7069
).thenReturn(
71-
script
70+
_SCRIPT
7271
);
7372

7473
RenderStructureFieldMVCResourceCommand
@@ -81,56 +80,37 @@ public void testCreateDDMFormFieldRenderingContext() {
8180
DDMFormFieldRenderingContext ddmFormFieldRenderingContext =
8281
renderStructureFieldMVCResourceCommand.
8382
createDDMFormFieldRenderingContext(
84-
httpServletRequest,
83+
_httpServletRequest,
8584
Mockito.mock(HttpServletResponse.class));
8685

8786
Assert.assertEquals(
88-
HtmlUtil.escapeAttribute(script),
87+
HtmlUtil.escapeAttribute(_SCRIPT),
8988
ddmFormFieldRenderingContext.getNamespace());
9089
Assert.assertEquals(
91-
HtmlUtil.escapeAttribute(script),
90+
HtmlUtil.escapeAttribute(_SCRIPT),
9291
ddmFormFieldRenderingContext.getPortletNamespace());
9392
}
9493

9594
@Test
9695
public void testGetDDMFormField() {
97-
HttpServletRequest httpServletRequest = Mockito.mock(
98-
HttpServletRequest.class);
99-
100-
ThemeDisplay themeDisplay = Mockito.mock(ThemeDisplay.class);
101-
10296
Mockito.when(
103-
themeDisplay.getLocale()
97+
_httpServletRequest.getParameter("definition")
10498
).thenReturn(
105-
LocaleUtil.US
99+
_SCRIPT
106100
);
107101

108102
Mockito.when(
109-
httpServletRequest.getAttribute(WebKeys.THEME_DISPLAY)
103+
_httpServletRequest.getParameter("fieldName")
110104
).thenReturn(
111-
themeDisplay
112-
);
113-
114-
String script = "'\"></option><img onerror=alert(123) src=x>";
115-
116-
Mockito.when(
117-
httpServletRequest.getParameter("definition")
118-
).thenReturn(
119-
script
120-
);
121-
122-
Mockito.when(
123-
httpServletRequest.getParameter("fieldName")
124-
).thenReturn(
125-
HtmlUtil.escapeAttribute(script)
105+
HtmlUtil.escapeAttribute(_SCRIPT)
126106
);
127107

128108
DDMFormField ddmFormFieldMock = Mockito.mock(DDMFormField.class);
129109

130110
Mockito.when(
131111
ddmFormFieldMock.getName()
132112
).thenReturn(
133-
HtmlUtil.escapeAttribute(script)
113+
HtmlUtil.escapeAttribute(_SCRIPT)
134114
);
135115

136116
DDMForm ddmForm = Mockito.mock(DDMForm.class);
@@ -139,7 +119,7 @@ public void testGetDDMFormField() {
139119
ddmForm.getDDMFormFieldsMap(true)
140120
).thenReturn(
141121
Collections.singletonMap(
142-
HtmlUtil.escapeAttribute(script), ddmFormFieldMock)
122+
HtmlUtil.escapeAttribute(_SCRIPT), ddmFormFieldMock)
143123
);
144124

145125
DDMFormDeserializerDeserializeResponse
@@ -174,12 +154,17 @@ public void testGetDDMFormField() {
174154

175155
DDMFormField ddmFormField = ReflectionTestUtil.invoke(
176156
renderStructureFieldMVCResourceCommand, "_getDDMFormField",
177-
new Class<?>[] {HttpServletRequest.class}, httpServletRequest);
157+
new Class<?>[] {HttpServletRequest.class}, _httpServletRequest);
178158

179159
Assert.assertEquals(
180-
HtmlUtil.escapeAttribute(script), ddmFormField.getName());
160+
HtmlUtil.escapeAttribute(_SCRIPT), ddmFormField.getName());
181161
}
182162

163+
private static final String _SCRIPT =
164+
"'\"></option><img onerror=alert(123) src=x>";
165+
166+
private final HttpServletRequest _httpServletRequest = Mockito.mock(
167+
HttpServletRequest.class);
183168
private final Portal _portal = Mockito.mock(Portal.class);
184169

185170
}

0 commit comments

Comments
 (0)