Exemplo n.º 1
0
        public void PreviewImports_ReleaseImports_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value");
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 21);

            engine.PreviewImports(importer, null);

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.AddExport("Value", 22));

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.RemoveExport("Value"));

            engine.ReleaseImports(importer, null);

            exportProvider.AddExport("Value", 22);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 2
0
        public void SatisfyImports_OneRecomposable_OneNotRecomposable()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import1  = ImportDefinitionFactory.Create("Value", true);
            var import2  = ImportDefinitionFactory.Create("Value", false);
            var importer = PartFactory.CreateImporter(import1, import2);

            exportProvider.AddExport("Value", 21);

            engine.SatisfyImports(importer);

            // Initial compose values should be 21
            Assert.Equal(21, importer.GetImport(import1));
            Assert.Equal(21, importer.GetImport(import2));

            // Reset value to ensure it doesn't get set to same value again
            importer.ResetImport(import1);
            importer.ResetImport(import2);

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.ReplaceExportValue("Value", 42));

            Assert.Equal(null, importer.GetImport(import1));
            Assert.Equal(null, importer.GetImport(import2));

            GC.KeepAlive(importer);
        }
Exemplo n.º 3
0
        public void SatisfyImports_Recomposable_Unregister_ValueShouldChangeOnce()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            exportProvider.AddExport("Value", 21);

            var import   = ImportDefinitionFactory.Create("Value", true);
            var importer = PartFactory.CreateImporter(import);

            engine.SatisfyImports(importer);

            Assert.Equal(21, importer.GetImport(import));

            exportProvider.ReplaceExportValue("Value", 42);

            Assert.Equal(42, importer.GetImport(import));

            engine.ReleaseImports(importer, null);

            exportProvider.ReplaceExportValue("Value", 666);

            Assert.Equal(42, importer.GetImport(import));

            GC.KeepAlive(importer);
        }
Exemplo n.º 4
0
        public void SatisfyImports_TwoRecomposables_SingleExportValueChanged()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import1  = ImportDefinitionFactory.Create("Value1", true);
            var import2  = ImportDefinitionFactory.Create("Value2", true);
            var importer = PartFactory.CreateImporter(import1, import2);

            exportProvider.AddExport("Value1", 21);
            exportProvider.AddExport("Value2", 23);

            engine.SatisfyImports(importer);

            Assert.Equal(21, importer.GetImport(import1));
            Assert.Equal(23, importer.GetImport(import2));

            importer.ResetImport(import1);
            importer.ResetImport(import2);

            // Only change Value1
            exportProvider.ReplaceExportValue("Value1", 42);

            Assert.Equal(42, importer.GetImport(import1));

            Assert.Equal(null, importer.GetImport(import2));

            GC.KeepAlive(importer);
        }
Exemplo n.º 5
0
        public void GetExports_WhenCatalogDisposed_ShouldThrowObjectDisposed()
        {
            var catalog = CreateAssemblyCatalog();

            catalog.Dispose();
            var definition = ImportDefinitionFactory.Create();

            ExceptionAssert.ThrowsDisposed(catalog, () => catalog.GetExports(definition));
        }
        public void SetImport_WrongDefinitionAsDefinitionArgument_ShouldThrowArgument()
        {
            var part = CreateDefaultPart();

            var definition = ImportDefinitionFactory.Create();

            Assert.Throws <ArgumentException>("definition", () =>
            {
                part.SetImport(definition, Enumerable.Empty <Export>());
            });
        }
Exemplo n.º 7
0
        public void AddExportedValueOfT_ReturnedComposablePart_ExportsArrayWithNullElementAsExportsArgumentToSetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddExportedValue <string>("Value");
            var definition = ImportDefinitionFactory.Create();

            Assert.Throws <ArgumentException>("exports", () =>
            {
                part.SetImport(definition, new Export[] { null });
            });
        }
Exemplo n.º 8
0
        public void PreviewImports_ZeroCollectionImport_ShouldSucceed()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", ImportCardinality.ZeroOrMore);
            var importer = PartFactory.CreateImporter(import);

            engine.PreviewImports(importer, null);

            GC.KeepAlive(importer);
        }
        public void CreateParsesHeaderRowTrue()
        {
            // Arrange
            var input = "TABLENAME customTableName;\nHEADERROW;\nCOLUMN 0 MyColumnName STRING;";

            // Act
            var id = ImportDefinitionFactory.Create(input);

            // Assert
            Assert.IsTrue(id.IsValid());
            Assert.IsTrue(id.Definition.HeaderRow);
        }
Exemplo n.º 10
0
        public void AddExportedValueOfT_ReturnedComposablePart_SetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddExportedValue <string>("Value");
            var definition = ImportDefinitionFactory.Create();

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Throws <ArgumentException>("definition", () =>
            {
                part.SetImport(definition, Enumerable.Empty <Export>());
            });
        }
Exemplo n.º 11
0
        public void AddPart_ReturnedComposablePart_NullAsExportsArgumentToSetImports_ShouldThrowArgumentNull()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddPart(new Int32Importer());
            var definition = ImportDefinitionFactory.Create();

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Throws <ArgumentNullException>("exports", () =>
            {
                part.SetImport(definition, (IEnumerable <Export>)null);
            });
        }
Exemplo n.º 12
0
        public void AddPart_ReturnedComposablePart_WrongDefinitionAsDefinitionArgumentToSetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddPart(new Int32Importer());
            var definition = ImportDefinitionFactory.Create();

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Throws <ArgumentException>("definition", () =>
            {
                part.SetImport(definition, Enumerable.Empty <Export>());
            });
        }
Exemplo n.º 13
0
        public void AddExportedValue_ReturnedComposablePart_ExportsArrayWithNullElementAsExportsArgumentToSetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddExportedValue("Contract", "Value");
            var definition = ImportDefinitionFactory.Create();

            Assert.AreEqual(1, batch.PartsToAdd.Count);

            ExceptionAssert.ThrowsArgument <ArgumentException>("exports", () =>
            {
                part.SetImport(definition, new Export[] { null });
            });
        }
Exemplo n.º 14
0
        public void AddExportedValueOfT_ReturnedComposablePart_NullAsExportsArgumentToSetImports_ShouldThrowArgumentNull()
        {
            CompositionBatch batch = new CompositionBatch();

            var part       = batch.AddExportedValue <string>("Value");
            var definition = ImportDefinitionFactory.Create();

            Assert.AreEqual(1, batch.PartsToAdd.Count);

            ExceptionAssert.ThrowsArgument <ArgumentNullException>("exports", () =>
            {
                part.SetImport(definition, (IEnumerable <Export>)null);
            });
        }
Exemplo n.º 15
0
        public void AddExport_ReturnedComposablePart_SetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            var part       = batch.AddExport(export);
            var definition = ImportDefinitionFactory.Create();

            Assert.AreEqual(1, batch.PartsToAdd.Count);

            ExceptionAssert.ThrowsArgument <ArgumentException>("definition", () =>
            {
                part.SetImport(definition, Enumerable.Empty <Export>());
            });
        }
 private void CreateThrowsExceptionOnNullEquivalent(string definition)
 {
     try
     {
         var id = ImportDefinitionFactory.Create(definition);
         Assert.Fail("Expected exception, not thrown.");
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("ImportDefinitionString", ex.ParamName);
     }
     catch (Exception ex)
     {
         Assert.Fail("Expected ArgumentNullException, " + ex.GetType().Name + " thrown instead.");
     }
 }
Exemplo n.º 17
0
        public void PreviewImports_ZeroCollectionImport_NonRecomposable_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", ImportCardinality.ZeroOrMore, false, false);
            var importer = PartFactory.CreateImporter(import);

            engine.PreviewImports(importer, null);

            exportProvider.AddExport("Value", 21);
            exportProvider.AddExport("Value", 22);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 18
0
        public void GetExports_WhenCatalogDisposed_ShouldThrowObjectDisposed()
        {
            ExceptionAssert.Throws <ObjectDisposedException>(RetryMode.DoNotRetry, () =>
            {
                using (var app = new Application())
                {
                    app.AppMain(() =>
                    {
                        var catalog = new ApplicationCatalog();
                        catalog.Dispose();

                        var definition = ImportDefinitionFactory.Create();
                        catalog.GetExports(definition);
                    });
                }
            });
        }
Exemplo n.º 19
0
        public void SatisfyImportsOnce_Unsuccessful_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value");
            var importer = PartFactory.CreateImporter(import);

            Assert.Throws <CompositionException>(() =>
                                                 engine.SatisfyImportsOnce(importer));

            exportProvider.AddExport("Value", 22);
            exportProvider.AddExport("Value", 23);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 20
0
        public static ImportDefinition CreateImportDefinition()
        {
            var sb = new StringBuilder();

            sb.AppendLine("TABLENAME TestTable;")
            .AppendLine("COLUMN 0 ABoolean BOOLEAN;")
            .AppendLine("COLUMN 1 AByte BYTE;")
            .AppendLine("COLUMN 2 ACharacter CHAR;")
            .AppendLine("COLUMN 3 ADateTime DATETIME;")
            .AppendLine("COLUMN 4 ADecimal DECIMAL;")
            .AppendLine("COLUMN 5 ADouble DOUBLE;")
            .AppendLine("COLUMN 6 AGuid GUID;")
            .AppendLine("COLUMN 7 AnInt16 INT16;")
            .AppendLine("COLUMN 8 AnInt32 INT32;")
            .AppendLine("COLUMN 9 AnInt64 INT64;")
            .AppendLine("COLUMN 10 AString STRING;")
            .AppendLine("DESTINATION DestinationBoolean BOOLEAN SET {true};")
            .AppendLine("DESTINATION DestinationByte BYTE SET {254};")
            .AppendLine("DESTINATION DestinationChar CHAR SET {a};")
            .AppendLine("DESTINATION DestinationDateTime DATETIME SET {2018-01-01T05:00:00Z};")
            .AppendLine("DESTINATION DestinationDecimal DECIMAL SET {123.456};")
            .AppendLine("DESTINATION DestinationDouble DOUBLE SET {1.23};")
            .AppendLine("DESTINATION DestinationGuid GUID SET {4732B277-11B3-440D-9327-1E2E6613746C};")
            .AppendLine("DESTINATION DestinationINT16 INT16 SET {32767};")
            .AppendLine("DESTINATION DestinationINT32 INT32 SET {2147483647};")
            .AppendLine("DESTINATION DestinationINT64 INT64 SET {9223372036854775807};")
            .AppendLine("DESTINATION DestinationString STRING SET {This is a string.};")
            .AppendLine("DESTINATION DestinationSubstituteBoolean BOOLEAN SUBSTITUTE {boolean};")
            .AppendLine("DESTINATION DestinationSubstituteByte BYTE SUBSTITUTE {byte};")
            .AppendLine("DESTINATION DestinationSubstituteChar CHAR SUBSTITUTE {char};")
            .AppendLine("DESTINATION DestinationSubstituteDateTime DATETIME SUBSTITUTE {datetime};")
            .AppendLine("DESTINATION DestinationSubstituteDecimal DECIMAL SUBSTITUTE {decimal};")
            .AppendLine("DESTINATION DestinationSubstituteDouble DOUBLE SUBSTITUTE {double};")
            .AppendLine("DESTINATION DestinationSubstituteGuid GUID SUBSTITUTE {guid};")
            .AppendLine("DESTINATION DestinationSubstituteInt16 INT16 SUBSTITUTE {int16};")
            .AppendLine("DESTINATION DestinationSubstituteInt32 INT32 SUBSTITUTE {int32};")
            .AppendLine("DESTINATION DestinationSubstituteInt64 INT64 SUBSTITUTE {int64};")
            .AppendLine("DESTINATION DestinationSubstituteString STRING SUBSTITUTE {string};")
            .AppendLine("DESTINATION DestinationGenerateGuid GUID GENERATE;")
            .AppendLine("DESTINATION DestinationGenerateDateOnly DATETIME GENERATE DATEONLY;")
            .AppendLine("DESTINATION DestinationGenerateDateTime DATETIME GENERATE;");
            var importDefinitionString = sb.ToString();
            var id = ImportDefinitionFactory.Create(importDefinitionString);

            return(id.Definition);
        }
Exemplo n.º 21
0
        public void SatisfyImports_MissingOptionalImport_Recomposable_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", ImportCardinality.ZeroOrOne, true, false);
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 20);

            engine.SatisfyImports(importer);

            exportProvider.AddExport("Value", 21);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 22
0
        public ImportingComposablePart(string exportContractName, ImportCardinality cardinality, bool isRecomposable, params string[] contractNames)
        {
            if (exportContractName != null)
            {
                var definition = ExportDefinitionFactory.Create(exportContractName);

                _exportDefinitions.Add(definition);
            }

            foreach (string contractName in contractNames)
            {
                var definition = ImportDefinitionFactory.Create(contractName,
                                                                cardinality,
                                                                isRecomposable,
                                                                false);

                _importDefinitions.Add(definition);
            }
        }
Exemplo n.º 23
0
        private ImportDefinition CreateImportDefinition()
        {
            var sb = new StringBuilder();

            sb.AppendLine("TABLE SomeTableName;");
            sb.AppendLine("COLUMN 0 Boolean BOOLEAN;");
            sb.AppendLine("COLUMN 1 Byte BYTE;");
            sb.AppendLine("COLUMN 2 Char CHAR;");
            sb.AppendLine("COLUMN 3 Date DATETIME;");
            sb.AppendLine("COLUMN 4 Decimal DECIMAL;");
            sb.AppendLine("COLUMN 5 Double DOUBLE;");
            sb.AppendLine("COLUMN 6 UniqueIdentifier GUID;");
            sb.AppendLine("COLUMN 7 16-bit-Integer INT16;");
            sb.AppendLine("COLUMN 8 32-bit-Integer INT32;");
            sb.AppendLine("COLUMN 9 64-bit-Integer INT64;");
            sb.AppendLine("COLUMN 10 String STRING;");

            return(ImportDefinitionFactory.Create(sb.ToString()).Definition);
        }
Exemplo n.º 24
0
        public void PreviewImports_Unsuccessful_AtomicComposition_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value");
            var importer = PartFactory.CreateImporter(import);

            using (var atomicComposition = new AtomicComposition())
            {
                Assert.Throws <ChangeRejectedException>(() =>
                                                        engine.PreviewImports(importer, atomicComposition));
            }

            exportProvider.AddExport("Value", 22);
            exportProvider.AddExport("Value", 23);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 25
0
        public void SatisifyImportsOnce_Recomposable_ValueShouldNotChange_NoRecompositionRequested_ViaNonArgumentSignature()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            exportProvider.AddExport("Value", 21);

            var import   = ImportDefinitionFactory.Create("Value", true);
            var importer = PartFactory.CreateImporter(import);

            engine.SatisfyImportsOnce(importer);

            Assert.Equal(21, importer.GetImport(import));

            exportProvider.ReplaceExportValue("Value", 42);

            Assert.Equal(21, importer.GetImport(import));

            GC.KeepAlive(importer);
        }
Exemplo n.º 26
0
        public void SatisfyImports_ZeroCollectionImport_NonRecomposable_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", ImportCardinality.ZeroOrMore, false, false);
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 20);

            engine.SatisfyImports(importer);

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.AddExport("Value", 21));

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.RemoveExport("Value"));

            GC.KeepAlive(importer);
        }
Exemplo n.º 27
0
        public void SatisfyImports_Recomposable_Prerequisite_ValueShouldChange()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", true, true);
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 21);

            engine.SatisfyImports(importer);

            Assert.AreEqual(21, importer.GetImport(import));

            exportProvider.ReplaceExportValue("Value", 42);

            Assert.AreEqual(42, importer.GetImport(import), "Value should change!");

            GC.KeepAlive(importer);
        }
Exemplo n.º 28
0
        public void SatisfyImports_NonRecomposable_Prerequisite_ValueShouldNotChange()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value", false, true);
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 21);

            engine.SatisfyImports(importer);

            Assert.Equal(21, importer.GetImport(import));

            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.ReplaceExportValue("Value", 42));

            Assert.Equal(21, importer.GetImport(import));

            GC.KeepAlive(importer);
        }
Exemplo n.º 29
0
        public void PreviewImports_Successful_AtomicComposition_RolledBack_ShouldNotBlockChanges()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            var import   = ImportDefinitionFactory.Create("Value");
            var importer = PartFactory.CreateImporter(import);

            exportProvider.AddExport("Value", 21);

            using (var atomicComposition = new AtomicComposition())
            {
                engine.PreviewImports(importer, atomicComposition);

                // Let atomicComposition get disposed thus rolledback
            }

            exportProvider.AddExport("Value", 22);
            exportProvider.RemoveExport("Value");

            GC.KeepAlive(importer);
        }
Exemplo n.º 30
0
        public void SatisfyImports_NonRecomposable_ValueShouldNotChange()
        {
            var exportProvider = ExportProviderFactory.CreateRecomposable();
            var engine         = new ImportEngine(exportProvider);

            exportProvider.AddExport("Value", 21);

            var import   = ImportDefinitionFactory.Create("Value", false);
            var importer = PartFactory.CreateImporter(import);

            engine.SatisfyImports(importer);

            Assert.Equal(21, importer.GetImport(import));

            // After rejection batch failures throw ChangeRejectedException to indicate that
            // the failure did not affect the container
            Assert.Throws <ChangeRejectedException>(() =>
                                                    exportProvider.ReplaceExportValue("Value", 42));

            Assert.Equal(21, importer.GetImport(import));

            GC.KeepAlive(importer);
        }