ClassFilePathAssembler { public String createFilePath(final InputStream classFile, final String fileName, final String exceptionMessage) { String filePath = null; File file = fileFactory.createFile(fileName); String folderPart = file.getParent(); String filePart = file.getName(); String packageName; try { JavaClass javaClass = javaClassFactory.createJavaClass(classFile, fileName); packageName = javaClass.getPackageName(); String packageAsFolderName = packageName.replace('.', '/'); String folderStructure = folderPart + FORWARDSLASH + packageAsFolderName; filePath = folderStructure + FORWARDSLASH + filePart; } catch (IOException e) { throw new CannotUploadExternalResourceException(exceptionMessage + fileName, e); } catch (ClassFormatException e) { throw new CannotUploadExternalResourceException(exceptionMessage + String.format(CLASS_FORMAT_EXCEPTION_MESSAGE, fileName), e); } return filePath; }  String createFilePath(final InputStream classFile, final String fileName, final String exceptionMessage);  }
@Test public void testCreateFilePathShouldReturnProperPath() throws IOException { String expected = PARENT_DIRECTORY + "/" + PACKAGE_NAME.replace(".", "/") + "/" + SIMPLE_CLASS_NAME; given(fileFactory.createFile(FILE_NAME)).willReturn(file); given(file.getParent()).willReturn(PARENT_DIRECTORY); given(file.getName()).willReturn(SIMPLE_CLASS_NAME); given(javaClassFactory.createJavaClass(classFile, FILE_NAME)).willReturn(javaClass); given(javaClass.getPackageName()).willReturn(PACKAGE_NAME); given(javaClass.getClassName()).willReturn(PACKAGE_NAME + "." + SIMPLE_CLASS_NAME); String result = underTest.createFilePath(classFile, FILE_NAME, EXCEPTION_MESSAGE); assertEquals(result, expected); verify(fileFactory).createFile(FILE_NAME); verify(file).getParent(); verify(file).getName(); verify(javaClassFactory).createJavaClass(classFile, FILE_NAME); verify(javaClass).getPackageName(); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testCreateFilePathShouldRethrowIOExceptionAsCannotUploadExternalResourceException() throws IOException { given(fileFactory.createFile(FILE_NAME)).willReturn(file); given(file.getParent()).willReturn(PARENT_DIRECTORY); given(file.getName()).willReturn(SIMPLE_CLASS_NAME); given(javaClassFactory.createJavaClass(classFile, FILE_NAME)).willThrow(new IOException()); underTest.createFilePath(classFile, FILE_NAME, EXCEPTION_MESSAGE); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testCreateFilePathShouldRethrowClassFormatExceptionAsCannotUploadExternalResourceException() throws IOException { given(fileFactory.createFile(FILE_NAME)).willReturn(file); given(file.getParent()).willReturn(PARENT_DIRECTORY); given(file.getName()).willReturn(SIMPLE_CLASS_NAME); given(javaClassFactory.createJavaClass(classFile, FILE_NAME)).willThrow(new ClassFormatException()); underTest.createFilePath(classFile, FILE_NAME, EXCEPTION_MESSAGE); }