public void Comparisons()
		{
			EasyVersion left, right; int res;

			left = null;
			right = null;
			res = EasyVersion.Compare(left, right); Assert.AreEqual(0, res);

			left = null;
			right = EasyVersion.Empty;
			res = EasyVersion.Compare(left, right); Assert.AreEqual(-1, res);

			left = EasyVersion.Empty;
			right = null;
			res = EasyVersion.Compare(left, right); Assert.AreEqual(+1, res);

			left = new EasyVersion("b1.02.45");
			right = new EasyVersion("b1.02.45");
			Assert.IsTrue(left == right);

			left = new EasyVersion("b2");
			right = new EasyVersion("b1");
			Assert.IsTrue(left > right);

			left = new EasyVersion("b1");
			right = new EasyVersion("b2");
			Assert.IsTrue(left < right);
		}
		public void Empty_And_Tailing_Parts()
		{
			EasyVersion obj; string str; bool res;

			str = "1..   .4... .. .";
			obj = new EasyVersion(str);
			res = "1...4" == obj.ToString(); Assert.IsTrue(res);
		}
		public void Extracting_Spaces_And_Leading_Ceroes()
		{
			EasyVersion obj; string str; bool res;

			str = " 1 . 000 . 3 ";
			obj = new EasyVersion(str);
			res = "1.0.3" == obj.ToString(); Assert.IsTrue(res);

			str = " 1 . 00 0 . 000 3 00";
			obj = new EasyVersion(str);
			res = "1.0.300" == obj.ToString(); Assert.IsTrue(res);
		}
		public void Empty_Results()
		{
			EasyVersion obj; string str; bool res;

			str = null;
			obj = new EasyVersion(str);
			res = obj == EasyVersion.Empty; Assert.IsTrue(res);

			str = string.Empty;
			obj = new EasyVersion(str);
			res = obj == EasyVersion.Empty; Assert.IsTrue(res);

			str = "..   ..";
			obj = new EasyVersion(str);
			res = obj == EasyVersion.Empty; Assert.IsTrue(res);
		}
Beispiel #5
0
        /// <summary>
        /// Returns the latest registered engine that matches the conditions given, or null
        /// if no one is found.
        /// </summary>
        /// <param name="name">The invariant name of the engine, or its tail part, or the
        /// name of a connection string entry, or null to use the default 'connectionString'
        /// entry in the 'keroseneORM' section.</param>
        /// <param name="minVersion">If not null the minimum acceptable version.</param>
        /// <param name="maxVersion">If not null the maximum acceptable version.</param>
        /// <param name="validator">If not null the delegate to invoke for custom validation of
        /// a candidate engine, which is considered valid if the delegate returns true, or invalid
        /// if it returns false.</param>
        /// <param name="settings">If not null a dictionary containing the names and values of
        /// the properties to modify on the found engine, generating a clone with the new values.</param>
        /// <returns>An engine found, or null.</returns>
        public static IDataEngine Locate(
            string name       = null,
            string minVersion = null,
            string maxVersion = null,
            Func <IDataEngine, bool> validator    = null,
            IDictionary <string, object> settings = null)
        {
            name       = name.NullIfTrimmedIsEmpty();
            minVersion = minVersion.NullIfTrimmedIsEmpty(); var minEasyVersion = new EasyVersion(minVersion);
            maxVersion = maxVersion.NullIfTrimmedIsEmpty(); var maxEasyVersion = new EasyVersion(maxVersion);

            string cnVersion     = null;
            var    cnEasyVersion = EasyVersion.Empty;
            var    cn            = Configuration.ConnectionStringEx.Find(name); if (cn == null)

            {
                if (name == null)
                {
                    throw new ArgumentNullException(
                              "Engine name is null and no default entry was found.");
                }
            }
            else
            {
                if (cn.ProviderName == null)
                {
                    throw new InvalidOperationException(
                              "Connection string entry '{0}' contains no provider name property."
                              .FormatWith(cn.Name));
                }

                name = cn.ProviderName; try
                {
                    var factory = DbProviderFactories.GetFactory(cn.ProviderName);
                    var dbconn  = factory.CreateConnection();
                    dbconn.ConnectionString = cn.ConnectionString;
                    dbconn.Open();

                    cnVersion     = dbconn.ServerVersion.NullIfTrimmedIsEmpty();
                    cnEasyVersion = new EasyVersion(cnVersion);

                    dbconn.Close();
                    dbconn.Dispose();
                }
                catch
                {
                    DebugEx.IndentWriteLine("\n- Engine's provider '{0}' not found.".FormatWith(cn.ProviderName));
                    DebugEx.Unindent();
                }
            }

            IDataEngine engine = null; lock (_SyncRoot)

            {
                InitializeIfNeeded(); foreach (var item in _Engines)
                {
                    bool match = string.Compare(name, item.InvariantName, ignoreCase: true) == 0;
                    if (!match)
                    {
                        int i = item.InvariantName.LastIndexOf('.'); if (i >= 0)
                        {
                            var str = item.InvariantName.Substring(i + 1);
                            match = string.Compare(name, str, ignoreCase: true) == 0;
                        }
                    }
                    if (!match)
                    {
                        continue;
                    }

                    var entry = settings == null ? item : item.Clone(settings);
                    if (validator != null && !validator(entry))
                    {
                        continue;
                    }

                    var entryVersion = new EasyVersion(entry.ServerVersion.NullIfTrimmedIsEmpty());
                    if (minEasyVersion != EasyVersion.Empty && entryVersion < minEasyVersion)
                    {
                        continue;
                    }
                    if (maxEasyVersion != EasyVersion.Empty && entryVersion > maxEasyVersion)
                    {
                        continue;
                    }

                    // TODO: figure out what to do in this scenario...
                    //if (cnEasyVersion != EasyVersion.Empty &&
                    //	entryVersion != EasyVersion.Empty &&
                    //	entryVersion < cnEasyVersion) continue;

                    if (engine == null)
                    {
                        engine = entry;
                    }
                    else
                    {
                        // Bigger or last one has precedence...
                        var engineVersion = new EasyVersion(engine.ServerVersion);
                        if (entryVersion >= engineVersion)
                        {
                            engine = entry;
                        }
                    }
                }
            }

            return(engine);
        }
		/// <summary>
		/// Returns the latest registered engine that matches the conditions given, or null
		/// if no one is found.
		/// </summary>
		/// <param name="name">The invariant name of the engine, or its tail part, or the
		/// name of a connection string entry, or null to use the default 'connectionString'
		/// entry in the 'keroseneORM' section.</param>
		/// <param name="minVersion">If not null the minimum acceptable version.</param>
		/// <param name="maxVersion">If not null the maximum acceptable version.</param>
		/// <param name="validator">If not null the delegate to invoke for custom validation of
		/// a candidate engine, which is considered valid if the delegate returns true, or invalid
		/// if it returns false.</param>
		/// <param name="settings">If not null a dictionary containing the names and values of
		/// the properties to modify on the found engine, generating a clone with the new values.</param>
		/// <returns>An engine found, or null.</returns>
		public static IDataEngine Locate(
			string name = null,
			string minVersion = null,
			string maxVersion = null,
			Func<IDataEngine, bool> validator = null,
			IDictionary<string, object> settings = null)
		{
			name = name.NullIfTrimmedIsEmpty();
			minVersion = minVersion.NullIfTrimmedIsEmpty(); var minEasyVersion = new EasyVersion(minVersion);
			maxVersion = maxVersion.NullIfTrimmedIsEmpty(); var maxEasyVersion = new EasyVersion(maxVersion);

			string cnVersion = null;
			var cnEasyVersion = EasyVersion.Empty;
			var cn = Configuration.ConnectionStringEx.Find(name); if (cn == null)
			{
				if (name == null) throw new ArgumentNullException(
					"Engine name is null and no default entry was found.");
			}
			else
			{
				if (cn.ProviderName == null) throw new InvalidOperationException(
					"Connection string entry '{0}' contains no provider name property."
					.FormatWith(cn.Name));

				name = cn.ProviderName; try
				{
					var factory = DbProviderFactories.GetFactory(cn.ProviderName);
					var dbconn = factory.CreateConnection();
					dbconn.ConnectionString = cn.ConnectionString;
					dbconn.Open();

					cnVersion = dbconn.ServerVersion.NullIfTrimmedIsEmpty();
					cnEasyVersion = new EasyVersion(cnVersion);

					dbconn.Close();
					dbconn.Dispose();
				}
				catch
				{
					DebugEx.IndentWriteLine("\n- Engine's provider '{0}' not found.".FormatWith(cn.ProviderName));
					DebugEx.Unindent();
				}
			}

			IDataEngine engine = null; lock (_SyncRoot)
			{
				InitializeIfNeeded(); foreach (var item in _Engines)
				{
					bool match = string.Compare(name, item.InvariantName, ignoreCase: true) == 0;
					if (!match)
					{
						int i = item.InvariantName.LastIndexOf('.'); if (i >= 0)
						{
							var str = item.InvariantName.Substring(i + 1);
							match = string.Compare(name, str, ignoreCase: true) == 0;
						}
					}
					if (!match) continue;

					var entry = settings == null ? item : item.Clone(settings);
					if (validator != null && !validator(entry)) continue;

					var entryVersion = new EasyVersion(entry.ServerVersion.NullIfTrimmedIsEmpty());
					if (minEasyVersion != EasyVersion.Empty && entryVersion < minEasyVersion) continue;
					if (maxEasyVersion != EasyVersion.Empty && entryVersion > maxEasyVersion) continue;

					// TODO: figure out what to do in this scenario...
					//if (cnEasyVersion != EasyVersion.Empty && 
					//	entryVersion != EasyVersion.Empty &&
					//	entryVersion < cnEasyVersion) continue;

					if (engine == null) engine = entry;
					else
					{
						// Bigger or last one has precedence...
						var engineVersion = new EasyVersion(engine.ServerVersion);
						if (entryVersion >= engineVersion) engine = entry;
					}
				}
			}

			return engine;
		}
		public void Serialization()
		{
			Action<object, bool> del = (source, binary) =>
			{
				var mode = binary ? "binary" : "text/soap";
				ConsoleEx.WriteLine("\n- Source ({0}): {1}", mode, source.Sketch());

				var path = "c:\\temp\\data" + (binary ? ".bin" : ".xml");
				path.PathSerialize(source, binary);

				var temp = path.PathDeserialize(binary);
				ConsoleEx.WriteLine("- Created ({0}): {1}", mode, temp.Sketch());

				var result = source.IsEquivalentTo(temp);
				Assert.IsTrue(result,
					"With mode '{0}' source '{1}' and deserialized '{2}' are not equivalent."
					.FormatWith(mode, source.Sketch(), temp.Sketch()));
			};

			var obj = new EasyVersion("1.2new.3");
			del(obj, true);
			del(obj, false);
		}