Index: [Article Count Order] [Thread]

Date:  Thu, 28 Sep 2000 18:57:58 +0900
From:  firo <firo@....jp>
Subject:  [XP-jp:00973] Re: MemberList 更新
To:  extremeprogramming-jp@....jp (extremeprogramming-jp ML)
Message-Id:  <00Sep28.185802jst.115204@....jp>
References:  <97BA340C0480D411BDA800062939A1890607B2@....jp> <39D2421C.2455CCCF@....jp>
Posted:  Thu, 28 Sep 2000 18:58:01 +0900
X-Mail-Count: 00973

矢崎です。続きです。

花子さんモード

firo@....jp wrote:

>
> 花子としては、次の点を引き続き検討してみたいのだけど。
>
> 1.ファイルに同じアドレスの行が複数あった場合。重複は
> 別々じゃなくて、1つのMemberってしちゃえばいいのよね。
>

まず、テストから、、、、
MemberListTestに、アドレスが重複しているMemberListファイル
を作成するメソッドを書くわね。余分かもしれないけど、適当に
空白行のある行も作ってと、、

    private void createDuplicateMemberList(String aFileName) {
        BufferedWriter bufOut = null;

        try {
            bufOut = new BufferedWriter(
                new FileWriter(aFileName));

            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress() + "    ");
            bufOut.newLine();
            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.write("    " + secondMember.getMailAddress());
            bufOut.newLine();
            bufOut.write("    " + firstMember.getMailAddress() + "    ");
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress());
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress());
            bufOut.newLine();
        } catch (FileNotFoundException e) {
            fail(e.toString());
        } catch (IOException e) {
            fail(e.toString());
        } finally {
            try {
                if (bufOut != null)
                    bufOut.close();
            } catch (Exception e) {
            }
        }
    }

そして、テスト本体はこんな感じ

    public void testLoadDuplicateMemberList() throws FileNotFoundException{
        final String fileName = "duplicateMemberList";
        try {
            createDuplicateMemberList(fileName);

            MemberList memberList = new MemberList(fileName);
            assertEquals(2,memberList.size());
            assertEquals(firstMember, memberList.get(0));
            assertEquals(secondMember, memberList.get(1));
            try{
                memberList.get(2);
                fail("Should raise an IndexOutOfBoundsException");
            }catch(IndexOutOfBoundsException e){
                assert(true);
            }
        } finally {
            deleteMemberList(fileName);
        }
    }

MemberListファイルには7行あるけれど、どの行もfirstMember
かsecondMemberのアドレスが書いてあるので、MemberListオブ
ジェクトが持つMemberは2件しかないということよ。

じゃあ、コンパイルして、それからテストを実行します。

はい、無事に新しいテストメソッドは通りませんでした。(^^)
次はMemberListを直して、テストが通るようにします。


ありゃりゃ。MemberListのコンストラクタはこんな感じになってい
るけれど、、

            while (true) {
                fileLine = bufIn.readLine();
                if (fileLine == null)
                    break;

                String mailAddress = fileLine.trim();
                if (mailAddress.length() == 0)
                    continue;

                Member member = new Member(mailAddress);
                add(member);    /**********注目!***************/
            }

addメソッドは、こんな感じで、、、。

    public void add(Member aMember) {
        if (contains(aMember))
            throw new IllegalArgumentException();

        members.add(aMember);
    }

今のままだと、addメソッドのほうで、同じアドレスをもったMember
インスタンスをaddしようとすると、IllegalArgumentExceptionが
発生しちゃうわ。でも、これはコンストラクタがaddを使うところ
を次のように変えればいいわね。

                Member member = new Member(mailAddress);
                try{
                    add(member);
                }catch(IllegalArgumentException e){
                    continue;
                }

それと、[XP-jp:00972]で三郎君が指摘してくれたとおり、上記コ
ードのすぐ下の部分は、次のように修正するわ。

        } catch (FileNotFoundException e) {
            throw new FileNotFoundException();
じゃなくて
        } catch (FileNotFoundException e) {
            throw e;
こうね。

それじゃ、コンパイルして、テストをします。

はい、テスト通りました。

三郎君からは例外のこととか、Iteratorの利用のこととか
いろいろアドバイスいただいたし、私も考え中の引数なし
のコンストラクタのこととか、まだやらなきゃいけないこと
があるけれど、ここで休憩するわ。それと、今のマシンから
はCVSにコミットできないので、ソースの全文を以下に載せて
おくわね。どなたかコミットしていてくれたら、うれしいんだけ
ど、、、

/*
 * $Id: MemberListTest.java,v 1.3 2000/09/27 18:25:38 xp Exp $
 */
package XP.jp.co.esm.wiki.extremedomo;

import junit.framework.*;

import java.io.*;

public class MemberListTest extends TestCase {
    public MemberListTest(String name) {
        super(name);
    }
    public static void main(String[] args) {
        junit.textui.TestRunner.run(MemberListTest.class);
    }

    public void testLoadCorrectMemberList1() throws FileNotFoundException{
        final String fileName = "correctMemberList1";
        try {
            createCorrectMemberList1(fileName);

            MemberList memberList = new MemberList(fileName);
            assertEquals(2,memberList.size());
            assertEquals(firstMember, memberList.get(0));
            assertEquals(secondMember, memberList.get(1));
            try{
                memberList.get(2);
                fail("Should raise an IndexOutOfBoundsException");
            }catch(IndexOutOfBoundsException e){
                assert(true);
            }
        } finally {
            deleteMemberList(fileName);
        }
    }
    public void testLoadCorrectMemberList2() throws FileNotFoundException{
        final String fileName = "correctMemberList2";
        try {
            createCorrectMemberList2(fileName);

            MemberList memberList = new MemberList(fileName);
            assertEquals(2,memberList.size());
            assertEquals(firstMember, memberList.get(0));
            assertEquals(secondMember, memberList.get(1));
            try{
                memberList.get(2);
                fail("Should raise an IndexOutOfBoundsException");
            }catch(IndexOutOfBoundsException e){
                assert(true);
            }
        } finally {
            deleteMemberList(fileName);
        }
    }
    public void testLoadEmptyMemberList() throws FileNotFoundException{
        final String fileName = "emptyMemberList2";
        try {
            createEmptyMemberList(fileName);

            MemberList memberList = new MemberList(fileName);
            assertEquals(0,memberList.size());
           try{
                memberList.get(0);
                fail("Should raise an IndexOutOfBoundsException");
            }catch(IndexOutOfBoundsException e){
                assert(true);
            }
        } finally {
            deleteMemberList(fileName);
        }
    }
    public void testLoadNoMemberList() {
        final String fileName = "noMemberList";
        try {
            createEmptyMemberList(fileName);
            deleteMemberList(fileName);

            MemberList memberList = new MemberList(fileName);
            fail("Should raise an FileNotFoundExeption");
        }catch(FileNotFoundException e){
            assert(true);
        }
    }
    public void testLoadDuplicateMemberList() throws FileNotFoundException{
        final String fileName = "duplicateMemberList";
        try {
            createDuplicateMemberList(fileName);

            MemberList memberList = new MemberList(fileName);
            assertEquals(2,memberList.size());
            assertEquals(firstMember, memberList.get(0));
            assertEquals(secondMember, memberList.get(1));
            try{
                memberList.get(2);
                fail("Should raise an IndexOutOfBoundsException");
            }catch(IndexOutOfBoundsException e){
                assert(true);
            }
        } finally {
            deleteMemberList(fileName);
        }
    }
    public void testNewMemberList() {
        MemberList memberList = new MemberList();
        assertEquals(memberList.size(), 0);
    }
    public void testOneMemberList() {
        MemberList memberList = new MemberList();
        memberList.add(firstMember);
        assertEquals(memberList.size(), 1);
    }
    public void testDuplicationMember() {
        MemberList memberList = new MemberList();
        memberList.add(firstMember);
        try {
            memberList.add(firstMember);
            fail("Should raise an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            assert(true);
        }
    }
    public void testGetMember() {
        MemberList memberList = new MemberList();
        memberList.add(firstMember);
        memberList.add(secondMember);
        assertEquals(firstMember, memberList.get(0));
    }
    public void testContains(){
        MemberList memberList = new MemberList();

        Member thirdMember = new Member("ggg@....iii");
        Member forthMember = new Member("jjj@....lll");
        Member fifthMember = new Member("aaa@....ccc");
        Member sixthMember = new Member("ddd@....fff");

        memberList.add(firstMember);
        memberList.add(secondMember);
        memberList.add(thirdMember);

        assert("test1", memberList.contains(firstMember));
        assert("test2", memberList.contains(secondMember));
        assert("test3", memberList.contains(thirdMember));
        assert("test4", !memberList.contains(forthMember));
        assert("test5", memberList.contains(fifthMember));
        assert("test6", memberList.contains(sixthMember));
    }
    protected void setUp() {
        firstMember = new Member("aaa@....ccc");
        secondMember = new Member("ddd@....fff");
    }

    private void createCorrectMemberList1(String aFileName) {
        BufferedWriter bufOut = null;

        try {
            bufOut = new BufferedWriter(
                new FileWriter(aFileName));

            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress());
            bufOut.newLine();
        } catch (FileNotFoundException e) {
            fail(e.toString());
        } catch (IOException e) {
            fail(e.toString());
        } finally {
            try {
                if (bufOut != null)
                    bufOut.close();
            } catch (Exception e) {
            }
        }
    }
    private void createCorrectMemberList2(String aFileName) {
        BufferedWriter bufOut = null;

        try {
            bufOut = new BufferedWriter(
                new FileWriter(aFileName));

            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.newLine();
            bufOut.write("      ");
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress() + "          ");
            bufOut.newLine();
        } catch (FileNotFoundException e) {
            fail(e.toString());
        } catch (IOException e) {
            fail(e.toString());
        } finally {
            try {
                if (bufOut != null)
                    bufOut.close();
            } catch (Exception e) {
            }
        }
    }
    private void createEmptyMemberList(String aFileName) {
        BufferedWriter bufOut = null;

        try {
            bufOut = new BufferedWriter(
                  new FileWriter(aFileName));
        } catch (FileNotFoundException e) {
            fail(e.toString());
        } catch (IOException e) {
            fail(e.toString());
        } finally {
            try {
                if (bufOut != null)
                    bufOut.close();
            } catch (Exception e) {
            }
        }
    }
    private void createDuplicateMemberList(String aFileName) {
        BufferedWriter bufOut = null;

        try {
            bufOut = new BufferedWriter(
                new FileWriter(aFileName));

            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress() + "    ");
            bufOut.newLine();
            bufOut.write(firstMember.getMailAddress());
            bufOut.newLine();
            bufOut.write("    " + secondMember.getMailAddress());
            bufOut.newLine();
            bufOut.write("    " + firstMember.getMailAddress() + "    ");
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress());
            bufOut.newLine();
            bufOut.write(secondMember.getMailAddress());
            bufOut.newLine();
        } catch (FileNotFoundException e) {
            fail(e.toString());
        } catch (IOException e) {
            fail(e.toString());
        } finally {
            try {
                if (bufOut != null)
                    bufOut.close();
            } catch (Exception e) {
            }
        }
    }
    private void deleteMemberList(String aFileName) {
        File memberList = new File(aFileName);
        memberList.delete();
    }

    private final String nameOfCorrectMemberList1 = "correctMemberList1";
    private final String nameOfCorrectMemberList2 = "correctMemberList2";

    private Member firstMember;
    private Member secondMember;
}


/*
 * $Id: MemberList.java,v 1.3 2000/09/27 18:25:14 xp Exp $
 */
package XP.jp.co.esm.wiki.extremedomo;

import java.util.*;
import java.io.*;

public class MemberList {
    public MemberList() {
    }
    public MemberList(String nameOfMemberList) throws FileNotFoundException{
        BufferedReader bufIn = null;

        try {
            bufIn = new BufferedReader(new FileReader(nameOfMemberList));

            String fileLine;
            while (true) {
                fileLine = bufIn.readLine();
                if (fileLine == null)
                    break;

                String mailAddress = fileLine.trim();
                if (mailAddress.length() == 0)
                    continue;

                Member member = new Member(mailAddress);
                try{
                    add(member);
                }catch(IllegalArgumentException e){
                    continue;
                }
            }
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw new RuntimeException();
        } finally {
            try {
                if (bufIn != null)
                    bufIn.close();
            } catch (Exception e) {
            }
        }
    }
    public void add(Member aMember) {
        if (contains(aMember))
            throw new IllegalArgumentException();

        members.add(aMember);
    }
    public Member get(int index) {
        return (Member)members.get(index);
    }
    public int size() {
        return members.size();
    }
    public boolean contains(Member aMember) {
        return members.contains(aMember);
    }

    private List members = new ArrayList();
}



--
矢崎博英  firo@....jp