KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ut > HTC


1 /**
2  * $Id: HTC.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2002-2003,2007 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your option) any later
9  * version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.ut;
30
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.PrintStream JavaDoc;
34 import java.net.URL JavaDoc;
35
36 import org.apache.tools.ant.BuildFileTest;
37 import org.apache.tools.ant.BuildException;
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.AntXFixture;
40 import com.idaremedia.antx.Problems;
41 import com.idaremedia.antx.helpers.Tk;
42
43 /**
44  * Harness-Test-Case(<i>HTC</i>) plucked from the JWare/foundation/ut package (ssmc).
45  * Changes from the source HTC class:<ul>
46  * <li> Batched failure support removed</li>
47  * <li> Subclasses from the ant build file test helper instead of the JUnit test</li>
48  * <li> Adds dependency on AntX.Tk class for some URL->file conversion utilities</li>
49  * <li> Introduced some Ant buildfile-specific assertion helpers
50  * </ul>
51  *
52  * @since JWare0.6
53  * @author ssmc, &copy;2002-2003,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
54  * @version 0.5.1
55  * @.safety single
56  * @.group impl,test
57  * @.expects junit3.7+
58  **/

59
60 public abstract class HTC extends BuildFileTest
61 {
62     /**
63      * Creates new specific HTC as part of a labeled group.
64      * @param groupid group identifier (non-null)
65      * @param methodName test method's name (non-null)
66      **/

67     protected HTC(String JavaDoc groupid, String JavaDoc methodName)
68     {
69         super(methodName);
70         m_groupid= groupid==null ? "" : groupid;
71         initIfMustBlockCDATA();
72     }
73
74
75     /**
76      * Creates new specific HTC.
77      **/

78     protected HTC(String JavaDoc methodName)
79     {
80         super(methodName);
81         initIfMustBlockCDATA();
82     }
83
84
85     /**
86      * Creates new HTC for <i>checkBaseline</i> method.
87      **/

88     protected HTC()
89     {
90         super("checkBaseline");
91         initIfMustBlockCDATA();
92     }
93
94 // ---------------------------------------------------------------------------------------------------------|
95
// Internal Test Contract-Verification:
96
// ---------------------------------------------------------------------------------------------------------|
97

98     /**
99      * Fails a test with the given message.
100      **/

101     public static void cvfail(String JavaDoc message)
102     {
103         throw new TestError(message);
104     }
105
106
107     /**
108      * Precondition implementation: requires given condition to be <code>true</code>
109      * on entry to a method or block of code.
110      **/

111     public final void require_(boolean cond, String JavaDoc msg)
112     {
113         if (!cond) {
114             cvfail("require:"+msg);
115         }
116     }
117
118
119     /**
120      * Postcondition implementation: requires given condition to be <code>true</code>
121      * on exit from a method or block of code.
122      **/

123     public final void ensure_(boolean cond, String JavaDoc msg)
124     {
125         if (!cond) {
126             cvfail("ensure:"+msg);
127         }
128     }
129
130
131     /**
132      * Invariant implementation: requires given condition to be <code>true</code>
133      * at some well defined points (entry and/or exit to method and or blocks
134      * of codes).
135      **/

136     public final void verify_(boolean cond, String JavaDoc msg)
137     {
138         if (!cond) {
139             cvfail("verify:"+msg);
140         }
141     }
142
143
144     /**
145      * Use to write out internal class errors conditions (not failures).
146      * @param tbtn the throwable that was caught
147      * @param msg the error message (non-<code>null</code>)
148      **/

149     public void unexpected_(Throwable JavaDoc tbtn, String JavaDoc msg)
150     {
151         System.err.println("** ERROR[internal]:"+msg+tbtn);
152     }
153
154
155     /**
156      * Use to write out internal class errors conditions (not failures).
157      * @param msg the error message (non-<code>null</code>)
158      **/

159     protected void irupted_(InterruptedException JavaDoc ix, String JavaDoc msg)
160     {
161         System.err.println("** WARNG[internal]: Interrupted Thread:"+msg+ix);
162         Thread.currentThread().interrupt();
163     }
164
165
166 // ---------------------------------------------------------------------------------------------------------|
167
// Brain-Dead support for manual 'Eyeballing' test-generated output:
168
// ---------------------------------------------------------------------------------------------------------|
169

170     /** Used to uniqify tests labels. **/
171     private String JavaDoc m_groupid= "";
172
173     /** Default output stream (System.out). Subclasses can modify in their setUp. **/
174     protected PrintStream JavaDoc m_stdout= System.out;
175
176
177     /**
178      * Ugly. Until I think of a better way of doing this, it works.
179      **/

180     protected final boolean isBlockedCDATA(String JavaDoc s)
181     {
182         if (m_blockCDATA) {
183             if (s.indexOf("<![CDATA[")>=0) {
184                 return true;//burp
185
}
186         }
187         return false;
188     }
189
190     /**
191      * Convenient for diagnostics output from test (method).
192      * @see #setupStandardOut
193      **/

194     protected void println(String JavaDoc s)
195     {
196         if (!isBlockedCDATA(s)) {
197             m_stdout.println(s);
198         } else {
199             m_stdout.println("** ALERT[internal]: BLOCKED");
200         }
201     }
202
203
204     /**
205      * Another convenience for diagnostics output from test (method).
206      * @see #setupStandardOut
207      **/

208     protected void println(String JavaDoc label, Object JavaDoc o)
209     {
210         String JavaDoc s= String.valueOf(o);
211         if (!isBlockedCDATA(s)) {
212             m_stdout.println(label+"\n"+s);
213         } else {
214             m_stdout.println("** ALERT[internal]: BLOCKED");
215         }
216     }
217
218
219     /**
220      * Convenience for println just a newline.
221      **/

222     protected final void println()
223     {
224         println("");
225     }
226
227
228     /**
229      * Called by subclass's <code>setUp</code> if custom standard
230      * output PrintStream needed.
231      * @param outp standard output stream
232      * @see #println
233      **/

234     protected final void setupStandardOut(PrintStream JavaDoc outp)
235     {
236         require_(outp!=null,"stdout- nonzro stream");
237         m_stdout= outp;
238     }
239
240
241 // ---------------------------------------------------------------------------------------------------------|
242
// Retrieving Test Input Data
243
// ---------------------------------------------------------------------------------------------------------|
244

245
246     /**
247      * Helper to load an input file located in class hierarchy (with tests)
248      **/

249     protected final InputStream JavaDoc getFileResourceAsStream(String JavaDoc resource, Class JavaDoc forClass)
250     {
251         require_(resource!=null && forClass!=null,"getRez- nonzro inputs");
252         InputStream JavaDoc resFile = forClass.getResourceAsStream(resource);
253         if (resFile==null) { resFile= ClassLoader.getSystemResourceAsStream(resource); }
254         assertNotNil(resFile,"Class "+forClass.getName()+" input resource' ("+resource+")'");
255         return resFile;
256     }
257
258
259     /**
260      * Helper to load an input file for this test.
261      **/

262     protected final InputStream JavaDoc getFileResourceAsStream(String JavaDoc resource)
263     {
264         return getFileResourceAsStream(resource, getClass());
265     }
266
267
268     /**
269      * Helper to convert a class resource's location to a standard File. Fails
270      * if unable to locate file in class's location or system class path.
271      * @param forClass the class with which resource associated (non-null)
272      * @param resource the resource name (non-null)
273      * @return file reference for resource
274      **/

275     protected final File JavaDoc getFileResource(String JavaDoc resource, Class JavaDoc forClass)
276     {
277         require_(forClass!=null,"getFil- nonzro clas");
278         require_(resource!=null,"getFil- nonzro rez");
279
280         final String JavaDoc what = "Class "+forClass.getName()+" input resource' ("+resource+")'";
281
282         URL JavaDoc url = forClass.getResource(resource);
283         if (url==null) { url = ClassLoader.getSystemResource(resource); }
284         assertNotNil(url, what);
285
286         File JavaDoc f = Tk.toFile(url);
287         assertNotNil(f, what);
288         assertTrue(f.canRead(), "CanRead: "+what);
289
290         return f;
291     }
292
293
294     /**
295      * Helper to get an input file location for this test.
296      **/

297     protected final File JavaDoc getFileResource(String JavaDoc resource)
298     {
299         return getFileResource(resource, getClass());
300     }
301
302
303     /**
304      * Initialization that most tests want to do in their setUp()
305      * method.
306      **/

307     protected final void configureProjectFromResource(String JavaDoc resource)
308     {
309         configureProject(getFileResource(resource).getPath());
310     }
311
312
313 // ---------------------------------------------------------------------------------------------------------|
314
// Batchable Failures Support: *** S T U B I F I E D for outside JWare/ut (ssmc) ***
315
// ---------------------------------------------------------------------------------------------------------|
316

317
318     /**
319      * Extension of JUnit's base fail method, that works for multi-failure
320      * recording. If current thread not in batch-mode, immediately generates
321      * failure.
322      **/

323     public static final void failASAP(String JavaDoc msg)
324     {
325         fail(msg);// [batch-fail-modified]
326
}
327
328
329     /**
330      * Alternative to {@linkplain #failASAP(java.lang.String)} that takes a
331      * Throwable and uses it's message as the failure's information.
332      * @param t triggering problem (non-null)
333      **/

334     public static final void failASAP(Throwable JavaDoc t)
335     {
336         fail(t.getMessage()); // [batch-fail-modified]
337
}
338
339
340     /**
341      * Helper that generates the appropriate assertion message for failure due
342      * to unequal actual and expected.
343      **/

344     public static final void failNotEqualASAP(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
345     {
346         String JavaDoc formatted="";
347         if (msg!=null) {
348             formatted = msg+" ";
349         }
350         //NB: *ICK* but, 'failASAP' unrolled to lessen stack (ssmc)
351
formatted = formatted+"expected:[["+expected+"]] but was:[["+actual+"]]";
352         fail(formatted); // [batch-fail-modified]
353
}
354
355
356     /**
357      * Helper that generates the appropriate assertion message for failure due
358      * to unequal actual and expected.
359      **/

360     public static final void failEqualASAP(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
361     {
362         String JavaDoc formatted="";
363         if (msg!=null) {
364             formatted = msg+" ";
365         }
366         //NB: *ICK* but, 'failASAP' unrolled to lessen stack (ssmc)
367
formatted = formatted+"expected NOT to equal:[["+expected+"]]";
368         fail(formatted); // [batch-fail-modified]
369
}
370
371
372 // ---------------------------------------------------------------------------------------------------------|
373

374     /**
375      * Optional notification that test method about to be executed.
376      **/

377     protected void enter_(String JavaDoc methodName)
378     {
379         println("\n------("+m_groupid+methodName+"[Ant="+AntX.ANT_VERSIONSTRING+"]): \n");
380     }
381
382
383     /**
384      * Optional notification that test method has finished.
385      **/

386     protected void leave_(String JavaDoc methodName)
387     {
388     }
389
390
391     /**
392      * Called by TestResult to execute HTC. Tweaked to generate proper 'enter' and
393      * 'leave' notifications and to ensure batched failures are reported.
394      **/

395     public void runBare() throws Throwable JavaDoc
396     {
397         AntXFixture.reset(Problems.IgnoringHandler);//@since JWare/AntX 0.4 (ssmc)
398
enter_(getName());
399         setUp();
400         try {
401             runTest();
402             // [batch-fail-modified]
403
} finally {
404             // [batch-fail-modified]
405
tearDown();
406             leave_(getName());
407         }
408     }
409
410
411     /**
412      * Skip inherited teardown that expects a build file if no such file loaded.
413      * @since JWare/AntXtras 0.5.1
414      */

415     protected void tearDown() throws Exception JavaDoc {
416         if (getProject()!=null && getProject().getTargets()==null) {
417             super.tearDown();
418         }
419     }
420
421 // ---------------------------------------------------------------------------------------------------------|
422
// Essentials overridden from JUnit's Assertion API for batching failures and signatures:
423
// ---------------------------------------------------------------------------------------------------------|
424

425     /**
426      * Barfs with an assertion failure around given throwable's message.
427      * @see #failASAP(java.lang.Throwable)
428      **/

429     public static final void fail(Throwable JavaDoc t)
430     {
431         fail(t.getMessage());
432     }
433
434
435     /**
436      * Asserts to objects are equivalent. For non-null entities, equivalence is
437      * defined by the expected object's <i>equals</i> method.
438      **/

439     public static final void assertEqual(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
440     {
441         if (expected == null && actual == null) {
442             return;
443         }
444         if (expected != null && expected.equals(actual)) {
445             return;
446         }
447
448         //NB: *ICK* but, 'failNotEqualASAP' unrolled to lessen stack (ssmc)
449
String JavaDoc formatted="";
450         if (msg!=null) {
451             formatted = msg+" ";
452         }
453         formatted = formatted+"expected:[["+expected+"]] but was:[["+actual+"]]";
454         fail(formatted);// [batch-fail-modified]
455
}
456
457
458     /**
459      * Asserts that two objects are not equivalent. For non-null entities, equivalence
460      * is defined by the expected object's <i>equals</i> method.
461      **/

462     public static final void assertNotEqual(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
463     {
464         if (expected==null && actual!=null) {
465             return;
466         }
467         if (expected!=null && actual==null) {
468             return;
469         }
470         if (!actual.equals(expected)) {
471             return;
472         }
473
474         //NB: *ICK* but, 'failEqualASAP' unrolled to lessen stack (ssmc)
475
String JavaDoc formatted="";
476         if (msg!=null) {
477             formatted = msg+" ";
478         }
479         formatted = formatted+"expected NOT to equal:[["+expected+"]]";
480         fail(formatted);// [batch-fail-modified]
481
}
482
483 // ---------------------------------------------------------------------------------------------------------|
484

485     /**
486      * Asserts that to object references are identical (i&#46;e&#46; points to the same
487      * thing).
488      **/

489     public static final void assertIdent(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
490     {
491         if (expected==actual) {
492             return;
493         }
494         String JavaDoc formatted="";
495         if (msg!=null) {
496             formatted = msg+" ";
497         }
498         failASAP(formatted+"expected identical");
499     }
500
501
502     /** Asserts that two given objects refer to same object reference. **/
503     public static final void assertIdent(Object JavaDoc actual, Object JavaDoc expected)
504     {
505         assertIdent(expected,actual,null);
506     }
507
508
509     /**
510      * Asserts that to object references are NOT identical. Does not check
511      * equivalence of objects.
512      **/

513     public static final void assertNotIdent(Object JavaDoc actual, Object JavaDoc expected, String JavaDoc msg)
514     {
515         if (expected==actual) {
516             String JavaDoc formatted="";
517             if (msg!=null) {
518                 formatted = msg+" ";
519             }
520             failASAP(formatted+"expected NOT to be identical");
521         }
522     }
523
524
525     /** Asserts that two given objects do not refer to same object reference. **/
526     public static final void assertNotIdent(Object JavaDoc actual, Object JavaDoc expected)
527     {
528         assertNotIdent(actual,expected,null);
529     }
530
531 // ---------------------------------------------------------------------------------------------------------|
532

533     /**
534      * Asserts that value is false. More readable test since it makes a
535      * negative expectation explicit.
536      **/

537     public static final void assertFalse(boolean actual)
538     {
539         assertEqual(actual, false);
540     }
541
542
543     /**
544      * Asserts that value is false. More readable test since it makes a
545      * negative expectation explicit.
546      **/

547     public static final void assertFalse(boolean actual, String JavaDoc msg)
548     {
549         assertEqual(actual,false,msg);
550     }
551
552
553     /**
554      * Asserts that value is true. More readable test since it makes a
555      * positive expectation explicit.
556      **/

557     public static final void assertTrue(boolean actual)//for completeness
558
{
559         assertEqual(actual,true);
560     }
561
562
563     /**
564      * Asserts that value is true. More readable test since it makes a
565      * positive expectation explicit.
566      **/

567     public static final void assertTrue(boolean actual, String JavaDoc msg)
568     {
569         assertEqual(actual,true,msg);
570     }
571
572 // ---------------------------------------------------------------------------------------------------------|
573

574     /** Asserts that the given object is <i>null</i>. **/
575     public static final void assertNil(Object JavaDoc actual)
576     {
577         assertNil(actual,null);
578     }
579
580     /** Asserts that the given object is <i>null</i>. **/
581     public static final void assertNil(Object JavaDoc actual, String JavaDoc msg)
582     {
583         //NB: *ICK* but, 'failASAP' unrolled to lessen stack (ssmc)
584

585         //NB: this is specified here since failed-assertion messages based on
586
// using assertFalse never made sense...one always had to manually work
587
// back from 'false' is actually 'not-nil' or something...
588

589         if (actual!=null) {
590             String JavaDoc formatted="";
591             if (msg!=null) {
592                 formatted = msg+" ";
593             }
594
595             formatted = formatted+"expected TO BE NULL but was not.";
596             fail(formatted);
597         }
598     }
599
600     /** Asserts that the given object is <em>not</em> <i>null</i>. **/
601     public static final void assertNotNil(Object JavaDoc actual)
602     {
603         assertNotNil(actual,null);
604     }
605
606     /** Asserts that the given object is <em>not</em> <i>null</i>. **/
607     public static final void assertNotNil(Object JavaDoc actual, String JavaDoc msg)
608     {
609         //NB: *ICK* but, 'failASAP' unrolled to lessen stack (ssmc)
610

611         //NB: this is specified here since failed-assertion messages based on
612
// using assertFalse never made sense...one always had to manually work
613
// back from 'false' is actually 'not-nil' or something...
614

615         if (actual==null) {
616             String JavaDoc formatted="";
617             if (msg!=null) {
618                 formatted = msg+" ";
619             }
620
621             formatted = formatted+"expected NOT TO BE NULL but was.";
622             fail(formatted);
623         }
624     }
625
626 // ---------------------------------------------------------------------------------------------------------|
627
// HTC/JWare versions of a type-safe Assertion API:
628
// ---------------------------------------------------------------------------------------------------------|
629

630     /** Asserts that an value is equivalent to its expected value. **/
631     public static final void assertEqual(Object JavaDoc actual, Object JavaDoc expected)
632     {
633         assertEqual(actual,expected,null);
634     }
635
636     /** Asserts that a boolean value is as expected. **/
637     public static final void assertEqual(boolean actual, boolean expected)
638     {
639         assertEqual(actual,expected,null);
640     }
641     /** Asserts that a boolean value is as expected. **/
642     public static final void assertEqual(boolean actual, boolean expected, String JavaDoc msg)
643     {
644         assertEqual(actual ? Boolean.TRUE : Boolean.FALSE, expected ? Boolean.TRUE : Boolean.FALSE, msg);
645     }
646
647     /** Asserts that a character is as expected. **/
648     public static final void assertEqual(char actual, char expected)
649     {
650         assertEqual(actual,expected,null);
651     }
652     /** Asserts that a character is as expected. **/
653     public static final void assertEqual(char actual, char expected, String JavaDoc msg)
654     {
655         assertEqual(new Character JavaDoc(actual),new Character JavaDoc(expected),msg);
656     }
657
658     /** Asserts that a float is equal to expected value within a specified delta. **/
659     public static final void assertEqual(float actual, float expected, float delta)
660     {
661         assertEqual(actual,expected,delta,null);
662     }
663     /** Asserts that a float is equal to expected value within a specified delta. Infinites must match. **/
664     public static final void assertEqual(float actual, float expected, float delta, String JavaDoc msg)
665     {
666         if (Float.isInfinite(expected)) {
667             if (!(expected==actual)) {
668                 failNotEqualASAP(new Float JavaDoc(actual), new Float JavaDoc(expected), msg);
669             }
670         } else if (!(Math.abs(expected-actual)<=delta)) {
671             failNotEqualASAP(new Float JavaDoc(actual), new Float JavaDoc(expected), msg);
672         }
673     }
674     /** Asserts that a float is equal to an expected value. **/
675     public static final void assertEqual(float actual, float expected)
676     {
677         assertEqual(actual,expected,0f,null);
678     }
679
680     /** Asserts that a double is equal to expected value within a specified delta.**/
681     public static final void assertEqual(double actual, double expected, double delta)
682     {
683         assertEqual(actual,expected,delta,null);
684     }
685     /** Asserts that a double is equal to expected value within a specified delta. Infinites must match. **/
686     public static final void assertEqual(double actual, double expected, double delta, String JavaDoc msg)
687     {
688         if (Double.isInfinite(expected)) {
689             if (!(expected==actual)) {
690                 failNotEqualASAP(new Double JavaDoc(actual), new Double JavaDoc(expected), msg);
691             }
692         } else if (!(Math.abs(expected-actual)<=delta)) {
693             failNotEqualASAP(new Double JavaDoc(actual), new Double JavaDoc(expected), msg);
694         }
695     }
696     /** Asserts that a double is equal to an expected value. **/
697     public static final void assertEqual(double actual, double expected)
698     {
699         assertEqual(actual,expected,0d,null);
700     }
701
702     /** Asserts that a byte is equal to an expected value. **/
703     public static final void assertEqual(byte actual, byte expected)
704     {
705         assertEqual(actual,expected,null);
706     }
707     /** Asserts that a byte is equal to an expected value. **/
708     public static final void assertEqual(byte actual, byte expected, String JavaDoc msg)
709     {
710         assertEqual(new Byte JavaDoc(actual),new Byte JavaDoc(expected),msg);
711     }
712
713     /** Asserts that a short is equal to an expected value. **/
714     public static final void assertEqual(short actual, short expected)
715     {
716         assertEqual(actual,expected,null);
717     }
718     /** Asserts that a short is equal to an expected value. **/
719     public static final void assertEqual(short actual, short expected, String JavaDoc msg)
720     {
721         assertEqual(new Short JavaDoc(actual), new Short JavaDoc(expected), msg);
722     }
723
724     /** Asserts that an integer is equal to an expected value. **/
725     public static final void assertEqual(int actual, int expected)
726     {
727         assertEqual(actual,expected,null);
728     }
729     /** Asserts that an integer is equal to an expected value. **/
730     public static final void assertEqual(int actual, int expected, String JavaDoc msg)
731     {
732         assertEqual(new Integer JavaDoc(actual), new Integer JavaDoc(expected), msg);
733     }
734
735     /** Asserts that a long is equal to an expected value. **/
736     public static final void assertEqual(long actual, long expected)
737     {
738         assertEqual(actual,expected,null);
739     }
740     /** Asserts that a long is equal to an expected value. **/
741     public static final void assertEqual(long actual, long expected, String JavaDoc msg)
742     {
743         assertEqual(new Long JavaDoc(actual), new Long JavaDoc(expected), msg);
744     }
745
746 // ---------------------------------------------------------------------------------------------------------|
747
// HTC/JWare versions of a negative type-safe Assertion API:
748
// ---------------------------------------------------------------------------------------------------------|
749

750     /**
751      * Asserts that two objects are not equivalent. For non-null entities, equivalence
752      * is defined by the expected object's <i>equals</i> method.
753      **/

754     public static final void assertNotEqual(Object JavaDoc actual, Object JavaDoc expected)
755     {
756         assertNotEqual(actual,expected,null);
757     }
758
759     /** Asserts that two booleans are different. **/
760     public static void assertNotEqual(boolean actual, boolean expected, String JavaDoc msg)
761     {
762         assertNotEqual(new Boolean JavaDoc(actual), new Boolean JavaDoc(expected), msg);
763     }
764     /** Asserts that two booleans are different. **/
765     public static void assertNotEqual(boolean actual, boolean expected)
766     {
767         assertNotEqual(actual, expected, null);
768     }
769
770     /** Asserts that two characters are different. **/
771     public static final void assertNotEqual(char actual, char expected, String JavaDoc msg)
772     {
773         assertNotEqual(new Character JavaDoc(actual), new Character JavaDoc(expected), msg);
774     }
775     /** Asserts that two characters are different. **/
776     public static void assertNotEqual(char actual, char expected)
777     {
778         assertNotEqual(actual, expected, null);
779     }
780
781     /** Asserts that two bytes are different. **/
782     public static void assertNotEqual(byte actual, byte expected, String JavaDoc msg)
783     {
784         assertNotEqual(new Byte JavaDoc(actual), new Byte JavaDoc(expected), msg);
785     }
786     /** Asserts that two bytes are different. **/
787     public static void assertNotEqual(byte actual, byte expected)
788     {
789         assertNotEqual(actual, expected, null);
790     }
791
792     /** Asserts that two shorts are different. **/
793     public static void assertNotEqual(short actual, short expected, String JavaDoc msg)
794     {
795         assertNotEqual(new Short JavaDoc(actual), new Short JavaDoc(expected), msg);
796     }
797     /** Asserts that two shorts are different. **/
798     public static void assertNotEqual(short actual, short expected)
799     {
800         assertNotEqual(actual, expected, null);
801     }
802
803     /** Asserts that two integers are different. **/
804     public static void assertNotEqual(int actual, int expected, String JavaDoc msg)
805     {
806         assertNotEqual(new Integer JavaDoc(actual), new Integer JavaDoc(expected), msg);
807     }
808     /** Asserts that two integers are different. **/
809     public static void assertNotEqual(int actual, int expected)
810     {
811         assertNotEqual(actual, expected, null);
812     }
813
814     /** Asserts that two longs are different. **/
815     public static void assertNotEqual(long actual, long expected, String JavaDoc msg)
816     {
817         assertNotEqual(new Long JavaDoc(actual), new Long JavaDoc(expected), msg);
818     }
819     /** Asserts that two longs are different. **/
820     public static void assertNotEqual(long actual, long expected)
821     {
822         assertNotEqual(actual, expected, null);
823     }
824
825     /**
826      * Asserts that two floats differ by more than a specified delta.
827      **/

828     public static void assertNotEqual(float actual, float expected, float delta)
829     {
830         assertNotEqual(actual,expected,delta,null);
831     }
832     /**
833      * Asserts that two floats differ by more than a specified delta.
834      **/

835     public static void assertNotEqual(float actual, float expected, float delta, String JavaDoc msg)
836     {
837         if (Float.isInfinite(expected)) {
838             if (expected==actual) {
839                 failEqualASAP(new Float JavaDoc(actual),new Float JavaDoc(expected),msg);
840             }
841         } else if ((Math.abs(expected-actual)<=delta)) {
842             failEqualASAP(new Float JavaDoc(actual), new Float JavaDoc(expected), msg);
843         }
844     }
845     /**
846      * Asserts that two floats differ.
847      **/

848     public static void assertNotEqual(float actual, float expected)
849     {
850         assertNotEqual(actual,expected,0f,null);
851     }
852
853     /**
854      * Asserts that two doubles differ by more than a specified delta.
855      **/

856     public static void assertNotEqual(double actual, double expected, double delta, String JavaDoc msg)
857     {
858         if (Double.isInfinite(expected)) {
859             if (expected==actual) {
860                 failEqualASAP(new Double JavaDoc(actual),new Double JavaDoc(expected),msg);
861             }
862         } else if ((Math.abs(expected-actual)<=delta)) {
863             failEqualASAP(new Double JavaDoc(actual), new Double JavaDoc(expected), msg);
864         }
865     }
866     /**
867      * Asserts that two doubles differ by more than a specified delta.
868      **/

869     public static void assertNotEqual(double actual, double expected, double delta)
870     {
871         assertNotEqual(actual,expected,delta,null);
872     }
873     /**
874      * Asserts that two doubles differ.
875      **/

876     public static void assertNotEqual(double actual, double expected)
877     {
878         assertNotEqual(actual,expected,0d,null);
879     }
880
881 // ---------------------------------------------------------------------------------------------------------|
882
// HTC/JWare additions to Assertion API (ALL SAFE FOR BATCHED FAILURES):
883
// ---------------------------------------------------------------------------------------------------------|
884

885     /**
886      * Helper that verifies a thrown exception contains a distinguishing tag.
887      **/

888     protected final void assertExpected(Throwable JavaDoc thr, String JavaDoc tag)
889     {
890         require_(thr!=null, "verfyXpected- nonzro throwable");
891         String JavaDoc s= thr.getMessage();
892         if (s==null) {
893             failASAP("Barfage from("+tag+") expected but got NULL message");
894         } else if (s.indexOf(tag)<0) {
895             failASAP("Barfage from("+tag+") expected but not found. Actual:[["+s+"]]");
896         }
897     }
898
899
900     /**
901      * Helper that verifies a thrown exception is of an expected class and
902      * contains a distinguishing tag.
903      **/

904     protected final void assertExpected(Throwable JavaDoc thr, Class JavaDoc thrClass, String JavaDoc tag)
905     {
906         require_(thr!=null, "verfyXpected- nonzro throwable");
907         if (!thrClass.isAssignableFrom(thr.getClass())) {
908             failASAP("Barfage from("+tag+") expected throwable of class[["+thrClass.getName()+"]]. "+
909                      "Actual class[["+thr.getClass().getName()+"]]");
910         } else {
911             assertExpected(thr,tag);
912         }
913     }
914
915
916
917     /** Asserts that the given string is non-null, not empty, and contains
918         characters other than whitespace (as defined by Java specification). **/

919     public final void assertNotWhitespace(String JavaDoc s)
920     {
921         assertNotWhitespace(s,"Is whitespace?");
922     }
923
924
925     /** Asserts that the given string is non-null, not empty, and contains
926         characters other than whitespace (as defined by Java specification). **/

927     public final void assertNotWhitespace(String JavaDoc s, String JavaDoc msg)
928     {
929         if (s==null) {
930             failEqualASAP(s,"NULL string",msg);
931         } else if (s.length()==0) {
932             failEqualASAP(s,"zero length string",msg);
933         } else {
934             for (int i=0,n=s.length();i<n;i++) {
935                 if (!Character.isWhitespace(s.charAt(i))) {
936                     return;
937                 }
938             }
939         }
940         failASAP(msg);
941     }
942
943
944 // ---------------------------------------------------------------------------------------------------------|
945
// Custom assertions for BuildFile testing:
946
// ---------------------------------------------------------------------------------------------------------|
947

948     /**
949      * Assert that the given message has been logged when running
950      * the given target.
951      */

952     protected void expectFullLogContaining(String JavaDoc target, String JavaDoc log) {
953         executeTarget(target);
954         String JavaDoc realLog = getFullLog();
955         assertTrue("expecting fulllog to contain \""+log+"\" log was \""
956                    + realLog + "\"",
957                    realLog.indexOf(log) >= 0);
958     }
959
960
961     /**
962      * Runs target and automatically writes the log (error/warn/info)
963      * to this test's output stream.
964      **/

965     protected final String JavaDoc runTarget(String JavaDoc target)
966     {
967         require_(target!=null,"runTarget- nonzro name");
968         String JavaDoc log=null;
969         try {
970             executeTarget(target);
971         }finally {
972             log= getLog();
973             println("ANT's INFO+WARN+ERR LLLOOOGGG: ----",log);
974         }
975         return log;
976     }
977
978
979     /**
980      * Runs target and automatically writes the full log (everything)
981      * to this test's output stream.
982      **/

983     protected final String JavaDoc runTargetDumpFullLog(String JavaDoc target)
984     {
985         try {
986             return runTarget(target);
987         }finally {
988             println();
989             println("FFFUUUUUULLLLL LOG:",getFullLog());
990         }
991     }
992
993
994     /**
995      * Extension of {@linkplain #runTarget runTarget} the also checks
996      * that there's not output in the standard ant logs.
997      **/

998     protected final String JavaDoc runTargetNoOutput(String JavaDoc target)
999     {
1000        String JavaDoc log = runTarget(target);
1001        assertEqual(log.length(),0,"Generated log output");
1002        return log;
1003    }
1004
1005
1006    /**
1007     * Verify a marker string occurs only an expected number of times in
1008     * given output log.
1009     **/

1010    protected final void verifyOccurances(final String JavaDoc log, final String JavaDoc ofStr,
1011                                          final int Nexpected)
1012    {
1013        int Nfound = 0;
1014        int i, from=0;
1015        while ((i=log.indexOf(ofStr,from))>=0) {
1016            Nfound++;
1017            from = i+ofStr.length();
1018        }
1019        assertTrue("Expecting Log to contain \""+Nexpected+"\" occurances of ["+ofStr+
1020                   "] but Log was \"" + log + "\"",
1021                   Nfound==Nexpected);
1022    }
1023
1024
1025    /**
1026     * Like inherited expectBuildException except automatically writes
1027     * the standard err/warn/info log to this test's output stream.
1028     **/

1029    protected final String JavaDoc runExpecting(String JavaDoc target, String JavaDoc cause)
1030    {
1031        expectBuildException(target,cause);
1032        String JavaDoc log = getLog();
1033        println("ANT's INFO+WARN+ERR LLLOOOGGG: ----",log);
1034        return log;
1035    }
1036
1037
1038    /**
1039     * Verify the order in which certain marker string logged to an
1040     * output stream.
1041     **/

1042    protected final void verifyOutputInOrder(final String JavaDoc log, String JavaDoc[] strings)
1043    {
1044        final int Nmax = log.length();
1045        int ilast=0;
1046        for (int i=0;i<strings.length;i++) {
1047            int at = log.indexOf(strings[i], ilast);
1048            assertTrue("Expecting Log to contain \""+strings[i]+"\" in relative position ["+i+
1049                       "] but Log was \"" + log + "\"",
1050                       at>=0);
1051            ilast = at+strings[i].length();
1052            if ((ilast==Nmax) && (i!=strings.length-1)) {
1053                fail("Expecting Log to contain more messages("+strings[i+1]+", ...etc.) but "+
1054                     "Log is ended after ("+strings[i]+") Log was \""+ log + "\"");
1055            }
1056        }
1057    }
1058
1059
1060    /**
1061     * Checks whether a 'broken' build xml file can be loaded. Used for
1062     * tasks that verify structure as they're being built at parse-time.
1063     **/

1064    protected final void verifyCantLoadFile(String JavaDoc resource, String JavaDoc problem)
1065    {
1066        try {
1067            configureProjectFromResource(resource);
1068            if (AntX.STRICT_SCRIPTS || "true".equals(System.getProperty(AntX.STRICT_SCRIPTCHECK_PROP))) {
1069                runTarget(getProject().getDefaultTarget());//@since Ant1.6+UnknownElements!
1070
}
1071            fail("Should not be able parse file with "+problem+
1072                 "! Strict="+AntX.STRICT_SCRIPTS);
1073        }catch(BuildException x) {
1074            println("LOADFILE-BARFAGE: ",x.getMessage());
1075            /*burp*/
1076        }
1077    }
1078
1079
1080
1081// ---------------------------------------------------------------------------------------------------------|
1082
// HTC compatibility with default JUnitReport (hacked for now).
1083
// JUnitReport gets very confused by xml written to either System.out or System.err.
1084
// For now we silently eat anything written that contains CDATA flag!
1085
// ---------------------------------------------------------------------------------------------------------|
1086

1087    private boolean m_blockCDATA=false;
1088
1089    private void initIfMustBlockCDATA()
1090    {
1091        String JavaDoc s;
1092        s= readEnv(HTestConstants.BLOCK_CDATA_STDIO);
1093        if (s!=null) {
1094            s= s.toLowerCase();
1095        }
1096        if ("true".equals(s) || "on".equals(s)) {
1097            m_blockCDATA=true;
1098        }
1099    }
1100
1101    private String JavaDoc readEnv(String JavaDoc name)
1102    {
1103        try {
1104            return System.getProperty(name);
1105        } catch(SecurityException JavaDoc sx) {
1106        }
1107        return null;//for our purposes
1108
}
1109
1110}
1111
1112/* end-of-HTC.java */
1113
Popular Tags