Commit 20bc65aa9a155ce8d94e2161553223f719c89ac6
1 parent
d9ae9743
Exists in
master
Refatoração de teste unitário: FileRendererImplTest
Showing
1 changed file
with
171 additions
and
9 deletions
Show diff stats
impl/extension/jsf/src/test/java/br/gov/frameworkdemoiselle/internal/implementation/FileRendererImplTest.java
| @@ -36,7 +36,10 @@ | @@ -36,7 +36,10 @@ | ||
| 36 | */ | 36 | */ |
| 37 | package br.gov.frameworkdemoiselle.internal.implementation; | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | ||
| 39 | +import java.io.ByteArrayInputStream; | ||
| 40 | +import java.io.File; | ||
| 39 | import java.io.IOException; | 41 | import java.io.IOException; |
| 42 | +import java.io.InputStream; | ||
| 40 | 43 | ||
| 41 | import javax.faces.context.FacesContext; | 44 | import javax.faces.context.FacesContext; |
| 42 | import javax.servlet.ServletOutputStream; | 45 | import javax.servlet.ServletOutputStream; |
| @@ -123,7 +126,7 @@ public class FileRendererImplTest { | @@ -123,7 +126,7 @@ public class FileRendererImplTest { | ||
| 123 | } | 126 | } |
| 124 | 127 | ||
| 125 | @Test | 128 | @Test |
| 126 | - public void testRenderBytesSuccess() { | 129 | + public void testRenderBytesSuccess() throws IOException { |
| 127 | byte[] bytes = "Test".getBytes(); | 130 | byte[] bytes = "Test".getBytes(); |
| 128 | String fileName = "fileName.pdf"; | 131 | String fileName = "fileName.pdf"; |
| 129 | 132 | ||
| @@ -143,24 +146,183 @@ public class FileRendererImplTest { | @@ -143,24 +146,183 @@ public class FileRendererImplTest { | ||
| 143 | EasyMock.expectLastCall().times(1); | 146 | EasyMock.expectLastCall().times(1); |
| 144 | 147 | ||
| 145 | ServletOutputStream stream = PowerMock.createMock(ServletOutputStream.class); | 148 | ServletOutputStream stream = PowerMock.createMock(ServletOutputStream.class); |
| 149 | + stream.write(bytes, 0, bytes.length); | ||
| 150 | + EasyMock.expectLastCall().times(1); | ||
| 151 | + | ||
| 152 | + stream.flush(); | ||
| 153 | + EasyMock.expectLastCall().times(1); | ||
| 154 | + | ||
| 155 | + stream.close(); | ||
| 156 | + EasyMock.expectLastCall().times(1); | ||
| 157 | + | ||
| 158 | + EasyMock.expect(response.getOutputStream()).andReturn(stream).times(3); | ||
| 159 | + | ||
| 160 | + PowerMock.replayAll(); | ||
| 161 | + renderer.render(bytes, ContentType.PDF, fileName); | ||
| 162 | + PowerMock.verifyAll(); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + @Test | ||
| 166 | + public void testRenderStreamFail() { | ||
| 167 | + byte[] bytes = "Test".getBytes(); | ||
| 168 | + InputStream stream = new ByteArrayInputStream(bytes); | ||
| 169 | + String fileName = "fileName.pdf"; | ||
| 170 | + | ||
| 171 | + logger.debug(EasyMock.anyObject(String.class)); | ||
| 172 | + EasyMock.expectLastCall().anyTimes(); | ||
| 173 | + | ||
| 174 | + IOException exception = new IOException(); | ||
| 175 | + logger.info("Erro na geração do relatório. Incluíndo a exceção de erro em um FacesMessage", exception); | ||
| 176 | + EasyMock.expectLastCall().anyTimes(); | ||
| 177 | + | ||
| 178 | + response.setContentType(ContentType.PDF.getContentType()); | ||
| 179 | + EasyMock.expectLastCall().times(1); | ||
| 180 | + | ||
| 181 | + response.setContentLength(bytes.length); | ||
| 182 | + EasyMock.expectLastCall().times(1); | ||
| 183 | + | ||
| 184 | + response.setHeader("Content-Disposition", "filename=\"" + fileName + "\""); | ||
| 185 | + EasyMock.expectLastCall().times(1); | ||
| 186 | + | ||
| 187 | + facesContext.responseComplete(); | ||
| 188 | + EasyMock.expectLastCall().times(1); | ||
| 189 | + | ||
| 146 | try { | 190 | try { |
| 147 | - stream.write(bytes, 0, bytes.length); | ||
| 148 | - EasyMock.expectLastCall().times(1); | 191 | + EasyMock.expect(response.getOutputStream()).andThrow(exception); |
| 192 | + } catch (IOException e) { | ||
| 193 | + Assert.fail(); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + PowerMock.mockStatic(Faces.class); | ||
| 197 | + Faces.addMessage(exception); | ||
| 198 | + | ||
| 199 | + PowerMock.replayAll(); | ||
| 200 | + renderer.render(stream, ContentType.PDF, fileName, false); | ||
| 201 | + PowerMock.verifyAll(); | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + @Test | ||
| 205 | + public void testRenderStreamSuccess() throws IOException { | ||
| 206 | + byte[] bytes = "Test".getBytes(); | ||
| 207 | + InputStream inputStream = new ByteArrayInputStream(bytes); | ||
| 208 | + | ||
| 209 | + String fileName = "fileName.pdf"; | ||
| 210 | + | ||
| 211 | + logger.debug(EasyMock.anyObject(String.class)); | ||
| 212 | + EasyMock.expectLastCall().anyTimes(); | ||
| 149 | 213 | ||
| 150 | - stream.flush(); | ||
| 151 | - EasyMock.expectLastCall().times(1); | 214 | + facesContext.responseComplete(); |
| 215 | + EasyMock.expectLastCall().times(1); | ||
| 152 | 216 | ||
| 153 | - stream.close(); | ||
| 154 | - EasyMock.expectLastCall().times(1); | 217 | + response.setContentType(ContentType.PDF.getContentType()); |
| 218 | + EasyMock.expectLastCall().times(1); | ||
| 219 | + | ||
| 220 | + response.setContentLength(bytes.length); | ||
| 221 | + EasyMock.expectLastCall().times(1); | ||
| 155 | 222 | ||
| 156 | - EasyMock.expect(response.getOutputStream()).andReturn(stream).times(3); | 223 | + response.setHeader("Content-Disposition", "filename=\"" + fileName + "\""); |
| 224 | + EasyMock.expectLastCall().times(1); | ||
| 225 | + | ||
| 226 | + ServletOutputStream stream = new ServletOutputStream() { | ||
| 227 | + | ||
| 228 | + @Override | ||
| 229 | + public void write(int b) throws IOException { | ||
| 230 | + Assert.assertTrue(true); | ||
| 231 | + } | ||
| 232 | + }; | ||
| 233 | + | ||
| 234 | + EasyMock.expect(response.getOutputStream()).andReturn(stream).times(3); | ||
| 235 | + | ||
| 236 | + PowerMock.replayAll(); | ||
| 237 | + renderer.render(inputStream, ContentType.PDF, fileName); | ||
| 238 | + PowerMock.verifyAll(); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + @Test | ||
| 242 | + public void testRenderFileFail() throws IOException { | ||
| 243 | + | ||
| 244 | + File file = new File("fileName"); | ||
| 245 | + file.createNewFile(); | ||
| 246 | + | ||
| 247 | + String fileName = "fileName.pdf"; | ||
| 248 | + | ||
| 249 | + logger.debug(EasyMock.anyObject(String.class)); | ||
| 250 | + EasyMock.expectLastCall().anyTimes(); | ||
| 251 | + | ||
| 252 | + IOException exception = new IOException(); | ||
| 253 | + logger.info("Erro na geração do relatório. Incluíndo a exceção de erro em um FacesMessage", exception); | ||
| 254 | + EasyMock.expectLastCall().anyTimes(); | ||
| 255 | + | ||
| 256 | + response.setContentType(ContentType.PDF.getContentType()); | ||
| 257 | + EasyMock.expectLastCall().times(1); | ||
| 258 | + | ||
| 259 | + response.setContentLength((int) file.length()); | ||
| 260 | + EasyMock.expectLastCall().times(1); | ||
| 261 | + | ||
| 262 | + response.setHeader("Content-Disposition", "filename=\"" + fileName + "\""); | ||
| 263 | + EasyMock.expectLastCall().times(1); | ||
| 264 | + | ||
| 265 | + facesContext.responseComplete(); | ||
| 266 | + EasyMock.expectLastCall().times(1); | ||
| 267 | + | ||
| 268 | + try { | ||
| 269 | + EasyMock.expect(response.getOutputStream()).andThrow(exception); | ||
| 157 | } catch (IOException e) { | 270 | } catch (IOException e) { |
| 158 | Assert.fail(); | 271 | Assert.fail(); |
| 159 | } | 272 | } |
| 160 | 273 | ||
| 274 | + PowerMock.mockStatic(Faces.class); | ||
| 275 | + Faces.addMessage(exception); | ||
| 276 | + | ||
| 161 | PowerMock.replayAll(); | 277 | PowerMock.replayAll(); |
| 162 | - renderer.render(bytes, ContentType.PDF, fileName); | 278 | + renderer.render(file, ContentType.PDF, fileName); |
| 163 | PowerMock.verifyAll(); | 279 | PowerMock.verifyAll(); |
| 280 | + | ||
| 281 | + file.delete(); | ||
| 164 | } | 282 | } |
| 165 | 283 | ||
| 284 | + @Test | ||
| 285 | + public void testRenderFileNotFoundException() throws IOException { | ||
| 286 | + | ||
| 287 | + File file = new File("fileName"); | ||
| 288 | + file.createNewFile(); | ||
| 289 | + | ||
| 290 | + String fileName = "fileName.pdf"; | ||
| 291 | + | ||
| 292 | + logger.debug(EasyMock.anyObject(String.class)); | ||
| 293 | + EasyMock.expectLastCall().anyTimes(); | ||
| 294 | + | ||
| 295 | + IOException exception = new IOException(); | ||
| 296 | + logger.info("Erro na geração do relatório. Incluíndo a exceção de erro em um FacesMessage", exception); | ||
| 297 | + EasyMock.expectLastCall().anyTimes(); | ||
| 298 | + | ||
| 299 | + response.setContentType(ContentType.PDF.getContentType()); | ||
| 300 | + EasyMock.expectLastCall().times(1); | ||
| 301 | + | ||
| 302 | + response.setContentLength((int) file.length()); | ||
| 303 | + EasyMock.expectLastCall().times(1); | ||
| 304 | + | ||
| 305 | + response.setHeader("Content-Disposition", "filename=\"" + fileName + "\""); | ||
| 306 | + EasyMock.expectLastCall().times(1); | ||
| 307 | + | ||
| 308 | + facesContext.responseComplete(); | ||
| 309 | + EasyMock.expectLastCall().times(1); | ||
| 310 | + | ||
| 311 | + try { | ||
| 312 | + EasyMock.expect(response.getOutputStream()).andThrow(exception); | ||
| 313 | + } catch (IOException e) { | ||
| 314 | + Assert.fail(); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + | ||
| 318 | + | ||
| 319 | + PowerMock.mockStatic(Faces.class); | ||
| 320 | + Faces.addMessage(exception); | ||
| 321 | + | ||
| 322 | + PowerMock.replayAll(); | ||
| 323 | + renderer.render(file, ContentType.PDF, fileName); | ||
| 324 | + PowerMock.verifyAll(); | ||
| 325 | + | ||
| 326 | + file.delete(); | ||
| 327 | + } | ||
| 166 | } | 328 | } |