OSDN Git Service

#30840 不要になっていたTemplate:Documentation絡みの処理も除去,
[wptscs/wpts.git] / WptscsTest / Models / MockFactory.cs
1 // ================================================================================================
2 // <summary>
3 //      モックオブジェクト生成処理をまとめたファクトリークラスソース</summary>
4 //
5 // <copyright file="MockFactory.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2013 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Wptscs.Models
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.IO;
16     using System.Xml.Serialization;
17     using Honememo.Wptscs.Websites;
18
19     /// <summary>
20     /// モックオブジェクト生成処理をまとめたファクトリークラスです。
21     /// </summary>
22     public class MockFactory
23     {
24         #region 定数
25
26         /// <summary>
27         /// テスト用のconfig.xmlファイルパス。
28         /// </summary>
29         public static readonly string TestConfigXml = "Data\\config.xml";
30
31         /// <summary>
32         /// テストデータが格納されているフォルダパス。
33         /// </summary>
34         public static readonly string TestMediaWikiDir = "Data\\MediaWiki";
35
36         #endregion
37
38         #region private変数
39
40         /// <summary>
41         /// テスト用設定。
42         /// </summary>
43         private Config config;
44
45         #endregion
46
47         #region コンストラクタ
48
49         /// <summary>
50         /// テスト用のconfig.xmlを元に、モックファクトリーを生成する。
51         /// </summary>
52         public MockFactory()
53         {
54             this.config = MockFactory.GetConfig(MockFactory.TestConfigXml);
55         }
56
57         #endregion
58
59         #region テスト支援静的メソッド
60
61         /// <summary>
62         /// ファイルからアプリケーションの設定を取得する。
63         /// </summary>
64         /// <param name="file">設定ファイル名。</param>
65         /// <returns>作成したインスタンス。</returns>
66         public static Config GetConfig(string file)
67         {
68             // 設定ファイルを読み込み
69             using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
70             {
71                 return (Config)new XmlSerializer(typeof(Config)).Deserialize(stream);
72             }
73         }
74
75         #endregion
76
77         #region テスト支援メソッド
78
79         /// <summary>
80         /// アプリケーションの設定を取得する。
81         /// </summary>
82         /// <returns>作成したインスタンス。</returns>
83         public Config GetConfig()
84         {
85             return this.config;
86         }
87
88         /// <summary>
89         /// 指定された言語のMediaWikiを取得する。
90         /// </summary>
91         /// <param name="lang">言語コード。</param>
92         /// <returns>ウェブサイトの情報。</returns>
93         public MediaWiki GetMediaWiki(string lang)
94         {
95             Website site = this.config.GetWebsite(lang);
96             MediaWiki wiki = null;
97             if (site != null)
98             {
99                 wiki = site as MediaWiki;
100             }
101
102             if (wiki == null)
103             {
104                 wiki = new MediaWiki(new Language(lang));
105             }
106
107             // テスト用にサーバー設定を書き換えて返す
108             this.SetMockConfig(wiki);
109             return wiki;
110         }
111
112         /// <summary>
113         /// 渡されたMediaWikiオブジェクトにモックの設定を上書きする。
114         /// </summary>
115         /// <param name="wiki">設定するMediaWikiオブジェクト。</param>
116         public void SetMockConfig(MediaWiki wiki)
117         {
118             // テスト用にサーバー設定を書き換え
119             // ※ フルパスじゃないとURIで取得できないので、ここで書き換える必要有り
120             UriBuilder b = new UriBuilder("file", string.Empty);
121             b.Path = Path.GetFullPath(MockFactory.TestMediaWikiDir) + "\\";
122             wiki.Location = new Uri(b.Uri, wiki.Language.Code + "/").ToString();
123             wiki.ContentApi = "$1.xml";
124             wiki.MetaApi = "_api.xml";
125             wiki.InterlanguageApi = "interlanguages/$1.xml";
126         }
127
128         #endregion
129     }
130 }