BAEL-5942 Code changes (#13147)

* BAEL-5942 Code changes

* BAEL-5942 Code changes

* BAEL-5942 Code updates from Review

* BAEL-5942 Code updates from Review

* BAEL-5942 Code updates from Review

* BAEL-5942 Code updates from Review

* BAEL-5942 Code updates from Review

* BAEL-5942 Code updates from Review

* BAEL-5942 Test updates from Review

* BAEL-5942 Test updates from Review

* BAEL-5942 Test updates from Review
This commit is contained in:
brokenhardisk 2023-01-22 12:48:56 +01:00 committed by GitHub
parent fa6b78233c
commit b958c3fdcb
7 changed files with 189 additions and 0 deletions

View File

@ -23,6 +23,7 @@
<module>spring-mvc-basics-4</module>
<module>spring-mvc-basics-5</module>
<module>spring-mvc-crash</module>
<module>spring-mvc-file</module>
<module>spring-mvc-forms-jsp</module>
<module>spring-mvc-forms-thymeleaf</module>
<module>spring-mvc-java</module>

View File

@ -0,0 +1,9 @@
*.class
#folders#
/target
# Packaged files #
*.jar
*.war
*.ear

View File

@ -0,0 +1,9 @@
## Spring MVC File
### The Course
### Relevant Articles:

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-mvc-file</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-mvc-file</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<build>
<finalName>spring-mvc-file</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.Application</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.file;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
public class CustomMultipartFile implements MultipartFile {
private byte[] input;
public CustomMultipartFile(byte[] input) {
this.input = input;
}
@Override
public String getName() {
return null;
}
@Override
public String getOriginalFilename() {
return null;
}
@Override
public String getContentType() {
return null;
}
@Override
public boolean isEmpty() {
return input == null || input.length == 0;
}
@Override
public long getSize() {
return input.length;
}
@Override
public byte[] getBytes() throws IOException {
return input;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(input);
}
@Override
public void transferTo(File destination) throws IOException, IllegalStateException {
try(FileOutputStream fos = new FileOutputStream(destination)) {
fos.write(input);
}
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.file;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockMultipartFile;
class CustomMultipartFileUnitTest {
@Test
void whenProvidingByteArray_thenMultipartFileCreated() throws IOException {
byte[] inputArray = "Test String".getBytes();
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
Assertions.assertFalse(customMultipartFile.isEmpty());
Assertions.assertArrayEquals(inputArray, customMultipartFile.getBytes());
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
}
@Test
void whenProvidingEmptyByteArray_thenMockMultipartFileIsEmpty() throws IOException {
byte[] inputArray = "".getBytes();
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
Assertions.assertTrue(mockMultipartFile.isEmpty());
}
@Test
void whenProvidingNullByteArray_thenMockMultipartFileIsEmpty() throws IOException {
byte[] inputArray = null;
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
Assertions.assertTrue(mockMultipartFile.isEmpty());
}
@Test
void whenProvidingByteArray_thenMultipartFileInputSizeMatches() throws IOException {
byte[] inputArray = "Testing String".getBytes();
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
}
@Test
void whenProvidingByteArray_thenMockMultipartFileCreated() throws IOException {
byte[] inputArray = "Test String".getBytes();
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
Assertions.assertFalse(mockMultipartFile.isEmpty());
Assertions.assertArrayEquals(inputArray, mockMultipartFile.getBytes());
Assertions.assertEquals(inputArray.length, mockMultipartFile.getSize());
}
}