public static void Main(string[] args) { Console.WriteLine("Application Started"); //var logResults = LoadTopTen(); try { using (var cntx = new UserDBEntities()) { var user = new User(); user.Email = "*****@*****.**"; user.LastLogin = DateTime.Now; user.UserName = "******"; user.Password = "******"; var u = cntx.Users.FirstOrDefault(x => x.UserName == "test"); if (u == null) { cntx.Users.Add(user); cntx.SaveChanges(); } else { u.LastLogin = DateTime.Now; } } var listUri = new List <Uri>(); for (int i = 0; i < args.Length; i++) { try { string url = args[i]; if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { url = "http://" + url; } Uri uri; if (Uri.TryCreate(url, UriKind.Absolute, out uri) || Uri.TryCreate("http://" + url, UriKind.Absolute, out uri)) { listUri.Add(uri); } } catch (UriFormatException e) { Console.WriteLine("Error {1} web site uri {0} has wrong format. {2}", args[i], i, e.Message); } } if (listUri.Count == 0) { throw new Exception("No valid web urls. Check your command line params"); } TopTenComponent.TopTen tt = new TopTenComponent.TopTen(); tt.NamesOnly = false; tt.HtmlDownloadedEvent += ShowMessage; tt.HtmlRemovedEvent += ShowMessage; tt.WordListCreatedEvent += ShowMessage; tt.WordCountCalculatedEvent += ShowMessage; //tt.WordCountsCalculated2 += ShowMessageWithLong; tt.TopTenMadeEvent += ShowMessage; tt.TopTenMadeEvent2 += ShowMessageWithLong; var res1 = tt.getHtmlFromUrls(listUri); var res2 = tt.removeHTMLs(res1); var res3 = tt.getWordListFromTexts(res2); var res4 = new List <Dictionary <String, int> >(); foreach (var l in res3) { res4.Add(tt.getWordCounts(l)); } var res5 = tt.makeTop10s(res4); foreach (var item in res5) { if (item.Value > 10) { Console.WriteLine("Word {0} appears {1} times", item.Key, item.Value); } } SaveTopTen(res5); } catch (NullReferenceException e) { Console.WriteLine(e.Message + " Error "); } catch (WebException e) { // No internet connection Console.WriteLine(e.Message + " Error "); throw e; } catch (Exception e) { //Some "Generic" error Console.WriteLine(e.Message + " Error "); } finally { //Finally is executed ALWAYS //Greaat place to do clean up work Console.WriteLine("Application Closing"); } Console.ReadLine(); }
private void button1_Click(object sender, EventArgs e) { textBoxWords.Clear(); //string[] args = new string[] { "www.klix.ba", "www.radiosarajevo.ba" }; string[] args = textBoxWeb.Text.Split('\n'); //Console.WriteLine("Application Started"); //textBoxWords.AppendText("Application Started"); try { //create Urls and validate them var listUri = new List <Uri>(); for (int i = 0; i < args.Length; i++) { try { string url = args[i]; if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { url = "http://" + url; } Uri uri; if (Uri.TryCreate(url, UriKind.Absolute, out uri) || Uri.TryCreate("http://" + url, UriKind.Absolute, out uri)) { listUri.Add(uri); } } catch (UriFormatException ex) { // Console.WriteLine("Error {1} web site uri {0} has wrong format. {2}", args[i], i, e.Message); textBoxWords.AppendText("Error " + i.ToString() + " web site uri: " + args[i] + " has wrong format." + ex.Message + "\n"); } } if (listUri.Count == 0) { throw new Exception("No valid web urls. Check your command line params" + "\n"); } TopTenComponent.TopTen tt = new TopTenComponent.TopTen(); //Subscribe to events listBox1.Items.Clear(); tt.HtmlDownloadedEvent += Tt_HtmlRemovedEvent; tt.HtmlRemovedEvent += Tt_HtmlRemovedEvent; // tt.HtmlRemovedEvent2 += Tt_HtmlRemovedEvent2; //tt.TopTenMadeEvent2 += Tt_HtmlRemovedEvent2; tt.WordCountCalculatedEvent += Tt_HtmlRemovedEvent; tt.TopTenMadeEvent += Tt_HtmlRemovedEvent; var res1 = tt.getHtmlFromUrls(listUri); var res2 = tt.removeHTMLs(res1); var res3 = tt.getWordListFromTexts(res2); var res4 = new List <Dictionary <String, int> >(); foreach (var l in res3) { res4.Add(tt.getWordCounts(l)); } var res5 = tt.makeTop10s(res4); int counter = 0; foreach (var item in res5) { counter++; //Console.WriteLine("Word {0} appears {1} times", item.Key, item.Value); textBoxWords.AppendText(counter.ToString() + ". Word " + item.Key + " appears " + item.Value.ToString() + " times\n"); if (counter == 10) { break; } } } catch (NullReferenceException ex) { //Console.WriteLine(e.Message + " Error "); textBoxWords.AppendText(ex.Message + " Error "); } catch (WebException ex) { // No internet connection //Console.WriteLine(e.Message + " Error "); textBoxWords.AppendText(ex.Message + " Error "); } catch (Exception ex) { //Some "Generic" error //Console.WriteLine(e.Message + " Error "); textBoxWords.AppendText(ex.Message + " Error "); } finally { //Finally is executed ALWAYS //Greaat place to do clean up work //Console.WriteLine("Application Closing"); // textBoxWords.AppendText("\n"+"Application Closing"+"\n"); } }
public static void Main(string[] args) { Console.WriteLine("Application Started"); TopTenComponent.TopTen tt = new TopTenComponent.TopTen(); }
public static void Main(string[] args) { TopTenComponent.TopTen tt = new TopTenComponent.TopTen(); Console.WriteLine("Starting Test"); string testName; { testName = "TEST getHtmlFromUrls"; var result = tt.getHtmlFromUrls(new List <Uri> { new Uri("http://www.klix.ba") }); if (result[0].Length > 1000) { passed(testName); } else { failed(testName); } } { testName = "TEST getHtmlFromUrls 1"; // Create new Test Case for getHtmlFromUrls when invalid Uri is passed // when getHtmlFromUrls( new List {new Uri( " bad_uri "} ) is call with bad Url strings it should throw InvalidUriFormat exception try { var result = tt.getHtmlFromUrls(new List <Uri> { new Uri("www.klix.ba") }); //no exception was thrown failed(testName); } catch (Exception e) { //Console.WriteLine("Exception was thrown: " + e.Message); //exception was thrown - test is successfull passed(testName); } } { testName = "TEST removeHTMLs"; var result = tt.removeHTMLs(new List <String> { "<h1> TEST </h2>" }); if (result[0].Trim() == "TEST") { passed(testName); } else { failed(testName); } } { testName = "TEST getWordCounts"; var result = tt.getWordCounts(new List <String> { "juce", "danas", "sutra", "danas" }); if (result["danas"] == 2) { passed(testName); } else { failed(testName); } } { testName = "TEST getWordListFromTexts"; var result = tt.getWordListFromTexts(new List <String> { "juce,danas i sutra" });; if (result[0].Count == 4) { passed(testName); } else { failed(testName); } } { testName = "TEST makeTop10s"; Dictionary <string, int> d1 = new Dictionary <string, int>(); d1.Add("danas", 2); d1.Add("sutra", 1); Dictionary <string, int> d2 = new Dictionary <string, int>() { { "danas", 5 }, { "juce", 3 } }; var result = tt.makeTop10s(new List <Dictionary <string, int> > { d1, d2 }); if (result["danas"] == 7) { passed(testName); } else { failed(testName); } } // test Url without scheme { testName = "TEST URLsWithoutScheme"; bool ok = false; try { string[] test_urls = new string[] { "www.klix.ba", "www.radiosarajevo.ba" }; TopTenApp.MainClass ttApp = new TopTenApp.MainClass(); TopTenApp.MainClass.Main(test_urls); ok = true; } catch (Exception e) { ok = false; } if (ok == true) { passed(testName); } else { failed(testName); } } // test Url without scheme - fail { testName = "TEST URLsWithoutScheme 1"; bool ok = false; try { string[] test_urls = new string[] { "https://www.klix.ba", "ww.radiosarajevo.ba" }; TopTenApp.MainClass ttApp = new TopTenApp.MainClass(); TopTenApp.MainClass.Main(test_urls); ok = true; } catch { ok = false; } if (ok == true) { passed(testName); } else { failed(testName); } } Console.ReadLine(); }