5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using NUnit.Framework;
29
using Mono.Addins.Setup;
32
using System.Globalization;
37
public class TestSetupService: TestBase
44
ConsoleProgressStatus monitor;
50
public void TestSetup ()
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");
57
repoDir = Path.Combine (TempDir, "repo");
58
repoExtrasDir = Path.Combine (repoDir,"extras");
59
monitor = new ConsoleProgressStatus (true);
61
repoBaseUrl = new Uri (repoDir).ToString ();
62
repoUrl = new Uri (Path.Combine (repoDir, "main.mrep")).ToString ();
66
public void AddinPackageCreation ()
69
if (Directory.Exists (repoDir))
70
Directory.Delete (repoDir, true);
71
Directory.CreateDirectory (repoDir);
72
Directory.CreateDirectory (repoExtrasDir);
74
string asm = Path.Combine (baseDir, "SimpleApp.addin.xml");
75
string[] p = setup.BuildPackage (monitor, repoDir, asm);
76
Assert.IsTrue (File.Exists (p[0]));
78
asm = Path.Combine (addinsDir, "HelloWorldExtension.dll");
79
p = setup.BuildPackage (monitor, repoDir, asm);
80
Assert.IsTrue (File.Exists (p[0]));
82
asm = Path.Combine (addinsDir, "CommandExtension.addin.xml");
83
p = setup.BuildPackage (monitor, repoDir, asm);
84
Assert.IsTrue (File.Exists (p[0]));
86
asm = Path.Combine (addinsDir, "SystemInfoExtension.addin.xml");
87
p = setup.BuildPackage (monitor, repoDir, asm);
88
Assert.IsTrue (File.Exists (p[0]));
90
asm = Path.Combine (addinsDir, "MultiAssemblyAddin.dll");
91
p = setup.BuildPackage (monitor, repoDir, asm);
92
Assert.IsTrue (File.Exists (p[0]));
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]));
99
asm = Path.Combine (extras, "FileContentExtension.addin.xml");
100
p = setup.BuildPackage (monitor, repoDir, asm);
101
Assert.IsTrue (File.Exists (p[0]));
104
public void InitRepository ()
109
if (Directory.Exists (repoDir))
110
Directory.Delete (repoDir, true);
111
Directory.CreateDirectory (repoDir);
112
Directory.CreateDirectory (repoExtrasDir);
114
string asm = Path.Combine (baseDir, "SimpleApp.addin.xml");
115
setup.BuildPackage (monitor, repoDir, asm);
117
asm = Path.Combine (addinsDir, "HelloWorldExtension.dll");
118
setup.BuildPackage (monitor, repoDir, asm);
120
asm = Path.Combine (addinsDir, "CommandExtension.addin.xml");
121
setup.BuildPackage (monitor, repoDir, asm);
123
asm = Path.Combine (addinsDir, "SystemInfoExtension.addin.xml");
124
setup.BuildPackage (monitor, repoDir, asm);
126
asm = Path.Combine (addinsDir, "MultiAssemblyAddin.dll");
127
setup.BuildPackage (monitor, repoDir, asm);
129
string extras = Path.Combine (addinsDir, "extras");
130
asm = Path.Combine (extras, "ExtraExtender.addin.xml");
131
setup.BuildPackage (monitor, repoExtrasDir, asm);
133
asm = Path.Combine (extras, "FileContentExtension.addin.xml");
134
setup.BuildPackage (monitor, repoExtrasDir, asm);
140
public void BuildRepository ()
143
setup.BuildRepository (monitor, repoDir);
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")));
151
public void ManageRepository ()
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));
159
setup.Repositories.RemoveRepository (repoUrl);
160
Assert.IsFalse (setup.Repositories.ContainsRepository (repoUrl));
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));
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);
173
setup.Repositories.RemoveRepository (repoUrl);
174
Assert.IsFalse (setup.Repositories.ContainsRepository (repoUrl));
178
public void QueryRepository ()
181
setup.BuildRepository (monitor, repoDir);
182
setup.Repositories.RegisterRepository (monitor, repoUrl, true);
184
var addins = setup.Repositories.GetAvailableAddins ();
185
Assert.AreEqual (7, addins.Length);
187
var oldc = System.Threading.Thread.CurrentThread.CurrentCulture;
188
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ca-ES");
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");
199
System.Threading.Thread.CurrentThread.CurrentCulture = oldc;
200
setup.Repositories.RemoveRepository (repoUrl);
203
void CheckAddin (AddinRepositoryEntry[] areps, string id, string ns, string version, string compat, string name, string category, string author, string copyright, string desc)
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);
218
void CheckProperties (AddinRepositoryEntry[] areps, string id, params string[] props)
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]));
226
public void GetSupportFile ()
229
setup.BuildRepository (monitor, repoDir);
230
setup.Repositories.RegisterRepository (monitor, repoUrl, true);
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 ());
239
setup.Repositories.RemoveRepository (repoUrl);
243
public void UpgradeCoreAddin ()
246
CreateTestPackage ("10.3");
247
CreateTestPackage ("10.4");
249
ExtensionNodeList list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
250
Assert.AreEqual (1, list.Count);
251
Assert.AreEqual ("10.2", ((ItemSetNode)list[0]).Label);
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
257
setup.Repositories.RegisterRepository (monitor, repoUrl, true);
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);
263
// Install the add-in
265
setup.Install (new ConsoleProgressStatus (true), pkg);
267
adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest,10.2");
268
Assert.IsFalse (adn.Enabled);
270
list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
271
Assert.AreEqual (1, list.Count);
272
Assert.AreEqual ("10.4", ((ItemSetNode)list[0]).Label);
274
adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest");
275
Assert.IsTrue (adn.Enabled);
276
Assert.IsTrue (adn.Description.CanUninstall);
278
// Uninstall the add-in
280
setup.Uninstall (new ConsoleProgressStatus (true), "SimpleApp.AddRemoveTest,10.4");
282
list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
283
Assert.AreEqual (0, list.Count);
285
adn = AddinManager.Registry.GetAddin ("SimpleApp.AddRemoveTest");
286
Assert.IsFalse (adn.Enabled);
290
list = AddinManager.GetExtensionNodes ("/SimpleApp/InstallUninstallTest");
291
Assert.AreEqual (1, list.Count);
292
Assert.AreEqual ("10.2", ((ItemSetNode)list[0]).Label);
295
void CreateTestPackage (string newVersion)
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);
301
string tmpFile = Path.Combine (TempDir, "AddRemoveTest_" + newVersion + ".addin.xml");
302
File.WriteAllText (tmpFile, txt);
304
setup.BuildPackage (monitor, repoDir, tmpFile);
305
setup.BuildRepository (monitor, repoDir);