FileWriter { public void write(final InputStream inputStream, final String fileName, final String exceptionMessage) { boolean isClass = fileName.endsWith(".class"); boolean isJar = fileName.endsWith(".jar"); if (isClass) { writeClass(fileName, inputStream, exceptionMessage); } else if (isJar) { writeJar(fileName, inputStream, exceptionMessage); } else { writeOther(fileName, inputStream, exceptionMessage); } }  void write(final InputStream inputStream, final String fileName, final String exceptionMessage);  }
@Test public void testWriteShouldWriteFileToOutputStream() throws IOException { given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); verify(fileOutputStream).write(Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt()); }
@Test public void testWriteWhenFileExistsShouldNotCreatNewFile() throws IOException { given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); given(file.exists()).willReturn(true); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); verify(file, Mockito.never()).createNewFile(); }
@Test public void testWriteShouldCloseOutputStream() throws IOException { given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); verify(fileOutputStream).close(); }
@Test public void testWriteWhenFileIsInPackageShouldCreateFolderStructure() throws IOException { String directories = "com/epam/wilma"; given(file.getParent()).willReturn(directories); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); verify(fileFactory.createFile(directories)).mkdirs(); }
@Test public void testWriteWhenFileIsNotInPackageShouldCreateNotFolderStructure() throws IOException { given(file.getParent()).willReturn(null); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); verify(fileFactory.createFile(Mockito.anyString()), never()).mkdirs(); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testWriteWhenCannotWriteShouldThrowException() throws IOException { given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willThrow(new FileNotFoundException()); underTest.write(inputStream, FILE_NAME, EXCEPTION_MESSAGE); }
@Test public void testWriteWhenFileIsAClassFile() throws IOException { String fileName = "MyClass.class"; given(byteArrayInputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); given(inputStreamUtil.transformToByteArray(inputStream)).willReturn(FILE_AS_BYTES); given(byteArrayInputStreamFactory.createByteArrayInputStream(FILE_AS_BYTES)).willReturn(byteArrayInputStream); given(classFilePathAssembler.createFilePath(byteArrayInputStream, fileName, EXCEPTION_MESSAGE)).willReturn(fileName); given(fileFactory.createFile(fileName)).willReturn(file); given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); given(file.exists()).willReturn(true); underTest.write(inputStream, fileName, EXCEPTION_MESSAGE); verify(fileFactory).createFile(Mockito.anyString()); verify(inputStreamUtil).transformToByteArray(inputStream); verify(byteArrayInputStreamFactory, times(2)).createByteArrayInputStream(FILE_AS_BYTES); verify(classFilePathAssembler).createFilePath(byteArrayInputStream, fileName, EXCEPTION_MESSAGE); verify(fileFactory).createFile(fileName); verify(file).getParent(); verify(fileOutputStreamFactory).createFileOutputStream(file); verify(file).exists(); }
@Test public void testWriteWhenFileIsAValidJarFile() throws IOException { String fileName = "myjar.jar"; given(byteArrayInputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); given(inputStreamUtil.transformToByteArray(inputStream)).willReturn(FILE_AS_BYTES); given(byteArrayInputStreamFactory.createByteArrayInputStream(FILE_AS_BYTES)).willReturn(byteArrayInputStream); given(fileFactory.createFile(fileName)).willReturn(file); given(file.getParent()).willReturn(DIRECTORIES); given(fileOutputStreamFactory.createFileOutputStream(file)).willReturn(fileOutputStream); given(file.exists()).willReturn(true); underTest.write(inputStream, fileName, EXCEPTION_MESSAGE); verify(fileFactory).createFile(Mockito.anyString()); verify(fileFactory).createFile(fileName); verify(file).getParent(); verify(fileOutputStreamFactory).createFileOutputStream(file); verify(file).exists(); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testWriteWhenFileIsAnInvalidJarFile() throws IOException { String fileName = "myjar.jar"; given(byteArrayInputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); given(inputStreamUtil.transformToByteArray(inputStream)).willReturn(FILE_AS_BYTES); given(byteArrayInputStreamFactory.createByteArrayInputStream(FILE_AS_BYTES)).willReturn(byteArrayInputStream); doThrow(new CannotUploadExternalResourceException(EXCEPTION_MESSAGE)).when(jarValidator).validateInputStream(byteArrayInputStream); underTest.write(inputStream, fileName, EXCEPTION_MESSAGE); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testWriteWhenFileIsAClassFileShouldThrowCannotUploadExternalResourceExceptionWhenTransformingFails() throws IOException { String fileName = "MyClass.class"; given(inputStreamUtil.transformToByteArray(inputStream)).willThrow(new IOException()); underTest.write(inputStream, fileName, EXCEPTION_MESSAGE); }