Skip to content

Commit b958c3f

Browse files
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
1 parent fa6b782 commit b958c3f

7 files changed

Lines changed: 189 additions & 0 deletions

File tree

spring-web-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<module>spring-mvc-basics-4</module>
2424
<module>spring-mvc-basics-5</module>
2525
<module>spring-mvc-crash</module>
26+
<module>spring-mvc-file</module>
2627
<module>spring-mvc-forms-jsp</module>
2728
<module>spring-mvc-forms-thymeleaf</module>
2829
<module>spring-mvc-java</module>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
6+
# Packaged files #
7+
*.jar
8+
*.war
9+
*.ear
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Spring MVC File
2+
3+
4+
5+
### The Course
6+
7+
8+
### Relevant Articles:
9+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-mvc-file</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<name>spring-mvc-file</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-boot-2</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../../parent-boot-2</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>commons-io</groupId>
30+
<artifactId>commons-io</artifactId>
31+
<version>${commons-io.version}</version>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<finalName>spring-mvc-file</finalName>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-maven-plugin</artifactId>
42+
<configuration>
43+
<mainClass>com.baeldung.Application</mainClass>
44+
<layout>JAR</layout>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.file;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
import org.springframework.web.multipart.MultipartFile;
10+
11+
public class CustomMultipartFile implements MultipartFile {
12+
private byte[] input;
13+
14+
public CustomMultipartFile(byte[] input) {
15+
this.input = input;
16+
}
17+
18+
@Override
19+
public String getName() {
20+
return null;
21+
}
22+
23+
@Override
24+
public String getOriginalFilename() {
25+
return null;
26+
}
27+
28+
@Override
29+
public String getContentType() {
30+
return null;
31+
}
32+
33+
@Override
34+
public boolean isEmpty() {
35+
return input == null || input.length == 0;
36+
}
37+
38+
@Override
39+
public long getSize() {
40+
return input.length;
41+
}
42+
43+
@Override
44+
public byte[] getBytes() throws IOException {
45+
return input;
46+
}
47+
48+
@Override
49+
public InputStream getInputStream() throws IOException {
50+
return new ByteArrayInputStream(input);
51+
}
52+
53+
@Override
54+
public void transferTo(File destination) throws IOException, IllegalStateException {
55+
try(FileOutputStream fos = new FileOutputStream(destination)) {
56+
fos.write(input);
57+
}
58+
}
59+
60+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.file;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.mock.web.MockMultipartFile;
8+
9+
class CustomMultipartFileUnitTest {
10+
11+
@Test
12+
void whenProvidingByteArray_thenMultipartFileCreated() throws IOException {
13+
byte[] inputArray = "Test String".getBytes();
14+
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
15+
Assertions.assertFalse(customMultipartFile.isEmpty());
16+
Assertions.assertArrayEquals(inputArray, customMultipartFile.getBytes());
17+
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
18+
}
19+
20+
@Test
21+
void whenProvidingEmptyByteArray_thenMockMultipartFileIsEmpty() throws IOException {
22+
byte[] inputArray = "".getBytes();
23+
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
24+
Assertions.assertTrue(mockMultipartFile.isEmpty());
25+
}
26+
27+
@Test
28+
void whenProvidingNullByteArray_thenMockMultipartFileIsEmpty() throws IOException {
29+
byte[] inputArray = null;
30+
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
31+
Assertions.assertTrue(mockMultipartFile.isEmpty());
32+
}
33+
34+
@Test
35+
void whenProvidingByteArray_thenMultipartFileInputSizeMatches() throws IOException {
36+
byte[] inputArray = "Testing String".getBytes();
37+
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
38+
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
39+
}
40+
41+
@Test
42+
void whenProvidingByteArray_thenMockMultipartFileCreated() throws IOException {
43+
byte[] inputArray = "Test String".getBytes();
44+
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
45+
Assertions.assertFalse(mockMultipartFile.isEmpty());
46+
Assertions.assertArrayEquals(inputArray, mockMultipartFile.getBytes());
47+
Assertions.assertEquals(inputArray.length, mockMultipartFile.getSize());
48+
}
49+
}

0 commit comments

Comments
 (0)