Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Created June 10, 2013 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoyamkung/5746031 to your computer and use it in GitHub Desktop.
Save tomoyamkung/5746031 to your computer and use it in GitHub Desktop.
[Java]ファイルの拡張子が JPG であるかを問い合わせるスニペット
package net.tomoyamkung;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ImageUtil {
/**
* ファイルの拡張子が JPG であるかを問い合わせる。
*
* @param fileName 画像ファイルのパス
* @return パスが ".jpg", ".jpeg", ".JPG", ".JPEG" で終わっていれば true
* @throws IOException
*/
public static Boolean isJpg(String fileName) throws IOException {
List<String> exts = new ArrayList<String>();
exts.add(".jpeg");
exts.add(".jpg");
for (String ext : exts) {
if (fileName.endsWith(ext)) {
return true;
}
if (fileName.endsWith(ext.toUpperCase())) {
return true;
}
}
return false;
}
}
package net.tomoyamkung;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class ImageUtilTest {
@Test
public void isJpg_jpg() throws Exception {
// Setup
String fileName = "a.jpg";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(true));
}
@Test
public void isJpg_JPG() throws Exception {
// Setup
String fileName = "a.JPG";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(true));
}
@Test
public void isJpg_jpeg() throws Exception {
// Setup
String fileName = "a.jpeg";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(true));
}
@Test
public void isJpg_JPEG() throws Exception {
// Setup
String fileName = "a.JPEG";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(true));
}
@Test
public void isJpg_gif() throws Exception {
// Setup
String fileName = "a.gif";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(false));
}
@Test
public void isJpg_GIF() throws Exception {
// Setup
String fileName = "a.GIF";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(false));
}
@Test
public void isJpg_png() throws Exception {
// Setup
String fileName = "a.png";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(false));
}
@Test
public void isJpg_PNG() throws Exception {
// Setup
String fileName = "a.PNG";
// Exercise
// Verify
assertThat(ImageUtil.isJpg(fileName), is(false));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment