@Test public void testQueryForList() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(true, true, false); given(this.resultSet.getObject(1)).willReturn(11, 12); List<Map<String, Object>> li = this.template.queryForList(sql); assertEquals("All rows returned", 2, li.size()); assertEquals("First row is Integer", 11, ((Integer) li.get(0).get("age")).intValue()); assertEquals("Second row is Integer", 12, ((Integer) li.get(1).get("age")).intValue()); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test public void deferredQueryWithSingleResult() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").getSingleResult(); verify(query).getSingleResult(); verify(targetEm).close(); }
@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); }
@Before public void setup() { this.session = mock(Session.class); given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class)); given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class)); this.webSocketHandler = mock(WebSocketHandler.class); this.webSocketSession = new JettyWebSocketSession(null, null); this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession); }
@Test public void testQueryForObjectWithBigInteger() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getObject(1, BigInteger.class)).willReturn(new BigInteger("22")); assertEquals(new BigInteger("22"), this.template.queryForObject(sql, BigInteger.class)); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test public void deferredQueryWithResultList() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").getResultList(); verify(query).getResultList(); verify(targetEm).close(); }
@SuppressWarnings("unchecked") @Test public void responseBodyAdvice() { RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class); ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class); List<Object> advice = Arrays.asList(requestAdvice, responseAdvice); RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice); String expected = "body++"; given(responseAdvice.supports(this.returnType, this.converterType)).willReturn(true); given(responseAdvice.beforeBodyWrite(eq(this.body), eq(this.returnType), eq(this.contentType), eq(this.converterType), same(this.request), same(this.response))).willReturn(expected); String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType, this.converterType, this.request, this.response); assertEquals(expected, actual); }
@Test public void testQueryForObjectWithBigDecimal() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getBigDecimal(1)).willReturn(new BigDecimal("22.5")); assertEquals(new BigDecimal("22.5"), this.template.queryForObject(sql, BigDecimal.class)); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test public void deferredQueryWithUpdate() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").executeUpdate(); verify(query).executeUpdate(); verify(targetEm).close(); }
@Test public void testQueryForListWithSingleRowAndColumn() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getObject(1)).willReturn(11); List<Map<String, Object>> li = this.template.queryForList(sql); assertEquals("All rows returned", 1, li.size()); assertEquals("First row is Integer", 11, ((Integer) li.get(0).get("age")).intValue()); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test public void deferredQueryWithResultStream() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").getResultStream(); verify(query).getResultStream(); verify(targetEm).close(); }
@Test public void testQueryForObjectWithInteger() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(22); assertEquals(Integer.valueOf(22), this.template.queryForObject(sql, Integer.class)); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test public void resolveDestinationForQueue() throws JMSException { Session session = mock(Session.class); DestinationResolver destinationResolver = mock(DestinationResolver.class); Destination destination = mock(Destination.class); given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination); JmsResponse<String> jmsResponse = JmsResponse.forQueue("foo", "myQueue"); Destination actual = jmsResponse.resolveDestination(destinationResolver, session); assertSame(destination, actual); }
@Test public void testQueryForInt() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(22); int i = this.template.queryForObject(sql, Integer.class).intValue(); assertEquals("Return of an int", 22, i); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test // SPR-15041 public void applicationOctetStreamDefaultContentType() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); ClassPathResource body = Mockito.mock(ClassPathResource.class); BDDMockito.given(body.getFilename()).willReturn("spring.dat"); BDDMockito.given(body.contentLength()).willReturn(12L); BDDMockito.given(body.getInputStream()).willReturn(new ByteArrayInputStream("Spring Framework".getBytes())); HttpRange range = HttpRange.createByteRange(0, 5); ResourceRegion resourceRegion = range.toResourceRegion(body); converter.write(Collections.singletonList(resourceRegion), null, outputMessage); assertThat(outputMessage.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM)); assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/12")); assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring")); }
@Test public void testQueryForLong() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getLong(1)).willReturn(87L); long l = this.template.queryForObject(sql, Long.class).longValue(); assertEquals("Return of a long", 87, l); verify(this.resultSet).close(); verify(this.statement).close(); }
@Test @SuppressWarnings("resource") public void getPrincipalWithNativeSession() { TestPrincipal user = new TestPrincipal("joe"); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(user); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); assertSame(user, session.getPrincipal()); }
@Test public void testQueryForListWithIntegerElement() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(11); List<Integer> li = this.template.queryForList(sql, Integer.class); assertEquals("All rows returned", 1, li.size()); assertEquals("Element is Integer", 11, li.get(0).intValue()); verify(this.resultSet).close(); verify(this.statement).close(); }
@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 public void testQueryForListWithArgsAndSingleRowAndColumn() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getObject(1)).willReturn(11); List<Map<String, Object>> li = this.template.queryForList(sql, new Object[] {3}); assertEquals("All rows returned", 1, li.size()); assertEquals("First row is Integer", 11, ((Integer) li.get(0).get("age")).intValue()); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); verify(this.preparedStatement).close(); }