package de.superx.servlet; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; import java.util.List; import jakarta.servlet.http.HttpServletRequest; import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import de.superx.common.SxUser; public class SuperXmlMaskeTest extends BaseServletTest { private SuperXmlMaske superXmlMaske = new SuperXmlMaske(); @Ignore public void testInitServletConfig() { // fail("Not yet implemented"); assertTrue(true); } @Ignore public void testDoPostHttpServletRequestHttpServletResponse() { fail("Not yet implemented"); } @Ignore public void testDoGet() { fail("Not yet implemented"); } /** * @author hiber Test method for * {@link de.superx.servlet.AbstractSuperXServlet#getServletInfo()}. */ @Test public void testGetServletInfo() { final String EXPECTED_SERVLET_INFO = "SuperXDBServlet, v.1.2"; // see getServletInfo() in // // SuperXmlMaske.java line 122 ff assertEquals("Servlet Info equals", this.superXmlMaske.getServletInfo(), EXPECTED_SERVLET_INFO); } public void testAbstractSuperXServlet() { fail("Not yet implemented"); } /** * Test method for * {@link de.superx.servlet.AbstractSuperXServlet#getTid(HttpServletRequest)}. */ @Test public void getTid() { final String TID = "123456"; Mockito.when(this.request.getParameter("tid")).thenReturn(TID); assertEquals("Servlet returns the tid passed with the HttpServletRequest", this.superXmlMaske.getTid(this.request), TID); } /** * Test method for * {@link de.superx.servlet.AbstractSuperXServlet#getTid(HttpServletRequest)}. * Method should throw exception if tid is {@code null} or empty */ @Test public void getTidThrowsException() { final String EXCEPTION_MESSAGE = "tid Argument fehlt"; List tids = Arrays.asList(null, ""); for (String tid : tids) { Mockito.when(this.request.getParameter("tid")).thenReturn(tid); Exception excpetedException = null; try { this.superXmlMaske.getTid(this.request); } catch (Exception e) { excpetedException = e; } assertNotNull("Servlet throws exception when tid = \"" + tid + "\"", excpetedException); assertEquals("Servlet throws IllegalArgumentException", excpetedException.getClass(), IllegalArgumentException.class); assertEquals("Exception message is equals '" + EXCEPTION_MESSAGE + "'", excpetedException.getMessage(), EXCEPTION_MESSAGE); } } /** * Test (using reflection) for private method * {@link de.superx.servlet.SuperXmlMaske#isAjaxRequest(HttpServletRequest)} * * @throws NoSuchMethodException * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ @Test public void testIsAjaxRequest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // prepare private method Method method = this.superXmlMaske.getClass().getDeclaredMethod("isAjaxRequest", HttpServletRequest.class); method.setAccessible(true); assertTrue("Method is accessible", method.isAccessible()); // prepare mock request to return true Mockito.when(this.request.getParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("getXML_"))); Boolean result = (Boolean) method.invoke(this.superXmlMaske, this.request); assertTrue("request is an ajax request", result.booleanValue()); // prepare mock request to return false Mockito.when(this.request.getParameterNames()).thenReturn(Collections.enumeration(Arrays.asList(""))); result = (Boolean) method.invoke(this.superXmlMaske, this.request); assertFalse("request is not an ajax request", result.booleanValue()); method.setAccessible(false); assertFalse("Method is not accessible", method.isAccessible()); } public void testCheckCacheClearing() { fail("Not yet implemented"); } public void testForward() { fail("Not yet implemented"); } public void testErstmalAnmelden() { fail("Not yet implemented"); } /** * Test method for * {@link de.superx.servlet.AbstractSuperXServlet#hasProperSession(HttpServletRequest)}. */ @Test public void testHasProperSession() { this.setProperSession(); assertTrue("has proper session returns true", this.superXmlMaske.hasProperSession(this.request)); } /** * Test method for * {@link de.superx.servlet.AbstractSuperXServlet#hasProperSession(HttpServletRequest)}. * Method should return false if sessiontype is {@code null} or does not equal * 'superx' */ @Test public void testHasProperSessionWithWrongSessionType() { Mockito.when(this.session.getAttribute("sessiontype")).thenReturn(null); Mockito.when(this.session.getAttribute("user")).thenReturn(new SxUser("klenke")); Mockito.when(this.request.getSession(false)).thenReturn(this.session); assertFalse("has proper session returns false", this.superXmlMaske.hasProperSession(this.request)); Mockito.when(this.session.getAttribute("sessiontype")).thenReturn("supery"); assertFalse("has proper session returns false", this.superXmlMaske.hasProperSession(this.request)); } /** * Test method for * {@link de.superx.servlet.AbstractSuperXServlet#hasProperSession(HttpServletRequest)}. * Method should return false if user is {@code null} */ @Test public void testHasProperSessionWithMissingUser() { Mockito.when(this.session.getAttribute("sessiontype")).thenReturn("supery"); Mockito.when(this.session.getAttribute("user")).thenReturn(null); Mockito.when(this.request.getSession(false)).thenReturn(this.session); assertFalse("has proper session returns false", this.superXmlMaske.hasProperSession(this.request)); } /** * Test method for {@link de.superx.servlet#isJSONRequest(HttpServletRequest)}. * Method should return false if user is {@code null} */ @Test public void testIsJSONRequest() { final String PARAMETER_NAME = "getJSON"; Mockito.when(this.request.getParameterNames()).thenReturn(Collections.enumeration(Arrays.asList(PARAMETER_NAME))); assertTrue("Method returns true when request contains parameter '" + PARAMETER_NAME + "'", this.superXmlMaske.isJSONRequest(this.request)); Mockito.when(this.request.getParameterNames()).thenReturn(Collections.enumeration(Arrays.asList(""))); assertFalse("Method returns false when request does not contain parameter '" + PARAMETER_NAME + "'", this.superXmlMaske.isJSONRequest(this.request)); } /* * static Method of AbstractSuperXServlet */ @Ignore public void testSetEncoding() throws UnsupportedEncodingException { fail("Not yet implemented"); } public void testServiceServletRequestServletResponse() { fail("Not yet implemented"); } public void testHttpServlet() { fail("Not yet implemented"); } public void testDoGetHttpServletRequestHttpServletResponse1() { fail("Not yet implemented"); } public void testGetLastModified() { fail("Not yet implemented"); } public void testDoHead() { fail("Not yet implemented"); } public void testDoPostHttpServletRequestHttpServletResponse1() { fail("Not yet implemented"); } public void testDoPut() { fail("Not yet implemented"); } public void testDoDelete() { fail("Not yet implemented"); } public void testDoOptions() { fail("Not yet implemented"); } public void testDoTrace() { fail("Not yet implemented"); } public void testServiceHttpServletRequestHttpServletResponse() { fail("Not yet implemented"); } public void testGenericServlet() { fail("Not yet implemented"); } public void testDestroy() { fail("Not yet implemented"); } public void testGetInitParameter() { fail("Not yet implemented"); } public void testGetInitParameterNames() { fail("Not yet implemented"); } public void testGetServletConfig() { fail("Not yet implemented"); } public void testGetServletContext() { fail("Not yet implemented"); } public void testGetServletInfo1() { fail("Not yet implemented"); } public void testInitServletConfig1() { fail("Not yet implemented"); } public void testInit() { fail("Not yet implemented"); } public void testLogString() { fail("Not yet implemented"); } public void testLogStringThrowable() { fail("Not yet implemented"); } public void testGetServletName() { fail("Not yet implemented"); } public void testObject() { fail("Not yet implemented"); } public void testGetClass() { fail("Not yet implemented"); } public void testHashCode() { fail("Not yet implemented"); } public void testEquals() { fail("Not yet implemented"); } public void testClone() { fail("Not yet implemented"); } public void testToString() { fail("Not yet implemented"); } public void testNotify() { fail("Not yet implemented"); } public void testNotifyAll() { fail("Not yet implemented"); } public void testWaitLong() { fail("Not yet implemented"); } public void testWaitLongInt() { fail("Not yet implemented"); } public void testWait() { fail("Not yet implemented"); } public void testFinalize() { fail("Not yet implemented"); } }