-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathTestResourceLoader.cs
More file actions
229 lines (194 loc) · 9.73 KB
/
Copy pathTestResourceLoader.cs
File metadata and controls
229 lines (194 loc) · 9.73 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#region Apache License
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
//**********************************************************************************
//* クラス名 :TestResourceLoader
//* クラス日本語名 :ResourceLoader・EmbeddedResourceLoaderのテスト
//*
//* 作成者 :玄人 幸道
//* 更新履歴 :
//*
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2026/08/06 玄人 幸道 新規作成(#522)
//* 2026/08/17 玄人 幸道 LoadAsStreamのテストを追加(#552)
//**********************************************************************************
using System;
using System.IO;
using System.Text;
using Touryo.Infrastructure.Public.Diagnostics;
using Touryo.Infrastructure.Public.IO;
namespace TestCode
{
/// <summary>ResourceLoader・EmbeddedResourceLoaderのテスト</summary>
/// <remarks>
/// SQL 定義ファイルやログ設定など、**リソースの読み込み全般がここに乗る**(#522)。
///
/// <なぜここを見るか>
/// 実際に踏んだ失敗がある。SimpleBatch が
/// resource file [C:\...\ShipperCount.sql] was not found.
/// で落ちたのは、この経路で解決できなかったため。
///
/// <埋め込みリソース>
/// MyBaseDao.UseEmbeddedResource を立てると、SQL 定義ファイルを
/// DLL に埋め込んだ構成に切り替わる(PaaS 向け)。その読み手がこちら。
///
/// <出力にパスを出さないこと>
/// 一時フォルダも配置フォルダも実行環境で変わる。
/// **結果ファイルの比較に載せるのは、真偽と中身だけにする。**
/// </remarks>
public class TestResourceLoader
{
#region 定数
/// <summary>埋め込みリソースの論理名</summary>
/// <remarks>
/// csproj で LogicalName を明示しているため、net48 と .NET (Core) で同じ名前になる。
/// **既定のままだと "既定の名前空間 + ファイル名" になり、
/// プロジェクト名が違う分だけ名前が食い違う。**
/// </remarks>
private const string EmbeddedName = "TestCode.TestEmbedded.txt";
#endregion
#region public
/// <summary>Root</summary>
public static void Root()
{
TestResourceLoader.TestResolveAndLoad();
MyDebug.OutputDebugAndConsole("----------------------------------------------------------------------------------------------------");
TestResourceLoader.TestEmbedded();
}
#endregion
#region private
/// <summary>実ファイルの解決と読み込み</summary>
private static void TestResolveAndLoad()
{
MyDebug.OutputDebugAndConsole("ResourceLoader");
string dir = Path.Combine(Path.GetTempPath(), "OpenTouryoTestResource");
string path = Path.Combine(dir, "TestResource.txt");
Directory.CreateDirectory(dir);
File.WriteAllText(path, "リソースの内容", new UTF8Encoding(false));
const string envName = "OPENTOURYO_TEST_RESDIR";
string orgEnv = Environment.GetEnvironmentVariable(envName, EnvironmentVariableTarget.Process);
try
{
// --- 絶対パス ---
MyDebug.OutputDebugAndConsole(
"絶対パスで解決 : " + (ResourceLoader.ResolveFilePath(path) != null));
MyDebug.OutputDebugAndConsole(
"絶対パスで読み込み : " + ResourceLoader.LoadAsString(path, Encoding.UTF8));
// --- 環境変数入りのパス ---
// ResolveFilePath は先頭で環境変数を展開する。
Environment.SetEnvironmentVariable(envName, dir, EnvironmentVariableTarget.Process);
MyDebug.OutputDebugAndConsole(
"環境変数入りで解決 : "
+ (ResourceLoader.ResolveFilePath("%" + envName + "%\\TestResource.txt") != null));
// --- EXE の配置フォルダ基準の相対パス ---
// SampleLogConf.xml は出力フォルダにコピーされる(csproj の Content)。
// カレント ディレクトリに関係なく解決できること。
MyDebug.OutputDebugAndConsole(
"配置フォルダ基準 : "
+ (ResourceLoader.ResolveFilePath("SampleLogConf.xml") != null));
// --- 見つからない場合 ---
MyDebug.OutputDebugAndConsole(
"見つからない(解決) : "
+ (ResourceLoader.ResolveFilePath("NotExist_OpenTouryo.txt") == null ? "null" : "not null"));
MyDebug.OutputDebugAndConsole(
"見つからない(Exists): "
+ ResourceLoader.Exists("NotExist_OpenTouryo.txt", false));
// throwException = true の場合
try
{
ResourceLoader.Exists("NotExist_OpenTouryo.txt", true);
MyDebug.OutputDebugAndConsole("見つからない(例外) : 例外にならない");
}
catch (Exception ex)
{
// メッセージは環境の言語で変わるため、型名だけを出す。
MyDebug.OutputDebugAndConsole("見つからない(例外) : " + ex.GetType().FullName);
}
// --- 空とnull ---
MyDebug.OutputDebugAndConsole(
"空文字(解決) : "
+ (ResourceLoader.ResolveFilePath("") == null ? "null" : "not null"));
MyDebug.OutputDebugAndConsole(
"null(解決) : "
+ (ResourceLoader.ResolveFilePath(null) == null ? "null" : "not null"));
}
finally
{
// 後始末。**環境変数は必ず戻す。**
Environment.SetEnvironmentVariable(envName, orgEnv, EnvironmentVariableTarget.Process);
try
{
File.Delete(path);
Directory.Delete(dir);
}
catch
{
// 消せなくてもテストの成否には関係しない。
}
}
}
/// <summary>埋め込みリソースの読み込み</summary>
private static void TestEmbedded()
{
MyDebug.OutputDebugAndConsole("EmbeddedResourceLoader");
MyDebug.OutputDebugAndConsole(
"存在する : "
+ EmbeddedResourceLoader.Exists(TestResourceLoader.EmbeddedName, false));
MyDebug.OutputDebugAndConsole(
"読み込み : "
+ EmbeddedResourceLoader.LoadAsString(TestResourceLoader.EmbeddedName, Encoding.UTF8).Trim());
MyDebug.OutputDebugAndConsole(
"存在しない : "
+ EmbeddedResourceLoader.Exists("TestCode.NotExist.txt", false));
// throwException = true の場合
try
{
EmbeddedResourceLoader.Exists("TestCode.NotExist.txt", true);
MyDebug.OutputDebugAndConsole("存在しない(例外) : 例外にならない");
}
catch (Exception ex)
{
MyDebug.OutputDebugAndConsole("存在しない(例外) : " + ex.GetType().FullName);
}
// **ストリームで読む。** LoadAsString と同じ中身が返るはず。
using (Stream st = EmbeddedResourceLoader.LoadAsStream(TestResourceLoader.EmbeddedName))
{
if (st == null)
{
MyDebug.OutputDebugAndConsole("LoadAsStream : (null)");
}
else
{
using (StreamReader sr = new StreamReader(st, Encoding.UTF8))
{
MyDebug.OutputDebugAndConsole("LoadAsStream : " + sr.ReadToEnd().Trim());
}
}
}
// **XML として読む。** 埋め込みの TestXml.xml は OpenTouryo.Public 側にある。
string xml = EmbeddedResourceLoader.LoadXMLAsString(
"OpenTouryo.Public", "Touryo.Infrastructure.Public.Xml.TestXml.xml");
MyDebug.OutputDebugAndConsole(
"LoadXMLAsString : 長さ " + xml.Length.ToString()
+ " / ルート要素 " + (xml.Contains("<bookstore") ? "bookstore" : "(不明)"));
// **XML 宣言の encoding は、読み込み時に落ちる。**
// 文字列にした時点で .NET の string なので、宣言を残すと矛盾するため。
MyDebug.OutputDebugAndConsole(
"LoadXMLAsString : XML宣言が残るか " + xml.TrimStart().StartsWith("<?xml"));
}
#endregion
}
}