• R/O
  • SSH
  • HTTPS

drfx: Commit


Commit MetaInfo

Révision183 (tree)
l'heure2010-03-24 00:06:50
Auteursasaminn

Message de Log

CHANGE: DrFxRule クラスからデータ読み込み機能を分離

Change Summary

Modification

--- drfx/trunk/test/unit/Core.Tests.Unit/TestingUtilities/StreamGenerator.cs (nonexistent)
+++ drfx/trunk/test/unit/Core.Tests.Unit/TestingUtilities/StreamGenerator.cs (revision 183)
@@ -0,0 +1,21 @@
1+using System;
2+using System.IO;
3+
4+namespace Sasa.QualityTools.DrFx.Core.Tests.Unit.TestingUtilities
5+{
6+ internal static class StreamGenerator
7+ {
8+ internal static Stream GenerateWith(string content)
9+ {
10+ MemoryStream stream = new MemoryStream();
11+
12+ StreamWriter writer = new StreamWriter(stream);
13+ writer.Write(content);
14+ writer.Flush();
15+
16+ stream.Seek(0, SeekOrigin.Begin);
17+
18+ return stream;
19+ }
20+ }
21+}
--- drfx/trunk/test/unit/Core.Tests.Unit/Translation/DrFxRuleTest.cs (revision 182)
+++ drfx/trunk/test/unit/Core.Tests.Unit/Translation/DrFxRuleTest.cs (nonexistent)
@@ -1,46 +0,0 @@
1-using NUnit.Framework;
2-using Sasa.QualityTools.DrFx.Core.Translation;
3-using System;
4-using System.Collections.Generic;
5-
6-namespace Sasa.QualityTools.DrFx.Core.Tests.Unit.Translation
7-{
8- [TestFixture]
9- public class DrFxRuleTest
10- {
11- [Test]
12- public void TestParseXmlContents()
13- {
14- const string contents =
15- @"<?xml version=""1.0"" encoding=""utf-8"" ?>" +
16- @"<Rules>" +
17- @" <Rule CheckId=""CA0000"" Category=""Category1"">" +
18- @" <Name>Name1</Name>" +
19- @" <Resolution>" +
20- @" <Content>Content1</Content>" +
21- @" <Pattern>Pattern1</Pattern>" +
22- @" </Resolution>" +
23- @" <Resolution>" +
24- @" <Content>Content2</Content>" +
25- @" <Pattern>Pattern2</Pattern>" +
26- @" </Resolution>" +
27- @" </Rule>" +
28- @"</Rules>";
29-
30- List<DrFxRule> rules = new List<DrFxRule>(DrFxRule.Parse(contents));
31-
32- Assert.AreEqual(1, rules.Count);
33- DrFxRule rule = rules[0];
34- Assert.AreEqual("CA0000", rule.CheckId);
35- Assert.AreEqual("Category1", rule.Category);
36- Assert.AreEqual("Name1", rule.Name);
37-
38- List<Resolution> resolutions = new List<Resolution>(rule.Resolutions);
39- Assert.AreEqual(2, resolutions.Count);
40- Assert.AreEqual("Content1", rule.Resolutions[0].Content);
41- Assert.AreEqual("Pattern1", rule.Resolutions[0].Pattern);
42- Assert.AreEqual("Content2", rule.Resolutions[1].Content);
43- Assert.AreEqual("Pattern2", rule.Resolutions[1].Pattern);
44- }
45- }
46-}
--- drfx/trunk/test/unit/Core.Tests.Unit/Translation/DrFxRuleLoaderTest.cs (nonexistent)
+++ drfx/trunk/test/unit/Core.Tests.Unit/Translation/DrFxRuleLoaderTest.cs (revision 183)
@@ -0,0 +1,50 @@
1+using NUnit.Framework;
2+using Sasa.QualityTools.DrFx.Core.Translation;
3+using Sasa.QualityTools.DrFx.Core.Tests.Unit.TestingUtilities;
4+using System;
5+using System.Collections.Generic;
6+using System.IO;
7+using System.Text;
8+
9+namespace Sasa.QualityTools.DrFx.Core.Tests.Unit.Translation
10+{
11+ [TestFixture]
12+ public class DrFxRuleLoaderTest
13+ {
14+ [Test]
15+ public void TestLoadDrFxRulesFromStream()
16+ {
17+ const string contents =
18+ @"<?xml version=""1.0"" encoding=""utf-8"" ?>" +
19+ @"<Rules>" +
20+ @" <Rule CheckId=""CA0000"" Category=""Category1"">" +
21+ @" <Name>Name1</Name>" +
22+ @" <Resolution>" +
23+ @" <Content>Content1</Content>" +
24+ @" <Pattern>Pattern1</Pattern>" +
25+ @" </Resolution>" +
26+ @" <Resolution>" +
27+ @" <Content>Content2</Content>" +
28+ @" <Pattern>Pattern2</Pattern>" +
29+ @" </Resolution>" +
30+ @" </Rule>" +
31+ @"</Rules>";
32+
33+ Stream stream = StreamGenerator.GenerateWith(contents);
34+ List<DrFxRule> rules = new List<DrFxRule>(DrFxRuleLoader.Load(stream));
35+
36+ Assert.AreEqual(1, rules.Count);
37+ DrFxRule rule = rules[0];
38+ Assert.AreEqual("CA0000", rule.CheckId);
39+ Assert.AreEqual("Category1", rule.Category);
40+ Assert.AreEqual("Name1", rule.Name);
41+
42+ List<Resolution> resolutions = new List<Resolution>(rule.Resolutions);
43+ Assert.AreEqual(2, resolutions.Count);
44+ Assert.AreEqual("Content1", rule.Resolutions[0].Content);
45+ Assert.AreEqual("Pattern1", rule.Resolutions[0].Pattern);
46+ Assert.AreEqual("Content2", rule.Resolutions[1].Content);
47+ Assert.AreEqual("Pattern2", rule.Resolutions[1].Pattern);
48+ }
49+ }
50+}
--- drfx/trunk/test/unit/Core.Tests.Unit/Translation/TranslatorTest.cs (revision 182)
+++ drfx/trunk/test/unit/Core.Tests.Unit/Translation/TranslatorTest.cs (revision 183)
@@ -1,7 +1,9 @@
11 using NUnit.Framework;
2+using Sasa.QualityTools.DrFx.Core.Tests.Unit.TestingUtilities;
23 using Sasa.QualityTools.DrFx.Core.Translation;
34 using System;
45 using System.Collections.Generic;
6+using System.IO;
57 using System.Xml;
68
79 namespace Sasa.QualityTools.DrFx.Core.Tests.Unit.Translation
@@ -41,7 +43,9 @@
4143 @"</Rules>";
4244
4345 Translator translator = new Translator();
44- translator.AddRules(DrFxRule.Parse(ruleContents));
46+ Stream stream = StreamGenerator.GenerateWith(ruleContents);
47+ translator.AddRules(DrFxRuleLoader.Load(stream));
48+
4549 return translator.TranslateIssue(category, checkId, issue);
4650 }
4751
@@ -88,8 +92,11 @@
8892 @" </Rules>" + "\r\n" +
8993 @"</FxCopReport>";
9094
95+
9196 Translator translator = new Translator();
92- translator.AddRules(DrFxRule.Parse(ruleContents));
97+ Stream stream = StreamGenerator.GenerateWith(ruleContents);
98+ translator.AddRules(DrFxRuleLoader.Load(stream));
99+
93100 Assert.AreEqual(expected, translator.Translate(reportContents));
94101 }
95102
--- drfx/trunk/src/Explorer/Rule/FxCopRuleLoader.cs (revision 182)
+++ drfx/trunk/src/Explorer/Rule/FxCopRuleLoader.cs (revision 183)
@@ -47,14 +47,7 @@
4747
4848 public void Translate(IEnumerable<FxCopRule> rules)
4949 {
50- // TODO: 再検討
51- List<DrFxRule> translationRules = new List<DrFxRule>();
52- string directory = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "Resources");
53- foreach (string file in Directory.GetFiles(directory, "*.xml"))
54- {
55- string rule = File.ReadAllText(file);
56- translationRules.AddRange(DrFxRule.Parse(rule));
57- }
50+ List<DrFxRule> translationRules = new List<DrFxRule>(DrFxRuleLoader.Load());
5851 var lookup = translationRules.ToLookup(r => r.CheckId);
5952
6053 foreach (FxCopRule rule in rules)
--- drfx/trunk/src/Console/Program.cs (revision 182)
+++ drfx/trunk/src/Console/Program.cs (revision 183)
@@ -41,12 +41,7 @@
4141 internal Program()
4242 {
4343 this.translator = new Translator();
44- string directory = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "Resources");
45- foreach (string file in Directory.GetFiles(directory, "*.xml"))
46- {
47- string rule = File.ReadAllText(file);
48- this.translator.AddRules(DrFxRule.Parse(rule));
49- }
44+ this.translator.AddRules(DrFxRuleLoader.Load());
5045 }
5146
5247
--- drfx/trunk/src/Core/Translation/DrFxRule.cs (revision 182)
+++ drfx/trunk/src/Core/Translation/DrFxRule.cs (revision 183)
@@ -42,17 +42,17 @@
4242 /// <summary>
4343 /// FxCop ルールのチェック ID。
4444 /// </summary>
45- public string CheckId { get; private set; }
45+ public string CheckId { get; set; }
4646
4747 /// <summary>
4848 /// FxCop ルールのカテゴリ。
4949 /// </summary>
50- public string Category { get; private set; }
50+ public string Category { get; set; }
5151
5252 /// <summary>
5353 /// FxCop ルール名。
5454 /// </summary>
55- public string Name { get; private set; }
55+ public string Name { get; set; }
5656
5757 /// <summary>
5858 /// ルール違反の解決方法を表すオブジェクトのコレクション。
@@ -70,49 +70,9 @@
7070 /// <summary>
7171 /// 新しいインスタンスを初期化します。
7272 /// </summary>
73- private DrFxRule()
73+ public DrFxRule()
7474 {
7575 this.resolutions = new List<Resolution>();
7676 }
77-
78-
79-
80- /// <summary>
81- /// 翻訳ルールコンテンツを解析し、翻訳ルールコレクションを作成します。
82- /// </summary>
83- /// <param name="content">翻訳ルールコンテンツ。</param>
84- /// <returns>翻訳ルールコレクション。</returns>
85- public static IEnumerable<DrFxRule> Parse(string content)
86- {
87- List<DrFxRule> rules = new List<DrFxRule>();
88- XmlDocument doc = new XmlDocument();
89- doc.LoadXml(content);
90- foreach (XmlNode node in doc.SelectNodes("/Rules/Rule"))
91- {
92- DrFxRule rule = new DrFxRule();
93- rule.CheckId = node.Attributes["CheckId"].Value;
94- rule.Category = node.Attributes["Category"].Value;
95- rule.Name = node["Name"].InnerText;
96- rule.ParseResolutions(node);
97- rules.Add(rule);
98- }
99- return rules;
100- }
101-
102- /// <summary>
103- /// 指定された Rule ノードを解析し、ルール違反の解決方法を表すオブジェクトを生成して、
104- /// フィールドで保持します。
105- /// </summary>
106- /// <param name="ruleNode">解析対象の Rule ノード。</param>
107- private void ParseResolutions(XmlNode ruleNode)
108- {
109- foreach (XmlNode node in ruleNode.SelectNodes("Resolution"))
110- {
111- Resolution resolution = new Resolution();
112- resolution.Content = node["Content"].InnerText;
113- resolution.Pattern = node["Pattern"].InnerText;
114- this.resolutions.Add(resolution);
115- }
116- }
11777 }
11878 }
--- drfx/trunk/src/Core/Translation/DrFxRuleLoader.cs (revision 182)
+++ drfx/trunk/src/Core/Translation/DrFxRuleLoader.cs (revision 183)
@@ -32,28 +32,47 @@
3232
3333
3434 /// <summary>
35- /// 翻訳ルールコンテンツを解析し、翻訳ルールコレクションを作成します。
35+ /// アセンブリに埋め込まれている翻訳ルールコンテンツを解析し、翻訳ルールコレクションを作成します。
3636 /// </summary>
37- /// <param name="content">翻訳ルールコンテンツ。</param>
3837 /// <returns>翻訳ルールコレクション。</returns>
3938 public static IEnumerable<DrFxRule> Load()
4039 {
40+ using (Stream stream = typeof(DrFxRuleLoader).Assembly.GetManifestResourceStream(resourceName))
41+ {
42+ return Load(stream);
43+ }
44+ }
45+
46+ /// <summary>
47+ /// 指定されたストリームから翻訳ルールコンテンツを読み込んで解析し、翻訳ルールコレクションを作成します。
48+ /// </summary>
49+ /// <param name="stream">翻訳ルールコンテンツ読み込み用ストリーム。</param>
50+ /// <returns>翻訳ルールコレクション。</returns>
51+ internal static IEnumerable<DrFxRule> Load(Stream stream)
52+ {
4153 List<DrFxRule> rules = new List<DrFxRule>();
54+
4255 XmlDocument doc = new XmlDocument();
56+ doc.Load(stream);
4357
44- using (Stream stream = typeof(DrFxRuleLoader).Assembly.GetManifestResourceStream(resourceName))
58+ foreach (XmlNode ruleNode in doc.SelectNodes("/Rules/Rule"))
4559 {
46- doc.Load(stream);
47- //foreach (XmlNode node in doc.SelectNodes("/Rules/Rule"))
48- //{
49- // TranslationRule rule = new TranslationRule();
50- // rule.CheckId = node.Attributes["CheckId"].Value;
51- // rule.Category = node.Attributes["Category"].Value;
52- // rule.Name = node["Name"].InnerText;
53- // rule.ParseResolutions(node);
54- // rules.Add(rule);
55- //}
60+ DrFxRule rule = new DrFxRule();
61+ rule.CheckId = ruleNode.Attributes["CheckId"].Value;
62+ rule.Category = ruleNode.Attributes["Category"].Value;
63+ rule.Name = ruleNode["Name"].InnerText;
64+
65+ foreach (XmlNode resolutionNode in ruleNode.SelectNodes("Resolution"))
66+ {
67+ Resolution resolution = new Resolution();
68+ resolution.Content = resolutionNode["Content"].InnerText;
69+ resolution.Pattern = resolutionNode["Pattern"].InnerText;
70+ rule.Resolutions.Add(resolution);
71+ }
72+
73+ rules.Add(rule);
5674 }
75+
5776 return rules;
5877 }
5978 }
Afficher sur ancien navigateur de dépôt.