PageRenderTime 69ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/CompositionContainerCollectionTests.cs

https://bitbucket.org/danipen/mono
C# | 929 lines | 739 code | 178 blank | 12 comment | 0 complexity | 2569a4a79e98c9e6f4d6fd603fa802cf MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel.Composition;
  9. using System.Linq;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. using System.ComponentModel.Composition.Factories;
  12. using System.UnitTesting;
  13. using System.ComponentModel.Composition.UnitTesting;
  14. using System.ComponentModel.Composition.Hosting;
  15. namespace System.ComponentModel.Composition
  16. {
  17. [TestClass]
  18. public class CompositionContainerCollectionTests
  19. {
  20. public class SupportedImportCollectionAssignments<T>
  21. {
  22. // Fields
  23. [ImportMany("Value")]
  24. public IEnumerable<T> IEnumerableOfTField;
  25. [ImportMany("Value")]
  26. public IEnumerable<object> IEnumerableOfObjectField;
  27. [ImportMany("Value")]
  28. public T[] ArrayOfTField;
  29. [ImportMany("Value")]
  30. public object[] ArrayOfObjectField;
  31. [ImportMany("Value")]
  32. public IEnumerable<T> IEnumerableOfTProperty { get; set; }
  33. [ImportMany("Value")]
  34. public IEnumerable<object> IEnumerableOfObjectProperty { get; set; }
  35. [ImportMany("Value")]
  36. public T[] ArrayOfTProperty { get; set; }
  37. [ImportMany("Value")]
  38. public object[] ArrayOfObjectProperty { get; set; }
  39. public void VerifyImports(params T[] expectedValues)
  40. {
  41. // Fields
  42. EnumerableAssert.AreEqual(IEnumerableOfTField, expectedValues);
  43. EnumerableAssert.AreEqual(IEnumerableOfObjectField, expectedValues.Cast<object>());
  44. EnumerableAssert.AreEqual(ArrayOfTField, expectedValues);
  45. EnumerableAssert.AreEqual(ArrayOfObjectField, expectedValues.Cast<object>());
  46. // Properties
  47. EnumerableAssert.AreEqual(IEnumerableOfTProperty, expectedValues);
  48. EnumerableAssert.AreEqual(IEnumerableOfObjectProperty, expectedValues.Cast<object>());
  49. EnumerableAssert.AreEqual(ArrayOfTProperty, expectedValues);
  50. EnumerableAssert.AreEqual(ArrayOfObjectProperty, expectedValues.Cast<object>());
  51. }
  52. }
  53. [TestMethod]
  54. public void ValidateSupportedImportCollectionAssignments()
  55. {
  56. var container = ContainerFactory.Create();
  57. var batch = new CompositionBatch();
  58. var importer = new SupportedImportCollectionAssignments<int>();
  59. batch.AddPart(importer);
  60. batch.AddExportedValue("Value", 21);
  61. batch.AddExportedValue("Value", 32);
  62. batch.AddExportedValue("Value", 43);
  63. container.Compose(batch);
  64. importer.VerifyImports(21, 32, 43);
  65. }
  66. public class SupportedImportCollectionMutation<T>
  67. {
  68. public SupportedImportCollectionMutation()
  69. {
  70. ICollectionOfTReadOnlyField = new List<T>();
  71. ListOfTReadOnlyField = new List<T>();
  72. CollectionOfTField = new Collection<T>();
  73. CollectionOfTReadOnlyField = new Collection<T>();
  74. _iCollectionOfTReadOnlyProperty = new List<T>();
  75. _listOfTReadOnlyProperty = new List<T>();
  76. CollectionOfTProperty = new Collection<T>();
  77. _collectionOfTReadOnlyProperty = new Collection<T>();
  78. #if !SILVERLIGHT
  79. ObservableCollectionOfTReadOnlyField = new ObservableCollection<T>();
  80. _observableCollectionOfTReadOnlyProperty = new ObservableCollection<T>();
  81. #endif // !SILVERLIGHT
  82. }
  83. [ImportMany("Value")]
  84. public readonly ICollection<T> ICollectionOfTReadOnlyField;
  85. [ImportMany("Value")]
  86. public List<T> ListOfTField;
  87. [ImportMany("Value")]
  88. public readonly List<T> ListOfTReadOnlyField;
  89. [ImportMany("Value")]
  90. public Collection<T> CollectionOfTField;
  91. [ImportMany("Value")]
  92. public Collection<object> CollectionOfObjectField;
  93. [ImportMany("Value")]
  94. public readonly Collection<T> CollectionOfTReadOnlyField;
  95. [ImportMany("Value")]
  96. public ICollection<T> ICollectionOfTReadOnlyProperty { get { return _iCollectionOfTReadOnlyProperty; } }
  97. private ICollection<T> _iCollectionOfTReadOnlyProperty;
  98. [ImportMany("Value")]
  99. public List<T> ListOfTProperty { get; set; }
  100. [ImportMany("Value")]
  101. public List<T> ListOfTReadOnlyProperty { get { return _listOfTReadOnlyProperty; } }
  102. private readonly List<T> _listOfTReadOnlyProperty;
  103. [ImportMany("Value")]
  104. public Collection<T> CollectionOfTProperty { get; set; }
  105. [ImportMany("Value")]
  106. public Collection<T> CollectionOfTReadOnlyProperty { get { return _collectionOfTReadOnlyProperty; } }
  107. private readonly Collection<T> _collectionOfTReadOnlyProperty;
  108. #if !SILVERLIGHT
  109. [ImportMany("Value")]
  110. public ObservableCollection<T> ObservableCollectionOfTField;
  111. [ImportMany("Value")]
  112. public readonly ObservableCollection<T> ObservableCollectionOfTReadOnlyField;
  113. [ImportMany("Value")]
  114. public ObservableCollection<T> ObservableCollectionOfTProperty { get; set; }
  115. [ImportMany("Value")]
  116. public ObservableCollection<T> ObservableCollectionOfTReadOnlyProperty { get { return _observableCollectionOfTReadOnlyProperty; } }
  117. private readonly ObservableCollection<T> _observableCollectionOfTReadOnlyProperty;
  118. #endif // !SILVERLIGHT
  119. public void VerifyImports(params T[] expectedValues)
  120. {
  121. EnumerableAssert.AreEqual(ICollectionOfTReadOnlyField, expectedValues);
  122. EnumerableAssert.AreEqual(ListOfTField, expectedValues);
  123. EnumerableAssert.AreEqual(ListOfTReadOnlyField, expectedValues);
  124. EnumerableAssert.AreEqual(CollectionOfTField, expectedValues);
  125. EnumerableAssert.AreEqual(CollectionOfTReadOnlyField, expectedValues);
  126. EnumerableAssert.AreEqual(ICollectionOfTReadOnlyProperty, expectedValues);
  127. EnumerableAssert.AreEqual(ListOfTProperty, expectedValues);
  128. EnumerableAssert.AreEqual(ListOfTReadOnlyProperty, expectedValues);
  129. EnumerableAssert.AreEqual(CollectionOfTProperty, expectedValues);
  130. EnumerableAssert.AreEqual(CollectionOfTReadOnlyProperty, expectedValues);
  131. #if !SILVERLIGHT
  132. EnumerableAssert.AreEqual(ObservableCollectionOfTField, expectedValues);
  133. EnumerableAssert.AreEqual(ObservableCollectionOfTReadOnlyField, expectedValues);
  134. EnumerableAssert.AreEqual(ObservableCollectionOfTProperty, expectedValues);
  135. EnumerableAssert.AreEqual(ObservableCollectionOfTReadOnlyProperty, expectedValues);
  136. #endif // !SILVERLIGHT
  137. }
  138. }
  139. [TestMethod]
  140. public void ValidateSupportedImportCollectionMutation()
  141. {
  142. var container = ContainerFactory.Create();
  143. var batch = new CompositionBatch();
  144. var importer = new SupportedImportCollectionMutation<int>();
  145. batch.AddPart(importer);
  146. batch.AddExportedValue("Value", 21);
  147. batch.AddExportedValue("Value", 32);
  148. batch.AddExportedValue("Value", 43);
  149. container.Compose(batch);
  150. importer.VerifyImports(21, 32, 43);
  151. }
  152. public class ImportCollectionNullValue
  153. {
  154. [ImportMany("Value")]
  155. public List<int> NullValue { get; set; }
  156. }
  157. public class NamelessImporter
  158. {
  159. [ImportMany]
  160. public int[] ReadWriteIList { get; set; }
  161. [ImportMany]
  162. public Collection<Lazy<int>> ReadWriteMetadata { get; set; }
  163. }
  164. public class NamelessExporter
  165. {
  166. public NamelessExporter(int value)
  167. {
  168. Value = value;
  169. }
  170. [Export]
  171. public int Value { get; set; }
  172. }
  173. [TestMethod]
  174. public void ImportCollectionsNameless()
  175. {
  176. // Verifing that the contract name gets the correct value
  177. var container = ContainerFactory.Create();
  178. NamelessImporter importer = new NamelessImporter();
  179. NamelessExporter exporter42 = new NamelessExporter(42);
  180. NamelessExporter exporter0 = new NamelessExporter(0);
  181. CompositionBatch batch = new CompositionBatch();
  182. batch.AddParts(importer, exporter42, exporter0);
  183. container.Compose(batch);
  184. EnumerableAssert.AreEqual(importer.ReadWriteIList, 42, 0);
  185. }
  186. public class InvalidImporterReadOnlyEnumerable
  187. {
  188. IEnumerable<int> readOnlyEnumerable = new List<int>();
  189. [ImportMany("Value")]
  190. public IEnumerable<int> ReadOnlyEnumerable
  191. {
  192. get
  193. {
  194. return readOnlyEnumerable;
  195. }
  196. }
  197. }
  198. [TestMethod]
  199. public void ImportCollectionsExceptionReadOnlyEnumerable()
  200. {
  201. ExpectedErrorOnPartActivate(new InvalidImporterReadOnlyEnumerable(),
  202. ErrorId.ReflectionModel_ImportCollectionNotWritable);
  203. }
  204. public class ImporterWriteOnlyExportCollection
  205. {
  206. [ImportMany("Value")]
  207. public Collection<Lazy<int>> WriteOnlyExportCollection
  208. {
  209. set { PublicExportCollection = value; }
  210. }
  211. public Collection<Lazy<int>> PublicExportCollection { get; set; }
  212. }
  213. [TestMethod]
  214. public void ImportCollections_WriteOnlyExportCollection()
  215. {
  216. var container = ContainerFactory.Create();
  217. var batch = new CompositionBatch();
  218. var importer = new ImporterWriteOnlyExportCollection();
  219. List<int> values = new List<int>() { 21, 32, 43 };
  220. batch.AddPart(importer);
  221. values.ForEach(v => batch.AddExportedValue("Value", v));
  222. container.Compose(batch);
  223. EnumerableAssert.AreEqual(values, importer.PublicExportCollection.Select(export => export.Value));
  224. }
  225. public class ImporterWriteOnlyIEnumerableOfT
  226. {
  227. [ImportMany("Value")]
  228. public IEnumerable<int> WriteOnlyIEnumerable
  229. {
  230. set { PublicIEnumerable = value; }
  231. }
  232. public IEnumerable<int> PublicIEnumerable { get; set; }
  233. }
  234. [TestMethod]
  235. public void ImportCollections_WriteOnlyIEnumerableOfT()
  236. {
  237. var container = ContainerFactory.Create();
  238. var batch = new CompositionBatch();
  239. var importer = new ImporterWriteOnlyIEnumerableOfT();
  240. List<int> values = new List<int>() { 21, 32, 43 };
  241. batch.AddPart(importer);
  242. values.ForEach(v => batch.AddExportedValue("Value", v));
  243. container.Compose(batch);
  244. EnumerableAssert.AreEqual(values, importer.PublicIEnumerable);
  245. }
  246. public class ImporterWriteOnlyArray
  247. {
  248. [ImportMany("Value")]
  249. public int[] WriteOnlyArray
  250. {
  251. set { PublicArray = value; }
  252. }
  253. public int[] PublicArray { get; set; }
  254. }
  255. [TestMethod]
  256. public void ImportCollections_WriteOnlyArray()
  257. {
  258. var container = ContainerFactory.Create();
  259. var batch = new CompositionBatch();
  260. var importer = new ImporterWriteOnlyArray();
  261. List<int> values = new List<int>() { 21, 32, 43 };
  262. batch.AddPart(importer);
  263. values.ForEach(v => batch.AddExportedValue("Value", v));
  264. container.Compose(batch);
  265. EnumerableAssert.AreEqual(values, importer.PublicArray);
  266. }
  267. public class InvalidImporterNonCollection
  268. {
  269. [Import("Value")]
  270. public int Value { get; set; }
  271. }
  272. [TestMethod]
  273. public void ImportCollectionsExceptionNonCollection()
  274. {
  275. ExpectedChangeRejectedErrorOnSetImport(new InvalidImporterNonCollection(),
  276. ErrorId.ImportEngine_ImportCardinalityMismatch);
  277. }
  278. public class InvalidImporterNonAssignableCollection
  279. {
  280. [ImportMany("Value", typeof(int))]
  281. public IEnumerable<string> StringCollection { get; set; }
  282. }
  283. [TestMethod]
  284. public void ImportCollectionsExceptionNonAssignableCollection()
  285. {
  286. ExpectedErrorOnSetImport(new InvalidImporterNonAssignableCollection(),
  287. ErrorId.ReflectionModel_ImportNotAssignableFromExport);
  288. }
  289. public class InvalidImporterNullReadOnlyICollection
  290. {
  291. ICollection<int> readOnlyICollection = null;
  292. [ImportMany("Value")]
  293. public ICollection<int> Values { get { return readOnlyICollection; } }
  294. }
  295. [TestMethod]
  296. public void ImportCollectionsExceptionNullReadOnlyICollection()
  297. {
  298. ExpectedErrorOnPartActivate(new InvalidImporterNullReadOnlyICollection(),
  299. ErrorId.ReflectionModel_ImportCollectionNull);
  300. }
  301. public class ImporterWeakIEnumerable
  302. {
  303. public ImporterWeakIEnumerable()
  304. {
  305. ReadWriteEnumerable = new IntCollection();
  306. }
  307. [ImportMany("Value")]
  308. public IntCollection ReadWriteEnumerable { get; set; }
  309. public class IntCollection : IEnumerable
  310. {
  311. List<int> ints = new List<int>();
  312. public void Add(int item) { ints.Add(item); }
  313. public void Clear() { ints.Clear(); }
  314. public bool Remove(int item) { return ints.Remove(item); }
  315. public IEnumerator GetEnumerator() { return ints.GetEnumerator(); }
  316. }
  317. }
  318. [TestMethod]
  319. public void ImportCollectionsExceptionWeakCollectionNotSupportingICollectionOfT()
  320. {
  321. ExpectedErrorOnPartActivate(new ImporterWeakIEnumerable(),
  322. ErrorId.ReflectionModel_ImportCollectionNotWritable);
  323. }
  324. public class ImporterThrowsOnGetting
  325. {
  326. [ImportMany("Value")]
  327. public List<int> Value
  328. {
  329. get
  330. {
  331. throw new NotSupportedException();
  332. }
  333. }
  334. }
  335. [TestMethod]
  336. public void ImportCollectionsExceptionGettingValue()
  337. {
  338. var container = ContainerFactory.Create();
  339. ImporterThrowsOnGetting importer = new ImporterThrowsOnGetting();
  340. CompositionBatch batch = new CompositionBatch();
  341. batch.AddPart(importer);
  342. batch.AddExportedValue("Value", 42);
  343. batch.AddExportedValue("Value", 0);
  344. CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
  345. ErrorId.ReflectionModel_ImportCollectionGetThrewException, RetryMode.DoNotRetry, () =>
  346. {
  347. container.Compose(batch);
  348. });
  349. }
  350. public class CustomCollectionThrowsDuringConstruction : Collection<int>
  351. {
  352. public CustomCollectionThrowsDuringConstruction()
  353. {
  354. throw new NotSupportedException();
  355. }
  356. }
  357. public class ImportCustomCollectionThrowsDuringConstruction
  358. {
  359. public ImportCustomCollectionThrowsDuringConstruction()
  360. {
  361. }
  362. [ImportMany("Value")]
  363. public CustomCollectionThrowsDuringConstruction Values { get; set; }
  364. }
  365. [TestMethod]
  366. public void ImportCollections_ImportTypeThrowsOnConstruction()
  367. {
  368. ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringConstruction(),
  369. ErrorId.ReflectionModel_ImportCollectionConstructionThrewException);
  370. }
  371. public class CustomCollectionThrowsDuringClear : Collection<int>
  372. {
  373. protected override void ClearItems()
  374. {
  375. throw new NotSupportedException();
  376. }
  377. }
  378. public class ImportCustomCollectionThrowsDuringClear
  379. {
  380. public ImportCustomCollectionThrowsDuringClear()
  381. {
  382. }
  383. [ImportMany("Value")]
  384. public CustomCollectionThrowsDuringClear Values { get; set; }
  385. }
  386. [TestMethod]
  387. public void ImportCollections_ImportTypeThrowsOnClear()
  388. {
  389. ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringClear(),
  390. ErrorId.ReflectionModel_ImportCollectionClearThrewException);
  391. }
  392. public class CustomCollectionThrowsDuringAdd : Collection<int>
  393. {
  394. protected override void InsertItem(int index, int item)
  395. {
  396. throw new NotSupportedException();
  397. }
  398. }
  399. public class ImportCustomCollectionThrowsDuringAdd
  400. {
  401. public ImportCustomCollectionThrowsDuringAdd()
  402. {
  403. }
  404. [ImportMany("Value")]
  405. public CustomCollectionThrowsDuringAdd Values { get; set; }
  406. }
  407. [TestMethod]
  408. public void ImportCollections_ImportTypeThrowsOnAdd()
  409. {
  410. ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringAdd(),
  411. ErrorId.ReflectionModel_ImportCollectionAddThrewException);
  412. }
  413. public class CustomCollectionThrowsDuringIsReadOnly : ICollection<int>
  414. {
  415. void ICollection<int>.Add(int item)
  416. {
  417. throw new NotImplementedException();
  418. }
  419. void ICollection<int>.Clear()
  420. {
  421. throw new NotImplementedException();
  422. }
  423. bool ICollection<int>.Contains(int item)
  424. {
  425. throw new NotImplementedException();
  426. }
  427. void ICollection<int>.CopyTo(int[] array, int arrayIndex)
  428. {
  429. throw new NotImplementedException();
  430. }
  431. int ICollection<int>.Count
  432. {
  433. get { throw new NotImplementedException(); }
  434. }
  435. bool ICollection<int>.IsReadOnly
  436. {
  437. get { throw new NotSupportedException(); }
  438. }
  439. bool ICollection<int>.Remove(int item)
  440. {
  441. throw new NotImplementedException();
  442. }
  443. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  444. {
  445. throw new NotImplementedException();
  446. }
  447. IEnumerator IEnumerable.GetEnumerator()
  448. {
  449. throw new NotImplementedException();
  450. }
  451. }
  452. public class ImportCustomCollectionThrowsDuringIsReadOnly
  453. {
  454. public ImportCustomCollectionThrowsDuringIsReadOnly()
  455. {
  456. Values = new CustomCollectionThrowsDuringIsReadOnly();
  457. }
  458. [ImportMany("Value")]
  459. public CustomCollectionThrowsDuringIsReadOnly Values { get; set; }
  460. }
  461. [TestMethod]
  462. public void ImportCollections_ImportTypeThrowsOnIsReadOnly()
  463. {
  464. ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringIsReadOnly(),
  465. ErrorId.ReflectionModel_ImportCollectionIsReadOnlyThrewException);
  466. }
  467. public class CollectionTypeWithNoIList<T> : ICollection<T>
  468. {
  469. private int _count = 0;
  470. public CollectionTypeWithNoIList()
  471. {
  472. }
  473. public void Add(T item)
  474. {
  475. // Do Nothing
  476. this._count++;
  477. }
  478. public void Clear()
  479. {
  480. // Do Nothings
  481. }
  482. public bool Contains(T item)
  483. {
  484. throw new NotImplementedException();
  485. }
  486. public void CopyTo(T[] array, int arrayIndex)
  487. {
  488. throw new NotImplementedException();
  489. }
  490. public int Count
  491. {
  492. get { return this._count; }
  493. }
  494. public bool IsReadOnly
  495. {
  496. get { return false; }
  497. }
  498. public bool Remove(T item)
  499. {
  500. throw new NotImplementedException();
  501. }
  502. public IEnumerator<T> GetEnumerator()
  503. {
  504. throw new NotImplementedException();
  505. }
  506. IEnumerator IEnumerable.GetEnumerator()
  507. {
  508. throw new NotImplementedException();
  509. }
  510. }
  511. public class ImportCollectionWithNoIList
  512. {
  513. [ImportMany("Value")]
  514. public CollectionTypeWithNoIList<int> Values { get; set; }
  515. }
  516. [TestMethod]
  517. public void ImportCollections_NoIList_ShouldWorkFine()
  518. {
  519. var container = ContainerFactory.Create();
  520. var batch = new CompositionBatch();
  521. var importer = new ImportCollectionWithNoIList();
  522. batch.AddPart(importer);
  523. batch.AddExportedValue("Value", 42);
  524. batch.AddExportedValue("Value", 0);
  525. container.Compose(batch);
  526. Assert.AreEqual(2, importer.Values.Count);
  527. }
  528. public class CollectionWithMultipleInterfaces : ICollection<int>, ICollection<string>
  529. {
  530. private int _count = 0;
  531. public CollectionWithMultipleInterfaces()
  532. {
  533. }
  534. #region ICollection<int> Members
  535. void ICollection<int>.Add(int item)
  536. {
  537. throw new NotImplementedException();
  538. }
  539. void ICollection<int>.Clear()
  540. {
  541. throw new NotImplementedException();
  542. }
  543. bool ICollection<int>.Contains(int item)
  544. {
  545. throw new NotImplementedException();
  546. }
  547. void ICollection<int>.CopyTo(int[] array, int arrayIndex)
  548. {
  549. throw new NotImplementedException();
  550. }
  551. int ICollection<int>.Count
  552. {
  553. get { throw new NotImplementedException(); }
  554. }
  555. bool ICollection<int>.IsReadOnly
  556. {
  557. get { throw new NotImplementedException(); }
  558. }
  559. bool ICollection<int>.Remove(int item)
  560. {
  561. throw new NotImplementedException();
  562. }
  563. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  564. {
  565. throw new NotImplementedException();
  566. }
  567. #endregion
  568. #region IEnumerable Members
  569. IEnumerator IEnumerable.GetEnumerator()
  570. {
  571. throw new NotImplementedException();
  572. }
  573. #endregion
  574. #region ICollection<string> Members
  575. void ICollection<string>.Add(string item)
  576. {
  577. throw new NotImplementedException();
  578. }
  579. void ICollection<string>.Clear()
  580. {
  581. throw new NotImplementedException();
  582. }
  583. bool ICollection<string>.Contains(string item)
  584. {
  585. throw new NotImplementedException();
  586. }
  587. void ICollection<string>.CopyTo(string[] array, int arrayIndex)
  588. {
  589. throw new NotImplementedException();
  590. }
  591. int ICollection<string>.Count
  592. {
  593. get { throw new NotImplementedException(); }
  594. }
  595. bool ICollection<string>.IsReadOnly
  596. {
  597. get { throw new NotImplementedException(); }
  598. }
  599. bool ICollection<string>.Remove(string item)
  600. {
  601. throw new NotImplementedException();
  602. }
  603. IEnumerator<string> IEnumerable<string>.GetEnumerator()
  604. {
  605. throw new NotImplementedException();
  606. }
  607. #endregion
  608. }
  609. public class ImportCollectionWithMultipleInterfaces
  610. {
  611. [ImportMany("Value")]
  612. public CollectionWithMultipleInterfaces Values { get; set; }
  613. }
  614. [TestMethod]
  615. public void ImportCollections_MultipleICollections_ShouldCauseNotWriteable()
  616. {
  617. ExpectedErrorOnPartActivate(new ImportCollectionWithMultipleInterfaces(),
  618. ErrorId.ReflectionModel_ImportCollectionNotWritable);
  619. }
  620. public class ImportManyNonCollectionTypeString
  621. {
  622. [ImportMany("Value")]
  623. public string Foo { get; set; }
  624. }
  625. [TestMethod]
  626. public void ImportManyOnNonCollectionTypeString_ShouldCauseNotWritable()
  627. {
  628. ExpectedErrorOnPartActivate(new ImportManyNonCollectionTypeString(),
  629. ErrorId.ReflectionModel_ImportCollectionNotWritable);
  630. }
  631. public class ImportManyNonCollectionTypeObject
  632. {
  633. [ImportMany("Value")]
  634. public object Foo { get; set; }
  635. }
  636. [TestMethod]
  637. public void ImportManyOnNonCollectionTypeObject_ShouldCauseNotWritable()
  638. {
  639. ExpectedErrorOnPartActivate(new ImportManyNonCollectionTypeObject(),
  640. ErrorId.ReflectionModel_ImportCollectionNotWritable);
  641. }
  642. #if !SILVERLIGHT
  643. // SILVERLIGHT doesn't have SetEnvironmentVariable set in this test.
  644. public class ExportADictionaryObject
  645. {
  646. [Export]
  647. public IDictionary<string, object> MyDictionary
  648. {
  649. get
  650. {
  651. var dictionary = new Dictionary<string, object>();
  652. dictionary.Add("a", 42);
  653. dictionary.Add("b", "c");
  654. return dictionary;
  655. }
  656. }
  657. }
  658. public class ImportADictionaryObject
  659. {
  660. [Import]
  661. public IDictionary<string, object> MyDictionary { get; set; }
  662. }
  663. [TestMethod]
  664. public void ImportDictionaryAsSingleObject()
  665. {
  666. // Set variable to ensure the hack is turned off.
  667. Environment.SetEnvironmentVariable("ONLY_ALLOW_IMPORTMANY", "1");
  668. var container = ContainerFactory.Create();
  669. var batch = new CompositionBatch();
  670. var importer = new ImportADictionaryObject();
  671. var exporter = new ExportADictionaryObject();
  672. batch.AddPart(importer);
  673. batch.AddPart(exporter);
  674. container.Compose(batch);
  675. Assert.AreEqual(2, importer.MyDictionary.Count);
  676. }
  677. public class ExportACollectionObject
  678. {
  679. [Export]
  680. public Collection<string> MyCollection
  681. {
  682. get
  683. {
  684. var collection = new Collection<string>();
  685. collection.Add("a");
  686. collection.Add("b");
  687. return collection;
  688. }
  689. }
  690. }
  691. public class ImportACollectionObject
  692. {
  693. [Import]
  694. public Collection<string> MyCollection { get; set; }
  695. }
  696. [TestMethod]
  697. public void ImportCollectionAsSingleObject()
  698. {
  699. // Set variable to ensure the hack is turned off.
  700. Environment.SetEnvironmentVariable("ONLY_ALLOW_IMPORTMANY", "1");
  701. var container = ContainerFactory.Create();
  702. var batch = new CompositionBatch();
  703. var importer = new ImportACollectionObject();
  704. var exporter = new ExportACollectionObject();
  705. batch.AddPart(importer);
  706. batch.AddPart(exporter);
  707. container.Compose(batch);
  708. Assert.AreEqual(2, importer.MyCollection.Count);
  709. }
  710. #endif
  711. public void ExpectedErrorOnPartActivate(object importer, ErrorId expectedErrorId)
  712. {
  713. var container = ContainerFactory.Create();
  714. var batch = new CompositionBatch();
  715. batch.AddPart(importer);
  716. batch.AddExportedValue("Value", 42);
  717. batch.AddExportedValue("Value", 0);
  718. CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
  719. expectedErrorId, RetryMode.DoNotRetry, () =>
  720. {
  721. container.Compose(batch);
  722. });
  723. }
  724. public void ExpectedErrorOnSetImport(object importer, ErrorId expectedErrorId)
  725. {
  726. var container = ContainerFactory.Create();
  727. var batch = new CompositionBatch();
  728. batch.AddPart(importer);
  729. batch.AddExportedValue("Value", 42);
  730. batch.AddExportedValue("Value", 0);
  731. CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
  732. expectedErrorId, RetryMode.DoNotRetry, () =>
  733. {
  734. container.Compose(batch);
  735. });
  736. }
  737. public void ExpectedChangeRejectedErrorOnSetImport(object importer, ErrorId expectedErrorId)
  738. {
  739. var container = ContainerFactory.Create();
  740. var batch = new CompositionBatch();
  741. batch.AddPart(importer);
  742. batch.AddExportedValue("Value", 42);
  743. batch.AddExportedValue("Value", 0);
  744. CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport,
  745. expectedErrorId, RetryMode.DoNotRetry, () =>
  746. {
  747. container.Compose(batch);
  748. });
  749. }
  750. }
  751. }