Пример #1
0
        public ScarfDataSource CreateDataSourceInstance()
        {
            ScarfSection configuration = ScarfConfiguration.ConfigurationSection;

            if (configuration.DataSource == null)
            {
                return(new MemoryDataSource());
            }

            try
            {
                DataSourceElement dataAccessConfiguration = configuration.DataSource;
                if (string.IsNullOrWhiteSpace(dataAccessConfiguration.Type))
                {
                    return(new MemoryDataSource());
                }

                Type dataSourceType = Type.GetType(dataAccessConfiguration.Type);
                if (dataSourceType == null)
                {
                    return(new MemoryDataSource());
                }

                var dataSource = (ScarfDataSource)Activator.CreateInstance(dataSourceType);
                dataSource.Initialize(dataAccessConfiguration);
                return(dataSource);
            }
            catch
            {
                return(new MemoryDataSource());
            }
        }
Пример #2
0
        private void SaveChanged()
        {
            foreach (var item in this.listBoxItems)
            {
                if (item.Value.Status == DataSourceItemStatus.None)
                {
                    continue;
                }
                if (item.Value.Status == DataSourceItemStatus.Deleted)
                {
                    ConfigManager.DataSourceSection.DataSources.Remove(item.Value.Name);
                    continue;
                }

                if (item.Value.Status == DataSourceItemStatus.New)
                {
                    DataSourceElement element = new DataSourceElement();
                    element.Name             = item.Value.Name;
                    element.ConnectionString = item.Value.ConnectionString;
                    element.Exporter         = item.Value.Exporter;
                    ConfigManager.DataSourceSection.DataSources.Add(element);
                    continue;
                }
                if (item.Value.Status == DataSourceItemStatus.Edit)
                {
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].Name             = item.Value.Name;
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].ConnectionString = item.Value.ConnectionString;
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].Exporter         = item.Value.Exporter;
                    continue;
                }
            }
        }
Пример #3
0
 public void Initialize(DataSourceElement configuration)
 {
     if (Directory.Exists(configuration.Path) == false)
     {
         Directory.CreateDirectory(configuration.Path);
     }
     loggingFolder = configuration.Path;
 }
Пример #4
0
        public static T CreateInstance <T>(string dbName, string subNamespace, string typeName, string tableName)
        {
            dbName = dbName.Trim().ToLower();
            string            dataSourceName = ConfigHelper.GetAppSettings("dataSource");
            DataSourceElement config         = ConfigManager.DataSourceSection.DataSources[dataSourceName];

            subNamespace = subNamespace ?? config.Databases[dbName].Namespace;
            var connectionString = config.Databases[dbName].ConnectionString;
            var fullTypeName     = string.Format("{0}.{1}.{2},{3}", config.Namespace, subNamespace, typeName, config.Assembly);

            object[] args = string.IsNullOrEmpty(tableName) ?
                            new object[] { connectionString } :
            new object[] { tableName, connectionString };
            return((T)Activator.CreateInstance(Type.GetType(fullTypeName), args));
        }
Пример #5
0
        internal void Load()
        {
            XmlReader reader = null;

            try {
                Sources = new List <DataSourceElement>();
                reader  = CreateReader();

                XPathDocument xpathDocument = new XPathDocument(reader);

                XPathNavigator navigator = xpathDocument.CreateNavigator();

                XPathNodeIterator sourceIterator = navigator.Select(sourcePath);

                foreach (XPathNavigator sourceNavigator in sourceIterator)
                {
                    string nsUri      = sourceNavigator.NamespaceURI;
                    string sourceName = sourceNavigator.GetAttribute("name", nsUri);
                    string sourceType = sourceNavigator.GetAttribute("type", nsUri);
                    bool   isDefault;
                    isDefault = bool.TryParse(sourceNavigator.GetAttribute("isDefault", nsUri), out isDefault) ? isDefault : false;
                    string dataSource = sourceNavigator.GetAttribute("dataSource", nsUri);
                    string user       = sourceNavigator.GetAttribute("user", nsUri);
                    string password   = sourceNavigator.GetAttribute("password", nsUri);
                    string database   = sourceNavigator.GetAttribute("database", nsUri);
                    bool   supportsWindowsAuthentication;
                    supportsWindowsAuthentication = bool.TryParse(sourceNavigator.GetAttribute("supportsWindowsAuthentication", nsUri), out supportsWindowsAuthentication) ? supportsWindowsAuthentication : false;
                    bool isLocal;
                    isLocal = bool.TryParse(sourceNavigator.GetAttribute("isLocal", nsUri), out isLocal) ? isLocal : false;
                    bool disableMultiSubnetFailover;
                    disableMultiSubnetFailover = bool.TryParse(sourceNavigator.GetAttribute("disableMultiSubnetFailover", nsUri), out disableMultiSubnetFailover) ? disableMultiSubnetFailover : false;
                    bool disableNamedPipes;
                    disableMultiSubnetFailover = bool.TryParse(sourceNavigator.GetAttribute("disableNamedPipes", nsUri), out disableNamedPipes) ? disableNamedPipes : false;

                    DataSourceElement element = new DataSourceElement(sourceName, sourceType, null, dataSource, database, user, password, ds_isDefault: isDefault, ds_isLocal: isLocal, disableMultiSubnetFailoverSetup: disableMultiSubnetFailover, disableNamedPipes: disableNamedPipes);
                    Sources.Add(element);
                }
            }
            finally
            {
                reader.Dispose();
            }
        }
Пример #6
0
        internal void Load()
        {
            XmlReader reader = null;
            try { 
                Sources = new List<DataSourceElement>();
                reader = CreateReader();

                XPathDocument xpathDocument = new XPathDocument(reader);

                XPathNavigator navigator = xpathDocument.CreateNavigator();

                XPathNodeIterator sourceIterator = navigator.Select(sourcePath);

                foreach (XPathNavigator sourceNavigator in sourceIterator)
                {
                    string nsUri = sourceNavigator.NamespaceURI;
                    string sourceName = sourceNavigator.GetAttribute("name", nsUri);
                    string sourceType = sourceNavigator.GetAttribute("type", nsUri);
                    bool isDefault;
                    isDefault = bool.TryParse(sourceNavigator.GetAttribute("isDefault", nsUri), out isDefault) ? isDefault : false;
                    string dataSource = sourceNavigator.GetAttribute("dataSource", nsUri);
                    string user = sourceNavigator.GetAttribute("user", nsUri);
                    string password = sourceNavigator.GetAttribute("password", nsUri);
                    string database = sourceNavigator.GetAttribute("database", nsUri);
                    bool supportsWindowsAuthentication;
                    supportsWindowsAuthentication = bool.TryParse(sourceNavigator.GetAttribute("supportsWindowsAuthentication", nsUri), out supportsWindowsAuthentication) ? supportsWindowsAuthentication : false;
                    bool isLocal;
                    isLocal = bool.TryParse(sourceNavigator.GetAttribute("isLocal", nsUri), out isLocal) ? isLocal : false;
                    bool disableMultiSubnetFailover;
                    disableMultiSubnetFailover = bool.TryParse(sourceNavigator.GetAttribute("disableMultiSubnetFailover", nsUri), out disableMultiSubnetFailover) ? disableMultiSubnetFailover : false;
                    bool disableNamedPipes;
                    disableMultiSubnetFailover = bool.TryParse(sourceNavigator.GetAttribute("disableNamedPipes", nsUri), out disableNamedPipes) ? disableNamedPipes : false;

                    DataSourceElement element = new DataSourceElement(sourceName, sourceType, null, dataSource, database, user, password, ds_isDefault: isDefault, ds_isLocal: isLocal, disableMultiSubnetFailoverSetup: disableMultiSubnetFailover, disableNamedPipes: disableNamedPipes);
                    Sources.Add(element);
                }
            }
            finally
            {
                reader.Dispose();
            }
        }
Пример #7
0
        protected override void ExecuteCommand(Action <string> output, params string[] args)
        {
            WebClient wc = new WebClient();

            wc.Encoding = Encoding.UTF8;
            string urlFormat = "http://www.pinble.com/Template/WebService1.asmx/Present3DList?pageindex={0}&lottory={1}&pl3={2}&name={3}&isgp={4}";

            List <Category>             categories = CategoryBiz.Instance.GetEnabledCategories(false);
            Dictionary <string, string> dict       = new Dictionary <string, string>(categories.Count);
            string            dataSourceName       = ConfigHelper.GetAppSettings("dataSource");
            DataSourceElement config = ConfigManager.DataSourceSection.DataSources[dataSourceName];
            DwNumberBiz       biz    = new DwNumberBiz("shand11x5");

            foreach (var category in categories)
            {
                if (category.ParentId == 0)
                {
                    continue;
                }
                string   url         = string.Format(urlFormat, 1, category.Code, "", category.Name, category.IsGP);
                string   htmlText    = wc.DownloadString(url);
                int      peroidCount = ConvertHelper.GetInt32(Regex.Match(htmlText, "共\\:(\\d+)条", RegexOptions.Singleline | RegexOptions.IgnoreCase).Groups[1].Value);
                int      pageCount   = ConvertHelper.GetInt32(Regex.Match(htmlText, "分页\\:1/(\\d+)页", RegexOptions.Singleline | RegexOptions.IgnoreCase).Groups[1].Value);
                Category entity      = new Category()
                {
                    Id = category.Id, PeroidCount = peroidCount, DownPageCount = pageCount
                };
                CategoryBiz.Instance.Modify(entity, entity.Id, Category.C_DownPageCount, Category.C_PeroidCount);
                biz.DataAccessor.ConnectionString = config.Databases[category.DbName.Trim().ToLower()].ConnectionString;
                int maxSeq      = biz.DataAccessor.SelectMaxWithCondition("Seq", 10, string.Empty);
                int downPeroids = biz.Count;

                string propmt = string.Format("{0}:{1}",
                                              category.Name, string.Format("{0}期,下载,{1}期,Max Seq,{2}", peroidCount, downPeroids, maxSeq));
                output(propmt);

                if (category.Type.Equals("11X5"))
                {
                    this.CheckC5CX(output, category, biz.DataAccessor.ConnectionString);
                }
            }
        }
        static AttachRuntimeDataSources()
        {
            Logger.WriteLine("------------------");
            Logger.WriteLine("Starting Execution");

            var appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            //If the connectionStrings section doesn't exist, add it.
            if (!appConfig.Sections.Cast<ConfigurationSection>().Any(x => x.SectionInformation.Name == "connectionStrings"))
                appConfig.Sections.Add("connectionStrings", new ConnectionStringsSection());

            //If the test tools section doesn't exist, add it.
            if (!appConfig.Sections.Cast<ConfigurationSection>().Any(x => x.SectionInformation.Name == "microsoft.visualstudio.testtools"))
                appConfig.Sections.Add("microsoft.visualstudio.testtools", new TestConfigurationSection());

            var connectionStringsSection = (ConnectionStringsSection)appConfig.Sections["connectionStrings"];
            var testConfigurationSection = (TestConfigurationSection)appConfig.Sections["microsoft.visualstudio.testtools"];

            //Remove all connection strings that have the "_RuntimeDataSources" in the name.
            var connectionsToRemove = connectionStringsSection.ConnectionStrings.Cast<ConnectionStringSettings>().Where(x => x.Name.Contains("RuntimeDataSource")).ToList();
            foreach (var con in connectionsToRemove)
            {
                connectionStringsSection.ConnectionStrings.Remove(con);
            }

            //Make sure dir exists
            if (!Directory.Exists(DATASOURCES_PATH))
                Directory.CreateDirectory(DATASOURCES_PATH);

            //BUG: Other DLL's are not loaded into this app domain as its happening too early. We need to change this to
            //search the directory or something similar.
            var assembliesToSearch = AppDomain.CurrentDomain.GetAssemblies()
                                                            .Where(a => !a.FullName.ToLowerInvariant().StartsWith("microsoft"))
                                                            .SelectMany(GetLoadableTypes)
                                                            .Select(x => x.Assembly)
                                                            .Distinct();

            var dataSourceNames = assembliesToSearch.SelectMany(GetLoadableTypes)
                                                    .Where(type => type.IsSubclassOf(typeof(TestBase)))
                                                    .SelectMany(x => x.GetMethods())
                                                    .SelectMany(a => a.GetCustomAttributes(typeof(DataSourceAttribute), false))
                                                    .Select(x => ((DataSourceAttribute)x).DataSourceSettingName)
                                                    .Where(x => !string.IsNullOrWhiteSpace(x))
                                                    .Distinct()
                                                    .ToList();

            // read the exclusions from the app.config file
            var exclusionRegEx = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["ExclusionRegEx"]) ? ConfigurationManager.AppSettings["ExclusionRegEx"].ToString() : "";
            int excludedCount = 0;

            // if defined then log the regex pattern
            if (!string.IsNullOrEmpty(exclusionRegEx))
                Logger.WriteLine("Exclusion RegEx: {0}", exclusionRegEx);

            var configChanged = false;
            var totalTimeTaken = Stopwatch.StartNew();
            var totalIterationCount = 0;
            foreach (var dataSourceName in dataSourceNames)
            {
                if (!string.IsNullOrEmpty(exclusionRegEx))
                {
                    if (Regex.IsMatch(dataSourceName, exclusionRegEx))
                    {
                        excludedCount++;
                        Logger.WriteLine("Excluded datasource: {0}", dataSourceName);
                        continue;
                    }
                }

                try
                {
                    var individualTimeTaken = Stopwatch.StartNew();

                    var lastDotIndex = dataSourceName.LastIndexOf(".");
                    if (lastDotIndex == -1)
                        throw new Exception("Please specify the fully qualified type + property, method or field.");

                    var dataName = dataSourceName.Substring(lastDotIndex + 1);
                    var typeName = dataSourceName.Substring(0, lastDotIndex);

                    var type = GetBusinessEntityType(assembliesToSearch, typeName);
                    var data = new List<object>();
                    foreach (var x in new ProviderReference(type, dataName).GetInstance())
                    {
                        data.Add(x);
                    }

                    var connectionStringName = dataSourceName + "_RuntimeDataSource";
                    var dataSourceFilePath = Path.Combine(DATASOURCES_PATH, Guid.NewGuid().ToString("N") + ".xml");

                    //Add connection string
                    connectionStringsSection.ConnectionStrings.Add(new ConnectionStringSettings(connectionStringName, dataSourceFilePath, "Microsoft.VisualStudio.TestTools.DataSource.XML"));

                    //Add datasource
                    var dataSource = new DataSourceElement()
                    {
                        Name = dataSourceName,
                        ConnectionString = connectionStringName,
                        DataTableName = "Row",
                        DataAccessMethod = "Sequential"
                    };

                    testConfigurationSection.DataSources.Add(dataSource);
                    configChanged = true;

                    //Create the iterations element and set the dataSourceName as an attribute for debugging.
                    var iterationsElement = new XElement("Iterations");
                    iterationsElement.SetAttributeValue("DataSourceName", dataSourceName);

                    //Create the file
                    File.WriteAllText(dataSourceFilePath, new XDocument(new XDeclaration("1.0", "utf-8", "true"), iterationsElement).ToString());

                    //Load the file
                    var doc = XDocument.Load(dataSourceFilePath);

                    //Add the iterations
                    doc.Element("Iterations").Add(

                        from iteration in data
                        select new XElement(dataSource.DataTableName,
                               new XElement("Payload", JsonConvert.SerializeObject(iteration))));

                    //Save the file
                    doc.Save(dataSourceFilePath);

                    totalIterationCount += data.Count;
                    Logger.WriteLine("Successfully created datasource: {0}, Iteration Count: {1}, Elapsed Time : {2}", dataSourceName, data.Count, individualTimeTaken.Elapsed);
                }
                catch (Exception ex)
                {
                    Logger.WriteLine(dataSourceName + ": " + ex.ToString());
                }
            }

            if (configChanged)
            {
                appConfig.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("connectionStrings");
                ConfigurationManager.RefreshSection("microsoft.visualstudio.testtools");
            }

            Logger.WriteLine("No of Assemblies Searched: {0}, No of DataSources Found: {1}, No of DataSources Excluded: {2}, No of Iterations Created: {3}, Total Time Taken: {4}.", assembliesToSearch.Count(), dataSourceNames.Count, excludedCount, totalIterationCount, totalTimeTaken.Elapsed);
            Logger.WriteLine("Finishing Execution");
            Logger.WriteLine("------------------");
        }
Пример #9
0
 public void Initialize(DataSourceElement configuration)
 {
 }
Пример #10
0
        static AttachRuntimeDataSources()
        {
            Logger.WriteLine("------------------");
            Logger.WriteLine("Starting Execution");

            var appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            //If the connectionStrings section doesn't exist, add it.
            if (!appConfig.Sections.Cast <ConfigurationSection>().Any(x => x.SectionInformation.Name == "connectionStrings"))
            {
                appConfig.Sections.Add("connectionStrings", new ConnectionStringsSection());
            }

            //If the test tools section doesn't exist, add it.
            if (!appConfig.Sections.Cast <ConfigurationSection>().Any(x => x.SectionInformation.Name == "microsoft.visualstudio.testtools"))
            {
                appConfig.Sections.Add("microsoft.visualstudio.testtools", new TestConfigurationSection());
            }

            var connectionStringsSection = (ConnectionStringsSection)appConfig.Sections["connectionStrings"];
            var testConfigurationSection = (TestConfigurationSection)appConfig.Sections["microsoft.visualstudio.testtools"];

            //Remove all connection strings that have the "_RuntimeDataSources" in the name.
            var connectionsToRemove = connectionStringsSection.ConnectionStrings.Cast <ConnectionStringSettings>().Where(x => x.Name.Contains("RuntimeDataSource")).ToList();

            foreach (var con in connectionsToRemove)
            {
                connectionStringsSection.ConnectionStrings.Remove(con);
            }

            //Make sure dir exists
            if (!Directory.Exists(DATASOURCES_PATH))
            {
                Directory.CreateDirectory(DATASOURCES_PATH);
            }

            //BUG: Other DLL's are not loaded into this app domain as its happening too early. We need to change this to
            //search the directory or something similar.
            var assembliesToSearch = AppDomain.CurrentDomain.GetAssemblies()
                                     .Where(a => !a.FullName.ToLowerInvariant().StartsWith("microsoft"))
                                     .SelectMany(GetLoadableTypes)
                                     .Select(x => x.Assembly)
                                     .Distinct();

            var dataSourceNames = assembliesToSearch.SelectMany(GetLoadableTypes)
                                  .Where(type => type.IsSubclassOf(typeof(TestBase)))
                                  .SelectMany(x => x.GetMethods())
                                  .SelectMany(a => a.GetCustomAttributes(typeof(DataSourceAttribute), false))
                                  .Select(x => ((DataSourceAttribute)x).DataSourceSettingName)
                                  .Where(x => !string.IsNullOrWhiteSpace(x))
                                  .Distinct()
                                  .ToList();

            // read the exclusions from the app.config file
            var exclusionRegEx = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["ExclusionRegEx"]) ? ConfigurationManager.AppSettings["ExclusionRegEx"].ToString() : "";
            int excludedCount  = 0;

            // if defined then log the regex pattern
            if (!string.IsNullOrEmpty(exclusionRegEx))
            {
                Logger.WriteLine("Exclusion RegEx: {0}", exclusionRegEx);
            }

            var configChanged       = false;
            var totalTimeTaken      = Stopwatch.StartNew();
            var totalIterationCount = 0;

            foreach (var dataSourceName in dataSourceNames)
            {
                if (!string.IsNullOrEmpty(exclusionRegEx))
                {
                    if (Regex.IsMatch(dataSourceName, exclusionRegEx))
                    {
                        excludedCount++;
                        Logger.WriteLine("Excluded datasource: {0}", dataSourceName);
                        continue;
                    }
                }

                try
                {
                    var individualTimeTaken = Stopwatch.StartNew();

                    var lastDotIndex = dataSourceName.LastIndexOf(".");
                    if (lastDotIndex == -1)
                    {
                        throw new Exception("Please specify the fully qualified type + property, method or field.");
                    }

                    var dataName = dataSourceName.Substring(lastDotIndex + 1);
                    var typeName = dataSourceName.Substring(0, lastDotIndex);

                    var type = GetBusinessEntityType(assembliesToSearch, typeName);
                    var data = new List <object>();
                    foreach (var x in new ProviderReference(type, dataName).GetInstance())
                    {
                        data.Add(x);
                    }

                    var connectionStringName = dataSourceName + "_RuntimeDataSource";
                    var dataSourceFilePath   = Path.Combine(DATASOURCES_PATH, Guid.NewGuid().ToString("N") + ".xml");

                    //Add connection string
                    connectionStringsSection.ConnectionStrings.Add(new ConnectionStringSettings(connectionStringName, dataSourceFilePath, "Microsoft.VisualStudio.TestTools.DataSource.XML"));

                    //Add datasource
                    var dataSource = new DataSourceElement()
                    {
                        Name             = dataSourceName,
                        ConnectionString = connectionStringName,
                        DataTableName    = "Row",
                        DataAccessMethod = "Sequential"
                    };

                    testConfigurationSection.DataSources.Add(dataSource);
                    configChanged = true;

                    //Create the iterations element and set the dataSourceName as an attribute for debugging.
                    var iterationsElement = new XElement("Iterations");
                    iterationsElement.SetAttributeValue("DataSourceName", dataSourceName);

                    //Create the file
                    File.WriteAllText(dataSourceFilePath, new XDocument(new XDeclaration("1.0", "utf-8", "true"), iterationsElement).ToString());

                    //Load the file
                    var doc = XDocument.Load(dataSourceFilePath);

                    //Add the iterations
                    doc.Element("Iterations").Add(

                        from iteration in data
                        select new XElement(dataSource.DataTableName,
                                            new XElement("Payload", JsonConvert.SerializeObject(iteration))));

                    //Save the file
                    doc.Save(dataSourceFilePath);

                    totalIterationCount += data.Count;
                    Logger.WriteLine("Successfully created datasource: {0}, Iteration Count: {1}, Elapsed Time : {2}", dataSourceName, data.Count, individualTimeTaken.Elapsed);
                }
                catch (Exception ex)
                {
                    Logger.WriteLine(dataSourceName + ": " + ex.ToString());
                }
            }

            if (configChanged)
            {
                appConfig.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("connectionStrings");
                ConfigurationManager.RefreshSection("microsoft.visualstudio.testtools");
            }

            Logger.WriteLine("No of Assemblies Searched: {0}, No of DataSources Found: {1}, No of DataSources Excluded: {2}, No of Iterations Created: {3}, Total Time Taken: {4}.", assembliesToSearch.Count(), dataSourceNames.Count, excludedCount, totalIterationCount, totalTimeTaken.Elapsed);
            Logger.WriteLine("Finishing Execution");
            Logger.WriteLine("------------------");
        }
        private void SaveChanged()
        {
            foreach (var item in this.listBoxItems)
            {
                if (item.Value.Status == DataSourceItemStatus.None) continue;
                if (item.Value.Status == DataSourceItemStatus.Deleted)
                {
                    ConfigManager.DataSourceSection.DataSources.Remove(item.Value.Name);
                    continue;
                }

                if (item.Value.Status == DataSourceItemStatus.New)
                {
                    DataSourceElement element = new DataSourceElement();
                    element.Name = item.Value.Name;
                    element.ConnectionString = item.Value.ConnectionString;
                    element.Exporter = item.Value.Exporter;
                    ConfigManager.DataSourceSection.DataSources.Add(element);
                    continue;
                }
                if (item.Value.Status == DataSourceItemStatus.Edit)
                {
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].Name = item.Value.Name;
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].ConnectionString = item.Value.ConnectionString;
                    ConfigManager.DataSourceSection.DataSources[item.Value.Name].Exporter = item.Value.Exporter;
                    continue;
                }
            }
        }
Пример #12
0
 public void Initialize(DataSourceElement configuration)
 {
     connectionStringName = configuration.ConnectionStringName;
 }