とりあえず試験
ant もハードル高いんですが、まず試験を検討。
試験対象にしたのはとりあえず以下の手続き。
public static String getAuthKey(HttpClient httpClient, String source, String id, String passwd) throws Exception { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("Email", id)); nvps.add(new BasicNameValuePair("Passwd", passwd)); nvps.add(new BasicNameValuePair("service", "ah")); nvps.add(new BasicNameValuePair("source", source)); nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE")); //Login at Google.com HttpPost httpost = new HttpPost("https://www.google.com/accounts/ClientLogin"); HttpResponse response = null; httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpClient.execute(httpost); String authKey = null; BufferedReader br = null; br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = null; while ((line = br.readLine()) != null) { String[] s = line.split("="); if (s.length == 2 && s[0].equalsIgnoreCase("auth")) { authKey = s[1]; break; } } if (br != null) br.close(); return authKey; }
試験てきには authKey がちゃんと戻ってるのが確認できれば良いのですが、とりあえず HttpClient なクラスは試験の中でローカルに定義する事に。以下なカンジ。
import jp.shuri.yamanetoshi.gaeaccess.GoogleAuth; private class HttpClient { public HttpResponse execute(HttpPost p) { HttpResponse ret = new HttpResponse(); return ret; } } public class GoogleAuthTest { }
で、問題なのは execute 手続きが戻す HttpResponse について、以下な形で BufferdReader をでっち上げている事。
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
HttpResponse とか BasicHttpContext とか BasicStatusLine とか色々穿りかえしてたんですがワケワカらんので無理矢理 InputStream を作る方向。
もう少し色々確認してみます。ってかそもそも現状の実装で試験がきちんと動くかどうかも分からない状態だったりして。
追記
ええと、自分で HttpResponse も定義すれば良いのか。getEntity が戻すオブジェクトに getContent() というメソドを定義しといて InputStream を戻させれば良いはず。
良く考えたら試験そのものがちゃんと動作する試験をしてないんですが仕方が無いので勢いでこのまま進める事に。
で
準備はできたんですが、junit な試験の書き方を完全に忘れてる罠。
調べた
test ディレクトリを作成してその中に junit test なソースを投入してたんですが、Eclipse は何しても何の反応も無い。とりあえずスルーで進めてたんですが、できた時点で実行できない事が判明orz
とりあえずプロジェクトのプロパティの Java Build Path の Source タブにて add したら (ソース行方不明) がっつりコンパイルエラーがナニ。
でっち上がったのが以下で
import java.io.IOException; import java.io.InputStream; import jp.shuri.yamanetoshi.gaeaccess.GoogleAuth; import junit.framework.TestCase; import java.io.ByteArrayInputStream; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; public class GoogleAuthTest extends TestCase { class TmpClass { public InputStream getContent() { ByteArrayInputStream ret = new ByteArrayInputStream("auth=ABC".getBytes()); return (InputStream)ret; } } class HttpResponse { TmpClass my; HttpResponse () { my = new TmpClass(); } public TmpClass getEntity() { return my; } } class MyHttpClient implements HttpClient { public HttpResponse execute(HttpPost p) { HttpResponse ret = new HttpResponse(); return ret; } public org.apache.http.HttpResponse execute(HttpUriRequest arg0) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public org.apache.http.HttpResponse execute(HttpUriRequest arg0, HttpContext arg1) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public org.apache.http.HttpResponse execute(HttpHost arg0, HttpRequest arg1) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public org.apache.http.HttpResponse execute(HttpHost arg0, HttpRequest arg1, HttpContext arg2) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, HttpContext arg3) throws IOException, ClientProtocolException { // TODO Auto-generated method stub return null; } public ClientConnectionManager getConnectionManager() { // TODO Auto-generated method stub return null; } public HttpParams getParams() { // TODO Auto-generated method stub return null; } } public GoogleAuthTest(String name) { super(name); } public void testgetAuthKey() { try { String tmp = GoogleAuth.getAuthKey(new MyHttpClient(), "", "", ""); assertEquals("ABC", tmp); } catch (Exception e) { } } }
一応試験を実行したら試験にはパスしてます。いいのかなぁ。