Skip to content

Instantly share code, notes, and snippets.

@froop
froop / TextBoxValidateTest.java
Created February 26, 2012 14:01
[Java] TextBox入力検証
public static boolean validateTextBox(String text, int limitBytes) {
return !containsControl(text) && !isOverBytes(text, limitBytes);
}
public static boolean validateTextArea(String text, int limitBytes) {
return !containsInvisible(text) && !isOverBytes(text, limitBytes);
}
private static boolean containsControl(String text) {
return !text.matches("\\P{Cntrl}*");
@froop
froop / AlphaNumPunctTest.java
Created February 22, 2012 11:41
[Java] 半角英数句読文字チェック(パスワードとか用)
public static boolean isAlphaNumPunct(String string) {
if (string == null) {
return false;
}
return string.matches("\\p{Graph}+");
}
@Test
public void testIsAlphaNumPunctOK() {
assertTrue(isAlphaNumPunct("abcxyzABCXYZ150!$/:=@[_`~"));
@froop
froop / FileDownloadTest.java
Created February 20, 2012 14:03
[Java][WebDriver] ファイルダウンロードテスト例
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.apache.commons.io.FileUtils;
public class FileDownloadTest {
private static WebDriver driver;
private static final String DOWNLOAD_DIR = "C:\\work\\download";
private static final String COMPARE_DIR = "C:\\work\\compare";
@froop
froop / UrlServlet.java
Created February 18, 2012 01:29
[Java][Servlet] RequestURLのContextPathまで
URL url = new URL(request.getRequestURL().toString());
String baseUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(),
request.getContextPath()).toString();
@froop
froop / MailPop3.properties
Created February 17, 2012 14:33
[Java][Servlet] メール送信機能(複数メール・単一接続)&受信テスト
mail.pop3.host=localhost
mail.debug=false
@froop
froop / MailSender.java
Created February 16, 2012 14:41
[Java] メール送信(単一メール・複数BCC宛先)
import javax.mail.*;
public class MailSender {
private final Properties props = loadProperties("/mail/MailSender.properties");
private final Session session = createSession();
private Properties loadProperties(String propPath) {
try {
Properties res = new Properties();
res.load(MailSender.class.getResourceAsStream(propPath));
return res;
@froop
froop / MailAddressCheckTest.java
Created February 16, 2012 12:22
[Java] メールアドレスチェック
public static boolean chackMailAddress(String address) {
try {
new javax.mail.internet.InternetAddress(address, true);
} catch (AddressException e) {
return false;
}
return true;
}
@Test
@froop
froop / RandomStringTest.java
Created February 16, 2012 11:41
[Java] ランダム文字列生成(仮パスワード用)
import org.apache.commons.lang3.RandomStringUtils;
@Test public void testRandomStringUtils() {
String random1 = RandomStringUtils.randomAlphanumeric(10);
String random2 = RandomStringUtils.randomAlphanumeric(10);
assertTrue(random1, random1.matches("[a-zA-Z0-9]{10}"));
assertTrue(random2, random2.matches("[a-zA-Z0-9]{10}"));
assertFalse(random1.equals(random2));
System.out.println(random1);
System.out.println(random2);
}
@froop
froop / PasswordHash.java
Created February 14, 2012 13:09
[Java] パスワードをSHA-256でハッシュ化
import org.apache.commons.codec.digest.DigestUtils;
public static String hash(String password, String salt) {
return DigestUtils.sha256Hex(password + salt);
}
public static String hashAndStretch(String password, String salt, int count) {
String hashed = password;
for (int i = 0; i < count; i++) {
hashed = hash(hashed, salt);
}
@froop
froop / UrlRewriteTestServlet.java
Created February 4, 2012 05:54
[Java][Servlet] REST風URL化 /hoge?id=123 -> /123/hoge してid引継ぎをなくす例(UrlRewriteFilter版)
@WebServlet({"/rewrite/hoge", "/rewrite/fuga"})
public class UrlRewriteTestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Writer writer = response.getWriter();
writer.write("<html>");
writer.write(request.getRequestURI() + "<br/>");
writer.write("id=" + request.getParameter("id") + "<br/>");
writer.write("param1=" + request.getParameter("param1") + "<br/>");
writer.write("<a href='fuga'>fuga</a>");