/// <summary>
 /// Creates a new instance of the generator
 /// </summary>
 /// <param name="singleValueDef">The property definition that indicates which property the values are being generated for</param>
 /// <param name="regExPhrase">The regular expression against which generated values must match</param>
 public ValidValueGeneratorRegEx(ISingleValueDef singleValueDef, string regExPhrase)
     : base(singleValueDef)
 {
     if (regExPhrase == null) throw new ArgumentNullException("regExPhrase");
     if (regExPhrase == "") throw new ArgumentException("Must supply a non-empty regular expression", "regExPhrase");
     RegExPhrase = regExPhrase;
 }
        /// <summary>
        /// Resolves the registered <see cref="ValidValueGenerator"/> for the PropDef if one is registered.
        /// Else tries to find a <see cref="ValidValueGenerator"/> for the specified PropDefs Property Type
        /// using the <see cref="ValidValueGeneratorRegistry"/>
        /// </summary>
        /// <returns></returns>
        public virtual ValidValueGenerator Resolve(ISingleValueDef propDef)
        {
            if (propDef == null)
            {
                throw new ArgumentNullException("propDef");
            }
            lock (_lockProp)
            {
                var keyName = GetKeyName(propDef);
                if (_validValueGenTypesPerPropDef.ContainsKey(keyName))
                {
                    var validValueGen     = this._validValueGenTypesPerPropDef[keyName];
                    var validValueGenType = validValueGen.GeneratorType;
                    var parameter         = validValueGen.Parameter;
                    if (parameter != null)
                    {
                        if (!GeneratorHasConstructorWithExtraParameter(validValueGenType))
                        {
                            throw new ArgumentException(
                                      "An extra parameter was provided for a valid value generator type (" +
                                      validValueGenType.Name +
                                      "), but no suitable constructor was found with a second parameter.");
                        }
                        return((ValidValueGenerator)Activator.CreateInstance(validValueGenType, propDef, parameter));
                    }
                    return((ValidValueGenerator)Activator.CreateInstance(validValueGenType, propDef));
                }

                return(ValidValueGeneratorRegistry.Instance.Resolve(propDef));
            }
        }
 protected ValidValueGenerator(ISingleValueDef singleValueDef)
 {
     if (singleValueDef == null)
     {
         throw new ArgumentNullException("singleValueDef");
     }
     this.SingleValueDef = singleValueDef;
 }
 /// <summary>
 /// Returns True if a value Gen is registered with the singleValueDef <paramref name="propDef"/>
 /// </summary>
 /// <param name="propDef"></param>
 /// <returns></returns>
 public bool IsRegistered(ISingleValueDef propDef)
 {
     lock (_lockProp)
     {
         var keyName = GetKeyName(propDef);
         return(this._validValueGenTypesPerPropDef.ContainsKey(keyName));
     }
 }
 /// <summary>
 /// Constructs a new instance of the generator
 /// </summary>
 /// <param name="propDef">The definition of the property for which random values will be generated</param>
 /// <param name="fileName">The filename that contains the available values, including the path.  If the path is relative,
 ///   it will relate to the execution directory.  If no filename is specified, the filename used will be the
 ///   property name (e.g. FirstName) with a ".txt" extension.</param>
 public ValidValueGeneratorTextFile(ISingleValueDef propDef, string fileName = null)
     : base(propDef)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         fileName = propDef.PropertyName + ".txt";
     }
     FileName = fileName;
 }
        private void ClearPreviousInstances(ISingleValueDef propDef)
        {
            var keyName = GetKeyName(propDef);

            if (this._validValueGenTypesPerPropDef.ContainsKey(keyName))
            {
                this._validValueGenTypesPerPropDef.Remove(keyName);
            }
        }
Exemplo n.º 7
0
 private static void ValidateProp(Type type, ISingleValueDef def, string propName)
 {
     if (def == null)
     {
         throw new HabaneroDeveloperException(
                   string.Format("The property '{0}' for the ClassDef for '{1}' is not defined", propName, type),
                   DEVELOPER_MESSAGE);
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructs a new instance of the generator
 /// </summary>
 /// <param name="propDef">The definition of the property for which random values will be generated</param>
 /// <param name="fileName">The filename that contains the available values, including the path.  If the path is relative,
 ///   it will relate to the execution directory.  If no filename is specified, the filename used will be the
 ///   property name (e.g. FirstName) with a ".txt" extension.</param>
 public ValidValueGeneratorTextFile(ISingleValueDef propDef, string fileName = null)
     : base(propDef)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         fileName = propDef.PropertyName + ".txt";
     }
     FileName = fileName;
 }
        private void ClearPreviousInstances(ISingleValueDef propDef)
        {
            var keyName = GetKeyName(propDef);

            if (this._validValueGenTypesPerPropDef.ContainsKey(keyName))
            {
                this._validValueGenTypesPerPropDef.Remove(keyName);
            }
        }
Exemplo n.º 10
0
        protected static ISingleValueDef GetPropDef(IClassDef classDef, string propName, bool raiseErrIfNotExists)
        {
            ISingleValueDef def = classDef.GetPropDef(propName, false);

            if (raiseErrIfNotExists)
            {
                ValidateProp(classDef.ClassType, def, propName);
            }
            return(def);
        }
 /// <summary>
 /// Register a Valid Value Generator to be used for generating values for a specified PropDef.
 /// </summary>
 /// <param name="propDef">The property definition for which this generator type is being assigned</param>
 /// <param name="validValuGenType">The type of generator which will be instantiated when a valid value is needed</param>
 /// <param name="parameter">An additional parameter to pass to the constructor of the generator</param>
 public virtual void Register(ISingleValueDef propDef, Type validValuGenType, object parameter = null)
 {
     _logger.Log("Register valid value gen for PropDef : " + propDef.PropertyName, LogCategory.Debug);
     string boTypeName = GetClassType(propDef);
     ValidateGeneratorType(validValuGenType, boTypeName);
     lock (_lockProp)
     {
         this.ClearPreviousInstances(propDef);
         _validValueGenTypesPerPropDef.Add(GetKeyName(propDef), new ValidValueGeneratorInfo(validValuGenType, parameter));
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Returns a valid prop value for <paramref name="propDef"/>
        /// using any <see cref="IPropRule"/>s for the Prop.
        /// Note_ this value does not take into consideration any
        /// <see cref="InterPropRule"/>s
        /// </summary>
        public virtual object GetValidPropValue(ISingleValueDef propDef)
        {
            if (_defaultValueRegistry.IsRegistered(propDef.PropertyName))
            {
                return(_defaultValueRegistry.Resolve(propDef.PropertyName));
            }

            var valueGenerator = GetValidValueGenerator(propDef);

            return((valueGenerator == null) ? null : valueGenerator.GenerateValidValue());
        }
 protected void CheckPropertyTypeIsCorrect(ISingleValueDef valueDef)
 {
     if (!typeof(T).IsAssignableFrom(valueDef.PropertyType))
     {
         var message =
             string.Format(
                 Messages.You_cannot_use_a_ValidValueGeneratorFromList_for_generating_values_of_typeT,
                 typeof(T), valueDef.PropertyType);
         throw new HabaneroArgumentException(message +
                                             valueDef.ClassName + "_" + valueDef.PropertyName + "'");
     }
 }
        /// <summary>
        /// Register a Valid Value Generator to be used for generating values for a specified PropDef.
        /// </summary>
        /// <param name="propDef">The property definition for which this generator type is being assigned</param>
        /// <param name="validValuGenType">The type of generator which will be instantiated when a valid value is needed</param>
        /// <param name="parameter">An additional parameter to pass to the constructor of the generator</param>
        public virtual void Register(ISingleValueDef propDef, Type validValuGenType, object parameter = null)
        {
            _logger.Log("Register valid value gen for PropDef : " + propDef.PropertyName, LogCategory.Debug);
            string boTypeName = GetClassType(propDef);

            ValidateGeneratorType(validValuGenType, boTypeName);
            lock (_lockProp)
            {
                this.ClearPreviousInstances(propDef);
                _validValueGenTypesPerPropDef.Add(GetKeyName(propDef), new ValidValueGeneratorInfo(validValuGenType, parameter));
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// returns a valid value generator for of the specified type based on the
        /// <see cref="ISingleValueDef"/>.<see cref="ISingleValueDef.PropertyType"/>
        /// </summary>
        /// <param name="propDef"></param>
        /// <returns></returns>
        public ValidValueGenerator GetValidValueGenerator(ISingleValueDef propDef)
        {
            if (propDef.IsLookupList())
            {
                return(new ValidValueGeneratorLookupList(propDef));
            }
            if (propDef.PropertyType.IsEnum)
            {
                return(new ValidValueGeneratorEnum(propDef));
            }

            return(_validValueGenRegistry.Resolve(propDef));
        }
 /// <summary>
 /// Creates a new instance of the generator
 /// </summary>
 /// <param name="singleValueDef">The property definition that indicates which property the values are being generated for</param>
 /// <param name="regExPhrase">The regular expression against which generated values must match</param>
 public ValidValueGeneratorRegEx(ISingleValueDef singleValueDef, string regExPhrase)
     : base(singleValueDef)
 {
     if (regExPhrase == null)
     {
         throw new ArgumentNullException("regExPhrase");
     }
     if (regExPhrase == "")
     {
         throw new ArgumentException("Must supply a non-empty regular expression", "regExPhrase");
     }
     RegExPhrase = regExPhrase;
 }
        /// <summary>
        /// Resolves the registered <see cref="ValidValueGenerator"/> if one is registered.
        /// Else returns null./>
        /// </summary>
        /// <returns></returns>
        public virtual ValidValueGenerator Resolve(ISingleValueDef propDef)
        {
            if (propDef == null)
            {
                throw new ArgumentNullException("propDef");
            }
            var valueType = propDef.PropertyType;

            if (this._validValueGenTypes.ContainsKey(valueType))
            {
                Type validValueGenType = this._validValueGenTypes[valueType];
                return((ValidValueGenerator)Activator.CreateInstance(validValueGenType, propDef));
            }
            return(null);
        }
        protected virtual int GetNextNameReference(ISingleValueDef propDef)
        {
            if (!_nextItemDictionaryRef.ContainsKey(propDef))
            {
                _nextItemDictionaryRef.Add(propDef, 0);
            }
            var nextNameReference = _nextItemDictionaryRef[propDef];

            nextNameReference++;
            if (nextNameReference >= AvailableItemsList.Count)
            {
                nextNameReference = 0;
            }
            _nextItemDictionaryRef[propDef] = nextNameReference;
            return(nextNameReference);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Returns a valid Value for either a single relationship or a property for the <see cref="BusinessObject"/>'s
        /// relationship or prop is identified by the <paramref name="name"/>
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private object GetValidValue(Type type, string name)
        {
            ISingleValueDef relationshipDef = GetRelationshipDef(type, name, false) as ISingleValueDef;

            if (relationshipDef != null)
            {
                return(this.GetValidRelationshipValue(relationshipDef));
            }
            ISingleValueDef def = GetPropDef(type, name, false);

            if (def != null)
            {
                return(this.GetValidPropValue(def));
            }
            ThrowPropDoesNotExist(type, name);
            return(null);
        }
        public void Test_Construct_WithValueDefNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            ISingleValueDef relDefIsNull = null;

            //---------------Assert Precondition----------------
            Assert.IsNull(relDefIsNull);
            //---------------Execute Test ----------------------
            try
            {
                new ValidValueGeneratorFromBOList <FakeBO2>(relDefIsNull, GetEmptyList <FakeBO2>());
                Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("singleValueDef", ex.ParamName);
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Returns a Valid Relationship Value for the relationship <paramref name="relationshipDef"/>
        /// </summary>
        /// <param name="relationshipDef"></param>
        /// <returns></returns>
        public virtual IBusinessObject GetValidRelationshipValue(ISingleValueDef relationshipDef)
        {
            string relationshipName = relationshipDef.PropertyName;

            if (_defaultValueRegistry.IsRegistered(relationshipName))
            {
                return(_defaultValueRegistry.Resolve(relationshipName) as IBusinessObject);
            }
            if (_validValueGenRegistry.IsRegistered(relationshipDef))
            {
                var validValueGenerator = _validValueGenRegistry.Resolve(relationshipDef);
                if (validValueGenerator != null)
                {
                    return(validValueGenerator.GenerateValidValue() as IBusinessObject);
                }
            }
            var classType     = relationshipDef.PropertyType;
            var boTestFactory = BOTestFactoryRegistry.Instance.Resolve(classType);

            return(boTestFactory.CreateValidBusinessObject());
        }
 public ValidValueGeneratorEnum(ISingleValueDef propDef) : base(propDef)
 {
 }
 public ValidValueGeneratorLookupList(ISingleValueDef propDef)
     : base(propDef)
 {
 }
 private static bool IsCompulsory(ISingleValueDef singleValueDef)
 {
     return(singleValueDef != null && singleValueDef.Compulsory);
 }
Exemplo n.º 25
0
 internal static bool IsLookupList(this ISingleValueDef propDef)
 {
     return(!(propDef.LookupList is NullLookupList));
 }
Exemplo n.º 26
0
 private static bool IsNotWriteNotNew(ISingleValueDef propDef)
 {
     return(propDef.ReadWriteRule != PropReadWriteRule.WriteNotNew);
 }
        /// <summary>
        /// returns a valid value generator for of the specified type based on the 
        /// <see cref="ISingleValueDef"/>.<see cref="ISingleValueDef.PropertyType"/>
        /// </summary>
        /// <param name="propDef"></param>
        /// <returns></returns>
        public ValidValueGenerator GetValidValueGenerator(ISingleValueDef propDef)
        {
            if (propDef.IsLookupList())
            {
                return new ValidValueGeneratorLookupList(propDef);
            }
            if (propDef.PropertyType.IsEnum)
            {
                return new ValidValueGeneratorEnum(propDef);
            }

            return _validValueGenRegistry.Resolve(propDef);
        }
        /// <summary>
        /// Returns a valid prop value for <paramref name="propDef"/>
        /// using any <see cref="IPropRule"/>s for the Prop.
        /// Note_ this value does not take into consideration any 
        /// <see cref="InterPropRule"/>s
        /// </summary>
        public virtual object GetValidPropValue(ISingleValueDef propDef)
        {
            if (_defaultValueRegistry.IsRegistered(propDef.PropertyName))
            {
                return _defaultValueRegistry.Resolve(propDef.PropertyName);
            }

            var valueGenerator = GetValidValueGenerator(propDef);
            return ((valueGenerator == null) ? null : valueGenerator.GenerateValidValue());
        }
 /// <summary>
 /// Returns a Valid Relationship Value for the relationship <paramref name="relationshipDef"/>
 /// </summary>
 /// <param name="relationshipDef"></param>
 /// <returns></returns>
 public virtual IBusinessObject GetValidRelationshipValue(ISingleValueDef relationshipDef)
 {
     string relationshipName = relationshipDef.PropertyName;
     if (_defaultValueRegistry.IsRegistered(relationshipName))
     {
         return _defaultValueRegistry.Resolve(relationshipName) as IBusinessObject;
     }
     if (_validValueGenRegistry.IsRegistered(relationshipDef))
     {
         var validValueGenerator = _validValueGenRegistry.Resolve(relationshipDef);
         if (validValueGenerator != null)
         {
             return validValueGenerator.GenerateValidValue() as IBusinessObject;
         }
     }
     var classType = relationshipDef.PropertyType;
     var boTestFactory = BOTestFactoryRegistry.Instance.Resolve(classType);
     return boTestFactory.CreateValidBusinessObject();
 }
 private static ValidValueGeneratorRegEx CreateValueGenerator(string regExPhrase, ISingleValueDef singleValueDef = null)
 {
     if (singleValueDef == null)
     {
         singleValueDef = new PropDefFake
         {
             PropertyType = typeof(string)
         };
     }
     return new ValidValueGeneratorRegEx(singleValueDef, regExPhrase);
 }
Exemplo n.º 31
0
 public ValidValueGeneratorFromBOList(ISingleValueDef singleValueDef, IList <T> availableItems)
     : base(singleValueDef, availableItems)
 {
 }
 private static bool IsInteger(ISingleValueDef propDef)
 {
     return(typeof(int) == propDef.PropertyType || typeof(long) == propDef.PropertyType);
 }
 public ValidValueGeneratorEnum(ISingleValueDef propDef) : base(propDef)
 {
 }
 private static string GetKeyName(ISingleValueDef propDef)
 {
     return GetClassType(propDef) + propDef.PropertyName;
 }
 private static string GetClassType(ISingleValueDef propDef)
 {
     return propDef.ClassDef == null ? "" : propDef.ClassDef.ClassNameFull;
 }
 private static bool IsNotWriteNotNew(ISingleValueDef propDef)
 {
     return propDef.ReadWriteRule != PropReadWriteRule.WriteNotNew;
 }
 /// <summary>
 /// Returns True if a value Gen is registered with the singleValueDef <paramref name="propDef"/>
 /// </summary>
 /// <param name="propDef"></param>
 /// <returns></returns>
 public bool IsRegistered(ISingleValueDef propDef)
 {
     lock (_lockProp)
     {
         var keyName = GetKeyName(propDef);
         return this._validValueGenTypesPerPropDef.ContainsKey(keyName);
     }
 }
 private static void ValidateProp(Type type, ISingleValueDef def, string propName)
 {
     if (def == null)
     {
         throw new HabaneroDeveloperException(
             string.Format("The property '{0}' for the ClassDef for '{1}' is not defined", propName, type),
             DEVELOPER_MESSAGE);
     }
 }
Exemplo n.º 39
0
 public SingleValueTesterTestDouble(ISingleValueDef singleValueDef)
 {
     _singleValueDef = singleValueDef;
 }
        /// <summary>
        /// Resolves the registered <see cref="ValidValueGenerator"/> for the PropDef if one is registered.
        /// Else tries to find a <see cref="ValidValueGenerator"/> for the specified PropDefs Property Type 
        /// using the <see cref="ValidValueGeneratorRegistry"/>
        /// </summary>
        /// <returns></returns>
        public virtual ValidValueGenerator Resolve(ISingleValueDef propDef)
        {
            if (propDef == null)
            {
                throw new ArgumentNullException("propDef");
            }
            lock (_lockProp)
            {
                var keyName = GetKeyName(propDef);
                if (_validValueGenTypesPerPropDef.ContainsKey(keyName))
                {
                    var validValueGen = this._validValueGenTypesPerPropDef[keyName];
                    var validValueGenType = validValueGen.GeneratorType;
                    var parameter = validValueGen.Parameter;
                    if (parameter != null)
                    {
                        if (!GeneratorHasConstructorWithExtraParameter(validValueGenType))
                        {
                            throw new ArgumentException(
                                "An extra parameter was provided for a valid value generator type (" +
                                validValueGenType.Name +
                                "), but no suitable constructor was found with a second parameter.");
                        }
                        return (ValidValueGenerator) Activator.CreateInstance(validValueGenType, propDef, parameter);
                    }
                    return (ValidValueGenerator) Activator.CreateInstance(validValueGenType, propDef);
                }

                return ValidValueGeneratorRegistry.Instance.Resolve(propDef);
            }
        }
 private static string GetClassType(ISingleValueDef propDef)
 {
     return(propDef.ClassDef == null ? "" : propDef.ClassDef.ClassNameFull);
 }
Exemplo n.º 42
0
        /// <summary>
        /// Returns a valid prop value for <paramref name="propName"/>
        /// for the Business object of type <paramref name="classDef"/>
        /// using any <see cref="IPropRule"/>s for the Prop.
        /// Note_ this value does nto atake into consideration any
        /// <see cref="InterPropRule"/>s
        /// </summary>
        protected virtual object GetValidPropValue(IClassDef classDef, string propName)
        {
            ISingleValueDef def = GetPropDef(classDef, propName, true);

            return(this.GetValidPropValue(def));
        }
 private static string GetKeyName(ISingleValueDef propDef)
 {
     return(GetClassType(propDef) + propDef.PropertyName);
 }
Exemplo n.º 44
0
        /// <summary>
        /// Returns a valid prop value for <paramref name="propName"/>
        /// for the Business object of type <paramref name="type"/>
        /// using any <see cref="IPropRule"/>s for the Prop.
        /// Note_ this value does nto atake into consideration any
        /// <see cref="InterPropRule"/>s
        /// </summary>
        public virtual object GetValidPropValue(Type type, string propName)
        {
            ISingleValueDef def = GetPropDef(type, propName, true);

            return(this.GetValidPropValue(def));
        }
Exemplo n.º 45
0
 public ValidValueGeneratorFromBOList(ISingleValueDef singleValueDef) : base(singleValueDef)
 {
 }
 private static ValidValueGeneratorRegEx CreateValueGenerator(string regExPhrase, ISingleValueDef singleValueDef = null)
 {
     if (singleValueDef == null)
     {
         singleValueDef = new PropDefFake
         {
             PropertyType = typeof(string)
         };
     }
     return(new ValidValueGeneratorRegEx(singleValueDef, regExPhrase));
 }