package org.orbeon.oxf.portlet; import java.io.PrintWriter; import java.io.StringWriter; import javax.portlet.MimeResponse; import org.mockito.Mockito; import org.mockito.internal.stubbing.answers.ThrowsExceptionClass; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.liferay.portal.kernel.portlet.LiferayPortletURL; public class WSRP2UtilsTest { private static final String NAMESPACE_DUMMY = "namespaceDummy"; @Test(dataProvider = "escapeTestData") public void escapeTest(String input, String expected) throws Exception { assertCorrect(input, expected); } @Test(dataProvider = "namespaceTestData") public void namespaceTest(String input, String expected) throws Exception { assertCorrect(input, expected); } @Test(dataProvider = "urlTestData") public void urlTest(String input, String expected) throws Exception { assertCorrect(input, expected); } public void assertCorrect(String input, String expected) throws Exception { StringWriter writer = new StringWriter(); MimeResponse mimeResponse = Mockito.mock(MimeResponse.class, new ThrowsExceptionClass(RuntimeException.class)); Mockito.doReturn(new PrintWriter(writer)).when(mimeResponse).getWriter(); LiferayPortletURL url = Mockito.mock(LiferayPortletURL.class, new ThrowsExceptionClass(RuntimeException.class)); Mockito.doReturn(url).when(mimeResponse).createActionURL(); Mockito.doReturn(url).when(mimeResponse).createRenderURL(); Mockito.doReturn(url).when(mimeResponse).createResourceURL(); Answer resourceIdAnswer = new SetterGetterAnswer(); Mockito.doAnswer(resourceIdAnswer).when(url).setResourceID(Mockito.anyString()); Mockito.doNothing().when(url).setCacheability(Mockito.anyString()); Mockito.doAnswer(resourceIdAnswer).when(url).toString(); CopyOfWSRP2Utils.write(mimeResponse, input, NAMESPACE_DUMMY, false); Assert.assertEquals(writer.toString(), expected); } @DataProvider public String[][] escapeTestData() { return withAndWithoutSurroundingText("wsrp_rewritewsrp_rewrite", "wsrp_rewrite"); } @DataProvider public String[][] namespaceTestData() { return withAndWithoutSurroundingText("wsrp_rewrite_", NAMESPACE_DUMMY); } @DataProvider public String[][] urlTestData() { String in = "wsrp_rewrite?wsrp-urlType=resource&wsrp-navigationalState=orbeon.path%3D%252Fxforms-server%252Forbeon.css/wsrp_rewrite"; String out = "/xforms-server/orbeon.css"; return withAndWithoutSurroundingText(in, out); } private static String[][] withAndWithoutSurroundingText(String in, String out) { return new String[][] { {in, out}, {"beforeText" + in, "beforeText" + out}, {in + "afterText", out + "afterText"}, {"beforeText" + in + "afterText", "beforeText" + out + "afterText"} }; } private static class SetterGetterAnswer implements Answer { private String string; @Override public String answer(InvocationOnMock invocation) throws Throwable { if (invocation.getArguments().length >= 1) string = (String)invocation.getArguments()[0]; return string; } } }