次は、CartHtml の outputMain のテストを行ってみます。
本来であれば、ShopHtml の inputMain でセッションに購入品が格納されているはずです。
冒頭の実行例のように、セロハンテープを5個、A4ノートを10冊をカートに入れた状態にしてみます。
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
import paraselene.*; import paraselene.supervisor.*; import paraselene.test.*; import test.*; import test.base.*; public class UnitTest { public static void main( String[] argc ) throws Exception { Page page = new test.logic.CartHtml(); Gate.startup(); TestSession session = new TestSession(); TestRequest req = new TestRequest( RequestParameter.Method.GET, // リクエスト時のメソッド session, // セッション null, // クッキー null, // クライアントのIPアドレス null, // リクエスト時のURI new Gate() ); Cart cart = Cart.getCart( req ); cart.add( 2, 5 ); cart.add( 0, 10 ); Page from = PageLoader.getPageFactory().getPage( PageType.SHOP_HTML ); page = page.output( from, req ); System.out.println( page.toString() ); } }
先程との違いは、TestSession を渡している点と、output に第一引数(遷移元ページ)を渡しています。 ※outputMain で参照していなければ、null でも構いません。 もう1つ、CartHtml を直接 new していますが、理由は後述します。 この結果が以下となります。
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
<html> <head> <meta content="text/css" http-equiv="Content-Style-Type"> <title>カート</title></head> <body> <form action="PARASELENE(PAGE)399851037"> <input name="paraselene$form" value="1" type="hidden"> <table name="list" border="1"> <tr> <th>商品</th> <th>単価</th> <th>数量</th> <th>合計金額</th> <th>キャンセル</th></tr> <tr> <td>セロハンテープ</td> <td align="right">45円</td> <td align="right">5</td> <td align="right">225円</td> <td> <input name="cancel" value="キャンセル" type="submit"></td></tr> <tr> <td>A4ノート</td> <td align="right">189円</td> <td align="right">10</td> <td align="right">1890円</td> <td> <input name="cancel$0" value="キャンセル" type="submit"></td></tr> <tr> <td colspan="3" align="right">計</td> <td align="right" name="sum">2115円</td> <td align="right"></td></tr></table></form> <form action="PARASELENE(PAGE)1578358733"> <input name="paraselene$form" value="2" type="hidden"> <input name="return" value="買い物を続ける" type="submit"> <input name="buy" value="決済" type="submit"></form></body></html>
次に、A4 ノートのキャンセル押下を再現してみます。
この HTML からキャンセルを押すと、
xxxx?paraselene$form=1&cancel$0=キャンセル
という形の URL でリクエストされるはずです。
先程のソースでは、CartHtml を最初(Gate.startup より前)に new していましたが、これは「paraselene$form」の値を一定に保つためです。
ページファクトリーは複数のインスタンスを立ち上げるため、paraselene$form が実行の度に不定となります。
これを避けるため、最初に直接 new して、それを使用します。
もう1つ、name=cancel のボタンのように、配列化が起きる場合ですが、同一ロジック、同一データで処理した場合には、毎回、同じ name が設定されます。
それを踏まえた上で、inputMain を呼び出すコードを追加します。
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
public class UnitTest { public static void main( String[] argc ) throws Exception { Page page = new test.logic.CartHtml(); Gate.startup(); TestSession session = new TestSession(); TestRequest req = new TestRequest( RequestParameter.Method.GET, // リクエスト時のメソッド session, // セッション null, // クッキー null, // クライアントのIPアドレス null, // リクエスト時のURI new Gate() ); Cart cart = Cart.getCart( req ); cart.add( 2, 5 ); cart.add( 0, 10 ); Page from = PageLoader.getPageFactory().getPage( PageType.SHOP_HTML ); page = page.output( from, req ); //System.out.println( page.toString() ); // ユーザーがA4ノートをキャンセルした。 req = new TestRequest( RequestParameter.Method.GET, // リクエスト時のメソッド session, // セッション null, // クッキー null, // クライアントのIPアドレス null, // リクエスト時のURI new Gate() ); req.addItem( "paraselene$form", "1" ); req.addItem( "cancel$0", "キャンセル" ); page.reflect( req ); Forward fw = new Forward( PageType.CART_HTML, true, true ); fw = page.input( req, fw ); System.out.println( fw.toString() ); } }
実行結果です。
page:CART_HTML
正しく、CART_HTML へ遷移しようとしている事が確認できました。
実際の処理ではこれに続けて、CartHtml の output が呼ばれる事になります。
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
public class UnitTest { public static void main( String[] argc ) throws Exception { Page page = new test.logic.CartHtml(); Gate.startup(); TestSession session = new TestSession(); TestRequest req = new TestRequest( RequestParameter.Method.GET, // リクエスト時のメソッド session, // セッション null, // クッキー null, // クライアントのIPアドレス null, // リクエスト時のURI new Gate() ); Cart cart = Cart.getCart( req ); cart.add( 2, 5 ); cart.add( 0, 10 ); Page from = PageLoader.getPageFactory().getPage( PageType.SHOP_HTML ); page = page.output( from, req ); //System.out.println( page.toString() ); // ユーザーがA4ノートをキャンセルした。 req = new TestRequest( RequestParameter.Method.GET, // リクエスト時のメソッド session, // セッション null, // クッキー null, // クライアントのIPアドレス null, // リクエスト時のURI new Gate() ); req.addItem( "paraselene$form", "1" ); req.addItem( "cancel$0", "キャンセル" ); page.reflect( req ); Forward fw = new Forward( PageType.CART_HTML, true, true ); fw = page.input( req, fw ); //System.out.println( fw.toString() ); page = page.output( page, req ); System.out.println( page.toString() ); } }
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
<html> <head> <meta content="text/css" http-equiv="Content-Style-Type"> <title>カート</title></head> <body> <form action="PARASELENE(PAGE)399851037"> <input name="paraselene$form" value="1" type="hidden"> <table name="list" border="1"> <tr> <th>商品</th> <th>単価</th> <th>数量</th> <th>合計金額</th> <th>キャンセル</th></tr> <tr> <td>セロハンテープ</td> <td align="right">45円</td> <td align="right">5</td> <td align="right">225円</td> <td> <input name="cancel" value="キャンセル" type="submit"></td></tr> <tr> <td colspan="3" align="right">計</td> <td align="right" name="sum">225円</td> <td align="right"></td></tr></table></form> <form action="PARASELENE(PAGE)1578358733"> <input name="paraselene$form" value="2" type="hidden"> <input name="return" value="買い物を続ける" type="submit"> <input name="buy" value="決済" type="submit"></form></body></html>
A4 ノートが削除されている事が確認できます。