Skip to content

Instantly share code, notes, and snippets.

@froop
froop / FileUploadTest.java
Created February 1, 2012 13:00
[Java][WebDriver] ファイルアップロードテスト例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FileUploadTest {
private static WebDriver driver;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver = new FirefoxDriver();
@froop
froop / DbUnitSampleTest.java
Created January 17, 2012 22:38
[Java][JUnit] DbUnit例
import org.dbunit.*;
public class DbUnitSampleTest extends DatabaseTestCase {
@Override
protected IDatabaseConnection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sample1", "user", "pass");
return new DatabaseConnection(jdbcConn);
@froop
froop / build-database.xml
Created January 16, 2012 22:55
[Ant][SQL] データベース自動作成サンプル
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<project basedir="." default="all" name="sample">
<property name="db.user" value="root" />
<property name="db.pass" value="root" />
<property name="db.host" value="localhost" />
<property name="db.port" value="3306" />
<property name="db.name" value="sample1" />
<property name="db.charset" value="utf8" />
@froop
froop / resize.js
Created January 13, 2012 05:20
[JavaScript] ウィンドウのリサイズに合わせてコンテンツの高さを調整
function resizeContents() {
var contentsElem = document.getElementById("contents"),
headerElem = document.getElementById("header"),
parentH = contentsElem.offsetParent.clientHeight,
headerH = headerElem.offsetHeight,
paddingH = 10 * 2;
try {
contentsElem.style.height = (parentH - headerH - paddingH) + "px";
} catch (e) {
// ignore error
@froop
froop / SelfExtractFileUtil.java
Created January 13, 2012 03:24
[Java][Windows] 7-Zipで自己解凍書庫ファイル作成
File execute7Zip(String workDir, String baseName, String wildCard) {
String path = "C:\\Program Files\\7-Zip\\7z.exe";
WindowsCommandUtils.executeCommand(
new ProcessBuilder(path, "a", "-sfx7z.sfx", baseName, wildCard)
.directory(new File(workDir)));
return new File(workDir, baseName + ".exe");
}
@froop
froop / WindowsCommandUtils.java
Created January 13, 2012 03:00
[Java][Windows] OSコマンド実行、WSH用スクリプト実行
public class WindowsCommandUtils {
public static void executeCommand(ProcessBuilder builder) {
ProcessResult result = executeAndCollectResult(builder);
validateProcessResult(result);
}
/**
* For Windows Scripting Host. (VBScript, etc.)
*/
public static void executeWshScript(File file, File workDir,
@froop
froop / FileUtil.java
Created January 12, 2012 08:47
[Java] Javaパッケージ内のファイルをファイルシステムにコピー
import org.apache.commons.io.IOUtils;
public static File copyResourceFile(String dir, String name, Class<?> clazz) throws IOException {
File file = new File(dir, name);
InputStream in = null;
OutputStream out = null;
try {
in = clazz.getResourceAsStream(name);
out = new FileOutputStream(file);
IOUtils.copy(in, out);
} finally {
@froop
froop / TempDir.java
Created January 12, 2012 08:33
[Java] OSのTEMPに一時ディレクトリ作成
class TempDir {
private final File dir;
public TempDir(String prefix) {
String name = prefix + "_" + UUID.randomUUID().toString();
dir = new File(System.getProperty("java.io.tmpdir"), name);
boolean success = dir.mkdir();
if (!success) {
throw new RuntimeException(dir.getPath());
}
dir.deleteOnExit();
@froop
froop / LoginFilter.java
Created January 12, 2012 08:07
[Java][Servlet] 未ログインならログインページにリダイレクトするFilter
@WebFilter(urlPatterns = { "/*" })
public class LoginFilter implements Filter {
private static final String URL_LOGIN = "/login/";
private static final String[] URL_EXCLUDES = {URL_LOGIN, "/common/"};
private static final String ATTR_LOGIN = "login";
private static final String ATTR_ORIGIN_URL = "originUrl";
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
@froop
froop / CharacterEncodingFilter.java
Created January 12, 2012 06:35
[Java][Servlet] HTTP-POSTのRequestにsetCharacterEncodingするFilter
public class CharacterEncodingFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
if ("POST".equalsIgnoreCase(httpReq.getMethod())) {
request.setCharacterEncoding("Windows-31J");
}
chain.doFilter(request, response);
}
}