/// <summary> /// Creates an instance for a specific type /// </summary> /// <param name="type">The type the instance should be created for</param> /// <returns>the instance</returns> public static object CreateTypeInstance(Type type) { Bouncer .ForCheckData(() => type) .Assert(new IsNotNullRule <Type>()); return(GetMock(type.FullName) ?? Activator.CreateInstance(type)); }
/// <summary> /// Creates an instance for a specific type /// </summary> /// <param name="type">The type the instance should be created for</param> /// <param name="param1">The type of the 1st ctor parameter for instanciating the class</param> /// <typeparam name="TCtorParam1">The parameter value for instanciating the class</typeparam> /// <returns>the instance</returns> public static object CreateTypeInstance <TCtorParam1>(Type type, TCtorParam1 param1) { Bouncer .ForCheckData(() => type) .Assert(new IsNotNullRule <Type>()); return(GetMock(string.Join(":", type.FullName, typeof(TCtorParam1).FullName)) ?? Activator.CreateInstance(type, param1)); }
public void CheckIntValid0Ax() { const string SomeParameter = ""; Bouncer.ForCheckData(SomeParameter, "someParameter") .Assert(new IsNotNullRule <string>()) .Assert(x => x.ToString() != "0000-00-00") .Assert(new ImplementsInterfaceRule <string>(), typeof(IComparable <>)); }
public void CheckRuleForCheckMultipleFail2() { var x = Bouncer .ForCheckData(() => MessageOneFailRegEx) .ForCheckData(() => MessageOneOk) .Assert(); Assert.IsNotNull(x); }
/// <summary> /// We need to use the type MySaveCustomer in order to correctly resolve type inference for /// Bouncer.ForCheckData(() => customer).Assert(); /// </summary> /// <param name="customer">this customer type does have rule-attributes attached to its properties</param> internal new void WriteCustomerProperties(MyCustomer customer) { Bouncer.ForCheckData(() => customer).Assert(); Console.WriteLine( "calling customer {0} with Id {1}", GetTheName(customer), FormatTheId(customer)); }
/// <summary> /// processes a class name to make it full qualifies include in the assembly name /// </summary> /// <param name="className">The class name that may need processing.</param> /// <returns>The processed full qualified class name.</returns> public string EnrichClassName(string className) { Bouncer.ForCheckData(() => className) .Assert(new StringNotNullOrEmptyRule()); var returnValue = new StringBuilder(); var isFirstFragement = true; var names = className.Split(new[] { " of " }, StringSplitOptions.None); foreach (var name in names) { if (!isFirstFragement) { returnValue.Append(" of "); } if (!name.Contains(",")) { string assemblyName; if (!name.Contains(".")) { if (string.IsNullOrEmpty(this.DefaultNamespace)) { throw new ConfigurationErrorsException("This factory class needs a DefaultNamespace set by the constructor of the DefaultNamespace property to add the default namespace to class names."); } assemblyName = this.DefaultNamespace.Trim(); returnValue.Append(assemblyName).Append("."); if (className == "StdContact" || className == "StdContact") { returnValue.Append("DetailData."); } } else { assemblyName = name.Substring(0, name.LastIndexOf(".", StringComparison.Ordinal)).Trim(); } returnValue.Append(name.Trim()).Append(", ").Append(assemblyName); } else { returnValue.Append(name.Trim()); } isFirstFragement = false; } var result = returnValue.ToString(); if (result.StartsWith("{", StringComparison.Ordinal) && result.EndsWith("}", StringComparison.Ordinal)) { result = ConfigurationManager.AppSettings["factoryName-" + result.Substring(1, result.Length - 2)]; } return(result); }
/// <summary> /// Performs an invoke of an <see cref="Action{T}"/> with the element of the IEnumerable and another parameter for all elements inside the IEnumerable /// </summary> /// <param name="enumerable"> The IEnumerable to get the elements from. </param> /// <param name="action"> The action to be performed (eg. a lambda). </param> /// <param name="parameter1"> The parameter to call the action. </param> /// <typeparam name="T1"> the type parameter of the IEnumerable member </typeparam> /// <typeparam name="T2"> the type parameter of the parameter <paramref name="parameter1"/> member </typeparam> public static void ForEach <T1, T2>(this IEnumerable <T1> enumerable, Action <T1, T2> action, T2 parameter1) { Bouncer .ForCheckData(() => enumerable) .ForCheckData(() => action) .Assert(x => x != null); foreach (var element in enumerable) { action.Invoke(element, parameter1); } }
/// <summary> /// Copy all entries from the source client to the destination client; /// Overwrite existing entries /// </summary> /// <param name="sourceClient"> The source client instance that is the source of data for the copy operation. </param> /// <param name="targetClient"> The source client instance that is the target for the data of the copy operation. </param> /// <param name="baseliClient"> The baseline client is not utilized in this command. </param> /// <param name="sourceStorePath"> The storage path for the source connector. </param> /// <param name="targetStorePath"> The storage path for the target connector </param> /// <param name="baselineStorePath"> The baseline client (and so the storage path) is not utilized in this command. </param> /// <param name="commandParameter"> In this command there is no need for a parameter. </param> /// <returns> Always true. </returns> public bool ExecuteCommand( IClientBase sourceClient, IClientBase targetClient, IClientBase baseliClient, string sourceStorePath, string targetStorePath, string baselineStorePath, string commandParameter) { Bouncer.ForCheckData(() => sourceClient).Assert(new IsNotNullRule <IClientBase>()); Bouncer.ForCheckData(() => targetClient).Assert(new IsNotNullRule <IClientBase>()); targetClient.AddRange(sourceClient.GetAll(sourceStorePath), targetStorePath); return(true); }
/// <summary> /// This command that uses the <see cref="SyncComponent.UiProvider"/> to ask the user if the process should continue. /// </summary> /// <param name="sourceClient">The source client can be NULL - there is no interaction with clients in this command.</param> /// <param name="targetClient">The target client can be NULL - there is no interaction with clients in this command.</param> /// <param name="baseliClient">The baseline client can be NULL - there is no interaction with clients in this command.</param> /// <param name="sourceStorePath">The source storage path can be NULL - there is no interaction with clients in this command.</param> /// <param name="targetStorePath">The target storage path can be NULL - there is no interaction with clients in this command.</param> /// <param name="baselineStorePath">The baseline storage path can be NULL - there is no interaction with clients in this command.</param> /// <param name="commandParameter">The message to be displayed. Must be a non-zero-length string.</param> /// <returns>True if the response from the <see cref="SyncComponent.UiProvider"/> is "continue".</returns> public bool ExecuteCommand( IClientBase sourceClient, IClientBase targetClient, IClientBase baseliClient, string sourceStorePath, string targetStorePath, string baselineStorePath, string commandParameter) { ////Bouncer.ForCheckData(() => this.UiProvider).Assert(new IsNotNullRule<IUiInteraction>()); Bouncer.ForCheckData(() => commandParameter).Assert(new StringMinLengthRule(), 1); return(this.UiProvider == null || this.UiProvider.AskForConfirm( commandParameter, (targetClient == null) ? "Sem.Sync" : targetClient.FriendlyClientName)); }
public void CheckCustomerWithWithMethodAttributes(string customerId, int amount, MyCustomer theCustomer) { Bouncer .ForCheckData(() => customerId) .ForCheckData(() => amount) .ForCheckData(() => theCustomer) .Assert(); var results = Bouncer .ForMessages(() => customerId) .ForMessages(() => amount) .ForMessages(() => theCustomer) .Assert(); Util.PrintEntries(results); }
/// <summary> /// This command deletes (!)files(!) specified by one or more path pattern separated by a line break. /// Deletes files from a folder using a search pattern. Use "*" as a place holder for any /// number of any chars; use "?" as a placeholder for a single char. /// </summary> /// <param name="sourceClient"> The source client. </param> /// <param name="targetClient"> The target client. </param> /// <param name="baseliClient"> The baseline client. </param> /// <param name="sourceStorePath"> The source storage path. </param> /// <param name="targetStorePath"> The target storage path. </param> /// <param name="baselineStorePath"> The baseline storage path. </param> /// <param name="commandParameter"> The command parameter. </param> /// <returns> True if the response from the <see cref="SyncComponent.UiProvider"/> is "continue" </returns> public bool ExecuteCommand( IClientBase sourceClient, IClientBase targetClient, IClientBase baseliClient, string sourceStorePath, string targetStorePath, string baselineStorePath, string commandParameter) { if (string.IsNullOrEmpty(commandParameter)) { commandParameter = targetStorePath; } Bouncer.ForCheckData(() => commandParameter) .Assert(new StringMinLengthRule(), 1); Bouncer.ForCheckData(() => commandParameter).Assert(new StringMinLengthRule(), 1); var deletionCounter = 0; var paths = commandParameter.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var singlePath in paths) { var singlePathWithoutSpaces = singlePath.Trim(); if (!string.IsNullOrEmpty(singlePathWithoutSpaces)) { Tools.EnsurePathExist(Path.GetDirectoryName(singlePathWithoutSpaces)); foreach ( var file in Directory.GetFiles( Path.GetDirectoryName(singlePathWithoutSpaces), Path.GetFileName(singlePathWithoutSpaces))) { File.Delete(file); deletionCounter++; this.LogProcessingEvent(Resources.uiFilesDeleted + ": " + file); } } } this.LogProcessingEvent( Resources.uiFilesDeleted + ": " + deletionCounter.ToString(CultureInfo.CurrentCulture)); return(true); }
/// <summary> /// Creates an instance of an generic type. /// </summary> /// <param name="genericClassName"> /// full class name of the generic type: "namespace.classname, FilenameOfTheAssembly"; see <see cref="Factory"/> for information about the convinience features. /// </param> /// <param name="className"> /// full class name of the type parameter for the generic type: "namespace.classname, FilenameOfTheAssembly"; see <see cref="Factory"/> for information about the convinience features. /// </param> /// <returns> /// a new instance of the class specified with the class name /// </returns> public object GetNewObject(string genericClassName, string className) { var genericClassType = Type.GetType(this.EnrichClassName(genericClassName.Trim())); var classType = Type.GetType(this.EnrichClassName(className.Trim())); Bouncer .ForCheckData(() => classType) .Assert(new IsNotNullRule <Type>()); Bouncer .ForCheckData(() => genericClassType) .Assert(new IsNotNullRule <Type>()); var typeParams = new[] { classType }; Debug.Assert(genericClassType != null, "genericClassType != null"); var constructedType = genericClassType.MakeGenericType(typeParams); return(CreateTypeInstance(constructedType)); }
/// <summary> /// This command deletes files specified by one or more path pattern separated by a line break. /// Deletes files from a folder using a search pattern. Use "*" as a place holder for any /// number of any chars; use "?" as a placeholder for a single char. /// </summary> /// <param name="sourceClient">The source client - will delete "all elements" if this parameter is NULL.</param> /// <param name="targetClient">The target client - must not be null.</param> /// <param name="baseliClient">The baseline client - can be null, because there is no interaction with the baseline in this command.</param> /// <param name="sourceStorePath">The source storage path - the elements found in this path will be deleted.</param> /// <param name="targetStorePath">The target storage path - the deletion will take place in this path.</param> /// <param name="baselineStorePath">The baseline storage path - can be null, because there is no interaction with the baseline in this command.</param> /// <param name="commandParameter">The command parameter - can be null, because there is no parameter for this command.</param> /// <returns>True if the response from the <see cref="SyncComponent.UiProvider"/> is "continue".</returns> public bool ExecuteCommand( IClientBase sourceClient, IClientBase targetClient, IClientBase baseliClient, string sourceStorePath, string targetStorePath, string baselineStorePath, string commandParameter) { Bouncer.ForCheckData(() => targetClient).Assert(new IsNotNullRule <IClientBase>()); targetClient.DeleteElements( sourceClient != null ? sourceClient.GetAll(sourceStorePath) : null, targetStorePath); this.LogProcessingEvent("elements deleted from storage path {0}", targetStorePath); return(true); }
/// <summary> /// Subscribes an object to all messages for which <see cref="IHandleThis{T}"/> is implemented. You cannot /// subscibe for a specific message, the object will be added to the list of subscibers and will receive all /// messages for which the interface <see cref="IHandleThis{T}"/> is implemented. /// </summary> /// <param name="subscriber">The object that implements <see cref="IHandleThis{T}"/> and wants to subscribe /// to messages of type T.</param> public void Subscribe(object subscriber) { Bouncer.ForCheckData(subscriber, "subscriber") .Assert(new IsNotNullRule <object>()) .Assert(x => x.ToString() != "hallo") .Assert(new ImplementsInterfaceRule <object>(), typeof(IHandleThis <>)); if (this.GetWeakReference(subscriber) != null) { return; } lock (_SyncRoot) { if (this.GetWeakReference(subscriber) == null) { this.Subscriptions.Add(new WeakReference(subscriber)); } } }
public void AddRuleForTypeMustSucceed() { Bouncer.ForCheckData(() => this.attributedSampleClass).Assert(); }
public void CheckIntValidWithParameter1() { Bouncer.ForCheckData(1, "var1").Assert((x, y) => x == 1 && y == 7, 7); }
public void AddRuleForTypeMustFail2() { Bouncer.ForCheckData(() => this._MessageOneFail).Assert(); }
public void AddRuleForTypeOk() { var x = Bouncer.ForCheckData(() => MessageOneOk).Assert(); Assert.IsNotNull(x); }
public void AddRuleForTypeMustFailMinMaxNull() { Bouncer.ForCheckData(() => MessageOneFailMinMax).Assert(); }
public void AddRuleForTypeMustFailRegEx() { Bouncer.ForCheckData(() => MessageOneFailRegEx).Assert(); }
public void CheckIntInvalid() { Bouncer.ForCheckData(0, "var0").Assert(x => x == 1); }
/// <summary> /// converts a StdContact into a vCard (binary content) /// </summary> /// <param name="contact"> /// The contact to be converted. /// </param> /// <returns> /// a binary vCard representation /// </returns> public static byte[] StdContactToVCard(StdContact contact) { Bouncer .ForCheckData(() => contact) .Assert(x => x.Name != null); var vCard = new StringBuilder(); vCard.AppendLine("BEGIN:VCARD"); vCard.AppendLine("VERSION:2.1"); AddAttributeToStringBuilder( vCard, "N", contact.Name.LastName, contact.Name.FirstName, contact.Name.MiddleName, contact.Name.AcademicTitle); AddAttributeToStringBuilder(vCard, "FN", contact.GetFullName()); AddAttributeToStringBuilder(vCard, "SORT-STRING", contact.Name.LastName); AddAttributeToStringBuilder(vCard, "EMAIL;TYPE=INTERNET;TYPE=WORK;TYPE=PREF", contact.BusinessEmailPrimary); AddAttributeToStringBuilder(vCard, "EMAIL;TYPE=INTERNET;TYPE=HOME", contact.PersonalEmailPrimary); AddAttributeToStringBuilder(vCard, "URL;TYPE=HOME", contact.PersonalHomepage); if (contact.DateOfBirth.Year > 1900 && contact.DateOfBirth.Year < 2200) { AddAttributeToStringBuilder( vCard, "BDAY", contact.DateOfBirth.ToString("yyyyMMdd", CultureInfo.CurrentCulture)); } if (contact.BusinessAddressPrimary != null) { AddAttributeToStringBuilder( vCard, "ADR;TYPE=WORK", null, null, contact.BusinessAddressPrimary.StreetName, contact.BusinessAddressPrimary.CityName, contact.BusinessAddressPrimary.StateName, contact.BusinessAddressPrimary.PostalCode, contact.BusinessAddressPrimary.CountryName); AddAttributeToStringBuilder(vCard, "TEL;TYPE=WORK", contact.BusinessAddressPrimary.Phone); } if (contact.PersonalAddressPrimary != null) { AddAttributeToStringBuilder( vCard, "ADR;TYPE=HOME", null, null, contact.PersonalAddressPrimary.StreetName, contact.PersonalAddressPrimary.CityName, contact.PersonalAddressPrimary.StateName, contact.PersonalAddressPrimary.PostalCode, contact.PersonalAddressPrimary.CountryName); AddAttributeToStringBuilder(vCard, "TEL;TYPE=HOME", contact.PersonalAddressPrimary.Phone); } AddAttributeToStringBuilder(vCard, "TEL;TYPE=CELL,HOME", contact.PersonalPhoneMobile); AddAttributeToStringBuilder(vCard, "TEL;TYPE=CELL,WORK", contact.BusinessPhoneMobile); AddAttributeToStringBuilder(vCard, "ORG", contact.BusinessCompanyName); AddAttributeToStringBuilder(vCard, "URL;TYPE=WORK", contact.BusinessHomepage); AddAttributeToStringBuilder(vCard, "URL;TYPE=HOME", contact.PersonalHomepage); AddAttributeToStringBuilder(vCard, "TITLE", contact.BusinessPosition); AddAttributeToStringBuilder(vCard, "NOTE", contact.AdditionalTextData); AddAttributeToStringBuilder(vCard, "X-MATZEN-STDUID", contact.Id); AddAttributeToStringBuilder(vCard, "X-MATZEN-GENERATOR", "generated by Sem.Sync - www.svenerikmatzen.info"); AddAttributeToStringBuilder(vCard, "PRODID", "-//MATZEN//www.svenerikmatzen.info//Sem.Sync//Version 1.0"); AddAttributeToStringBuilder(vCard, "PHOTO;TYPE=JPEG", contact.PictureData); AddAttributeToStringBuilder(vCard, "UID", contact.Id.ToString("N")); vCard.AppendLine("END:VCARD"); return(Encoding.UTF8.GetBytes(vCard.ToString())); }
public void AddRuleForTypeMustFail() { Bouncer.ForCheckData(() => (string)null).Assert(); }
public void AddRuleForTypeOk() { Bouncer.ForCheckData(() => BouncerAttributedRuleTest.MessageOneOk).Assert(); }
public void AddRuleForTypeMustFail() { Bouncer.ForCheckData(() => BouncerAttributedRuleTest.MessageFailNamespace).Assert(); }
public void CheckIntInvalidWithParameter() { Bouncer.ForCheckData(0, "var0").Assert((x, y) => x == 1, 7); }
public void AddRuleForType1() { Bouncer.ForCheckData(() => "2").Assert(); }
public void AddRuleForType2() { Bouncer.ForCheckData(() => this._MessageTwoOk).Assert(); }
public void AddRuleForTypeOk() { this.attributedSampleClass.MustBeLengthAndNamespace = "hello!"; Bouncer.ForCheckData(() => this.attributedSampleClass).Assert(); }
public void AddRuleForType2MustFail() { Bouncer.ForCheckData(() => this._MessageTwoFail).Assert(); }