Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/DbTestApp.cs @ 1347

Last change on this file since 1347 was 1025, checked in by svonolfe, 15 years ago

Fixed deletion (#372)

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.PluginInfrastructure;
26using System.Net;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using System.Diagnostics;
32
33namespace HeuristicLab.Hive.Server {
34  [ClassInfo(Name = "Hive DB Test App",
35      Description = "Test Application for the Hive DataAccess Layer",
36      AutoRestart = true)]
37  class HiveDbTestApplication : ApplicationBase {
38    private void TestClientAdapter() {
39      IClientAdapter clientAdapter =
40        ServiceLocator.GetClientAdapter();
41
42      ClientInfo client = new ClientInfo();
43      client.Login = DateTime.Now;
44      client.ClientId = Guid.NewGuid();
45      clientAdapter.Update(client);
46
47      ClientInfo clientRead =
48        clientAdapter.GetById(client.ClientId);
49      Debug.Assert(
50        clientRead != null &&
51        client.ClientId == clientRead.ClientId);
52
53      client.CpuSpeedPerCore = 2000;
54      clientAdapter.Update(client);
55      clientRead =
56        clientAdapter.GetById(client.ClientId);
57      Debug.Assert(
58       clientRead != null &&
59       client.ClientId == clientRead.ClientId &&
60       clientRead.CpuSpeedPerCore == 2000);
61
62      ICollection<ClientInfo> clients =
63        clientAdapter.GetAll();
64      int count = clients.Count;
65
66      clientAdapter.Delete(client);
67
68      clients = clientAdapter.GetAll();
69      Debug.Assert(count - 1 == clients.Count);
70    }
71
72    private void TestUserAdapter() {
73      IUserAdapter userAdapter =
74        ServiceLocator.GetUserAdapter();
75
76      User user = new User();
77      user.Name = "TestDummy";
78
79      userAdapter.Update(user);
80
81      User userRead =
82        userAdapter.GetById(user.Id);
83      Debug.Assert(
84        userRead != null &&
85        user.Id == userRead.Id);
86
87      user.Password = "passme";
88      userAdapter.Update(user);
89      userRead =
90        userAdapter.GetByName(user.Name);
91      Debug.Assert(
92       userRead != null &&
93       userRead.Name == user.Name &&
94       userRead.Password == user.Password);
95
96      ICollection<User> users =
97       userAdapter.GetAll();
98      int count = users.Count;
99
100      userAdapter.Delete(user);
101
102      users = userAdapter.GetAll();
103      Debug.Assert(count - 1 == users.Count);
104    }
105
106    private void TestUserGroupAdapter() {
107      IUserGroupAdapter userGroupAdapter =
108       ServiceLocator.GetUserGroupAdapter();
109
110      User user =
111        new User();
112      user.Name = "Stefan";
113
114      User user2 =
115        new User();
116      user2.Name = "Martin";
117
118      User user3 =
119        new User();
120      user3.Name = "Heinz";
121
122      UserGroup group =
123        new UserGroup();
124
125      UserGroup subGroup =
126        new UserGroup();
127      subGroup.Members.Add(user);
128
129      group.Members.Add(user3);
130      group.Members.Add(user2);
131      group.Members.Add(subGroup);
132
133      userGroupAdapter.Update(group);
134
135      UserGroup read =
136        userGroupAdapter.GetById(group.Id);
137
138      ICollection<UserGroup> userGroups =
139        userGroupAdapter.GetAll();
140
141      IUserAdapter userAdapter =
142        ServiceLocator.GetUserAdapter();
143
144      userAdapter.Delete(user3);
145     
146      read =
147         userGroupAdapter.GetById(group.Id);
148
149      userGroupAdapter.Delete(subGroup);
150
151      read =
152         userGroupAdapter.GetById(group.Id);
153
154      userGroups =
155        userGroupAdapter.GetAll();
156
157      userGroupAdapter.Delete(group);
158
159      userGroups =
160        userGroupAdapter.GetAll();
161
162      userAdapter.Delete(user);
163      userAdapter.Delete(user2);
164    }
165
166    private void TestClientGroupAdapter() {
167      IClientGroupAdapter clientGroupAdapter =
168       ServiceLocator.GetClientGroupAdapter();
169
170      ClientInfo client =
171        new ClientInfo();
172      client.Name = "Stefan";
173      client.ClientId = Guid.NewGuid();
174      client.Login = DateTime.Now;
175
176      ClientInfo client2 =
177        new ClientInfo();
178      client2.Name = "Martin";
179      client2.ClientId = Guid.NewGuid();
180      client2.Login = DateTime.Now;
181
182      ClientInfo client3 =
183        new ClientInfo();
184      client3.Name = "Heinz";
185      client3.ClientId = Guid.NewGuid();
186      client3.Login = DateTime.Now;
187
188      ClientGroup group =
189        new ClientGroup();
190
191      ClientGroup subGroup =
192        new ClientGroup();
193      subGroup.Resources.Add(client);
194
195      group.Resources.Add(client3);
196      group.Resources.Add(client2);
197      group.Resources.Add(subGroup);
198
199      clientGroupAdapter.Update(group);
200
201      ClientGroup read =
202        clientGroupAdapter.GetById(group.Id);
203
204      ICollection<ClientGroup> clientGroups =
205        clientGroupAdapter.GetAll();
206
207      IClientAdapter clientAdapter =
208        ServiceLocator.GetClientAdapter();
209
210      clientAdapter.Delete(client3);
211
212      read =
213         clientGroupAdapter.GetById(group.Id);
214
215      clientGroupAdapter.Delete(subGroup);
216
217      read =
218         clientGroupAdapter.GetById(group.Id);
219
220      clientGroups =
221        clientGroupAdapter.GetAll();
222
223      clientGroupAdapter.Delete(group);
224
225      clientGroups =
226        clientGroupAdapter.GetAll();
227
228      clientAdapter.Delete(client);
229      clientAdapter.Delete(client2);
230    }
231
232    private void TestJobAdapter() {
233      IJobAdapter jobAdapter =
234        ServiceLocator.GetJobAdapter();
235      IClientAdapter clientAdapter =
236        ServiceLocator.GetClientAdapter();
237
238      Job job = new Job();
239
240      ClientInfo client = new ClientInfo();
241      client.ClientId = Guid.NewGuid();
242      client.Login = DateTime.Now;
243
244      job.Client = client;
245      jobAdapter.Update(job);
246
247      ICollection<Job> jobs = jobAdapter.GetAll();
248
249      jobAdapter.Delete(job);
250      clientAdapter.Delete(client);
251
252      jobs = jobAdapter.GetAll();
253    }
254
255    private void TestJobResultsAdapter() {
256      Job job = new Job();
257
258      ClientInfo client = new ClientInfo();
259      client.ClientId = Guid.NewGuid();
260      client.Login = DateTime.Now;
261
262      job.Client = client;
263
264      IJobResultsAdapter resultsAdapter =
265        ServiceLocator.GetJobResultsAdapter();
266
267      byte[] resultByte = {0x0f, 0x1f, 0x2f, 0x3f, 0x4f};
268
269      JobResult result = new JobResult();
270      result.Client = client;
271      result.Job = job;
272      result.Result = resultByte;
273
274      resultsAdapter.Update(result);
275
276      JobResult read =
277        resultsAdapter.GetById(result.Id);
278      Debug.Assert(
279        read.Id == result.Id &&
280        result.Client.Id == read.Client.Id &&
281        result.Job.Id == read.Job.Id &&
282        result.Result == result.Result);
283
284      int count =
285        resultsAdapter.GetAll().Count;
286
287      resultsAdapter.Delete(result);
288
289      ICollection<JobResult> allResults =
290        resultsAdapter.GetAll();
291
292      Debug.Assert(allResults.Count == count - 1);
293
294      IJobAdapter jboAdapter =
295        ServiceLocator.GetJobAdapter();
296      jboAdapter.Delete(job);
297      IClientAdapter clientAdapter =
298        ServiceLocator.GetClientAdapter();
299      clientAdapter.Delete(client);
300    }
301
302    public override void Run() {
303      ITransactionManager transactionManager =
304        ServiceLocator.GetTransactionManager();
305     
306      TestClientAdapter();
307      transactionManager.UpdateDB();
308
309      TestUserAdapter();
310      transactionManager.UpdateDB();
311
312      TestUserGroupAdapter();
313      transactionManager.UpdateDB();
314
315      TestClientGroupAdapter();
316      transactionManager.UpdateDB();   
317
318      TestJobAdapter();
319      transactionManager.UpdateDB(); 
320
321      TestJobResultsAdapter();
322      transactionManager.UpdateDB();     
323    }
324  }
325}
Note: See TracBrowser for help on using the repository browser.