Exemplo n.º 1
0
        private string UserRequestsOverwrite_Helper()
        {
            // This model has the same label but a non-colliding filename. Proves overwrite the code will always overwrite.
            var alreadyExistingModelWithSameLabel = new DictionaryConfigurationModel
            {
                Label        = configLabel,
                Publications = new List <string> {
                    "Main Dictionary", "unknown pub 1", "unknown pub 2"
                },
                FilePath = Path.Combine(_projectConfigPath, "Different" + configFilename)
            };

            FileUtils.WriteStringtoFile(alreadyExistingModelWithSameLabel.FilePath, "arbitrary file content", Encoding.UTF8);
            var anotherAlreadyExistingModel = new DictionaryConfigurationModel
            {
                Label        = "importexportConfiguration-Imported1",
                Publications = new List <string> {
                    "Main Dictionary", "unknown pub 1", "unknown pub 2"
                },
                FilePath = Path.GetTempPath() + configFilename
            };

            DictionaryConfigurationManagerController.GenerateFilePath(_projectConfigPath, _controller._configurations,
                                                                      anotherAlreadyExistingModel);
            FileUtils.WriteStringtoFile(anotherAlreadyExistingModel.FilePath, "arbitrary file content", Encoding.UTF8);

            _controller._configurations.Add(alreadyExistingModelWithSameLabel);
            _controller._configurations.Add(anotherAlreadyExistingModel);

            _controller.PrepareImport(_zipFile);

            return(alreadyExistingModelWithSameLabel.FilePath);
        }
Exemplo n.º 2
0
        public void GenerateFilePath_AccountsForFilesOnDisk()
        {
            List <DictionaryConfigurationModel> conflictingConfigs;
            var configToRename = GenerateFilePath_Helper(out conflictingConfigs);

            FileUtils.WriteStringtoFile(Path.Combine(_projectConfigPath, "configuration3_3.fwdictconfig"), "file contents of config file that is in the way on disk but not actually registered in the list of configurations", Encoding.UTF8);

            // SUT
            DictionaryConfigurationManagerController.GenerateFilePath(_controller._projectConfigDir, _controller._configurations, configToRename);

            var newFilePath = configToRename.FilePath;

            Assert.That(newFilePath, Is.EqualTo(Path.Combine(_projectConfigPath, "configuration3_4.fwdictconfig")), "Did not account for collision with unregistered configuration on disk");
        }
Exemplo n.º 3
0
        public void DeleteConfigurationRemovesFromDisk()
        {
            var configurationToDelete = _configurations[0];

            DictionaryConfigurationManagerController.GenerateFilePath(_controller._projectConfigDir, _controller._configurations, configurationToDelete);
            var pathToConfiguration = configurationToDelete.FilePath;

            FileUtils.WriteStringtoFile(pathToConfiguration, "file contents", Encoding.UTF8);
            Assert.That(FileUtils.FileExists(pathToConfiguration), "Unit test not set up right");

            // SUT
            _controller.DeleteConfiguration(configurationToDelete);

            Assert.That(!FileUtils.FileExists(pathToConfiguration), "File should have been deleted");
            Assert.That(_controller.IsDirty, "made changes; should be dirty");
        }
Exemplo n.º 4
0
        public void GenerateFilePath()
        {
            List <DictionaryConfigurationModel> conflictingConfigs;
            var configToRename = GenerateFilePath_Helper(out conflictingConfigs);

            // SUT
            DictionaryConfigurationManagerController.GenerateFilePath(_controller._projectConfigDir, _controller._configurations, configToRename);

            var newFilePath = configToRename.FilePath;

            StringAssert.StartsWith(_projectConfigPath, newFilePath);
            StringAssert.EndsWith(DictionaryConfigurationModel.FileExtension, newFilePath);
            Assert.AreEqual(DictionaryConfigurationManagerController.FormatFilePath(_controller._projectConfigDir, "configuration3_3"), configToRename.FilePath, "The file path should be based on the label");
            foreach (var config in conflictingConfigs)
            {
                Assert.AreNotEqual(Path.GetFileName(newFilePath), Path.GetFileName(config.FilePath), "File name should be unique");
            }
        }
Exemplo n.º 5
0
        public void DoImport_UseUniqueFileNameToAvoidCollision()
        {
            var alreadyExistingModelWithSameLabel = new DictionaryConfigurationModel
            {
                Label        = configLabel,
                Publications = new List <string>(),
            };

            DictionaryConfigurationManagerController.GenerateFilePath(_projectConfigPath, _controller._configurations,
                                                                      alreadyExistingModelWithSameLabel);

            var anotherAlreadyExistingModel = new DictionaryConfigurationModel
            {
                Label        = "importexportConfiguration-Imported1",
                Publications = new List <string>(),
            };

            DictionaryConfigurationManagerController.GenerateFilePath(_projectConfigPath, _controller._configurations,
                                                                      anotherAlreadyExistingModel);

            _controller._configurations.Add(alreadyExistingModelWithSameLabel);
            _controller._configurations.Add(anotherAlreadyExistingModel);

            // File that exists but isn't actually registered in the list of configurations
            var fileInTheWay = Path.Combine(_projectConfigPath, "importexportConfiguration-Imported2.fwdictconfig");

            // ReSharper disable once LocalizableElement
            File.WriteAllText(fileInTheWay, "arbitrary file content");

            _controller.PrepareImport(_zipFile);

            // SUT
            _controller.DoImport();
            // What the filename is will be a combination of label collision handling in PrepareImport() and existing file collision handling performed by DictionaryConfigurationManagerController.
            Assert.That(File.Exists(Path.Combine(_projectConfigPath, "importexportConfiguration-Imported2_1.fwdictconfig")),
                        "Configuration not imported or to the right place. Perhaps the existing but not-registered importexportConfiguration-Imported2.fwdictconfig file was not accounted for.");
            Assert.That(_controller._configurations.Any(config => config.Label == "importexportConfiguration-Imported2"),
                        "Imported configuration was not registered, or with the expected label.");
            Assert.That(_controller.NewConfigToImport.FilePath,
                        Is.EqualTo(Path.Combine(_projectConfigPath, "importexportConfiguration-Imported2_1.fwdictconfig")),
                        "FilePath of imported config was not set as expected.");
        }
Exemplo n.º 6
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.º 7
0
        /// <summary>
        /// Perform the configuration import that the controller has prepared to do.
        /// </summary>
        internal void DoImport()
        {
            Debug.Assert(NewConfigToImport != null);

            ImportCustomFields(_importLiftLocation);

            // If the configuration to import has the same label as an existing configuration in the project folder
            // then overwrite the existing configuration.
            var existingConfigurationInTheWay = _configurations.FirstOrDefault(config => config.Label == NewConfigToImport.Label &&
                                                                               Path.GetDirectoryName(config.FilePath) == _projectConfigDir);

            NewConfigToImport.Publications.ForEach(
                publication =>
            {
                AddPublicationTypeIfNotPresent(publication, _cache);
            });
            try
            {
                ImportStyles(_importStylesLocation);
                ImportHappened = true;
            }
            catch (InstallationException e)             // This is the exception thrown if the dtd guid in the style file doesn't match our program
            {
#if DEBUG
                if (_view == null)                 // _view is sometimes null in unit tests, and it's helpful to know what exactly went wrong.
                {
                    throw new Exception(xWorksStrings.kstidCannotImport, e);
                }
#endif
                _view.explanationLabel.Text = xWorksStrings.kstidCannotImport;
            }

            // We have re-loaded the model from disk to preserve custom field state so the Label must be set here
            NewConfigToImport.FilePath = _temporaryImportConfigLocation;
            NewConfigToImport.Load(_cache);
            if (existingConfigurationInTheWay != null)
            {
                _configurations.Remove(existingConfigurationInTheWay);
                if (existingConfigurationInTheWay.FilePath != null)
                {
                    FileUtils.Delete(existingConfigurationInTheWay.FilePath);
                }
            }
            else
            {
                NewConfigToImport.Label = _proposedNewConfigLabel;
            }

            // Set a filename for the new configuration. Use a unique filename that isn't either registered with another configuration, or existing on disk. Note that in this way, we ignore what the original filename was of the configuration file in the .zip file.
            DictionaryConfigurationManagerController.GenerateFilePath(_projectConfigDir, _configurations, NewConfigToImport);

            var outputConfigPath = existingConfigurationInTheWay != null ? existingConfigurationInTheWay.FilePath : NewConfigToImport.FilePath;

            File.Move(_temporaryImportConfigLocation, outputConfigPath);

            NewConfigToImport.FilePath = outputConfigPath;
            _configurations.Add(NewConfigToImport);

            // phone home (analytics)
            var configType = NewConfigToImport.Type;
            var configDir  = DictionaryConfigurationListener.GetDefaultConfigurationDirectory(
                configType == DictionaryConfigurationModel.ConfigType.Reversal
                                        ? DictionaryConfigurationListener.ReversalIndexConfigurationDirectoryName
                                        : DictionaryConfigurationListener.DictionaryConfigurationDirectoryName);
            var isCustomizedOriginal = DictionaryConfigurationManagerController.IsConfigurationACustomizedOriginal(NewConfigToImport, configDir, _cache);
            UsageReporter.SendEvent("DictionaryConfigurationImport", "Import", "Import Config",
                                    string.Format("Import of [{0}{1}]:{2}",
                                                  configType, isCustomizedOriginal ? string.Empty : "-Custom", ImportHappened ? "succeeded" : "failed"), 0);
        }