2
using System.Collections;
5
using System.Threading;
12
namespace ServiceTagTest
15
/// Simias tag testing
17
public class Service : IThreadService
21
/// Used to log messages.
23
private static readonly ISimiasLog log =
24
SimiasLogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
26
private AutoResetEvent testEvent = null;
27
private Thread testThread = null;
33
/// Initializes a new instance of the object class.
40
#region IThreadService Members
42
/// Starts the thread service.
44
/// <param name="config">
45
/// Configuration file object for the configured store
48
// public void Start( Configuration config )
51
log.Debug( "Start called" );
59
log.Error( e.Message );
60
log.Error( e.StackTrace );
65
/// Resumes a paused service.
72
/// Pauses a service's execution.
81
/// <param name="message"></param>
82
/// <param name="data"></param>
83
public int Custom(int message, string data)
89
/// Stops the service from executing.
93
log.Debug( "Stop called" );
98
#region Private Methods
99
internal int StartTestThread()
105
testEvent = new AutoResetEvent( false );
106
testThread = new Thread( new ThreadStart( TestThread ) );
107
testThread.IsBackground = true;
110
catch( SimiasException e )
112
log.Error( e.Message );
113
log.Error( e.StackTrace );
120
private void TestThread()
125
log.Debug( "Waiting a bit before we start the tests!" );
126
testEvent.WaitOne( 30000, false );
127
log.Debug( "Starting tag tests" );
129
if ( this.TestOne() == true )
138
if ( this.TestTwo() == true )
147
if ( this.TestThree() == true )
156
if ( this.TestFour() == true )
165
if ( this.TestFive() == true )
174
if ( this.TestSix() == true )
183
if ( this.TestSeven() == true )
192
log.Debug( " Test Results" );
193
log.Debug( " Passed: " + testsPassed.ToString() );
194
log.Debug( " Failed: " + testsFailed.ToString() );
196
log.Debug( "Tests finished" );
199
private bool TestOne()
202
log.Debug( "Starting TestOne" );
203
Store store = Store.GetStore();
204
Domain domain = store.GetDomain( store.LocalDomain );
205
log.Debug( " retrieved the default domain: " + domain.Name );
207
log.Debug( " creating two tags in the default domain" );
208
Tag greenTag = new Tag( "Green" );
209
Tag redTag = new Tag( "Red" );
213
greenTag.Add( domain.ID );
214
redTag.Add( domain.ID );
221
log.Debug( " querying for all tags on the default domain" );
222
ICSList tagList = Simias.Tags.Query.Tags( domain );
223
if ( tagList.Count >= 2 )
225
foreach( ShallowNode sn in tagList )
227
Tag tag = new Tag( sn );
228
log.Debug( " Tag: " + tag.Name );
236
Node[] nodes = new Node[3];
237
log.Debug( " creating three generic nodes" );
238
nodes[0] = new Node( "Node1" );
239
nodes[1] = new Node( "Node2" );
240
nodes[2] = new Node( "Node3" );
241
domain.Commit( nodes );
243
log.Debug( " adding the green tag to node1" );
244
greenTag.TagNode( domain.ID, nodes[0] );
245
log.Debug( " query for all nodes with the green tag - should find 1 node" );
246
ICSList results = Simias.Tags.Query.Nodes( domain.ID, greenTag );
247
log.Debug( " found: " + results.Count.ToString() + " tag(s)" );
248
if ( results.Count == 1 )
250
foreach( ShallowNode sn in results )
252
log.Debug( " Node: " + sn.Name );
260
log.Debug( " adding red tag to node2" );
261
redTag.TagNode( domain.ID, nodes[1] );
263
log.Debug( " query for all nodes with the red and green tag - " );
264
log.Debug( " should find two nodes" );
265
Simias.Tags.Query query = new Simias.Tags.Query( domain.ID );
266
query.AddTag( greenTag );
267
query.AddTag( redTag );
268
ICSList results2 = query.QueryNodes();
269
if ( results2.Count == 2 )
271
foreach( ShallowNode sn in results2 )
273
log.Debug( " Node: " + sn.Name );
281
log.Debug( " adding red tag to node3" );
282
redTag.TagNode( domain.ID, nodes[2] );
284
log.Debug( " query for all nodes with the red and green tag - should find 3 nodes" );
285
ICSList results3 = query.QueryNodes();
286
if ( results3.Count == 3 )
288
foreach( ShallowNode sn in results3 )
290
log.Debug( " Node: " + sn.Name );
298
log.Debug( " cleaning up nodes and tags" );
299
domain.Commit( domain.Delete( nodes ) );
300
greenTag.Remove( domain.ID );
301
redTag.Remove( domain.ID );
303
log.Debug( "TestOne finished\n" );
307
private bool TestTwo()
310
log.Debug( "Starting TestTwo" );
311
Store store = Store.GetStore();
312
Domain domain = store.GetDomain( store.LocalDomain );
313
log.Debug( " retrieved the default domain: " + domain.Name );
315
log.Debug( " creating two tags in the default domain" );
316
Tag purpleTag = new Tag( "Purple" );
317
purpleTag.Add( domain.ID );
319
Tag whiteTag = new Tag( "White" );
320
whiteTag.Add( domain.ID );
322
// Can I read the tag back
323
Node whiteNode = domain.GetNodeByID( whiteTag.ID );
324
if ( whiteNode == null )
326
log.Debug( " nope couldn't read the white tag" );
329
Node[] nodes = new Node[500];
330
log.Debug( " creating five hundred generic nodes" );
332
// Cleanup any stragglers
333
for( int i = 0; i < 500; i++ )
335
Node node = domain.GetSingleNodeByName( "Node" + i.ToString() );
338
domain.Commit( domain.Delete( node ) );
342
for( int i = 0; i < 500; i++ )
344
nodes[i] = new Node( "Node" + i.ToString() );
347
domain.Commit( nodes );
349
// Add purple to the first half
350
log.Debug( " adding the purple tag to half the nodes" );
351
for( int i = 0; i < 250; i++ )
353
purpleTag.TagNode( domain.ID, nodes[i] );
356
log.Debug( " adding the white tag to the last two nodes" );
357
whiteTag.TagNode( domain.ID, nodes[498] );
358
whiteTag.TagNode( domain.ID, nodes[499] );
360
log.Debug( " query for all nodes with the purple tag - should find 250 nodes" );
361
ICSList results = Simias.Tags.Query.Nodes( domain.ID, purpleTag );
362
log.Debug( " found: " + results.Count.ToString() );
363
foreach( ShallowNode sn in results )
365
log.Debug( " Node: " + sn.Name );
368
log.Debug( " query for all purple and white nodes - should find 252 nodes" );
369
Simias.Tags.Query query = new Simias.Tags.Query( domain.ID );
370
query.AddTag( whiteTag );
371
query.AddTag( purpleTag );
372
ICSList results2 = query.QueryNodes();
373
log.Debug( " Found: " + results2.Count.ToString() );
374
foreach( ShallowNode sn in results2 )
376
log.Debug( " Node: " + sn.Name );
379
log.Debug( " cleaning up nodes and tags" );
380
domain.Commit( domain.Delete( nodes ) );
382
purpleTag.Remove( domain.ID );
383
whiteTag.Remove( domain.ID );
385
log.Debug( "TestTwo finished\n" );
389
private bool TestThree()
392
log.Debug( "Starting TestThree" );
393
log.Debug( "Create a tag and a node and leave them in the store" );
395
Store store = Store.GetStore();
396
Domain domain = store.GetDomain( store.LocalDomain );
397
log.Debug( " retrieved the default domain: " + domain.Name );
399
log.Debug( " creating \"ThreeTag\" in the default domain" );
400
Tag threeTag = new Tag( "ThreeTag" );
403
threeTag.Add( domain.ID );
405
catch( ExistsException e )
407
log.Debug( " ThreeTag already exists in the tags library" );
410
log.Debug( " creating ThreeNode" );
411
Node threeNode = domain.GetSingleNodeByName( "ThreeNode" );
412
if ( threeNode == null )
414
threeNode = new Node( "ThreeNode" );
415
domain.Commit( threeNode );
416
threeTag.TagNode( domain.ID, threeNode );
418
log.Debug( "Finished TestThree\n" );
422
private bool TestFour()
425
log.Debug( "Starting TestFour" );
426
log.Debug( "Create a tag and then attempt to create the same tag again" );
428
Store store = Store.GetStore();
429
Domain domain = store.GetDomain( store.LocalDomain );
430
log.Debug( " retrieved the default domain: " + domain.Name );
432
log.Debug( " creating \"DuplicateTag\" in the default domain" );
433
Tag dupTag = new Tag( "DuplicateTag" );
436
dupTag.Add( domain.ID );
440
log.Debug( " failed creating tag" );
444
log.Debug( " attempting to duplicate the same tag" );
445
Tag dup2Tag = new Tag( "DuplicateTag" );
448
dup2Tag.Add( domain.ID );
449
log.Debug( " an \"ExistsException\" should have been thrown" );
452
catch( ExistsException e )
454
log.Debug( " caught expected \"ExistsException\"" );
456
catch( Exception e2 )
458
log.Debug( " An unexpected exception was caught" );
462
dupTag.Remove( domain.ID );
464
//domain.Commit( domain.Delete( dupTag ) );
465
log.Debug( "Finished TestFour\n" );
469
private bool TestFive()
472
log.Debug( "Starting TestFive" );
473
log.Debug( "Create two tags, assign them to a node then delete one of the tags and verify it was deleted from the node" );
475
Store store = Store.GetStore();
476
Domain domain = store.GetDomain( store.LocalDomain );
477
log.Debug( " retrieved the default domain: " + domain.Name );
479
Tag tagFiveOne = new Tag( "TestFiveTagOne" );
480
Tag tagFiveTwo = new Tag( "TestFiveTagTwo" );
484
log.Debug( " creating \"TestFiveTagOne\" in the default domain" );
485
tagFiveOne.Add( domain.ID );
490
log.Debug( " failed creating TestFiveTagOne" );
493
log.Debug( " creating \"TestFiveTagTwo\" in the default domain" );
496
tagFiveTwo.Add( domain.ID );
498
catch( ExistsException e )
501
log.Debug( " failed creating TestFiveTagTwo with ExistsException" );
506
log.Debug( " failed creating TestFiveTagTwo with an unknown exception" );
509
log.Debug( " creating TestFiveNode" );
510
Node testFiveNode = new Node( "TestFiveNode" );
511
domain.Commit( testFiveNode );
515
log.Debug( " tagging TestFiveNode with \"TestFiveTagOne\" and \"TestFiveTagTwo\"" );
516
tagFiveOne.TagNode( domain.ID, testFiveNode );
517
tagFiveTwo.TagNode( domain.ID, testFiveNode );
521
log.Debug( " failed tagging node" );
527
log.Debug( " deleting tag: \"TestFiveTagTwo\"" );
528
tagFiveTwo.Remove( domain.ID );
532
log.Debug( " failed removing TestFiveTagTwo from the library" );
536
log.Debug( " deleting node: " + testFiveNode.Name );
537
domain.Commit( domain.Delete( testFiveNode ) );
541
log.Debug( " deleting tag: " + tagFiveOne.Name );
542
tagFiveOne.Remove( domain.ID );
546
log.Debug( " failed removing TestFiveTagOne from the library" );
550
log.Debug( "Finished TestFive\n" );
554
private bool TestSix()
557
int numberTags = 1000;
559
log.Debug( "Starting TestSix" );
560
log.Debug( "Create " + numberTags.ToString() + " tags, issue a query to retrieve them, then verify them" );
562
Store store = Store.GetStore();
563
Domain domain = store.GetDomain( store.LocalDomain );
564
log.Debug( " retrieved the default domain: " + domain.Name );
566
Hashtable tagTable = new Hashtable( numberTags );
567
Tag[] tags = new Tag[ numberTags ];
568
log.Debug( " creating " + numberTags.ToString() + " tags." );
569
for( int i = 0; i < numberTags; i++)
571
tags[i] = new Tag( "TestSixTag" + i.ToString() );
572
tagTable.Add( tags[i].Name, tags[i] );
577
Simias.Tags.Tag.Add( domain, tags );
581
log.Debug( " failed to create all tags!" );
585
if ( passed == true )
588
ICSList results = Simias.Tags.Query.Tags( domain );
589
if ( results.Count > 0 )
591
foreach( ShallowNode sn in results )
593
if ( tagTable.Contains( sn.Name ) == true )
600
if ( verified == numberTags )
602
log.Debug( " verified all " + verified.ToString() + " tags." );
606
log.Debug( " failed verifying all the tags. verified: " + verified.ToString() + " of the " + numberTags.ToString() + " created" );
611
// Remove all the previously created tags
612
log.Debug( " removing tags from the collection" );
613
Simias.Tags.Tag.Remove( domain, tags );
614
log.Debug( "Finished TestSix\n" );
618
private bool TestSeven()
621
log.Debug( "Starting TestSeven" );
622
log.Debug( "Test the TagNode and UntagNode methods of the framework" );
624
Store store = Store.GetStore();
625
Domain domain = store.GetDomain( store.LocalDomain );
626
log.Debug( " retrieved the default domain: " + domain.Name );
628
Tag tagOne = new Tag( "TestSevenTagOne" );
629
Tag tagTwo = new Tag( "TestSevenTagTwo" );
632
log.Debug( " creating tag: " + tagOne.Name );
633
tagOne.Add( domain.ID );
635
log.Debug( " creating tag: " + tagTwo.Name );
636
tagTwo.Add( domain.ID );
640
log.Debug( " failed creating tags" );
644
Node node = new Node( "TestSevenNode" );
645
log.Debug( " creating node: " + node.Name );
646
domain.Commit( node );
650
log.Debug( " tagging node \"" + node.Name + "\" with tag \"" + tagOne.Name + "\"" );
651
tagOne.TagNode( domain.ID, node );
653
log.Debug( " tagging node \"" + node.Name + "\" with tag \"" + tagTwo.Name + "\"" );
654
tagTwo.TagNode( domain.ID, node );
656
log.Debug( " untagging node \"" + node.Name + "\" from tag \"" + tagOne.Name + "\"" );
657
tagOne.UntagNode( domain.ID, node );
661
log.Debug( " failed tagging/untagging node" );
665
log.Debug( " deleting nodes and tags" );
666
domain.Commit( domain.Delete( node ) );
667
tagOne.Remove( domain.ID );
668
tagTwo.Remove( domain.ID );
669
log.Debug( "Finished TestSeven\n" );