KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > handlers > EnforceHandlerTest


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.csv.handlers;
23
24 import junit.framework.TestCase;
25 import org.dbunit.dataset.csv.IllegalInputCharacterException;
26
27 /**
28  * author: fede
29  * 4-set-2003 11.42.06
30  * $Revision: 1.1 $
31  */

32 public class EnforceHandlerTest extends TestCase {
33     Pipeline pipeline;
34
35     public void testOwnAnEnforcedHandler () {
36         PipelineComponent enforced = AllHandler.ACCEPT();
37         EnforceHandler enforceHandler = (EnforceHandler)EnforceHandler.ENFORCE(enforced);
38         pipeline.putFront(enforceHandler);
39
40         assertTrue(true);
41         assertSame(enforced, enforceHandler.getEnforcedComponents()[0]);
42         assertSame("enforced pipeline should be the same of the enforcing one", enforceHandler.getPipeline(), enforced.getPipeline());
43     }
44
45     public void testThrowExceptionWhenEnforcedDoesNotHandle () throws PipelineException, IllegalInputCharacterException {
46         PipelineComponent enforceHandler = EnforceHandler.ENFORCE(new MockHandler());
47         pipeline.putFront(enforceHandler);
48         try {
49             enforceHandler.handle('x');
50             fail ("Enforce handler should have thrown an exception");
51         } catch (IllegalInputCharacterException illEx) {}
52
53     }
54
55     public void testDontRemoveItselfOnException () throws PipelineException, IllegalInputCharacterException {
56         PipelineComponent enforceHandler = EnforceHandler.ENFORCE(new MockHandler());
57         pipeline.putFront(enforceHandler);
58         try {
59             pipeline.handle('x');
60             fail ("Enforce handler should have thrown an exception");
61         } catch (IllegalInputCharacterException illEx) {}
62
63         assertSame(pipeline.removeFront(), enforceHandler);
64     }
65
66     public void testRemoveItselfAfterEnforcing () throws PipelineException, IllegalInputCharacterException {
67         PipelineComponent enforceHandler = EnforceHandler.ENFORCE(AllHandler.ACCEPT());
68         pipeline.putFront(enforceHandler);
69         pipeline.handle('\"');
70         pipeline.thePieceIsDone();
71         assertNotSame(pipeline.removeFront(), enforceHandler);
72         assertEquals(1, pipeline.getProducts().size());
73         assertEquals("\"", pipeline.getProducts().get(0));
74     }
75
76     public void testEnforceOneBetweenMany () throws PipelineException, IllegalInputCharacterException {
77         PipelineComponent pass = SeparatorHandler.ACCEPT();
78         PipelineComponent accept = AllHandler.ACCEPT();
79         EnforceHandler enforceHandler = (EnforceHandler)EnforceHandler.ENFORCE(new PipelineComponent [] {pass, accept});
80         pipeline.putFront(enforceHandler);
81
82         pipeline.handle('\"');
83         pipeline.thePieceIsDone();
84
85         assertNotSame(pipeline.removeFront(), enforceHandler);
86         assertEquals(1, pipeline.getProducts().size());
87         assertEquals("\"", pipeline.getProducts().get(0));
88     }
89
90     protected void setUp() throws Exception JavaDoc {
91         pipeline = new Pipeline();
92     }
93
94 }
95
Popular Tags