Index: [Article Count Order] [Thread]

Date:  Sat, 16 Sep 2000 21:47:33 +0900
From:  firo@....jp
Subject:  [XP-jp:00892] Re: VXP タスク3 	--Config
To:  extremeprogramming-jp@....jp (extremeprogramming-jp ML)
Message-Id:  <39C37014.304BEDA5@....jp>
References:  <B5E8632F.3764%khosokawa@....com> <39C3387F.FA986A60@....jp>
Posted:  Sat, 16 Sep 2000 22:05:28 +0900
X-Mail-Count: 00892

矢崎です。

ConfigTest.javaの最新バージョンは[XP-jp:00891]
Config.javaの最新バージョンは[XP-jp:00888]です。

では続きです。

(太郎さん)
花子さんの[XP-jp00885]のメモに
>他にも、コンフィグファイルに「X-ML-Name」がなかったことが、「getName」
>メソッドを呼び出すまで分からないのは、困るわね。

というのがあるけれど、さすが花子さん。いわれてみれば
まったくそのとおり。早速テストを直しましょう。

testGetName1、testGetName2、testGetName3の
以下の部分を直せばよさそうだ。

・・・
        Config aConfig = new Config("badconfig");

        try{
            aConfig.getName();
            fail("Should raise an IllegalArgumentException1");
        }catch(IllegalArgumentException e){
            assert(true);
        }
・・・
(太郎さん)
この部分を、

・・・
        try{
            Config aConfig = new Config("badconfig");
            fail("Should raise an IllegalArgumentException1");
        }catch(IllegalArgumentException e){
            assert(true);
        }
・・・
(太郎さん)
こんな感じに。つまり、コンストラクタで、ファイルの異常、
つまりこの場合はX-ML-Name:が正しく取得できなかった
らExceptionを投げるということ。

testGetName1、testGetName2、testGetName3について
上記のように直して、それとメソッドの名前も
testNoNameConfig1、testNoNameConfig2、testNoNameConfig3
としよう。

#リファクタリングの手順は省略

・・・
(修正後ConfigTestの全文)
/*
 * @(#)ConfigTest.java
 */
package XP.jp.co.esm.wiki.extremedomo;

import junit.framework.*;
import java.io.*;
import java.util.*;

public class ConfigTest extends TestCase {

    public ConfigTest(String name) {
        super(name);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(ConfigTest.class);
    }

    private void createCorrectConfigFile(String filename) {
        FileOutputStream fo;
        try{
            fo = new FileOutputStream(filename);
        }catch(FileNotFoundException e){
            throw new RuntimeException("unable to open config file");
        }
        PrintWriter pw = new PrintWriter(fo);
        pw.println("X-ML-Name: extremeprogramming-jp");
        pw.println("Y-ML-Name: extremeprogramming-false");
        pw.println("X-Mail-Count: 00796");
        pw.println("Y-Mail-Count: 00888");
        pw.println("$-MemberList: memberlist");
        pw.println("$$-MemberList: memberrrlist");
        pw.println("X-MLServer: fml [fml 2.1_GAMMA#185](distribute only mode)");
        pw.println("Y-MLServer: fml [fml 2.1_GAMMA#185](not distribute only mode)");
        pw.println("X-ML-Info: If you have a question, contact with me");
        pw.println("Y-ML-Info: If you have a question, do not contact with me");
        pw.close();
        try{
            fo.close();
        }catch(IOException e){
            throw new RuntimeException("error on closing  config file");
        }
    }

    public void testCorrectConfig(){
        createCorrectConfigFile("config");

        Config aConfig = new Config("config");

        assertEquals("test1", "extremeprogramming-jp", aConfig.getName());

        Iterator iterator = aConfig.getXInfo();
        assertEquals("test2", "X-ML-Name: extremeprogramming-jp", iterator.next( ));
        assertEquals("test3", "X-Mail-Count: 00796", iterator.next( ));
        assertEquals("test4", "X-MLServer: fml [fml 2.1_GAMMA#185](distribute only mode)",
iterator.next());
        assertEquals("test5", "X-ML-Info: If you have a question, contact with me",
iterator.next());
        assert("test6", ! iterator.hasNext());

        assertEquals("test7", "memberlist", aConfig.getMemberListFileName());
    }

    public void testNoConfigFile(){

        try{
            Config aConfig = new Config("nonconfig");
            fail("Should raise an IllegalArgumentException");
        }catch(IllegalArgumentException e){
            assert(true);
        }

    }

    public void testNoNameConfig1(){
        FileOutputStream fo;
        try{
            fo = new FileOutputStream("badconfig");
        }catch(FileNotFoundException e){
            throw new RuntimeException("unable to open badconfig file");
        }
        PrintWriter pw = new PrintWriter(fo);
        pw.println("Y-ML-Name: extremeprogramming-false");
        pw.println("X-Mail-Count: 00796");
        pw.println("Y-Mail-Count: 00888");
        pw.println("X-MLServer: fml [fml 2.1_GAMMA#185](distribute only mode)");
        pw.println("Y-MLServer: fml [fml 2.1_GAMMA#185](not distribute only mode)");
        pw.println("X-ML-Info: If you have a question, contact with me");
        pw.println("Y-ML-Info: If you have a question, do not contact with me");
        pw.close();
        try{
            fo.close();
        }catch(IOException e){
            throw new RuntimeException("error on closing badconfig file");
        }

        try{
            Config aConfig = new Config("badconfig");
            fail("Should raise an IllegalArgumentException1");
        }catch(IllegalArgumentException e){
            assert(true);
        }

    }

    public void testNoNameConfig2(){
        FileOutputStream fo;
        try{
            fo = new FileOutputStream("badconfig");
        }catch(FileNotFoundException e){
            throw new RuntimeException("unable to open badconfig file");
        }
        PrintWriter pw = new PrintWriter(fo);
        pw.println("X-ML-Name:");
        pw.println("X-Mail-Count: 00796");
        pw.println("Y-Mail-Count: 00888");
        pw.println("X-MLServer: fml [fml 2.1_GAMMA#185](distribute only mode)");
        pw.println("Y-MLServer: fml [fml 2.1_GAMMA#185](not distribute only mode)");
        pw.println("X-ML-Info: If you have a question, contact with me");
        pw.println("Y-ML-Info: If you have a question, do not contact with me");
        pw.close();
        try{
            fo.close();
        }catch(IOException e){
            throw new RuntimeException("error on closing badconfig file");

        }

        try{
            Config aConfig = new Config("badconfig");
            fail("Should raise an IllegalArgumentException1");
        }catch(IllegalArgumentException e){
            assert(true);
        }

    }

    public void testNoNameConfig3(){
        FileOutputStream fo;
        try{
            fo = new FileOutputStream("badconfig");
        }catch(FileNotFoundException e){
            throw new RuntimeException("unable to open badconfig file");
        }
        PrintWriter pw = new PrintWriter(fo);
        pw.println("X-ML-Name:                              ");
        pw.println("X-Mail-Count: 00796");
        pw.println("Y-Mail-Count: 00888");
        pw.println("X-MLServer: fml [fml 2.1_GAMMA#185](distribute only mode)");
        pw.println("Y-MLServer: fml [fml 2.1_GAMMA#185](not distribute only mode)");
        pw.println("X-ML-Info: If you have a question, contact with me");
        pw.println("Y-ML-Info: If you have a question, do not contact with me");
        pw.close();
        try{
            fo.close();
        }catch(IOException e){
            throw new RuntimeException("error on closing badconfig file");

        }

        try{
            Config aConfig = new Config("badconfig");
            fail("Should raise an IllegalArgumentException1");
        }catch(IllegalArgumentException e){
            assert(true);
        }

    }

    protected void setUp() {
    }

    protected void tearDown(){
    }

}
・・・
(太郎さん)
コンパイルして、テストして、これは通らないので、
Config.javaを修正します。修正個所は以下のwhile文
を抜けたところでgetNameをcallすればいいんじゃないかな?

・・・
(修正前)
            while (true) {
                fileLine = bufIn.readLine();
                if (fileLine == null)
                    break;

                if (fileLine.startsWith("X-ML-Name:"))
                    name = fileLine.substring("X-ML-Name:".length()).trim();
                if (fileLine.startsWith("$-MemberList:"))
                    nameOfMemberListFile = fileLine.substring("$-MemberList:".length()).trim();
                if (fileLine.startsWith("X-"))
                    xList.add(fileLine.trim());
            }
        } catch (FileNotFoundException e) {

・・・
(修正後)
          while (true) {
                fileLine = bufIn.readLine();
                if (fileLine == null)
                    break;

                if (fileLine.startsWith("X-ML-Name:"))
                    name = fileLine.substring("X-ML-Name:".length()).trim();
                if (fileLine.startsWith("$-MemberList:"))
                    nameOfMemberListFile = fileLine.substring("$-MemberList:".length()).trim();
                if (fileLine.startsWith("X-"))
                    xList.add(fileLine.trim());
            }
/*追加*/   getName();
        } catch (FileNotFoundException e) {

・・・
(太郎さん)
コンパイル、テスト、よし通った。
後、$-MemberList行がなかった場合も同じような処理
をすればConfigはおしまいかな?


・・・
(修正後Configの全文)

/*
 * @(#)Config.java
 */
package XP.jp.co.esm.wiki.extremedomo;

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

public class Config {

    public Config(String nameOfConfigFile) {

        BufferedReader bufIn = null;
        try {
            bufIn = new BufferedReader(new FileReader(nameOfConfigFile));

            String fileLine;

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

                if (fileLine.startsWith("X-ML-Name:"))
                    name = fileLine.substring("X-ML-Name:".length()).trim();
                if (fileLine.startsWith("$-MemberList:"))
                    nameOfMemberListFile = fileLine.substring("$-MemberList:".length()).trim();
                if (fileLine.startsWith("X-"))
                    xList.add(fileLine.trim());
            }
            getName();
        } catch (FileNotFoundException e) {
            // What is exception handling carried out?
            throw new IllegalArgumentException();

        } catch (IOException e) {
            // What is exception handling carried out?
        } finally {
            try {
                if (bufIn != null)
                    bufIn.close();
            } catch (Exception e) {
            }
        }
    }

    public String getName() {
        if (name == null) throw new IllegalArgumentException();
        if (name.length() == 0) throw new IllegalArgumentException();
        return name;
    }

    public Iterator getXInfo() {
        return xList.iterator();
    }

    public String getMemberListFileName(){
        return nameOfMemberListFile;
    }

    private String name;
    private List xList = new ArrayList();
    private String nameOfMemberListFile;

}

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