~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Test/UnitTests/TestSetupService.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TestSetupService.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using NUnit.Framework;
 
29
using Mono.Addins.Setup;
 
30
using Mono.Addins;
 
31
using System.IO;
 
32
using System.Globalization;
 
33
 
 
34
namespace UnitTests
 
35
{
 
36
        [TestFixture]
 
37
        public class TestSetupService: TestBase
 
38
        {
 
39
                SetupService setup;
 
40
                string addinsDir;
 
41
                string repoDir;
 
42
                string repoExtrasDir;
 
43
                string baseDir;
 
44
                ConsoleProgressStatus monitor;
 
45
                bool repoBuilt;
 
46
                string repoUrl;
 
47
                string repoBaseUrl;
 
48
                
 
49
                [SetUp]
 
50
                public void TestSetup ()
 
51
                {
 
52
                        setup = new SetupService ();
 
53
                        baseDir = Path.GetDirectoryName (new Uri (typeof(TestBase).Assembly.CodeBase).LocalPath);
 
54
                        addinsDir = new DirectoryInfo (baseDir).Parent.Parent.Parent.FullName;
 
55
                        addinsDir = Path.Combine (addinsDir, "lib");
 
56
                        
 
57
                        repoDir = Path.Combine (TempDir, "repo");
 
58
                        repoExtrasDir = Path.Combine (repoDir,"extras");
 
59
                        monitor = new ConsoleProgressStatus (true);
 
60
                        
 
61
                        repoBaseUrl = new Uri (repoDir).ToString ();
 
62
                        repoUrl = new Uri (Path.Combine (repoDir, "main.mrep")).ToString ();
 
63
                }
 
64
                
 
65
                [Test]
 
66
                public void AddinPackageCreation ()
 
67
                {
 
68
                        repoBuilt = false;
 
69
                        if (Directory.Exists (repoDir))
 
70
                                Directory.Delete (repoDir, true);
 
71
                        Directory.CreateDirectory (repoDir);
 
72
                        Directory.CreateDirectory (repoExtrasDir);
 
73
                        
 
74
                        string asm = Path.Combine (baseDir, "SimpleApp.addin.xml");
 
75
                        string[] p = setup.BuildPackage (monitor, repoDir, asm);
 
76
                        Assert.IsTrue (File.Exists (p[0]));
 
77
                        
 
78
                        asm = Path.Combine (addinsDir, "HelloWorldExtension.dll");
 
79
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
80
                        Assert.IsTrue (File.Exists (p[0]));
 
81
                        
 
82
                        asm = Path.Combine (addinsDir, "CommandExtension.addin.xml");
 
83
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
84
                        Assert.IsTrue (File.Exists (p[0]));
 
85
                        
 
86
                        asm = Path.Combine (addinsDir, "SystemInfoExtension.addin.xml");
 
87
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
88
                        Assert.IsTrue (File.Exists (p[0]));
 
89
                        
 
90
                        asm = Path.Combine (addinsDir, "MultiAssemblyAddin.dll");
 
91
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
92
                        Assert.IsTrue (File.Exists (p[0]));
 
93
                        
 
94
                        string extras = Path.Combine (addinsDir, "extras");
 
95
                        asm = Path.Combine (extras, "ExtraExtender.addin.xml");
 
96
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
97
                        Assert.IsTrue (File.Exists (p[0]));
 
98
                        
 
99
                        asm = Path.Combine (extras, "FileContentExtension.addin.xml");
 
100
                        p = setup.BuildPackage (monitor, repoDir, asm);
 
101
                        Assert.IsTrue (File.Exists (p[0]));
 
102
                }
 
103
                
 
104
                public void InitRepository ()
 
105
                {
 
106
                        if (repoBuilt)
 
107
                                return;
 
108
                        
 
109
                        if (Directory.Exists (repoDir))
 
110
                                Directory.Delete (repoDir, true);
 
111
                        Directory.CreateDirectory (repoDir);
 
112
                        Directory.CreateDirectory (repoExtrasDir);
 
113
                        
 
114
                        string asm = Path.Combine (baseDir, "SimpleApp.addin.xml");
 
115
                        setup.BuildPackage (monitor, repoDir, asm);
 
116
                        
 
117
                        asm = Path.Combine (addinsDir, "HelloWorldExtension.dll");
 
118
                        setup.BuildPackage (monitor, repoDir, asm);
 
119
                        
 
120
                        asm = Path.Combine (addinsDir, "CommandExtension.addin.xml");
 
121
                        setup.BuildPackage (monitor, repoDir, asm);
 
122
                        
 
123
                        asm = Path.Combine (addinsDir, "SystemInfoExtension.addin.xml");
 
124
                        setup.BuildPackage (monitor, repoDir, asm);
 
125
                        
 
126
                        asm = Path.Combine (addinsDir, "MultiAssemblyAddin.dll");
 
127
                        setup.BuildPackage (monitor, repoDir, asm);
 
128
                        
 
129
                        string extras = Path.Combine (addinsDir, "extras");
 
130
                        asm = Path.Combine (extras, "ExtraExtender.addin.xml");
 
131
                        setup.BuildPackage (monitor, repoExtrasDir, asm);
 
132
                        
 
133
                        asm = Path.Combine (extras, "FileContentExtension.addin.xml");
 
134
                        setup.BuildPackage (monitor, repoExtrasDir, asm);
 
135
                        
 
136
                        repoBuilt = true;
 
137
                }
 
138
                
 
139
                [Test]
 
140
                public void BuildRepository ()
 
141
                {
 
142
                        InitRepository ();
 
143
                        setup.BuildRepository (monitor, repoDir);
 
144
                        
 
145
                        Assert.IsTrue (File.Exists (Path.Combine (repoDir, "main.mrep")));
 
146
                        Assert.IsTrue (File.Exists (Path.Combine (repoDir, "root.mrep")));
 
147
                        Assert.IsTrue (File.Exists (Path.Combine (repoExtrasDir, "main.mrep")));
 
148
                }
 
149
                
 
150
                [Test]
 
151
                public void ManageRepository ()
 
152
                {
 
153
                        // Register without .mrep reference
 
154
                        var ar = setup.Repositories.RegisterRepository (monitor, repoBaseUrl, false);
 
155
                        Assert.IsTrue (ar.Enabled);
 
156
                        Assert.AreEqual (repoUrl, ar.Url);
 
157
                        Assert.IsTrue (setup.Repositories.ContainsRepository (repoUrl));
 
158
                        
 
159
                        setup.Repositories.RemoveRepository (repoUrl);
 
160
                        Assert.IsFalse (setup.Repositories.ContainsRepository (repoUrl));
 
161
                        
 
162
                        // Register with .mrep reference
 
163
                        ar = setup.Repositories.RegisterRepository (monitor, repoUrl, false);
 
164
                        Assert.IsTrue (ar.Enabled);
 
165
                        Assert.AreEqual (repoUrl, ar.Url);
 
166
                        Assert.IsTrue (setup.Repositories.ContainsRepository (repoUrl));
 
167
                        
 
168
                        var reps = setup.Repositories.GetRepositories ();
 
169
                        Assert.AreEqual (1, reps.Length);
 
170
                        Assert.IsTrue (reps[0].Enabled);
 
171
                        Assert.AreEqual (repoUrl, reps[0].Url);
 
172
                        
 
173
                        setup.Repositories.RemoveRepository (repoUrl);
 
174
                        Assert.IsFalse (setup.Repositories.ContainsRepository (repoUrl));
 
175
                }
 
176
                
 
177
                [Test]
 
178
                public void QueryRepository ()
 
179
                {
 
180
                        InitRepository ();
 
181
                        setup.BuildRepository (monitor, repoDir);
 
182
                        setup.Repositories.RegisterRepository (monitor, repoUrl, true);
 
183
                        
 
184
                        var addins = setup.Repositories.GetAvailableAddins ();
 
185
                        Assert.AreEqual (7, addins.Length);
 
186
                        
 
187
                        var oldc = System.Threading.Thread.CurrentThread.CurrentCulture;
 
188
                        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ca-ES");
 
189
                        
 
190
//                      CheckAddin (addins, "", "", "0.0.0.0", "", "", "", "", "", "");
 
191
                        CheckAddin (addins, "SimpleApp.CommandExtension,0.1.0", "SimpleApp", "0.1.0", "", "CommandExtension", "SimpleApp/Extensions", "Lluis Sanchez", "GPL", "CommandExtension");
 
192
                        CheckAddin (addins, "SimpleApp.Core,0.1.0", "SimpleApp", "0.1.0", "", "Una aplicació simple", "SimpleApp", "Lluis Sanchez", "GPL", "SimpleApp description");
 
193
                        CheckProperties (addins, "SimpleApp.Core,0.1.0", "Prop1", "Val1Cat", "Prop2", "Val2Cat");
 
194
                        CheckAddin (addins, "SimpleApp.HelloWorldExtension,0.1.0", "SimpleApp", "0.1.0", "", "SimpleApp.HelloWorldExtension", "", "", "", "");
 
195
                        CheckAddin (addins, "SimpleApp.SystemInfoExtension,0.1.0", "SimpleApp", "0.1.0", "", "SystemInfoExtension", "SimpleApp/Extensions", "Lluis Sanchez", "GPL", "SystemInfoExtension");
 
196
                        CheckAddin (addins, "SimpleApp.ExtraExtender,0.1.0", "SimpleApp", "0.1.0", "", "SimpleApp.ExtraExtender", "", "", "", "");
 
197
                        CheckAddin (addins, "SimpleApp.FileContentExtension,0.1.0", "SimpleApp", "0.1.0", "", "FileContentExtension", "SimpleApp/Extensions", "Lluis Sanchez", "GPL", "FileContentExtension");
 
198
                        
 
199
                        System.Threading.Thread.CurrentThread.CurrentCulture = oldc;
 
200
                        setup.Repositories.RemoveRepository (repoUrl);
 
201
                }
 
202
                
 
203
                void CheckAddin (AddinRepositoryEntry[] areps, string id, string ns, string version, string compat, string name, string category, string author, string copyright, string desc)
 
204
                {
 
205
                        AddinRepositoryEntry arep = areps.FirstOrDefault (a => a.Addin.Id == id);
 
206
                        Assert.IsNotNull (arep, "Add-in " + id + " not found");
 
207
                        Assert.AreEqual (id, arep.Addin.Id);
 
208
                        Assert.AreEqual (ns, arep.Addin.Namespace);
 
209
                        Assert.AreEqual (version, arep.Addin.Version);
 
210
                        Assert.AreEqual (compat, arep.Addin.BaseVersion);
 
211
                        Assert.AreEqual (name, arep.Addin.Name);
 
212
                        Assert.AreEqual (category, arep.Addin.Category);
 
213
                        Assert.AreEqual (author, arep.Addin.Author);
 
214
                        Assert.AreEqual (copyright, arep.Addin.Copyright);
 
215
                        Assert.AreEqual (desc, arep.Addin.Description);
 
216
                }
 
217
                
 
218
                void CheckProperties (AddinRepositoryEntry[] areps, string id, params string[] props)
 
219
                {
 
220
                        AddinRepositoryEntry arep = areps.FirstOrDefault (a => a.Addin.Id == id);
 
221
                        for (int n=0; n<props.Length; n+=2)
 
222
                                Assert.AreEqual (props[n+1], arep.Addin.Properties.GetPropertyValue (props[n]));
 
223
                }
 
224
                
 
225
                [Test]
 
226
                public void GetSupportFile ()
 
227
                {
 
228
                        InitRepository ();
 
229
                        setup.BuildRepository (monitor, repoDir);
 
230
                        setup.Repositories.RegisterRepository (monitor, repoUrl, true);
 
231
                        
 
232
                        AddinRepositoryEntry arep = setup.Repositories.GetAvailableAddin ("SimpleApp.Core", "0.1.0")[0];
 
233
                        IAsyncResult res = arep.BeginDownloadSupportFile (arep.Addin.Properties.GetPropertyValue ("Prop3"), null, null);
 
234
                        Stream s = arep.EndDownloadSupportFile (res);
 
235
                        StreamReader sr = new StreamReader (s);
 
236
                        Assert.AreEqual ("Some support file", sr.ReadToEnd ());
 
237
                        sr.Close ();
 
238
                        
 
239
                        setup.Repositories.RemoveRepository (repoUrl);
 
240
                }
 
241
                
 
242
                [Test]
 
243
                public void UpgradeCoreAddin ()
 
244
                {
 
245
                        InitRepository ();
 
246
                        CreateTestPackage ("10.3");
 
247
                        CreateTestPackage ("10.4");
 
248
                        
 
249
                        ExtensionNodeList list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
 
250
                        Assert.AreEqual (1, list.Count);
 
251
                        Assert.AreEqual ("10.2", ((ItemSetNode)list[0]).Label);
 
252
                        
 
253
                        Addin adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest,10.2");
 
254
                        Assert.IsTrue (adn.Enabled);
 
255
                        Assert.IsFalse (adn.Description.CanUninstall); // Core add-in can't be uninstalled
 
256
                        
 
257
                        setup.Repositories.RegisterRepository (monitor, repoUrl, true);
 
258
                        
 
259
                        var pkg = setup.Repositories.GetAvailableAddinUpdates ("SimpleApp.AddRemoveTest", RepositorySearchFlags.LatestVersionsOnly).FirstOrDefault ();
 
260
                        Assert.IsNotNull (pkg);
 
261
                        Assert.AreEqual ("SimpleApp.AddRemoveTest,10.4", pkg.Addin.Id);
 
262
                        
 
263
                        // Install the add-in
 
264
                        
 
265
                        setup.Install (new ConsoleProgressStatus (true), pkg);
 
266
                        
 
267
                        adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest,10.2");
 
268
                        Assert.IsFalse (adn.Enabled);
 
269
                        
 
270
                        list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
 
271
                        Assert.AreEqual (1, list.Count);
 
272
                        Assert.AreEqual ("10.4", ((ItemSetNode)list[0]).Label);
 
273
                        
 
274
                        adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest");
 
275
                        Assert.IsTrue (adn.Enabled);
 
276
                        Assert.IsTrue (adn.Description.CanUninstall);
 
277
                        
 
278
                        // Uninstall the add-in
 
279
                        
 
280
                        setup.Uninstall (new ConsoleProgressStatus (true), "SimpleApp.AddRemoveTest,10.4");
 
281
                        
 
282
                        list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
 
283
                        Assert.AreEqual (0, list.Count);
 
284
                        
 
285
                        adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest");
 
286
                        Assert.IsFalse (adn.Enabled);
 
287
                        
 
288
                        adn.Enabled = true;
 
289
                        
 
290
                        list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
 
291
                        Assert.AreEqual (1, list.Count);
 
292
                        Assert.AreEqual ("10.2", ((ItemSetNode)list[0]).Label);
 
293
                }
 
294
                
 
295
                void CreateTestPackage (string newVersion)
 
296
                {
 
297
                        string file = AddinManager.CurrentAddin.GetFilePath (Path.Combine ("SampleAddins","AddRemoveTest1.addin.xml"));
 
298
                        string txt = File.ReadAllText (file);
 
299
                        txt = txt.Replace ("10.1", newVersion);
 
300
                        
 
301
                        string tmpFile = Path.Combine (TempDir, "AddRemoveTest_" + newVersion + ".addin.xml");
 
302
                        File.WriteAllText (tmpFile, txt);
 
303
                        
 
304
                        setup.BuildPackage (monitor, repoDir, tmpFile);
 
305
                        setup.BuildRepository (monitor, repoDir);
 
306
                }
 
307
        }
 
308
}
 
309