private void genrateSampleTemplate() { string strFrom = textBoxFrom.Text; string strTo = textBoxTo.Text; string strCC = textBoxCC.Text; string strSubject = textBoxSubject.Text; string strTemplate = textBoxTemplate.Text; string strWorkPath = textBoxWorkPath.Text; string strPreProcessedFolder = textBoxPreProcessedFolder.Text; string strProcessedFolder = textBoxProcessedFolder.Text; string strTestcasePrefix = textBoxTestCasePrefix.Text; string strLogfilePrefix = textBoxLogfilePrefix.Text; int intKeepOpen = StrToIntDef(textBoxKeepOpen.Text, 3); processTimeoutInSecond = StrToIntDef(textBoxProcessTimeout.Text, 10); textBoxKeepOpen.Text = intKeepOpen.ToString(); Boolean isURLEncoded = checkBoxURLDecode.Checked; Boolean isAutoRun = checkBoxAutoRun.Checked; timestampLogger = new TimestampLogging(strWorkPath + "\\" + strLogfilePrefix); payloadsProcessedTestcaseFilesLogger = new SimpleLogging(strWorkPath + "\\" + textBoxProcessedTestcaseFilesFilename.Text); //logger.log("==Begin=="); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method Action <object, string, string, string, int> longMethod_showCloseMoveEmail = showCloseMoveEmail; try { string strPreProcessedFolderFullPath = strWorkPath + "\\" + strPreProcessedFolder + "\\"; string strProcessedFolderFullPath = strWorkPath + "\\" + strProcessedFolder + "\\"; System.IO.Directory.CreateDirectory(strPreProcessedFolderFullPath); System.IO.Directory.CreateDirectory(strProcessedFolderFullPath); if (isURLEncoded) { timestampLogger.log("URL Decoding..."); strTemplate = Uri.UnescapeDataString(strTemplate); } MailMessage msg = new MailMessage(); // Set recipients information if (!String.IsNullOrEmpty(strFrom)) { msg.From = strFrom; } if (!String.IsNullOrEmpty(strTo)) { msg.To = strTo; } if (!String.IsNullOrEmpty(strCC)) { msg.CC = strCC; } // Set the subject msg.Subject = strSubject; // Set HTML body msg.HtmlBody = strTemplate; // Add an attachment // msg.Attachments.Add(new Aspose.Email.Mail.Attachment("test.txt")); // Local filenames string strTestCaseFileName = MakeValidFileName(strTestcasePrefix + strSubject + ".msg"); string strInProgressFilePath = strPreProcessedFolderFullPath + strTestCaseFileName; //msg.Save(strFullFilePath,SaveOptions.DefaultMsg); MapiMessage outlookMsg = MapiMessage.FromMailMessage(msg); outlookMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_SUBMIT); while (File.Exists(strInProgressFilePath)) { strInProgressFilePath = strPreProcessedFolderFullPath + Guid.NewGuid() + "_" + strTestCaseFileName; } timestampLogger.log("Saving the " + strTestCaseFileName + " file in: " + strInProgressFilePath); outlookMsg.Save(strInProgressFilePath); if (isAutoRun) { new Thread(delegate() { //showCloseMoveEmail(strInProgressFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method object monitorSync = new object(); bool timedOut; lock (monitorSync) { longMethod_showCloseMoveEmail.BeginInvoke(monitorSync, strInProgressFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen, null, null); timedOut = !Monitor.Wait(monitorSync, TimeSpan.FromSeconds(processTimeoutInSecond)); } if (timedOut) { object _lock = new object(); lock (_lock) { killProcess("OUTLOOK", processTimeoutInSecond); timestampLogger.log("Process killed due to " + processTimeoutInSecond.ToString() + " seconds timeout"); } } }).Start(); } } catch (Exception e) { timestampLogger.log("Error in genrateSampleTemplate(): " + e.StackTrace); } //logger.log("==End=="); }
private void RunTestCases() { string strWorkPath = textBoxWorkPath.Text; string strPreProcessedFolder = textBoxPreProcessedFolder.Text; string strProcessedFolder = textBoxProcessedFolder.Text; string strTestcasePrefix = textBoxTestCasePrefix.Text; string strLogfilePrefix = textBoxLogfilePrefix.Text; buttonControl.Enabled = true; int intKeepOpen = StrToIntDef(textBoxKeepOpen.Text, 3); processTimeoutInSecond = StrToIntDef(textBoxProcessTimeout.Text, 10); textBoxKeepOpen.Text = intKeepOpen.ToString(); timestampLogger = new TimestampLogging(strWorkPath + "\\" + strLogfilePrefix); string strPreProcessedFolderFullPath = strWorkPath + "\\" + strPreProcessedFolder + "\\"; string strProcessedFolderFullPath = strWorkPath + "\\" + strProcessedFolder + "\\"; payloadsProcessedTestcaseFilesLogger = new SimpleLogging(strWorkPath + "\\" + textBoxProcessedTestcaseFilesFilename.Text); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method Action <object, string, string, string, int> longMethod_showCloseMoveEmail = showCloseMoveEmail; int intTestcaseRunnerThread = StrToIntDef(textBoxThreadTestcaseRunner.Text, 10); if (Directory.Exists(strPreProcessedFolderFullPath)) { string[] files = Directory.GetFiles(strPreProcessedFolderFullPath, "*.msg", SearchOption.AllDirectories); long totalEvents = files.Length; ResetCaseID(); setLabelStatus(0, totalEvents); bool timedOut = false; Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = intTestcaseRunnerThread }, (strTestCaseFilePath, loopStateFiles) => { string strTestCaseFileName = Path.GetFileName(strTestCaseFilePath); mre.WaitOne(); //showCloseMoveEmail(strTestCaseFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method object monitorSync = new object(); timedOut = false; lock (monitorSync) { longMethod_showCloseMoveEmail.BeginInvoke(monitorSync, strTestCaseFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen, null, null); timedOut = !Monitor.Wait(monitorSync, TimeSpan.FromSeconds(processTimeoutInSecond)); } if (timedOut) { object _lock = new object(); lock (_lock) { killProcess("OUTLOOK", processTimeoutInSecond); timestampLogger.log("Process killed due to " + processTimeoutInSecond.ToString() + " seconds timeout"); } } long currentCaseID = GetNextValue(); if (currentCaseID % intTestcaseRunnerThread == 0 || currentCaseID == totalEvents) { setLabelStatus(currentCaseID, totalEvents); } }); if (timedOut) { additionalStatusDetails = "Completed with errors: use Testcase Runner again!"; } } else { MessageBox.Show("Directory not found: " + strPreProcessedFolderFullPath); } }
private void genrateSpecialTestCases() { string strFrom = textBoxFrom.Text; string strTo = textBoxTo.Text; string strCC = textBoxCC.Text; string strSubject = textBoxSubject.Text; string strTemplate = textBoxTemplate.Text; string strWorkPath = textBoxWorkPath.Text; string strPreProcessedFolder = textBoxPreProcessedFolder.Text; string strProcessedFolder = textBoxProcessedFolder.Text; string strTestcasePrefix = textBoxTestCasePrefix.Text; string strLogfilePrefix = textBoxLogfilePrefix.Text; string strPayloadPattern = textBoxPayloadPattern.Text; int intKeepOpen = StrToIntDef(textBoxKeepOpen.Text, 3); processTimeoutInSecond = StrToIntDef(textBoxProcessTimeout.Text, 10); textBoxKeepOpen.Text = intKeepOpen.ToString(); Boolean isURLEncoded = checkBoxURLDecode.Checked; Boolean isAutoRun = checkBoxAutoRun.Checked; timestampLogger = new TimestampLogging(strWorkPath + "\\" + strLogfilePrefix); payloadsListLogger = new SimpleLogging(strWorkPath + "\\" + textBoxPayloadsListFilename.Text); payloadsProcessedTestcaseFilesLogger = new SimpleLogging(strWorkPath + "\\" + textBoxProcessedTestcaseFilesFilename.Text); //logger.log("==Begin=="); try { caseID = 0; string[] arrPrefix = UniqueTextArrayFromFile(textBoxPrefix.Text); string[] arrSuffix = UniqueTextArrayFromFile(textBoxSuffix.Text); string[] arrFormula = UniqueTextArrayFromFile(textBoxFormula.Text); string[] arrSchemes = UniqueTextArrayFromFile(textBoxSchemes.Text); string[] arrTargets = UniqueTextArrayFromFile(textBoxTargets.Text); string[] arrSpecialFormula = UniqueTextArrayFromFile(textBoxSpecialFormula.Text); string strPreProcessedFolderFullPath = strWorkPath + "\\" + strPreProcessedFolder + "\\"; string strProcessedFolderFullPath = strWorkPath + "\\" + strProcessedFolder + "\\"; System.IO.Directory.CreateDirectory(strPreProcessedFolderFullPath); System.IO.Directory.CreateDirectory(strProcessedFolderFullPath); if (isURLEncoded) { timestampLogger.log("URL Decoding..."); strTemplate = Uri.UnescapeDataString(strTemplate); } string strTemplateSha1Sig = SHA1FromString(strTemplate); int intPrefixParalDegree = StrToIntDef(textBoxThreadPrefixHigh.Text, 1); int intTargetsParalDegree = StrToIntDef(textBoxThreadTargetsHigh.Text, 2); int intSpecialFormulaParalDegree = StrToIntDef(textBoxThreadSpecialFormulaHigh.Text, 5); int intSuffixParalDegree = StrToIntDef(textBoxThreadSuffixHigh.Text, 1); if (isAutoRun) { intPrefixParalDegree = StrToIntDef(textBoxThreadPrefixLow.Text, 1); intTargetsParalDegree = StrToIntDef(textBoxThreadTargetsLow.Text, 1); intSpecialFormulaParalDegree = StrToIntDef(textBoxThreadSpecialFormulaLow.Text, 3); intSuffixParalDegree = StrToIntDef(textBoxThreadSuffixLow.Text, 1); } long totalEvents = arrPrefix.Length * arrTargets.Length * arrSpecialFormula.Length * arrSuffix.Length; progressBarStatus.Value = 0; setLabelStatus(0, totalEvents); ResetCaseID(); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method Action <object, string, string, string, int> longMethod_showCloseMoveEmail = showCloseMoveEmail; Parallel.ForEach(arrPrefix, new ParallelOptions { MaxDegreeOfParallelism = intPrefixParalDegree }, (strPrefix, loopStatePrefix) => { Parallel.ForEach(arrTargets, new ParallelOptions { MaxDegreeOfParallelism = intTargetsParalDegree }, (strTarget, loopStateTargets) => { Parallel.ForEach(arrSpecialFormula, new ParallelOptions { MaxDegreeOfParallelism = intSpecialFormulaParalDegree }, (strSpecialFormula, loopSpecialFormula) => { Parallel.ForEach(arrSuffix, new ParallelOptions { MaxDegreeOfParallelism = intSuffixParalDegree }, (strSuffix, loopStateSuffix) => { if (Interlocked.Read(ref paused) == 1) { mre.WaitOne(); } long currentCaseID = GetNextValue(); string strPayload = strSpecialFormula.Replace("<$target$>", strTarget); strPayload = strPrefix + strPayload + strSuffix + currentCaseID.ToString(); payloadsListLogger.log(strTemplateSha1Sig + "," + strPayload); string strTempTemplate = System.Text.RegularExpressions.Regex.Replace(strTemplate, strPayloadPattern, strPayload); string strTempSubject = System.Text.RegularExpressions.Regex.Replace(strSubject, strPayloadPattern, strPayload); string strTempFrom = System.Text.RegularExpressions.Regex.Replace(strFrom, strPayloadPattern, strPayload); string strTempTo = System.Text.RegularExpressions.Regex.Replace(strTo, strPayloadPattern, strPayload); string strTempCC = System.Text.RegularExpressions.Regex.Replace(strCC, strPayloadPattern, strPayload); MailMessage msg = new MailMessage(); // Set recipients information if (!String.IsNullOrEmpty(strTempFrom)) { msg.From = strTempFrom; } if (!String.IsNullOrEmpty(strTempTo)) { msg.To = strTempTo; } if (!String.IsNullOrEmpty(strTempCC)) { msg.CC = strTempCC; } // Set the subject msg.Subject = strTempSubject; // Set HTML body msg.HtmlBody = strTempTemplate; // Add an attachment // msg.Attachments.Add(new Aspose.Email.Mail.Attachment("test.txt")); // Local filenames string strTestCaseFileName = MakeValidFileName(strTestcasePrefix + currentCaseID.ToString() + "-" + strTempSubject + ".msg"); string strInProgressFilePath = strPreProcessedFolderFullPath + strTestCaseFileName; //msg.Save(strFullFilePath,SaveOptions.DefaultMsg); MapiMessage outlookMsg = MapiMessage.FromMailMessage(msg); outlookMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_SUBMIT); while (File.Exists(strInProgressFilePath)) { strInProgressFilePath = strPreProcessedFolderFullPath + Guid.NewGuid() + "_" + strTestCaseFileName; } timestampLogger.log("Saving the " + strTestCaseFileName + " file in: " + strInProgressFilePath); outlookMsg.Save(strInProgressFilePath); if (isAutoRun) { mre.WaitOne(); //showCloseMoveEmail(strInProgressFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen); // to kill it on timeout: http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method object monitorSync = new object(); bool timedOut; lock (monitorSync) { longMethod_showCloseMoveEmail.BeginInvoke(monitorSync, strInProgressFilePath, strProcessedFolderFullPath, strTestCaseFileName, intKeepOpen, null, null); timedOut = !Monitor.Wait(monitorSync, TimeSpan.FromSeconds(processTimeoutInSecond)); } if (timedOut) { object _lock = new object(); lock (_lock) { killProcess("OUTLOOK", processTimeoutInSecond); timestampLogger.log("Process killed due to " + processTimeoutInSecond.ToString() + " seconds timeout"); } } if (currentCaseID % 10 == 0 || currentCaseID == totalEvents) { setLabelStatus(currentCaseID, totalEvents); } } else { if (currentCaseID % 100 == 0 || currentCaseID == totalEvents) { setLabelStatus(currentCaseID, totalEvents); } } } ); } ); } ); } ); } catch (Exception e) { timestampLogger.log("Error in genrateSpecialTestCases(): " + e.StackTrace); MessageBox.Show("An error occured!", "Error!"); } //logger.log("==End=="); }