Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Deployment/3.3/Tests/PluginStoreTest.cs @ 6103

Last change on this file since 6103 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 18.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Services.Deployment.DataAccess;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using System.Collections.Generic;
25using System.Linq;
26using System;
27using System.Diagnostics;
28
29namespace HeuristicLab.Services.Deployment.Test {
30
31
32  /// <summary>
33  ///This is a test class for PluginStoreTest and is intended
34  ///to contain all PluginStoreTest Unit Tests
35  ///</summary>
36  [TestClass()]
37  public class PluginStoreTest {
38
39    private System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
40    private TestContext testContextInstance;
41
42    /// <summary>
43    ///Gets or sets the test context which provides
44    ///information about and functionality for the current test run.
45    ///</summary>
46    public TestContext TestContext {
47      get {
48        return testContextInstance;
49      }
50      set {
51        testContextInstance = value;
52      }
53    }
54
55    #region Additional test attributes
56    //
57    //You can use the following additional attributes as you write your tests:
58    //
59    //Use ClassInitialize to run code before running the first test in the class
60    //[ClassInitialize()]
61    //public static void MyClassInitialize(TestContext testContext)
62    //{
63    //}
64    //
65    //Use ClassCleanup to run code after all tests in a class have run
66    //[ClassCleanup()]
67    //public static void MyClassCleanup()
68    //{
69    //}
70    //
71    //Use TestInitialize to run code before running each test
72    //[TestInitialize()]
73    //public void MyTestInitialize()
74    //{
75    //}
76    //
77    //Use TestCleanup to run code after each test has run
78    //[TestCleanup()]
79    //public void MyTestCleanup()
80    //{
81    //}
82    //
83    #endregion
84
85
86    /// <summary>
87    ///A test for PluginFile
88    ///</summary>
89    [TestMethod()]
90    public void PluginFileTest() {
91      PluginStore target = new PluginStore();
92      #region insert and retrieve a plugin file
93      int oldCount = target.Plugins.Count();
94      // store a new entry in the db
95      string name = RandomName();
96      target.Persist(new PluginDescription(name, new Version(0, 1)), enc.GetBytes("Zipped " + name));
97      int newCount = target.Plugins.Count();
98      // make sure the new entry was added to the db
99      Assert.AreEqual(oldCount + 1, newCount);
100
101      // get matching description from db and try to retrieve the stored file
102      PluginDescription pluginDescription = target.Plugins.Where(p => p.Name == name).Single();
103      byte[] expected = enc.GetBytes("Zipped " + name);
104      byte[] actual = target.PluginFile(pluginDescription);
105      // check retrieved file
106      Assert.AreEqual(expected.Length, actual.Length);
107      for (int i = 0; i < expected.Length; i++) {
108        Assert.AreEqual(expected[i], actual[i]);
109      }
110      #endregion
111    }
112
113    /// <summary>
114    ///A test for Persist
115    ///</summary>
116    [TestMethod()]
117    public void PersistPluginTest() {
118      var vers01 = new Version(0, 1);
119      PluginStore target = new PluginStore();
120
121      #region persist single plugin without dependencies
122      {
123        string name = RandomName();
124        int oldCount = target.Plugins.Count();
125        PluginDescription pluginDescription = new PluginDescription(name, vers01);
126        byte[] pluginPackage = enc.GetBytes("Zipped " + name);
127        target.Persist(pluginDescription, pluginPackage);
128        int newCount = target.Plugins.Count();
129        Assert.AreEqual(oldCount + 1, newCount);
130      }
131      #endregion
132
133      #region persist a product with same name and version as an existent product
134      {
135        string name = RandomName();
136        int oldCount = target.Plugins.Count();
137        PluginDescription pluginDescription = new PluginDescription(name, vers01);
138        byte[] pluginPackage = enc.GetBytes("Zipped " + name);
139        target.Persist(pluginDescription, pluginPackage);
140        int newCount = target.Plugins.Count();
141        Assert.AreEqual(oldCount + 1, newCount);
142
143        // insert same name and version
144        oldCount = target.Plugins.Count();
145        pluginDescription = new PluginDescription(name, vers01);
146        pluginPackage = enc.GetBytes("Zipped " + name);
147        target.Persist(pluginDescription, pluginPackage);
148        newCount = target.Plugins.Count();
149        // make sure old entry was updated
150        Assert.AreEqual(oldCount, newCount);
151
152        // insert new with different version
153        oldCount = target.Plugins.Count();
154        pluginDescription = new PluginDescription(name, new Version(0, 2));
155        pluginPackage = enc.GetBytes("Zipped " + name);
156        target.Persist(pluginDescription, pluginPackage);
157        newCount = target.Plugins.Count();
158        // make sure a new entry was created
159        Assert.AreEqual(oldCount + 1, newCount);
160      }
161      #endregion
162
163      #region persist a plugin with an already persisted dependency
164      {
165        string name = RandomName();
166        PluginDescription dependency = new PluginDescription(name, vers01);
167        // insert dependency first
168        target.Persist(dependency, enc.GetBytes("Zipped " + name));
169
170        // persist another plugin that has a dependency on the first plugin
171        string name2 = RandomName();
172        int oldCount = target.Plugins.Count();
173
174        PluginDescription newEntity = new PluginDescription(name2, vers01, Enumerable.Repeat(dependency, 1));
175        byte[] newEntityPackage = enc.GetBytes("Zipped " + name2);
176        target.Persist(newEntity, newEntityPackage);
177        int newCount = target.Plugins.Count();
178        Assert.AreEqual(oldCount + 1, newCount); // only one new plugin should be added
179
180        // retrieve second plugin and check dependencies
181        newEntity = target.Plugins.Where(p => p.Name == name2).Single();
182        Assert.AreEqual(newEntity.Dependencies.Count(), 1);
183        Assert.AreEqual(newEntity.Dependencies.First().Name, name);
184      }
185      #endregion
186
187      #region try to persist a new  plugin with a non-existant dependency and check the expected exception
188      {
189        // try to persist a new plugin with a non-existant dependency
190        try {
191          string pluginName = RandomName();
192          string dependencyName = RandomName();
193          var dependency = new PluginDescription(dependencyName, vers01);
194          var newEntity = new PluginDescription(pluginName, vers01, Enumerable.Repeat(dependency, 1));
195          target.Persist(newEntity, enc.GetBytes("Zipped " + pluginName));
196          Assert.Fail("persist should fail with ArgumentException");
197        }
198        catch (ArgumentException) {
199          // this is expected
200          Assert.IsTrue(true, "expected exception");
201        }
202      }
203      #endregion
204
205      #region update the plugin file of an existing plugin
206      {
207        // insert new plugin
208        string pluginName = RandomName();
209        var newPlugin = new PluginDescription(pluginName, vers01);
210        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
211
212        // update the plugin file
213        byte[] expected = enc.GetBytes("Zipped2 " + pluginName);
214        target.Persist(newPlugin, expected);
215        // check if the updated file is returned
216        byte[] actual = target.PluginFile(newPlugin);
217        // check retrieved file
218        Assert.AreEqual(expected.Length, actual.Length);
219        for (int i = 0; i < expected.Length; i++) {
220          Assert.AreEqual(expected[i], actual[i]);
221        }
222      }
223      #endregion
224
225      #region update the dependencies of an existing plugin
226      {
227        string dependency1Name = RandomName();
228        var newDependency1 = new PluginDescription(dependency1Name, vers01);
229        target.Persist(newDependency1, enc.GetBytes("Zipped " + dependency1Name));
230
231        string pluginName = RandomName();
232        var newPlugin = new PluginDescription(pluginName, vers01, Enumerable.Repeat(newDependency1, 1));
233        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
234
235        // retrieve plugin
236        var dbPlugin = target.Plugins.Where(p => p.Name == pluginName).Single();
237        Assert.AreEqual(dbPlugin.Dependencies.Count(), 1);
238        Assert.AreEqual(dbPlugin.Dependencies.First().Name, dependency1Name);
239
240        // change dependencies
241        string dependency2Name = RandomName();
242        var newDependency2 = new PluginDescription(dependency2Name, vers01);
243        target.Persist(newDependency2, enc.GetBytes("Zipped " + pluginName));
244
245        newPlugin = new PluginDescription(pluginName, vers01, new PluginDescription[] { newDependency1, newDependency2 });
246        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
247        // retrieve plugin
248        dbPlugin = target.Plugins.Where(p => p.Name == pluginName).Single();
249        Assert.AreEqual(dbPlugin.Dependencies.Count(), 2);
250      }
251      #endregion
252
253      #region try to insert a plugin that references the same dependency twice and check if the correct exception is thrown
254      {
255        string depName = RandomName();
256        var depPlugin = new PluginDescription(depName, vers01);
257        target.Persist(depPlugin, enc.GetBytes("Zipped " + depName));
258
259        // insert new plugin
260        string pluginName = RandomName();
261        var plugin = new PluginDescription(pluginName, vers01, new PluginDescription[] { depPlugin, depPlugin });
262        try {
263          target.Persist(plugin, enc.GetBytes("Zipped " + depName));
264          Assert.Fail("Expected ArgumentException");
265        }
266        catch (ArgumentException) {
267          Assert.IsTrue(true, "Exception thrown as expected");
268        }
269      }
270      #endregion
271
272      #region try to insert a plugin that with a cyclic reference
273      {
274        string depName = RandomName();
275        var depPlugin = new PluginDescription(depName, vers01);
276        target.Persist(depPlugin, enc.GetBytes("Zipped " + depName));
277
278        // update the plugin so that it has a dependency on itself
279        var plugin = new PluginDescription(depName, vers01, new PluginDescription[] { depPlugin });
280        try {
281          target.Persist(plugin, enc.GetBytes("Zipped " + depName));
282          Assert.Fail("Expected ArgumentException");
283        }
284        catch (ArgumentException) {
285          Assert.IsTrue(true, "Exception thrown as expected");
286        }
287      }
288      #endregion
289
290    }
291
292    /// <summary>
293    ///A test for Persist
294    ///</summary>
295    [TestMethod()]
296    public void PersistProductTest() {
297      Version vers01 = new Version(0, 1);
298      PluginStore target = new PluginStore();
299
300      #region persist a product without plugins to the db
301      {
302        string prodName = RandomName();
303        int oldCount = target.Products.Count();
304        ProductDescription product = new ProductDescription(prodName, vers01);
305        target.Persist(product);
306        int newCount = target.Products.Count();
307        Assert.AreEqual(oldCount + 1, newCount);
308      }
309      #endregion
310
311      #region persist a product with the same name and version as an existant product
312      {
313        string prodName = RandomName();
314        int oldCount = target.Products.Count();
315        ProductDescription product = new ProductDescription(prodName, vers01);
316        target.Persist(product);
317        int newCount = target.Products.Count();
318        Assert.AreEqual(oldCount + 1, newCount);
319
320        // write a product with same name and version
321        oldCount = target.Products.Count();
322        product = new ProductDescription(prodName, vers01);
323        target.Persist(product);
324        newCount = target.Products.Count();
325
326        // make sure that the old entry was updated
327        Assert.AreEqual(oldCount, newCount);
328
329        // write a product with same name and different version
330        oldCount = target.Products.Count();
331        product = new ProductDescription(prodName, new Version(0, 2));
332        target.Persist(product);
333        newCount = target.Products.Count();
334
335        // make sure that a new entry was created
336        Assert.AreEqual(oldCount + 1, newCount);
337      }
338      #endregion
339
340      #region try to persist a product referencing an non-existant plugin and check the expected exception
341      {
342        // try to persist a product referencing a non-existant plugin
343        string prodName = RandomName();
344        string pluginName = RandomName();
345        var plugin = new PluginDescription(pluginName, vers01);
346        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
347        try {
348          target.Persist(product);
349          Assert.Fail("persist should fail with ArgumentException");
350        }
351        catch (ArgumentException) {
352          // this is expected
353          Assert.IsTrue(true, "expected exception");
354        }
355      }
356      #endregion
357
358      #region persist a product with a single plugin reference
359      {
360        string prodName = RandomName();
361        string pluginName = RandomName();
362        var plugin = new PluginDescription(pluginName, vers01);
363        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
364
365        // persist the plugin first
366        int oldCount = target.Products.Count();
367        target.Persist(plugin, enc.GetBytes("Zipped " + plugin.Name));
368        target.Persist(product);
369        int newCount = target.Products.Count();
370        // make sure the store went through
371        Assert.AreEqual(oldCount + 1, newCount);
372        // retrieve the product and check if the plugin list was stored/retrieved correctly
373        var dbProd = target.Products.Where(p => p.Name == prodName).Single();
374        Assert.AreEqual(dbProd.Plugins.Count(), 1);
375        Assert.AreEqual(dbProd.Plugins.First().Name, pluginName);
376      }
377      #endregion
378
379      #region update the plugin list of an existing product
380      {
381        string prodName = RandomName();
382        string plugin1Name = RandomName();
383        var plugin1 = new PluginDescription(plugin1Name, vers01);
384        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 1));
385
386        // persist the plugin first
387        int oldCount = target.Products.Count();
388        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
389        target.Persist(product);
390        int newCount = target.Products.Count();
391        // make sure the store went through
392        Assert.AreEqual(oldCount + 1, newCount);
393
394        var plugin2Name = RandomName();
395        var plugin2 = new PluginDescription(plugin2Name, vers01);
396        target.Persist(plugin2, enc.GetBytes("Zipped " + plugin2.Name));
397        product = new ProductDescription(prodName, vers01, new PluginDescription[] { plugin1, plugin2 });
398        oldCount = target.Products.Count();
399        target.Persist(product);
400        newCount = target.Products.Count();
401        // make sure that a new entry was not created
402        Assert.AreEqual(oldCount, newCount);
403
404        // check the plugin list of the product
405        var dbProduct = target.Products.Where(p => p.Name == prodName).Single();
406        Assert.AreEqual(dbProduct.Plugins.Count(), 2);
407      }
408      #endregion
409
410      #region insert a product which references the same plugin twice and check if the correct exception is thrown
411      {
412        string prodName = RandomName();
413        string plugin1Name = RandomName();
414        var plugin1 = new PluginDescription(plugin1Name, vers01);
415        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 2));
416
417        // persist the plugin first
418        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
419        try {
420          target.Persist(product);
421          Assert.Fail("Expected ArgumentException");
422        }
423        catch (ArgumentException) {
424          Assert.IsTrue(true, "Expected exception was thrown.");
425        }
426      }
427      #endregion
428    }
429
430    [TestMethod]
431    public void InsertPluginPerformanceTest() {
432      int nPlugins = 100;
433      int maxDependencies = 10;
434      int avgZipFileLength = 32000;
435
436      var store = new PluginStore();
437      Version vers01 = new Version(0, 1);
438      // create a random byte array to represent file length
439      byte[] zippedConstant = new byte[avgZipFileLength];
440      Random r = new Random();
441      r.NextBytes(zippedConstant);
442
443      Stopwatch stopWatch = new Stopwatch();
444      stopWatch.Start();
445      // create plugins
446      List<PluginDescription> plugins = new List<PluginDescription>();
447      for (int i = 0; i < nPlugins; i++) {
448        string name = RandomName();
449        var dependencies = store.Plugins;
450        if (dependencies.Count() > maxDependencies) dependencies = dependencies.Take(maxDependencies);
451        var plugin = new PluginDescription(name, vers01, dependencies);
452        store.Persist(plugin, zippedConstant);
453        plugins.Add(plugin);
454      }
455      stopWatch.Stop();
456      Assert.Inconclusive("Created " + nPlugins + " plugins in " + stopWatch.ElapsedMilliseconds +
457        " ms (" + (double)stopWatch.ElapsedMilliseconds / nPlugins + " ms / plugin )");
458    }
459    [TestMethod]
460    public void InsertProductPerformanceTest() {
461      int nProducts = 50;
462      int avgProductPlugins = 30;
463
464      var store = new PluginStore();
465      Version vers01 = new Version(0, 1);
466      Stopwatch stopWatch = new Stopwatch();
467      Random r = new Random();
468      var plugins = store.Plugins.ToList();
469      stopWatch.Start();
470      // create products
471      for (int i = 0; i < nProducts; i++) {
472        string name = RandomName();
473        List<PluginDescription> prodPlugins = new List<PluginDescription>();
474        for (int j = 0; j < avgProductPlugins; j++) {
475          var selectedPlugin = plugins[r.Next(0, plugins.Count)];
476          if (!prodPlugins.Contains(selectedPlugin)) prodPlugins.Add(selectedPlugin);
477        }
478        var prod = new ProductDescription(name, vers01, prodPlugins);
479        store.Persist(prod);
480      }
481      stopWatch.Stop();
482      Assert.Inconclusive("Created " + nProducts + " products in " + stopWatch.ElapsedMilliseconds +
483        " ms (" + (double)stopWatch.ElapsedMilliseconds / nProducts + " ms / product )");
484    }
485
486    private string RandomName() {
487      return Guid.NewGuid().ToString();
488    }
489  }
490}
Note: See TracBrowser for help on using the repository browser.