public void Missing_environment_configuration_should_throw_typed_exception()
        {
            ConfigRoot originalApplicationConfig = CreateCannedApplicationConfig();
            ConfigRoot originalEnvironmentConfig = CreateCannedEnvironmentConfig();
            originalEnvironmentConfig.Data.Children.RemoveAt(1);

            Mock<IConfigProviderFactory> mockFactory = CreateFactory(originalApplicationConfig, originalEnvironmentConfig);
            var configProvider = new SampleApplicationConfigProvider(mockFactory.Object);

            try
            {
                configProvider.GetConfiguration();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof (UnableToAccessConfigurationException));
                Assert.IsNotNull(ex.InnerException);
                Assert.IsInstanceOfType(ex.InnerException, typeof (ConfigValidationException));
                var validationException = (ConfigValidationException) ex.InnerException;
                Assert.IsNotNull(validationException.ValidationErrors);
                Assert.AreEqual(1, validationException.ValidationErrors.Count);
                var error = validationException.ValidationErrors[0];
                Assert.AreEqual("Environment.ConnectionString", error);
                Assert.IsTrue(ex.ToString().Contains("Environment.ConnectionString"));
            }
        }
Example #2
0
 public void Test_sql_connection_string_validation()
 {
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;MultipleActiveResultSets=true;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;Trusted_Connection=Yes;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=(localdb)\v11.0;Integrated Security=true;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=C:\MyFolder\MyData.mdf;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=(localdb)\MyInstance;Integrated Security=true;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=np:\\.\pipe\LOCALDB#F365A78E\tsql\query;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=(localdb)\.\MyInstanceShare;Integrated Security=true;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Data Source=myServerAddress;Failover Partner=myMirrorServerAddress;Initial Catalog=myDataBase;Integrated Security=True;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerAddress;Database=myDataBase;Integrated Security=True;Asynchronous Processing=True;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;Packet Size=4096;"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"   Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;   "));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"foo"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(null));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@" "));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(string.Empty));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerName/myInstanceName;Database=myDataBase;UserId=myUsername;Password=myPassword;"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerName\myInstanceName;Database=myDataBase;UserId=myUsername;"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidSqlServerConnectionString(@"Server=myServerName\myInstanceName;UserId=myUsername;Password=myPassword;"));
 }
Example #3
0
        public void Missing_environment_configuration_should_throw_typed_exception()
        {
            ConfigRoot originalApplicationConfig = CreateCannedApplicationConfig();
            ConfigRoot originalEnvironmentConfig = CreateCannedEnvironmentConfig();

            originalEnvironmentConfig.Data.Children.RemoveAt(1);

            Mock <IConfigProviderFactory> mockFactory = CreateFactory(originalApplicationConfig, originalEnvironmentConfig);
            var configProvider = new SampleApplicationConfigProvider(mockFactory.Object);

            try
            {
                configProvider.GetConfiguration();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(UnableToAccessConfigurationException));
                Assert.IsNotNull(ex.InnerException);
                Assert.IsInstanceOfType(ex.InnerException, typeof(ConfigValidationException));
                var validationException = (ConfigValidationException)ex.InnerException;
                Assert.IsNotNull(validationException.ValidationErrors);
                Assert.AreEqual(1, validationException.ValidationErrors.Count);
                var error = validationException.ValidationErrors[0];
                Assert.AreEqual("Environment.ConnectionString", error);
                Assert.IsTrue(ex.ToString().Contains("Environment.ConnectionString"));
            }
        }
Example #4
0
 public void Test_exists_validation()
 {
     Assert.IsTrue(SampleApplicationConfigProvider.Exists(null));
     Assert.IsTrue(SampleApplicationConfigProvider.Exists(string.Empty));
     Assert.IsTrue(SampleApplicationConfigProvider.Exists(new object()));
     Assert.IsTrue(SampleApplicationConfigProvider.Exists("foo"));
 }
Example #5
0
 public void Test_email_address_validation()
 {
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidEmailAddress("taco"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidEmailAddress("*****@*****.**"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidEmailAddress("*****@*****.**"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidEmailAddress("[email protected],[email protected]"));
 }
Example #6
0
 public void Test_email_address_list_validation()
 {
     Assert.IsTrue(SampleApplicationConfigProvider.AreAllValidEmailAddresses("[email protected];[email protected]", ';'));
     Assert.IsTrue(SampleApplicationConfigProvider.AreAllValidEmailAddresses("*****@*****.**", ';'));
     Assert.IsFalse(SampleApplicationConfigProvider.AreAllValidEmailAddresses(";[email protected]", ';'));
     Assert.IsFalse(SampleApplicationConfigProvider.AreAllValidEmailAddresses("[email protected];;", ';'));
     Assert.IsFalse(SampleApplicationConfigProvider.AreAllValidEmailAddresses("[email protected] ; ; [email protected] ;", ';'));
     Assert.IsFalse(SampleApplicationConfigProvider.AreAllValidEmailAddresses("blah", ';'));
     Assert.IsFalse(SampleApplicationConfigProvider.AreAllValidEmailAddresses("blah;[email protected]", ';'));
     Assert.IsTrue(SampleApplicationConfigProvider.AreAllValidEmailAddresses("[email protected],[email protected]", ','));
 }
Example #7
0
        public void Test_xml_validation()
        {
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedXml("taco^#>"));
            var testDoc = new XmlDocument();
            var elem    = testDoc.CreateElement("Testing");
            var attrib  = testDoc.CreateAttribute("attrib");

            attrib.Value = "1";
            elem.SetAttributeNode(attrib);
            testDoc.AppendChild(elem);
            Assert.IsTrue(SampleApplicationConfigProvider.IsWellFormedXml(testDoc.OuterXml));
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedXml(long.MaxValue.ToString()));
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedXml(AppDomain.CurrentDomain.BaseDirectory));
        }
Example #8
0
 public void Test_interval_validation()
 {
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval("0.0.0.0"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval("abc"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval(""));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval(null));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval(" "));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidTimeInterval("1:1"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidTimeInterval("1:1:1"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidTimeInterval("01:01:01"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidTimeInterval("12:12"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval("25:16"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidTimeInterval("23:16:61"));
 }
Example #9
0
 public void Test_hostname_validation()
 {
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname(null));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname(string.Empty));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("   "));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("foobar "));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("123"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("123.123"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("123.123.123"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("123.123.123.123"));
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidHostname("abc"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidHostname(" www.yahoo.com "));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidHostname(" r2d2.protk.com"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidHostname(" protk.com"));
 }
Example #10
0
        public void Test_successful_conversion_of_generic_config_root_to_typed_object_graph()
        {
            ConfigRoot originalApplicationConfig = CreateCannedApplicationConfig();
            ConfigRoot originalEnvironmentConfig = CreateCannedEnvironmentConfig();

            Mock <IConfigProviderFactory> mockFactory = CreateFactory(originalApplicationConfig, originalEnvironmentConfig);
            var configProvider = new SampleApplicationConfigProvider(mockFactory.Object);

            SampleApplicationConfig actualConfig = configProvider.GetConfiguration();

            Assert.AreEqual(_expectedConfig.RestUrl, actualConfig.RestUrl);
            Assert.AreEqual(_expectedConfig.ConnectionString, actualConfig.ConnectionString);
            Assert.AreEqual(_expectedConfig.Timeout, actualConfig.Timeout);
            Assert.AreEqual(_expectedConfig.ConfigObject.Name, actualConfig.ConfigObject.Name);
            Assert.AreEqual(_expectedConfig.ConfigObject.Number, actualConfig.ConfigObject.Number);
        }
Example #11
0
        public void Test_integer_validation()
        {
            dynamic original = new ExpandoObject();

            original.Number = 5;
            var     serializedOriginal = JsonConvert.SerializeObject(original);
            dynamic newObject          = JsonConvert.DeserializeObject <ExpandoObject>(serializedOriginal);
            var     dictionary         = newObject as IDictionary <string, object> ?? new Dictionary <string, object>();
            var     testValue          = dictionary["Number"];

            Assert.IsTrue(SampleApplicationConfigProvider.IsValidInteger(testValue));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidInteger("taco"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidInteger("145"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidInteger(long.MaxValue.ToString()));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidInteger(int.MaxValue.ToString()));
        }
Example #12
0
        public void Test_url_validation()
        {
            dynamic original = new ExpandoObject();

            original.Url = new Uri("http://www.google.com");
            var     serializedOriginal = JsonConvert.SerializeObject(original);
            dynamic newObject          = JsonConvert.DeserializeObject <ExpandoObject>(serializedOriginal);
            var     dictionary         = newObject as IDictionary <string, object> ?? new Dictionary <string, object>();
            var     testValue          = dictionary["Url"];

            Assert.IsTrue(SampleApplicationConfigProvider.IsValidUrl(testValue));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidUrl("taco"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidUrl("http://www.google.com"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidUrl(long.MaxValue.ToString()));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidUrl("ftp://192.168.254.1"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidUrl("ftp:/192.168.254.1"));
        }
Example #13
0
        public void Test_path_validation()
        {
            dynamic original = new ExpandoObject();

            original.Path = @"C:\Protk\";
            var     serializedOriginal = JsonConvert.SerializeObject(original);
            dynamic newObject          = JsonConvert.DeserializeObject <ExpandoObject>(serializedOriginal);
            var     dictionary         = newObject as IDictionary <string, object> ?? new Dictionary <string, object>();
            var     testValue          = dictionary["Path"];

            Assert.IsTrue(SampleApplicationConfigProvider.IsWellFormedPath(testValue));
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedPath("taco^#>"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsWellFormedPath(@"c:\"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedPath(long.MaxValue.ToString()));
            Assert.IsTrue(SampleApplicationConfigProvider.IsWellFormedPath(AppDomain.CurrentDomain.BaseDirectory));
            Assert.IsFalse(SampleApplicationConfigProvider.IsWellFormedPath("ftp://192.168.254.1"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsWellFormedPath(@"\\protk.com\shares"));
        }
Example #14
0
        public void Test_ip_address_validation()
        {
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress(null));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress(string.Empty));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress("   "));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress("123.123.123.123"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress("256.255.255"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress("255.255.255.256"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress("abc"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress(" 123.123.123.123 "));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress("0.0.0.0"));
            Assert.IsFalse(SampleApplicationConfigProvider.IsValidIpAddress(" 0.0.-1.0"));

            // The following tests will fail if you do not have the .NET 4.5 runtime installed. There are breaking changes made to .NET 4.0!
            // In this case, an IP address like "123" will be interpreted as "123.0.0.0", a valid IP.  This is different behavior than pre-4.5.
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress("123"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress("123.123"));
            Assert.IsTrue(SampleApplicationConfigProvider.IsValidIpAddress("123.123.123"));
        }
 public void Setup()
 {
     _expectedConfig = CreateExpectedConfig();
     _applicationConfigProvider = new SampleApplicationConfigProvider(null);
 }
Example #16
0
 public void Setup()
 {
     _expectedConfig            = CreateExpectedConfig();
     _applicationConfigProvider = new SampleApplicationConfigProvider(null);
 }
        public void Test_successful_conversion_of_generic_config_root_to_typed_object_graph()
        {
            ConfigRoot originalApplicationConfig = CreateCannedApplicationConfig();
            ConfigRoot originalEnvironmentConfig = CreateCannedEnvironmentConfig();

            Mock<IConfigProviderFactory> mockFactory = CreateFactory(originalApplicationConfig, originalEnvironmentConfig);
            var configProvider = new SampleApplicationConfigProvider(mockFactory.Object);

            SampleApplicationConfig actualConfig = configProvider.GetConfiguration();
            Assert.AreEqual(_expectedConfig.RestUrl, actualConfig.RestUrl);
            Assert.AreEqual(_expectedConfig.ConnectionString, actualConfig.ConnectionString);
            Assert.AreEqual(_expectedConfig.Timeout, actualConfig.Timeout);
            Assert.AreEqual(_expectedConfig.ConfigObject.Name, actualConfig.ConfigObject.Name);
            Assert.AreEqual(_expectedConfig.ConfigObject.Number, actualConfig.ConfigObject.Number);
        }
Example #18
0
 public void Test_boolean_validation()
 {
     Assert.IsFalse(SampleApplicationConfigProvider.IsValidBoolean("taco"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidBoolean("true"));
     Assert.IsTrue(SampleApplicationConfigProvider.IsValidBoolean("False"));
 }