* ConfigurationManagerTest.cs: Added/improved tests for
[mono.git] / mcs / class / System.Configuration / Test / standalone / t46.cs
1 using System;
2 using System.Configuration;
3 using System.IO;
4
5 class Program : MarshalByRefObject
6 {
7         static void Main (string [] args)
8         {
9                 AppDomainSetup setup = new AppDomainSetup ();
10
11                 string basedir = AppDomain.CurrentDomain.BaseDirectory;
12                 setup.ConfigurationFile = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
13                         "t46.exe.config2");
14
15                 AppDomain domain = AppDomain.CreateDomain ("test",
16                         AppDomain.CurrentDomain.Evidence, setup);
17
18                 Program p;
19                 Foo f;
20                 Configuration c;
21
22                 p = GetRemote (domain);
23                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config2"),
24                         p.GetFilePath (string.Empty), "#A1");
25                 Assert.AreEqual ("Hello World2!",
26                         p.GetSettingValue (string.Empty, "hithere"), "#A2");
27                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config2"),
28                         p.GetFilePath ((string) null), "#A3");
29                 Assert.AreEqual ("Hello World2!",
30                         p.GetSettingValue ((string) null, "hithere"), "#A4");
31
32                 p = new Program ();
33
34                 c = p.OpenExeConfiguration (string.Empty);
35                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
36                         c.FilePath, "#B1");
37                 Assert.AreEqual ("Hello World!",
38                         c.AppSettings.Settings ["hithere"].Value, "#B2");
39                 c = p.OpenExeConfiguration ((string) null);
40                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
41                         c.FilePath, "#B3");
42                 Assert.AreEqual ("Hello World!",
43                         c.AppSettings.Settings ["hithere"].Value, "#B4");
44         
45                 f = Foo.GetRemote (domain);
46                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config2"),
47                         f.GetFilePath (string.Empty), "#C1");
48                 Assert.AreEqual ("Hello World2!",
49                         f.GetSettingValue (string.Empty, "hithere"), "#C2");
50                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config2"),
51                         f.GetFilePath ((string) null), "#C1");
52                 Assert.AreEqual ("Hello World2!",
53                         f.GetSettingValue ((string) null, "hithere"), "#C2");
54
55                 f = new Foo ();
56                 c = f.OpenExeConfiguration (string.Empty);
57                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
58                         c.FilePath, "#D1");
59                 Assert.AreEqual ("Hello World!",
60                         c.AppSettings.Settings ["hithere"].Value, "#D2");
61                 c = f.OpenExeConfiguration ((string) null);
62                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
63                         c.FilePath, "#D1");
64                 Assert.AreEqual ("Hello World!",
65                         c.AppSettings.Settings ["hithere"].Value, "#D2");
66
67                 AppDomain.Unload (domain);
68
69                 setup = new AppDomainSetup ();
70                 domain = AppDomain.CreateDomain ("test",
71                         AppDomain.CurrentDomain.Evidence, setup);
72
73                 p = GetRemote (domain);
74                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
75                         p.GetFilePath (string.Empty), "#E1");
76                 Assert.AreEqual ("Hello World!",
77                         p.GetSettingValue (string.Empty, "hithere"), "#E2");
78
79                 p = new Program ();
80
81                 c = p.OpenExeConfiguration (string.Empty);
82                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
83                         c.FilePath, "#F1");
84                 Assert.AreEqual ("Hello World!",
85                         c.AppSettings.Settings ["hithere"].Value, "#F2");
86
87                 f = Foo.GetRemote (domain);
88                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
89                         f.GetFilePath (string.Empty), "#G1");
90                 Assert.AreEqual ("Hello World!",
91                         f.GetSettingValue (string.Empty, "hithere"), "#G2");
92
93                 f = new Foo ();
94                 c = f.OpenExeConfiguration (string.Empty);
95                 Assert.AreEqual (Path.Combine (basedir, "t46.exe.config"),
96                         c.FilePath, "#H1");
97                 Assert.AreEqual ("Hello World!",
98                         c.AppSettings.Settings ["hithere"].Value, "#H2");
99
100                 Console.WriteLine ("configuration OK");
101         }
102
103         static Program GetRemote (AppDomain domain)
104         {
105                 Program test = (Program) domain.CreateInstanceAndUnwrap (
106                         typeof (Program).Assembly.FullName,
107                         typeof (Program).FullName, new object [0]);
108                 return test;
109         }
110
111         public string GetFilePath (string exePath)
112         {
113                 Configuration config = ConfigurationManager.OpenExeConfiguration (exePath);
114                 return config.FilePath;
115         }
116
117         public Configuration OpenExeConfiguration (string exePath)
118         {
119                 return ConfigurationManager.OpenExeConfiguration (exePath);
120         }
121
122         public string GetSettingValue (string exePath, string key)
123         {
124                 Configuration config = OpenExeConfiguration (exePath);
125                 return config.AppSettings.Settings [key].Value;
126         }
127 }