@Test // SPR-10806 public void unmarshalSaxSourceWithXmlOptions() throws Exception { final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { @Override protected javax.xml.bind.Unmarshaller createUnmarshaller() { return unmarshaller; } }; // 1. external-general-entities and dtd support disabled (default) marshaller.unmarshal(new SAXSource(new InputSource("1"))); ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class); verify(unmarshaller).unmarshal(sourceCaptor.capture()); SAXSource result = sourceCaptor.getValue(); assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); // 2. external-general-entities and dtd support enabled reset(unmarshaller); marshaller.setProcessExternalEntities(true); marshaller.setSupportDtd(true); marshaller.unmarshal(new SAXSource(new InputSource("1"))); verify(unmarshaller).unmarshal(sourceCaptor.capture()); result = sourceCaptor.getValue(); assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); }
@Test public void testStaticResultSetClosed() throws Exception { ResultSet resultSet2 = mock(ResultSet.class); reset(this.preparedStatement); given(this.preparedStatement.executeQuery()).willReturn(resultSet2); given(this.connection.createStatement()).willReturn(this.statement); try { this.template.query("my query", (ResultSetExtractor<Object>) rs -> { throw new InvalidDataAccessApiUsageException(""); }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException ex) { // ok } try { this.template.query(con -> con.prepareStatement("my query"), (ResultSetExtractor<Object>) rs2 -> { throw new InvalidDataAccessApiUsageException(""); }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException ex) { // ok } verify(this.resultSet).close(); verify(resultSet2).close(); verify(this.preparedStatement).close(); verify(this.connection, atLeastOnce()).close(); }
@Test // SPR-10806 public void unmarshalStreamSourceWithXmlOptions() throws Exception { final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { @Override protected javax.xml.bind.Unmarshaller createUnmarshaller() { return unmarshaller; } }; // 1. external-general-entities and dtd support disabled (default) marshaller.unmarshal(new StreamSource("1")); ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class); verify(unmarshaller).unmarshal(sourceCaptor.capture()); SAXSource result = sourceCaptor.getValue(); assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); // 2. external-general-entities and dtd support enabled reset(unmarshaller); marshaller.setProcessExternalEntities(true); marshaller.setSupportDtd(true); marshaller.unmarshal(new StreamSource("1")); verify(unmarshaller).unmarshal(sourceCaptor.capture()); result = sourceCaptor.getValue(); assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); }
@Test @SuppressWarnings("resource") public void getPrincipalNone() { Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(null); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertEquals(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception { JmsTemplate template = createTemplate(); template.setConnectionFactory(this.connectionFactory); template.setMessageConverter(new SimpleMessageConverter()); String s = "Hello world"; MessageProducer messageProducer = mock(MessageProducer.class); TextMessage textMessage = mock(TextMessage.class); reset(this.session); given(this.session.createProducer(this.queue)).willReturn(messageProducer); given(this.session.createTextMessage("Hello world")).willReturn(textMessage); willThrow(original).given(messageProducer).send(textMessage); try { template.convertAndSend(this.queue, s); fail("Should have thrown JmsException"); } catch (JmsException wrappedEx) { // expected assertEquals(thrownExceptionClass, wrappedEx.getClass()); assertEquals(original, wrappedEx.getCause()); } verify(messageProducer).close(); verify(this.session).close(); verify(this.connection).close(); }
@Test public void testFindCustomerMixed() throws SQLException { reset(connection); PreparedStatement preparedStatement2 = mock(PreparedStatement.class); ResultSet resultSet2 = mock(ResultSet.class);
@Test public void handleFrameMessageWhenNotOpen() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(); reset(this.handler); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); verifyNoMoreInteractions(this.handler); }
@Test @SuppressWarnings("resource") public void getPrincipalNotAvailable() { UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(protocol); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getPrincipalFromNativeSession() { TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(user); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(user, session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Before public void setup() { this.servletRequest = new MockHttpServletRequest(); this.asyncManager = WebAsyncUtils.getAsyncManager(servletRequest); this.asyncManager.setTaskExecutor(new SyncTaskExecutor()); this.asyncWebRequest = mock(AsyncWebRequest.class); this.asyncManager.setAsyncWebRequest(this.asyncWebRequest); verify(this.asyncWebRequest).addCompletionHandler((Runnable) notNull()); reset(this.asyncWebRequest); }
@Test public void isConcurrentHandlingStarted() { given(this.asyncWebRequest.isAsyncStarted()).willReturn(false); assertFalse(this.asyncManager.isConcurrentHandlingStarted()); reset(this.asyncWebRequest); given(this.asyncWebRequest.isAsyncStarted()).willReturn(true); assertTrue(this.asyncManager.isConcurrentHandlingStarted()); }
@Test @SuppressWarnings("unchecked") public void shouldHandleReturnValueWithResponseBodyAdvice() throws Exception { servletRequest.addHeader("Accept", "text/*"); servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML)); ResponseEntity<String> returnValue = new ResponseEntity<>(HttpStatus.OK); ResponseBodyAdvice<String> advice = mock(ResponseBodyAdvice.class); given(advice.supports(any(), any())).willReturn(true); given(advice.beforeBodyWrite(any(), any(), any(), any(), any(), any())).willReturn("Foo"); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor( Collections.singletonList(stringHttpMessageConverter), null, Collections.singletonList(advice)); reset(stringHttpMessageConverter); given(stringHttpMessageConverter.canWrite(String.class, MediaType.TEXT_HTML)).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertTrue(mavContainer.isRequestHandled()); verify(stringHttpMessageConverter).write(eq("Foo"), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class)); }
@Test public void handleTransportRequestXhrSendWithDifferentUser() throws Exception { String sockJsPath = sessionUrlPrefix + "xhr"; setRequest("POST", sockJsPrefix + sockJsPath); this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(200, this.servletResponse.getStatus()); // session created verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session); this.session.setPrincipal(new TestPrincipal("little red riding hood")); this.servletRequest.setUserPrincipal(new TestPrincipal("wolf")); resetResponse(); reset(this.xhrSendHandler); sockJsPath = sessionUrlPrefix + "xhr_send"; setRequest("POST", sockJsPrefix + sockJsPath); this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(404, this.servletResponse.getStatus()); verifyNoMoreInteractions(this.xhrSendHandler); }
reset(messageConverter);
reset(asyncWebRequest); given(asyncWebRequest.isAsyncStarted()).willReturn(false);