Exemplo n.º 1
0
 public void ExportConfiguration_ThrowsOnBadInput()
 {
     Assert.Throws <ArgumentNullException>(() => DictionaryConfigurationManagerController.ExportConfiguration(null, "a", Cache));
     Assert.Throws <ArgumentNullException>(() => DictionaryConfigurationManagerController.ExportConfiguration(_configurations[0], null, Cache));
     Assert.Throws <ArgumentNullException>(() => DictionaryConfigurationManagerController.ExportConfiguration(_configurations[0], "a", null));
     Assert.Throws <ArgumentNullException>(() => DictionaryConfigurationManagerController.ExportConfiguration(null, null, null));
     // Empty string
     Assert.Throws <ArgumentException>(() => DictionaryConfigurationManagerController.ExportConfiguration(_configurations[0], "", Cache));
 }
Exemplo n.º 2
0
        public void ExportConfiguration_ExportsZip()
        {
            // Writing to disk, not just in memory, so can use zip library.

            FileUtils.Manager.Reset();
            string expectedZipOutput = null;

            try
            {
                var configurationToExport = _configurations[0];
                DictionaryConfigurationManagerController.GenerateFilePath(_controller._projectConfigDir, _controller._configurations, configurationToExport);
                var pathToConfiguration = configurationToExport.FilePath;
                expectedZipOutput = Path.GetTempFileName();
                File.WriteAllText(pathToConfiguration, "file contents");
                Assert.That(File.Exists(pathToConfiguration), "Unit test not set up right");
                Assert.That(new FileInfo(expectedZipOutput).Length, Is.EqualTo(0),
                            "Unit test not set up right. File will exist for convenience of writing the test but should not have any content yet.");

                // SUT
                DictionaryConfigurationManagerController.ExportConfiguration(configurationToExport, expectedZipOutput, Cache);

                Assert.That(File.Exists(expectedZipOutput), "File not exported");
                Assert.That(new FileInfo(expectedZipOutput).Length, Is.GreaterThan(0),
                            "Exported file should have content");

                using (var zip = new ZipFile(expectedZipOutput))
                {
                    Assert.That(zip.Count, Is.GreaterThanOrEqualTo(3), "Zip file must be missing parts of the export");
                }
            }
            finally
            {
                if (expectedZipOutput != null)
                {
                    File.Delete(expectedZipOutput);
                }
                FileUtils.Manager.SetFileAdapter(_mockFilesystem);
            }
        }
Exemplo n.º 3
0
        public void Setup()
        {
            // Start out with a clean project configuration directory, and with a non-random name so it's easier to examine during testing.
            _projectConfigPath = Path.Combine(Path.GetTempPath(), "Dictionary");
            if (Directory.Exists(_projectConfigPath))
            {
                Directory.Delete(_projectConfigPath, true);
            }
            FileUtils.EnsureDirectoryExists(_projectConfigPath);

            _reversalProjectConfigPath = Path.Combine(Path.GetTempPath(), "ReversalIndex");
            if (Directory.Exists(_reversalProjectConfigPath))
            {
                Directory.Delete(_reversalProjectConfigPath, true);
            }
            FileUtils.EnsureDirectoryExists(_reversalProjectConfigPath);

            _controller = new DictionaryConfigurationImportController(Cache, _projectConfigPath,
                                                                      new List <DictionaryConfigurationModel>());

            _reversalController = new DictionaryConfigurationImportController(Cache, _reversalProjectConfigPath,
                                                                              new List <DictionaryConfigurationModel>());

            // Set up data for import testing.

            _zipFile         = null;
            _reversalZipFile = null;

            // Prepare configuration to export

            var configurationToExport = new DictionaryConfigurationModel
            {
                Label        = configLabel,
                Publications = new List <string> {
                    "Main Dictionary", "unknown pub 1", "unknown pub 2"
                },
                Parts = new List <ConfigurableDictionaryNode> {
                    new ConfigurableDictionaryNode {
                        FieldDescription = "LexEntry"
                    }
                },
                FilePath = Path.GetTempPath() + configFilename
            };

            CssGeneratorTests.PopulateFieldsForTesting(configurationToExport);

            _pathToConfiguration = configurationToExport.FilePath;

            // Create XML file
            configurationToExport.Save();

            // Prepare configuration to export
            var configurationReversalToExport = new DictionaryConfigurationModel
            {
                Label         = reversalConfigLabel,
                WritingSystem = "en",
                Publications  = new List <string> {
                    "Main Dictionary", "unknown pub 1", "unknown pub 2"
                },
                Parts = new List <ConfigurableDictionaryNode> {
                    new ConfigurableDictionaryNode {
                        FieldDescription = "LexEntry"
                    }
                },
                FilePath = Path.GetTempPath() + reversalConfigFilename
            };

            CssGeneratorTests.PopulateFieldsForTesting(configurationReversalToExport);
            _reversalPathToConfiguration = configurationReversalToExport.FilePath;
            configurationReversalToExport.Save();

            // Export a configuration that we know how to import

            _zipFile         = Path.GetTempFileName();
            _reversalZipFile = Path.GetTempFileName() + 1;

            // Add a test style to the cache
            NonUndoableUnitOfWorkHelper.Do(Cache.ActionHandlerAccessor, () =>
            {
                var styleFactory = Cache.ServiceLocator.GetInstance <IStStyleFactory>();

                styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Headword",
                                    ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, true, 2, true);
                var testStyle = styleFactory.Create(Cache.LangProject.StylesOC, "TestStyle", ContextValues.InternalConfigureView, StructureValues.Undefined,
                                                    FunctionValues.Line, true, 2, false);
                testStyle.Usage.set_String(Cache.DefaultAnalWs, "Test Style");
                var normalStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Normal", ContextValues.InternalConfigureView, StructureValues.Undefined,
                                                      FunctionValues.Line, false, 2, true);
                var propsBldr = TsPropsBldrClass.Create();
                propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, 0x2BACCA);                 // arbitrary color to create para element
                normalStyle.Rules        = propsBldr.GetTextProps();
                var styleWithNamedColors = styleFactory.Create(Cache.LangProject.StylesOC, "Nominal", ContextValues.InternalConfigureView, StructureValues.Undefined,
                                                               FunctionValues.Line, false, 2, false);
                styleWithNamedColors.BasedOnRA = normalStyle;
                propsBldr = TsPropsBldrClass.Create();
                propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, NamedRedBGR);
                propsBldr.SetIntPropValues((int)FwTextPropType.ktptForeColor, (int)FwTextPropVar.ktpvDefault, NamedRedBGR);
                styleWithNamedColors.Rules = propsBldr.GetTextProps();
                var styleWithCustomColors  = styleFactory.Create(Cache.LangProject.StylesOC, "Abnormal", ContextValues.InternalConfigureView, StructureValues.Undefined,
                                                                 FunctionValues.Line, false, 2, false);
                styleWithCustomColors.BasedOnRA = normalStyle;
                propsBldr = TsPropsBldrClass.Create();
                propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, CustomRedBGR);
                propsBldr.SetIntPropValues((int)FwTextPropType.ktptForeColor, (int)FwTextPropVar.ktpvDefault, CustomRedBGR);
                styleWithCustomColors.Rules = propsBldr.GetTextProps();
                DictionaryConfigurationManagerController.ExportConfiguration(configurationToExport, _zipFile, Cache);
                DictionaryConfigurationManagerController.ExportConfiguration(configurationReversalToExport, _reversalZipFile, Cache);
                Cache.LangProject.StylesOC.Clear();
            });
            Assert.That(File.Exists(_zipFile), "Unit test not set up right");
            Assert.That(new FileInfo(_zipFile).Length, Is.GreaterThan(0), "Unit test not set up right");

            // Clear the configuration away so we can see it gets imported

            File.Delete(_pathToConfiguration);
            Assert.That(!File.Exists(_pathToConfiguration),
                        "Unit test not set up right. Configuration should be out of the way for testing export.");
            Assert.That(_controller._configurations.All(config => config.Label != configLabel),
                        "Unit test not set up right. A config exists with the same label as the config we will import.");
            Assert.That(_controller._configurations.All(config => config.Label != configLabel),
                        "Unit test set up unexpectedly. Such a config should not be registered.");
            File.Delete(_reversalPathToConfiguration);
            Assert.That(!File.Exists(_reversalPathToConfiguration),
                        "Unit test not set up right. Reversal configuration should be out of the way for testing export.");
            Assert.That(_reversalController._configurations.All(config => config.Label != configLabel),
                        "Unit test not set up right. A reversal config exists with the same label as the reversal config we will import.");
            Assert.That(_reversalController._configurations.All(config => config.Label != configLabel),
                        "Unit test set up unexpectedly. Such a reversal config should not be registered.");
        }
Exemplo n.º 4
0
        public void PrepareImport_DoImport_CreatesCustomFieldsFromExportResult()
        {
            var customFieldSameLabel = "KeepMeImGood";
            var customFieldLabel     = "TempCustomField";
            var customFieldWrongType = "WrongTypeField";
            var zipFile = Path.GetTempFileName();

            using (new CustomFieldForTest(Cache, customFieldSameLabel, customFieldSameLabel, LexSenseTags.kClassId, StTextTags.kClassId, -1,
                                          CellarPropertyType.OwningAtomic, Guid.Empty))
            {
                using (new CustomFieldForTest(Cache, customFieldLabel, customFieldLabel, LexEntryTags.kClassId, StTextTags.kClassId, -1,
                                              CellarPropertyType.OwningAtomic, Guid.Empty))
                    using (new CustomFieldForTest(Cache, customFieldWrongType, customFieldWrongType, LexEntryTags.kClassId, CmPossibilityTags.kClassId, -1,
                                                  CellarPropertyType.ReferenceAtom, Cache.LangProject.LexDbOA.ComplexEntryTypesOA.Guid))
                    {
                        var importExportDCModel = new DictionaryConfigurationModel
                        {
                            Label        = "importexportConfiguration",
                            Publications = new List <string>(),
                            FilePath     = Path.GetTempPath() + "importexportConfigurationFile.fwdictconfig",
                        };

                        var configurationToExport = importExportDCModel;
                        _pathToConfiguration = configurationToExport.FilePath;
                        const string XmlOpenTagsThruRoot  = @"<?xml version=""1.0"" encoding=""utf-8""?>
			<DictionaryConfiguration name=""Root"" allPublications=""true"" isRootBased=""true"" version=""1"" lastModified=""2014-02-13"">"            ;
                        const string XmlCloseTagsFromRoot = @"</DictionaryConfiguration>";
                        const string XmlTagsHeaword       = @"<ConfigurationItem name=""Main Entry"" isEnabled=""true"" field=""LexEntry"">\r\n\t\t\t\t\t<ConfigurationItem name=""Testword"" nameSuffix=""2b"" before=""["" between="", "" after=""] "" style=""Dictionary-Headword"" isEnabled=""true"" field=""HeadWord"">""\r\n\r\n\r\n\t\t\t\t\t</ConfigurationItem>\r\n\t\t\t\t</ConfigurationItem>\r\n\t\t\t\t<SharedItems/>";
                        const string XmlTagsCustomField   = @" <ConfigurationItem name = ""CustomField1"" isEnabled=""true"" isCustomField=""true"" before="" "" field=""OwningEntry"" subField=""CustomField1"" />";
                        File.WriteAllText(_pathToConfiguration,
                                          XmlOpenTagsThruRoot + XmlTagsHeaword + XmlTagsCustomField + XmlCloseTagsFromRoot);
                        // This export should create the zipfile containing the custom field information (currently in LIFT format)
                        DictionaryConfigurationManagerController.ExportConfiguration(configurationToExport, zipFile, Cache);
                    }             // Destroy two of the custom fields and verify the state
                VerifyCustomFieldPresent(customFieldSameLabel, LexSenseTags.kClassId, StTextTags.kClassId, CellarPropertyType.OwningAtomic);
                VerifyCustomFieldAbsent(customFieldLabel, LexEntryTags.kClassId);
                VerifyCustomFieldAbsent(customFieldWrongType, LexEntryTags.kClassId);
                // Re-introduce one of the fields that was in the export, but with a different type
                using (new CustomFieldForTest(Cache, customFieldWrongType, customFieldWrongType, LexEntryTags.kClassId, StTextTags.kClassId, -1,
                                              CellarPropertyType.OwningAtomic, Guid.Empty))
                {
                    // SUT
                    _controller.PrepareImport(zipFile);
                    // Verify prepare import counted the custom fields
                    CollectionAssert.IsNotEmpty(_controller._customFieldsToImport, "No custom fields found in the lift file by PrepareImport");
                    CollectionAssert.AreEquivalent(_controller._customFieldsToImport, new[] { customFieldLabel, customFieldSameLabel, customFieldWrongType });

                    // Make sure the 'wrongType' custom field has been re-introduced by the test with a different type
                    VerifyCustomFieldPresent(customFieldWrongType, LexEntryTags.kClassId, StTextTags.kClassId);
                    // SUT
                    _controller.DoImport();
                    var configToImport = (DictionaryConfigurationModel)_controller.NewConfigToImport;
                    // Assert that the field which was Enabled or not
                    Assert.IsTrue(configToImport.Parts[1].IsEnabled, "CustomField1 should be enabled");
                    // Assert that the field which was present before the import is still there
                    VerifyCustomFieldPresent(customFieldSameLabel, LexSenseTags.kClassId, StTextTags.kClassId);
                    // Assert that the field which was not present before the import has been added
                    VerifyCustomFieldPresent(customFieldLabel, LexEntryTags.kClassId, StTextTags.kClassId);
                    // Assert that the field which was present with the wrong type is still present.
                    // In the future if we implement overwriting an existing custom field with the data from the export this
                    // is where we would assert the change.
                    VerifyCustomFieldPresent(customFieldWrongType, LexEntryTags.kClassId, StTextTags.kClassId);
                }
            }
        }