-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTutorial.html
1496 lines (1161 loc) · 48.8 KB
/
Tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Concordion - Tutorial</title>
<link media="all" rel="stylesheet" type="text/css" href="css/default.css"/>
<link media="print" rel="stylesheet" type="text/css" href="css/print.css"/>
<link rel="icon" type="image/vnd.microsoft.icon" href="favicon.ico" />
<style>
code a {
font-family: Courier New, Courier, Monospace;
}
h3 {
margin-top: 32px;
}
.section {
margin-top: 60px;
border-bottom: 1px dotted black;
padding-bottom: 8px;
}
.helpfulNote {
border: 1px solid gray;
padding: 8px;
margin-top: 30px;
background-color: #fffff0;
width: 600px;
}
.language {
margin-bottom: 12px;
}
.languageSelector {
float: right;
margin: 12px 0px 32px 32px;
font-size: 8pt;
}
.feature {
padding-bottom: 20px;
padding-top: 10px;
clear: both;
}
</style>
</head>
<body>
<div class="page">
<div class="header">
<div id="google_translate_element" class="language-translation"></div>
<div class="logo"><a href="index.html"> <img src="image/front-page-banner.png" alt="Specification by Example" /> </a></div>
</div><!-- header -->
<div class="menuBar">
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="Example.html">Example</a></li>
<li class="selectedTab"><a href="Tutorial.html">Tutorial</a></li>
<li><a href="Technique.html">Hints and Tips</a></li>
<li><a href="ExtensionsAPI.html">Extensions API</a></li>
<li><a href="Extensions.html">Extensions</a></li>
<li><a href="Download.html">Download</a></li>
<li><a href="Questions.html">FAQ</a></li>
</ul>
</div><!-- menuBar -->
<div class="content">
<div class="languageSelector">
<center>
<div class="language">
<a href="Tutorial.html"><img border="0" src="image/shared/Flag_en.png"/></a>
<a href="Tutorial.html"><br/>in English</a>
</div>
<div class="language">
<a href="Tutorial_es.html"><img border="0" src="image/shared/Flag_es.png"/></a>
<a href="Tutorial_es.html"><br/>en español</a>
</div>
<div class="language">
<a href="http://science.webhostinggeeks.com/concordion"><img border="0" src="image/shared/Flag_hr.png"/></a>
<br /><a class="extenalLink" href="http://science.webhostinggeeks.com/concordion">Serbo-Croatian</a>
<br /><span style="font-size: 10px">(translated by<br />Jovana Milutinovich)</span>
</div>
</center>
</div>
<h1>Tutorial</h1>
<p>
This guide explains the basic mechanics of turning specifications
into <em>active specifications</em> using Concordion. It shouldn't
take you more than 15-30 mins to complete, assuming you are already
familiar with Java, JUnit and XHTML.
</p>
<ul class="toc">
<li><a href="#installation">Installation</a></li>
<li><a href="#basics">The Basics</a></li>
<li><code><a href="#assertEquals">concordion:assertEquals</a></code></li>
<li><code><a href="#set">concordion:set</a></code></li>
<li><code><a href="#execute">concordion:execute</a></code></li>
<li><code><a href="#executeTable">concordion:execute on a <table></a></code></li>
<li><code><a href="#executeList">concordion:execute on a <list></a></code></li>
<li><code><a href="#verifyRows">concordion:verifyRows</a></code></li>
</ul>
<p>
Once you are comfortable with the basic mechanics, you might like to look at some <a href="#advanced">Advanced Features</a>.
</p>
<a name="installation" />
<h2 class="section">Installation</h2>
<p>
To kickstart the tutorial, we've created a "Hello World!" project with the following
structure:
</p>
<img src="image/tutorial/ProjectStructure.png" />
<p>
Download: <a href="http://dl.bintray.com/concordion/downloads/concordion-kickstart-1.5.0.zip">concordion-kickstart-1.5.0.zip</a>
</p>
<p>
It contains the latest stable version of Concordion and all its dependencies, a
simple "Hello World!" example and a basic Ant build file.
</p>
<style>
.spread {
margin-left: 30px;
}
.spread tr td {
padding-bottom: 8px;
}
</style>
<table class="spread">
<tr>
<td valign="top" style="padding-right: 10px"><img src="image/shared/1.png"/></td>
<td valign="top" ><code>"Greeter.java"</code> is the class we want to test</td>
</tr>
<tr>
<td valign="top" style="padding-right: 10px"><img src="image/shared/2.png"/></td>
<td valign="top" ><code>"specs"</code> is a source folder for the acceptance tests. JUnit tests are typically held in a folder <code>"test"</code>
but we prefer the name <code>"specs"</code>, for the Concordion acceptance tests, to emphasise the fact that
these tests are concerned with <i>external</i> behaviour.
</td>
</tr>
<tr>
<td valign="top" style="padding-right: 10px"><img src="image/shared/3.png"/></td>
<td valign="top" ><code>"HelloWorldFixture.java"</code> is a JUnit test. We'll explain how this works, below.
</td>
</tr>
<tr>
<td valign="top" style="padding-right: 10px"><img src="image/shared/4.png"/></td>
<td valign="top" ><code>"HelloWorld.html"</code> is the Concordion specification and is processed by the <code>"HelloWorldFixture.java"</code> test. The name of the test fixture and the HTML share the same base name. The test fixture has an optional suffix of <code>"Fixture"</code> or <code>"Test"</code> - for example, it may be named <code>"HelloWorld.java"</code>, <code>"HelloWorldFixture.java"</code> or <code>"HelloWorldTest.java"</code>.
</td>
</tr>
<tr>
<td valign="top" style="padding-right: 10px"><img src="image/shared/5.png"/></td>
<td valign="top" ><code>"concordion.css"</code> is a stylesheet to make the specs look nice while you're writing them.
</td>
</tr>
</table>
<p>
Concordion requires JDK 5.0 or above and the following JARs on the classpath:
</p>
<ul>
<li><b>concordion-1.5.1.jar</b></li>
<li><b>junit-4.12.jar</b> (or <b>junit-3.8.2.jar</b>)</li>
<li><b>hamcrest-core-1.3.jar</b> (required for JUnit 4.11 or later)</li>
<li><b>ognl-2.6.9.jar</b></li>
<li><b>xom-1.2.5.jar</b></li>
</ul>
<p>These dependencies are included in the distribution.</p>
<a name="basics"/>
<h2 class="section">The Basics</h2>
<p>
A Concordion active specification consists of two parts: (i) a well-formed XHTML
document describing the functionality, and (ii) fixture code written in Java
(a special Concordion extension of a standard JUnit test case) that finds concrete examples in the document
and uses them to verify the system under test. Both files must be in the same package.
</p>
<p>
In order for the magic to happen, the document must first be <em>instrumented</em>
with commands.
</p>
<p>
Concordion commands are specified as attributes on elements in the XHTML
document. Web browsers ignore attributes that they don't understand, so these
commands are effectively invisible.
</p>
<p>
The commands use a <code>"concordion"</code> namespace defined at the top of
each document as follows:
</p>
<pre class="html">
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
</pre>
<p>
If you plan using Unicode (in case of accents for example), specify the content type after the html tag as follows:
</p>
<pre class="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
</pre>
<p>
Let's start with a really simple example...
</p>
<a name="assertEquals"/>
<h2 class="section">concordion:assertEquals</h2>
<ol>
<li>
Create a Java package called <code>"example"</code>.
</li>
<li>
Create a file <code>"HelloWorld.html"</code> inside the package
containing:
<pre class="html">
<html>
<body>
<p>Hello World!</p>
</body>
</html>
</pre>
</li>
<li>
Now instrument the file as follows:
<pre class="html">
<html <b>xmlns:concordion="http://www.concordion.org/2007/concordion"</b>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p <b>concordion:assertEquals="getGreeting()"</b>>Hello World!</p>
</body>
</html>
</pre>
</li>
<li>
In the same <code>example</code> package, create a Java file
<code>"HelloWorldFixture.java"</code> containing:
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> HelloWorldFixture {
<span class="keyword">public</span> String getGreeting() {
<span class="keyword">return</span> <span class="string">"Hello World!"</span>;
}
}
</pre>
<li>
Now run the <code>HelloWorldFixture</code> class using JUnit.
</li>
</ol>
<p>
If you've done it right, JUnit should give you a green bar and a message like
this should be printed to the console:
</p>
<pre class="console">
C:\temp\concordion\example\HelloWorld.html
Successes: 1 Failures: 0
</pre>
<p>
The message shows the path of the output (results) file for the
test and a summary of success and failure counts. By default, Concordion
outputs to the directory specified by the system property
<code>java.io.tmpdir</code>.
</p>
<p>
Open the output file in a browser and you should see the
same content as the input document but with the words <code>Hello World!</code>
highlighted in green.
</p>
<img style="padding-right: 0px" src="image/tutorial/HelloWorldSuccess.png" alt="Hello World! successful outcome"/>
<h3>Java Bean Properties</h3>
<p>
In the example above, the call to <code>"getGreeting()"</code> can
be simplified to <code>"greeting"</code> since Concordion's
expression language understands simple bean properties.
</p>
<pre class="html">
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p <b>concordion:assertEquals="greeting"</b>>Hello World!</p>
</body>
</html>
</pre>
<a name="set"/>
<h2 class="section">concordion:set</h2>
<p>
Given a specification like this:
</p>
<pre class="html">
<html>
<body>
<p>
The greeting for user Bob will be: Hello Bob!
</p>
</body>
</html>
</pre>
<p>
We want the first name ("Bob") to be a parameter
and the greeting ("Hello Bob!") to be verified against
the result returned by the system.
</p>
<p>
To do this we place <code><span></code> tags around the two
significant pieces of text in the document. In HTML,
<code><span></code> tags don't have any effect on the
display of the output document.
</p>
<pre class="html">
<html>
<body>
<p>
The greeting for user <b><span></b>Bob<b></span></b>
will be: <b><span></b>Hello Bob!<b></span></b>
</p>
</body>
</html>
</pre>
<p>
Now we can instrument the document:
</p>
<pre class="html">
<html <b>xmlns:concordion="http://www.concordion.org/2007/concordion"</b>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
The greeting for user <span <b>concordion:set="#firstName"</b>>Bob</span>
will be:
<span <b>concordion:assertEquals="greetingFor(#firstName)"</b>>Hello Bob!</span>
</p>
</body>
</html>
</pre>
<p>
When Concordion processes the document, it will set a temporary
variable <code>#firstName</code> to be the value <code>"Bob"</code> and then
call the <code>greetingFor()</code> method with that value
and check that the result is equal to <code>"Hello Bob!"</code>.
</p>
<p>
Our Java fixture code will need to be updated:
</p>
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> HelloWorldFixture {
<span class="keyword">public</span> String greetingFor(String firstName) {
<span class="keyword">return</span> <span class="string">"TODO"</span>;
}
}
</pre>
<p>
Just as you do when writing unit tests, always make
the test fail before you make it pass - to give yourself
confidence that it's actually testing something.
With the code as it stands you should get a failure (we are
expecting "Hello Bob!" but get "TODO").
</p>
<p>
Now fix the code:
</p>
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> HelloWorldFixture {
<span class="keyword">public</span> String greetingFor(String firstName) {
<span class="keyword">return</span> <span class="string">"Hello "</span> + firstName + <span class="string">"!"</span>;
}
}
</pre>
<p>
Obviously in a real application, the implementation of <code>greetingFor()</code>
would be quite different. The behaviour would not be implemented here, but
in the system code and the fixture would simply call into the system. It might
do this at the system level or at a lower level. It may even call in at the unit
level, if writing a system test would be too slow or unwieldy.
</p>
<a name="execute"/>
<h2 class="section">concordion:execute</h2>
<p>
The execute command has three main uses:
</p>
<ol>
<li><a href="#executeVoid">Executing an instruction with a "void" result</a>.</li>
<li><a href="#executeObjectResult">Executing an instruction with an object result</a>
(to allow multiple properties of the object to be checked).
</li>
<li><a href="#executeUnusualSentences">Handling unusual sentence structures</a>.</li>
</ol>
<a name="executeVoid" />
<h3>Executing an instruction with a <code>void</code> result</h3>
<p>
It can occasionally be useful to execute an
instruction that sets up some system state. Every time you do
this, however, alarm bells should ring in your head and you should question
yourself to make sure that you are not inadvertently writing
a script instead of a specification. E.g. a call to <code>"clearDatabase()"</code>
would be a blatant misuse (see <a href="Technique.html">Technique</a> for more on this topic).
</p>
<p>
As a rule of thumb, methods with a <code>void</code> result
called from an <code>execute</code> should start with the word
<code>set</code> or <code>setUp</code>. E.g. <code>setUpUser(#username)</code>.
</p>
<p>
Take the following specification for example:
</p>
<pre class="html">
<html <b>xmlns:concordion="http://www.concordion.org/2007/concordion"</b>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
If the time is
<span <b>concordion:set="#time"</b>>09:00AM</span>
<span <b>concordion:execute="setCurrentTime(#time)"</b> />
then the greeting will say:
<span <b>concordion:assertEquals="getGreeting()"</b>>Good Morning World!</span>
</p>
</body>
</html>
</pre>
<p>
Our Java fixture code will look like this:
</p>
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> HelloWorldFixture {
<span class="keyword">public void</span> setCurrentTime(String time) {
<span class="comment">// TODO</span>
}
<span class="keyword">public</span> String getGreeting() {
<span class="keyword">return</span> <span class="string">"TODO"</span>;
}
}
</pre>
<p>
We can actually remove the need for the <code>concordion:set</code>
command by using the special variable <code>#TEXT</code> (which
contains the text of the current element). The abbreviated
instrumentation looks like this:
</p>
<pre class="html">
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
If the time is
<span <b>concordion:execute="setCurrentTime(#TEXT)"</b>>09:00AM</span>
then the greeting will say:
<span concordion:assertEquals="getGreeting()">Good Morning World!</span>
</p>
</body>
</html>
</pre>
<p>
An alternative would be to change the
<code>getGreeting()</code> method signature to allow the
time to be passed in as a parameter. This is the approach you
should normally take. An <code>execute</code> with no
return value often indicates a "bad smell" - e.g. you're
writing a script or your specification contains too many
variables and covers too many behaviours. However, the
functionality is there if you need it.
</p>
<a name="executeObjectResult" />
<h3>Executing an instruction with an object result</h3>
<p>
Sometimes you need to check more than one result of a behaviour.
For example, here we want to check that both the first name
and the last name are correctly extracted from the full name:
</p>
<a name="splittingNamesSpec"/>
<pre class="html">
<html <b>xmlns:concordion="http://www.concordion.org/2007/concordion"</b>>
<head>
<link href="../concordion.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Splitting Names</h1>
<p>
To help personalise our mailshots we want to have the first name
and last name of the customer. Unfortunately the customer data
that we are supplied only contains full names.
</p>
<p>
The system therefore attempts to break a supplied full name into
its constituents by splitting around whitespace.
</p>
<div class="example">
<h3>Example</h3>
<p>
The full name
<span <b>concordion:execute="#result = split(#TEXT)"</b>>John Smith</span>
will be broken into first name
<span <b>concordion:assertEquals="#result.firstName"</b>>John</span>
and last name
<span <b>concordion:assertEquals="#result.lastName"</b>>Smith</span>.
</p>
</div>
</body>
</html>
</pre>
<p>
The variable <code>#result</code> is going to be an object returned
by the <code>split()</code> method. This object will have a
<code>firstName</code> and <code>lastName</code> properties.
</p>
<p>
Assuming our HTML file is in the <code>example</code> package and is
called "SplittingNames.html" then we need a Java fixture called
<code>SplittingNamesTest</code> (or <code>SplittingNamesFixture</code>, or just <code>SplittingNames</code>):
</p>
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> SplittingNamesTest {
}
</pre>
<p>
If you run the fixture as it stands (i.e. empty), the output should
look something like this:
</p>
<div>
<img src="image/tutorial/execute/BrokenDueToMissingFixtureCode.png" alt="Output shows a broken test (due to missing fixture code)"/>
</div>
<p>
It tells you what you need to do. We'll flesh out our fixture
code:
</p>
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> SplittingNamesTest {
<span class="keyword">public</span> Result split(String fullName) {
<span class="keyword">return</span> new Result();
}
<span class="keyword">class</span> Result {
<span class="keyword">public</span> String firstName = <span class="string">"TODO"</span>;
<span class="keyword">public</span> String lastName = <span class="string">"TODO"</span>;
}
}
</pre>
<p>
Run it now and you get:
</p>
<div>
<img src="image/tutorial/execute/BrokenBecauseNotFullyImplemented.png"
alt="Output shows a broken test because the fixture is not fully implemented"/>
</div>
<p>
Let's implement the function. Obviously the implementation should
be in the real system not in the test case, but just for
fun...
</p>
<a name="splittingNamesTest" />
<pre class="java">
<span class="keyword">package</span> example;
<span class="keyword">import</span> org.concordion.integration.junit4.ConcordionRunner;
<span class="keyword">import</span> org.junit.runner.RunWith;
@RunWith(ConcordionRunner.class)
<span class="keyword">public class</span> SplittingNamesTest {
<span class="keyword">public</span> Result split(String fullName) {
Result result = <span class="keyword">new</span> Result();
String[] words = fullName.split(<span class="string">" "</span>);
result.firstName = words[0];
result.lastName = words[1];
<span class="keyword">return</span> result;
}
<span class="keyword">class</span> Result {
<span class="keyword">public</span> String firstName;
<span class="keyword">public</span> String lastName;
}
}
</pre>
<p>
The test now passes:
</p>
<div>
<img src="image/tutorial/execute/Successful.png" alt="Test is now successful"/>
</div>
<p>
Note: Our inner class <code>Result</code> could equally be implemented
with getters instead of public fields. The HTML instrumentation remains
the same.
</p>
<pre class="java">
<span class="keyword">class</span> Result {
<span class="keyword">private final</span> String firstName;
<span class="keyword">private final</span> String lastName;
<span class="keyword">public</span> Result(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
<span class="keyword">public</span> String getFirstName() {
<span class="keyword">return</span> firstName;
}
<span class="keyword">public</span> String getLastName() {
<span class="keyword">return</span> lastName;
}
}
</pre>
<p>
Alternatively, the <a href="#multiValueResult">MultiValueResult</a> class makes it easy to return multiple values without creating a new object, or you can <a href="#executeMapResult">return a Map result</a>.
</p>
<a name="executeUnusualSentences" />
<h3>Handling unusual sentence structures</h3>
<p>
One of the great things about Concordion is that when you're writing
the specifications you do not have to worry about how you're going
to instrument it. You can just concentrate on making the document as readable
as possible.
</p>
<p>
Most English sentences can be instrumented. If you can't work out
how to instrument it then you can always tweak the wording, but in
general this should not be necessary. The <code>execute</code>
command provides flexibility.
</p>
<p>
For example, say we have the specification:
</p>
<pre class="html">
<p>
Upon login, the greeting for user <span>Bob</span>
will be: <span>Hello Bob!</span>
</p>
</pre>
<p>
This is easy to instrument:
</p>
<pre class="html">
<p>
Upon login, the greeting for user <span <b>concordion:set="#firstName"</b>>Bob</span>
will be:
<span <b>concordion:assertEquals="greetingFor(#firstName)"</b>>Hello Bob!</span>
</p>
</pre>
<p>
But what if our specification was written like this:
</p>
<pre class="html">
<p>
The greeting "<span>Hello Bob!</span>" should be given
to user <span>Bob</span> when he logs in.
</p>
</pre>
<p>
In this case, the input parameter <code>Bob</code> occurs <em>after</em>
the output greeting we want to check. We can solve this problem by using
an <code>execute</code> command on the outer element (the <code><p></code>).
</p>
<pre class="html">
<p <b>concordion:execute="#greeting = greetingFor(#firstName)"</b>>
The greeting "<span <b>concordion:assertEquals="#greeting"</b>>Hello Bob!</span>"
should be given to user <span <b>concordion:set="#firstName"</b>>Bob</span>
when he logs in.
</p>
</pre>
<p>
How does this work? It works because the <code>execute</code> command is designed to process
commands on its child elements in a special order. First of all it processes any child
<code>set</code> commands then it runs its own command, then any child <code>execute</code> commands
and finally any child <code>assertEquals</code> commands.
</p>
<a name="executeTable"/>
<h2 class="section">concordion:execute on a <table></h2>
<p>
When you want to show several examples of a behaviour, repeating the same
sentence structure over and over again probably isn't going to be very
nice to read. It would be better to use a table.
</p>
<p>
For example:
</p>
<div>
<img src="image/tutorial/executeTable/HowTableIsDisplayed.png"
alt="How the table is displayed (nice and neat)"/>
</div>
<p>
You can instrument this table, in a long-winded way, as follows:
</p>
<pre class="html">
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
<link href="../concordion.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Splitting Names</h1>
<p>
To help personalise our mailshots we want to have the first name
and last name of the customer. Unfortunately the customer data
that we are supplied only contains full names.
</p>
<p>
The system therefore attempts to break a supplied full name into
its constituents by splitting around whitespace.
</p>
<div class="example">
<h3>Examples</h3>
<table>
<tr>
<th>Full Name</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr <b>concordion:execute="#result = split(#fullName)"</b>>
<td <b>concordion:set="#fullName"</b>>John Smith</td>
<td <b>concordion:assertEquals="#result.firstName"</b>>John</td>
<td <b>concordion:assertEquals="#result.lastName"</b>>Smith</td>
</tr>
<tr <b>concordion:execute="#result = split(#fullName)"</b>>
<td <b>concordion:set="#fullName"</b>>David Peterson</td>
<td <b>concordion:assertEquals="#result.firstName"</b>>David</td>
<td <b>concordion:assertEquals="#result.lastName"</b>>Peterson</td>
</tr>
</table>
</div>
</body>
</html>
</pre>
<p>
However, this is repetitive so Concordion provides a shortcut.
When you place an <code>execute</code> command on a <code><table></code> element
the commands on the header row (the row containing <th> elements) are copied to
each detail row (rows containing <td> elements) and the <code>execute</code>
command is run on each detail row.
</p>
<pre class="html">
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
<link href="../concordion.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Splitting Names</h1>
<p>
To help personalise our mailshots we want to have the first name
and last name of the customer. Unfortunately the customer data
that we are supplied only contains full names.
</p>
<p>
The system therefore attempts to break a supplied full name into
its constituents by splitting around whitespace.
</p>
<div class="example">
<h3>Examples</h3>
<table <b>concordion:execute="#result = split(#fullName)"</b>>
<tr>
<th <b>concordion:set="#fullName"</b>>Full Name</th>
<th <b>concordion:assertEquals="#result.firstName"</b>>First Name</th>
<th <b>concordion:assertEquals="#result.lastName"</b>>Last Name</th>
</tr>
<tr>
<td>John Smith</td>
<td>John</td>
<td>Smith</td>
</tr>
<tr>
<td>David Peterson</td>
<td>David</td>
<td>Peterson</td>
</tr>
</table>
</div>
</body>
</html>
</pre>
<p>
This instrumentation has identical behaviour to the previous example.
</p>
<a name="executeList"/>
<h2 class="section">concordion:execute on a <list></h2><i>since 1.4.6</i>
<p>
The execute command has special behavior when placed on a list element (<ol> or <ul>). Instead of executing once, it executes every list item in the list (and all its sub lists) and transfers the commands from the list element to each list item element. This feature can for example be used to setup a hierarchical structure of test data.
</p>
<p>
[<a target="specs" href="dist/1.5.1/spec/concordion/command/execute/ExecutingList.html">More details</a>]
</p>
<a name="verifyRows"/>
<h2 class="section">concordion:verifyRows</h2>
<p>
Sometimes you want to check the contents of a collection of results returned
from the system. In the Fit Framework you might use a <code>RowFixture</code>.
In Concordion, you use the <code>verifyRows</code> command.