00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using NUnit.Framework;
00005 using System.IO;
00006 using System.Reflection;
00007 using Shared;
00008 using System.Collections;
00009 using System.Data;
00010 using Server.Data;
00011
00012 namespace Server.Test
00013 {
00017 public class PersistentDataTest
00018 {
00023 public static void TestDatabase(IPersistentData database)
00024 {
00025 TestDatabase(database, 10, 10, 10, 2, 10, 10);
00026 }
00027
00032 public static void TestDatabase(IPersistentData database, int literatureCount, int projectCount, int peopleCount, int commentLevel, int commentCount, int tagCount)
00033 {
00034 DataContainer data = new DataContainer();
00035 Dictionary<int, Comment> allComments = new Dictionary<int,Comment>();
00036 for (int i = 0; i < peopleCount; i++)
00037 {
00038 Person p = new Person("user" + i, Utils.HashedValue(i + "ads"), "Person " + i, "Department " + (i % 10), "Email " + i);
00039 data.PersonTable.Add(p.UserName, p);
00040 }
00041 data.PersonTable["user"+5].Moderator = true;
00042 data.PersonTable["user"+(peopleCount-1)].Moderator = true;
00043
00044 for (int i = 0; i < literatureCount; i++)
00045 {
00046 int uid = UniquelyIdentifiable.NextID;
00047 Literature p = new Literature(uid, data.PersonTable["user"+5], "Literature " + uid, "Author " + uid, "Summary " + uid, "Source " + uid, "ISBN " + uid, DateTime.Now, LiteratureType.Book);
00048 p.ISBN = "ISBN " + uid;
00049 data.LiteratureTable.Add(p.ID, p);
00050
00051 CreateShitloadOfComments(commentLevel, commentCount, data, p.Comments, allComments, CommentType.Literature);
00052
00053 }
00054 Random rand = new Random();
00055 for (int i = 0; i < projectCount; i++)
00056 {
00057 int uid = UniquelyIdentifiable.NextID;
00058 Project p = new Project(uid, DateTime.Now, DateTime.Now, "Test Project " + uid, "Project " + uid, "Department " + uid, "Test Synopsis " + uid, false);
00059
00060 data.ProjectTable.Add(p.ID, p);
00061
00062 foreach(Person person in data.PersonTable.Values)
00063 {
00064 p.AddPerson(person, RoleType.Member);
00065 }
00066 p.Roles[0].Type = RoleType.Creator;
00067 for (int j = 1; j < peopleCount / 2; j++)
00068 p.Roles[j].Type = RoleType.Supervisor;
00069 int hat = data.LiteratureTable.Count / 3;
00070 IEnumerator<Literature> ea = data.LiteratureTable.Values.GetEnumerator();
00071 for (int j = 0; j < hat; j++)
00072 {
00073 if (!ea.MoveNext())
00074 break;
00075 Reference r = new Reference(p, ea.Current, DateTime.Now, true, "status " + p.ID + ea.Current.ID.ToString());
00076 CreateShitloadOfComments(commentLevel, commentCount, data, r.Comments, allComments, CommentType.Reference);
00077 p.References.Add(r);
00078 }
00079 for (int j = hat; j < hat*2; j++)
00080 {
00081 if (!ea.MoveNext())
00082 break;
00083
00084 Suggestion s = new Suggestion(p, p.Roles[rand.Next(peopleCount / 2 - 1) + 1].Person, ea.Current, DateTime.Now, true, "status " + p.ID + ea.Current.ID.ToString(), "reason " + p.ID + ea.Current.ID.ToString());
00085 CreateShitloadOfComments(commentLevel, commentCount, data, s.Comments, allComments, CommentType.Reference);
00086 p.References.Add(s);
00087 }
00088 for (int j = hat*2; j < hat*3; j++)
00089 {
00090 if (!ea.MoveNext())
00091 break;
00092
00093 Review rv = new Review(p, p.Roles[rand.Next(peopleCount / 2) + peopleCount / 2].Person, ea.Current, DateTime.Now, true, "status " + p.ID + ea.Current.ID.ToString(), "title " + p.ID + ea.Current.ID.ToString(), "text " + +p.ID + ea.Current.ID.ToString(), p.ID + ea.Current.ID);
00094 CreateShitloadOfComments(commentLevel, commentCount, data, rv.Comments, allComments, CommentType.Reference);
00095 p.References.Add(rv);
00096 }
00097
00098 }
00099 foreach(Literature l in data.LiteratureTable.Values) {
00100 for (int i = 0; i < tagCount; i++)
00101 {
00102 int pTag = rand.Next(peopleCount) + 1, p = 0;
00103
00104 foreach (Person person in data.PersonTable.Values)
00105 {
00106 if (p++ >= pTag)
00107 break;
00108
00109 data.Tags.Rows.Add("Tag" + i, l.ID, person.UserName);
00110 }
00111 }
00112 }
00113 foreach (Project l in data.ProjectTable.Values)
00114 {
00115 for (int i = 0; i < tagCount; i++)
00116 {
00117 int pTag = rand.Next(peopleCount) + 1, p = 0;
00118
00119 foreach (Person person in data.PersonTable.Values)
00120 {
00121 if (p++ >= pTag)
00122 break;
00123
00124 data.Tags.Rows.Add("Tag" + i, l.ID, person.UserName);
00125 }
00126 }
00127 }
00128 data.Tags.AcceptChanges();
00129
00130 database.SaveData(data);
00131
00132
00133
00134 DataContainer newData = database.LoadData();
00135
00136 Assert.AreEqual(data.ProjectTable.Count, newData.ProjectTable.Count, "Project Table Count Test");
00137 Assert.AreEqual(data.LiteratureTable.Count, newData.LiteratureTable.Count, "Literature Table Count Test");
00138 Assert.AreEqual(data.PersonTable.Count, newData.PersonTable.Count, "People Table Count Test");
00139 Assert.AreEqual(data.Tags.Rows.Count, newData.Tags.Rows.Count, "Tag Table Count Test");
00140
00141
00142 foreach (Project p in data.ProjectTable.Values)
00143 {
00144 Console.WriteLine(p.Info.ToString());
00145 bool exists = newData.ProjectTable.ContainsKey(p.ID);
00146 Assert.IsTrue(exists, "Project Table Consistency Test");
00147 if (exists)
00148 {
00149 Console.Write(p.Info.ToString());
00150
00151 Project p2 = newData.ProjectTable[p.ID];
00152 Assert.AreEqual(p.Info, p2.Info, "Project Attribute Test");
00153 foreach (Role role in p.Roles)
00154 {
00155 Role newRole = p2.GetPerson(role.Person.UserName);
00156 Assert.IsNotNull(newRole);
00157 if (newRole != null)
00158 {
00159 Assert.AreEqual(newRole.Info, role.Info, "Role Attribute Test");
00160 }
00161 }
00162
00163 foreach (Reference r in p.References)
00164 {
00165 Reference r2 = null;
00166 foreach (Reference r3 in p2.References)
00167 {
00168 if (r3.Info.Equals(r.Info))
00169 {
00170 r2 = r3;
00171 break;
00172 }
00173 }
00174 Assert.IsNotNull(r2, "Reference Exists Test");
00175
00176 if (r is Reference)
00177 {
00178 Assert.AreEqual(r.Info, r2.Info, "Reference Consistency Test");
00179 }
00180 else if(r is Review)
00181 {
00182 Assert.AreEqual(((Review)r).Info, ((Review)r2).Info, "Review Consistency Test");
00183 }
00184 else if (r is Suggestion)
00185 {
00186 Assert.AreEqual(((Suggestion)r).Info, ((Suggestion)r2).Info, "Suggestion Consistency Test");
00187 }
00188
00189 TestComments(r.Comments, r2.Comments);
00190 }
00191
00192
00193 }
00194 }
00195
00196 foreach (Literature p in data.LiteratureTable.Values)
00197 {
00198 bool exists = newData.LiteratureTable.ContainsKey(p.ID);
00199 Assert.IsTrue(exists, "Literature Table Consistency Test");
00200 if (exists)
00201 {
00202 Literature p2 = newData.LiteratureTable[p.ID];
00203 Assert.AreEqual(p.Info, p2.Info, "Literature Attribute Test");
00204 TestComments(p.Comments, p2.Comments);
00205 }
00206
00207 }
00208
00209 foreach (Person p in data.PersonTable.Values)
00210 {
00211 bool exists = newData.PersonTable.ContainsKey(p.UserName);
00212 Assert.IsTrue(exists, "Person Table Consistency Test");
00213 if (exists)
00214 {
00215 Person p2 = newData.PersonTable[p.UserName];
00216 Assert.AreEqual(p.Info, p2.Info, "Person Attribute Test");
00217 }
00218 }
00219
00220 foreach (DataRow row in data.Tags.Rows)
00221 {
00222 Assert.IsTrue(newData.Tags.Rows.Contains(new object[] { row[0], row[1], row[2] }));
00223 }
00224 }
00225
00231 protected static void TestComments(List<Comment> list, List<Comment> list_2)
00232 {
00233 Assert.AreEqual(list.Count, list_2.Count, "Comment list amount test");
00234 foreach (Comment c in list)
00235 {
00236 Comment c2 = null;
00237 foreach (Comment c3 in list_2)
00238 {
00239 if (c3.ID == c.ID)
00240 {
00241 c2 = c3;
00242 break;
00243 }
00244 }
00245
00246 Assert.IsNotNull(c2, "Comment Exists Test");
00247 Assert.AreEqual(c.Date, c2.Date, "Comment Date Test");
00248 Assert.AreEqual(c.Poster.UserName, c2.Poster.UserName, "Comment Poster Test");
00249 Assert.AreEqual(c.Text, c2.Text, "Comment Text Test");
00250 Assert.AreEqual(c.Title, c2.Title, "Comment Title Test");
00251 if (c2 != null)
00252 TestComments(c.Comments, c2.Comments);
00253 }
00254 }
00264 protected static void CreateShitloadOfComments(int level, int commentCount, DataContainer data, List<Comment> comments, Dictionary<int, Comment> allComments, CommentType type)
00265 {
00266 if (level <= 0)
00267 return;
00268
00269 Random r = new Random();
00270
00271 for (int i = 0; i < commentCount; i++)
00272 {
00273 int ui = UniquelyIdentifiable.NextID;
00274 Comment c = new Comment(ui, data.PersonTable["user" + r.Next(data.PersonTable.Count)], "Comment " + (ui), "Text " + (ui), type);
00275 comments.Add(c);
00276 allComments.Add(c.ID, c);
00277 CreateShitloadOfComments(level - 1, commentCount, data, comments, allComments, CommentType.Comment);
00278 }
00279 }
00280 }
00281 }