Skip to content

Commit c78bf78

Browse files
the-java-guypedja4
authored andcommitted
BAEL-1934 (eugenp#4768)
* Bean Object, server side and client side example for event streaming example * BAEL-1628 Access a File from the Classpath in a Spring Application * inputstream retrieval added * Removed files related to evaluation article * + Aligning code to the article. Removed Utility methods and classes * BAEL - 1628 * PMD fixes * Code Review changes Refactored : whenResourceUtils_thenReadSuccessful * BAEL-1934 * +indentation correction in pom.xml * synced with master * indentation correction
1 parent c5ae8f9 commit c78bf78

5 files changed

Lines changed: 1733 additions & 0 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@
147147
<artifactId>icu4j</artifactId>
148148
<version>${icu4j.version}</version>
149149
</dependency>
150+
<!-- Mime Type Resolution Libraries -->
151+
<dependency>
152+
<groupId>org.apache.tika</groupId>
153+
<artifactId>tika-core</artifactId>
154+
<version>${tika.version}</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>net.sf.jmimemagic</groupId>
158+
<artifactId>jmimemagic</artifactId>
159+
<version>${jmime-magic.version}</version>
160+
</dependency>
150161
<dependency>
151162
<groupId>org.apache.commons</groupId>
152163
<artifactId>commons-dbcp2</artifactId>
@@ -439,6 +450,9 @@
439450
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
440451
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
441452
<icu4j.version>61.1</icu4j.version>
453+
<!-- Mime Type Libraries -->
454+
<tika.version>1.18</tika.version>
455+
<jmime-magic.version>0.1.5</jmime-magic.version>
442456
</properties>
443457

444458
</project>
54 KB
Loading
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.baeldung.java.mimetype;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.FileNameMap;
8+
import java.net.MalformedURLException;
9+
import java.net.URLConnection;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
import javax.activation.MimetypesFileTypeMap;
14+
15+
import org.apache.tika.Tika;
16+
import org.junit.Test;
17+
18+
import net.sf.jmimemagic.Magic;
19+
import net.sf.jmimemagic.MagicException;
20+
import net.sf.jmimemagic.MagicMatch;
21+
import net.sf.jmimemagic.MagicMatchNotFoundException;
22+
import net.sf.jmimemagic.MagicParseException;
23+
24+
/**
25+
* Test class demonstrating various strategies to resolve MIME type of a file.
26+
* @author tritty
27+
*
28+
*/
29+
public class MimeTypeUnitTest {
30+
/**
31+
* Expected Ouput.
32+
*/
33+
public static final String PNG_EXT = "image/png";
34+
35+
/**
36+
* The location of the file.
37+
*/
38+
public static final String FILE_LOC = "src/test/resources/product.png";
39+
40+
/**
41+
* Test method, demonstrating usage in Java 7.
42+
*
43+
* @throws IOException
44+
*/
45+
@Test
46+
public void whenUsingJava7_thenSuccess() throws IOException {
47+
final Path path = new File(FILE_LOC).toPath();
48+
final String mimeType = Files.probeContentType(path);
49+
assertEquals(mimeType, PNG_EXT);
50+
}
51+
52+
/**
53+
* Test method demonstrating the usage of URLConnection to resolve MIME type.
54+
*
55+
* @throws MalformedURLException
56+
* @throws IOException
57+
*/
58+
@Test
59+
public void whenUsingGetContentType_thenSuccess() throws MalformedURLException, IOException {
60+
final File file = new File(FILE_LOC);
61+
final URLConnection connection = file.toURL()
62+
.openConnection();
63+
final String mimeType = connection.getContentType();
64+
assertEquals(mimeType, PNG_EXT);
65+
}
66+
67+
/**
68+
* Test method demonstrating the usage of URLConnection to resolve MIME type.
69+
*
70+
*/
71+
@Test
72+
public void whenUsingGuessContentTypeFromName_thenSuccess() {
73+
final File file = new File(FILE_LOC);
74+
final String mimeType = URLConnection.guessContentTypeFromName(file.getName());
75+
assertEquals(mimeType, PNG_EXT);
76+
}
77+
78+
/**
79+
* Test method demonstrating the usage of FileNameMap from URLConnection
80+
* to resolve MIME type of a file.
81+
*
82+
*/
83+
@Test
84+
public void whenUsingGetFileNameMap_thenSuccess() {
85+
final File file = new File(FILE_LOC);
86+
final FileNameMap fileNameMap = URLConnection.getFileNameMap();
87+
final String mimeType = fileNameMap.getContentTypeFor(file.getName());
88+
assertEquals(mimeType, PNG_EXT);
89+
}
90+
91+
/**
92+
* Test method demonstrating the usage of MimeTypesFileTypeMap for resolution of
93+
* MIME type.
94+
*
95+
*/
96+
@Test
97+
public void whenUsingMimeTypesFileTypeMap_thenSuccess() {
98+
final File file = new File(FILE_LOC);
99+
final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
100+
final String mimeType = fileTypeMap.getContentType(file.getName());
101+
assertEquals(mimeType, PNG_EXT);
102+
}
103+
104+
/**
105+
* Test method demonstrating usage of jMimeMagic.
106+
*
107+
* @throws MagicParseException
108+
* @throws MagicMatchNotFoundException
109+
* @throws MagicException
110+
*/
111+
@Test
112+
public void whenUsingJmimeMagic_thenSuccess() throws MagicParseException, MagicMatchNotFoundException, MagicException {
113+
final File file = new File(FILE_LOC);
114+
final Magic magic = new Magic();
115+
final MagicMatch match = magic.getMagicMatch(file, false);
116+
assertEquals(match.getMimeType(), PNG_EXT);
117+
}
118+
119+
/**
120+
* Test method demonstrating usage of Apache Tika.
121+
*
122+
* @throws IOException
123+
*/
124+
@Test
125+
public void whenUsingTika_thenSuccess() throws IOException {
126+
final File file = new File(FILE_LOC);
127+
final Tika tika = new Tika();
128+
final String mimeType = tika.detect(file);
129+
assertEquals(mimeType, PNG_EXT);
130+
}
131+
}

0 commit comments

Comments
 (0)