@Test // SPR-16773 public void shouldNotDrainWhenErrorStreamClosed() throws Exception { InputStream is = mock(InputStream.class); given(this.connection.getErrorStream()).willReturn(is); doNothing().when(is).close(); given(is.read(any())).willThrow(new NullPointerException("from HttpURLConnection#ErrorStream")); InputStream responseStream = this.response.getBody(); responseStream.close(); this.response.close(); verify(is).close(); }
@Test public void testCouldNotGetConnectionForOperationOrExceptionTranslator() throws SQLException { SQLException sqlException = new SQLException("foo", "07xxx"); this.dataSource = mock(DataSource.class); given(this.dataSource.getConnection()).willThrow(sqlException); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }
@Test(expected = TypeMismatchDataAccessException.class) // SPR-16483 public void doesNotUseConversionService() throws SQLException { SingleColumnRowMapper<LocalDateTime> rowMapper = SingleColumnRowMapper.newInstance(LocalDateTime.class, null); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData metaData = mock(ResultSetMetaData.class); given(metaData.getColumnCount()).willReturn(1); given(resultSet.getMetaData()).willReturn(metaData); given(resultSet.getObject(1, LocalDateTime.class)) .willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getTimestamp(1)).willReturn(new Timestamp(0)); rowMapper.mapRow(resultSet, 1); }
@Test // SPR-16483 public void useDefaultConversionService() throws SQLException { Timestamp timestamp = new Timestamp(0); SingleColumnRowMapper<LocalDateTime> rowMapper = SingleColumnRowMapper.newInstance(LocalDateTime.class); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData metaData = mock(ResultSetMetaData.class); given(metaData.getColumnCount()).willReturn(1); given(resultSet.getMetaData()).willReturn(metaData); given(resultSet.getObject(1, LocalDateTime.class)) .willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getTimestamp(1)).willReturn(timestamp); LocalDateTime actualLocalDateTime = rowMapper.mapRow(resultSet, 1); assertEquals(timestamp.toLocalDateTime(), actualLocalDateTime); }
@Test public void testCouldNotGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException { SQLException sqlException = new SQLException("foo", "07xxx"); this.dataSource = mock(DataSource.class); given(this.dataSource.getConnection()).willThrow(sqlException); this.template = new JdbcTemplate(); this.template.setDataSource(this.dataSource); this.template.afterPropertiesSet(); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }
@Test public void testLookupWhereBeanFactoryYieldsNonDataSourceType() throws Exception { final BeanFactory beanFactory = mock(BeanFactory.class); given(beanFactory.getBean(DATASOURCE_BEAN_NAME, DataSource.class)).willThrow( new BeanNotOfRequiredTypeException(DATASOURCE_BEAN_NAME, DataSource.class, String.class)); try { BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory); lookup.getDataSource(DATASOURCE_BEAN_NAME); fail("should have thrown DataSourceLookupFailureException"); } catch (DataSourceLookupFailureException ex) { /* expected */ } }
@Test(expected = HttpClientErrorException.class) public void handleErrorIOException() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); given(response.getStatusText()).willReturn("Not Found"); given(response.getHeaders()).willReturn(headers); given(response.getBody()).willThrow(new IOException()); handler.handleError(response); }
@Test // SPR-13620 @SuppressWarnings("unchecked") public void writeContentInputStreamThrowingNullPointerException() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); InputStream in = mock(InputStream.class); given(resource.getInputStream()).willReturn(in); given(in.read(any())).willThrow(NullPointerException.class); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test public void getFileUnsupported() throws IOException { Path path = mock(Path.class); given(path.normalize()).willReturn(path); given(path.toFile()).willThrow(new UnsupportedOperationException()); PathResource resource = new PathResource(path); thrown.expect(FileNotFoundException.class); resource.getFile(); }
@Test public void connectInfoRequestFailure() throws URISyntaxException { HttpServerErrorException exception = new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE); given(this.infoReceiver.executeInfoRequest(any(), any())).willThrow(exception); this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback); verify(this.connectCallback).onFailure(exception); assertFalse(this.webSocketTransport.invoked()); assertFalse(this.xhrTransport.invoked()); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void toResourceRegionExceptionLength() throws IOException { InputStreamResource resource = mock(InputStreamResource.class); given(resource.contentLength()).willThrow(IOException.class); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
@Test // SPR-12999 @SuppressWarnings("unchecked") public void writeContentNotGettingInputStream() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); given(resource.getInputStream()).willThrow(FileNotFoundException.class); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test // SPR-11919 public void handleInfoGetWildflyNPE() throws IOException { HttpServletResponse mockResponse = mock(HttpServletResponse.class); ServletOutputStream ous = mock(ServletOutputStream.class); given(mockResponse.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).willThrow(NullPointerException.class); given(mockResponse.getOutputStream()).willReturn(ous); this.response = new ServletServerHttpResponse(mockResponse); handleRequest("GET", "/echo/info", HttpStatus.OK); verify(mockResponse, times(1)).getOutputStream(); }
@Test public void testStoredProcedureExceptionTranslator() throws Exception { SQLException sqlException = new SQLException( "Syntax error or access violation exception", "42000"); given(callableStatement.execute()).willThrow(sqlException); given(connection.prepareCall("{call " + StoredProcedureExceptionTranslator.SQL + "()}") ).willReturn(callableStatement); StoredProcedureExceptionTranslator sproc = new StoredProcedureExceptionTranslator(dataSource); thrown.expect(CustomDataException.class); sproc.execute(); }
@Test public void supportsParameterThrowsException() throws Exception { given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception()); assertFalse("Parameter supported", adapter.supportsParameter(parameter)); verify(adaptee).resolveArgument(parameter, webRequest); }
@Test(expected = IllegalArgumentException.class) public void setValueAsMultipartFileWithBadBytes() throws Exception { MultipartFile file = mock(MultipartFile.class); given(file.getBytes()).willThrow(new IOException()); editor.setValue(file); }
@Test public void testGetFromDataSourceWithSQLException() throws Exception { SQLException expectedSQLException = new SQLException(); DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willThrow(expectedSQLException); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); assertIsEmpty(sec); }
@Test public void testNoSuchStoredProcedure() throws Exception { SQLException sqlException = new SQLException( "Syntax error or access violation exception", "42000"); given(callableStatement.execute()).willThrow(sqlException); given(connection.prepareCall("{call " + NoSuchStoredProcedure.SQL + "()}")).willReturn( callableStatement); NoSuchStoredProcedure sproc = new NoSuchStoredProcedure(dataSource); thrown.expect(BadSqlGrammarException.class); sproc.execute(); }
@Test(expected = Exception.class) public void resolveArgumentThrowsException() throws Exception { given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception()); adapter.resolveArgument(parameter, null, webRequest, null); }
@Test public void doesNotPropagateFatalExceptionOnResourceThatCannotBeResolvedToAFile() throws Exception { Resource resource = mock(Resource.class); given(resource.lastModified()).willThrow(new IOException()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); long lastModified = scriptSource.retrieveLastModifiedTime(); assertEquals(0, lastModified); }