View Javadoc
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.common.handlers;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * author: fede
28   * 4-set-2003 11.42.06
29   * $Revision$
30   */
31  public class EnforceHandlerTest extends TestCase {
32      Pipeline pipeline;
33  
34      public void testOwnAnEnforcedHandler () {
35          PipelineComponent enforced = AllHandler.ACCEPT();
36          EnforceHandler enforceHandler = (EnforceHandler)EnforceHandler.ENFORCE(enforced);
37          pipeline.putFront(enforceHandler);
38  
39          assertTrue(true);
40          assertSame(enforced, enforceHandler.getEnforcedComponents()[0]);
41          assertSame("enforced pipeline should be the same of the enforcing one", enforceHandler.getPipeline(), enforced.getPipeline());
42      }
43  
44      public void testThrowExceptionWhenEnforcedDoesNotHandle () throws PipelineException, IllegalInputCharacterException {
45          PipelineComponent enforceHandler = EnforceHandler.ENFORCE(new MockHandler());
46          pipeline.putFront(enforceHandler);
47          try {
48              enforceHandler.handle('x');
49              fail ("Enforce handler should have thrown an exception");
50          } catch (IllegalInputCharacterException illEx) {}
51  
52      }
53  
54      public void testDontRemoveItselfOnException () throws PipelineException, IllegalInputCharacterException {
55          PipelineComponent enforceHandler = EnforceHandler.ENFORCE(new MockHandler());
56          pipeline.putFront(enforceHandler);
57          try {
58              pipeline.handle('x');
59              fail ("Enforce handler should have thrown an exception");
60          } catch (IllegalInputCharacterException illEx) {}
61  
62          assertSame(pipeline.removeFront(), enforceHandler);
63      }
64  
65      public void testRemoveItselfAfterEnforcing () throws PipelineException, IllegalInputCharacterException {
66          PipelineComponent enforceHandler = EnforceHandler.ENFORCE(AllHandler.ACCEPT());
67          pipeline.putFront(enforceHandler);
68          pipeline.handle('\"');
69          pipeline.thePieceIsDone();
70          assertNotSame(pipeline.removeFront(), enforceHandler);
71          assertEquals(1, pipeline.getProducts().size());
72          assertEquals("\"", pipeline.getProducts().get(0));
73      }
74  
75      public void testEnforceOneBetweenMany () throws PipelineException, IllegalInputCharacterException {
76          PipelineComponent pass = SeparatorHandler.ACCEPT();
77          PipelineComponent accept = AllHandler.ACCEPT();
78          EnforceHandler enforceHandler = (EnforceHandler)EnforceHandler.ENFORCE(new PipelineComponent [] {pass, accept});
79          pipeline.putFront(enforceHandler);
80  
81          pipeline.handle('\"');
82          pipeline.thePieceIsDone();
83  
84          assertNotSame(pipeline.removeFront(), enforceHandler);
85          assertEquals(1, pipeline.getProducts().size());
86          assertEquals("\"", pipeline.getProducts().get(0));
87      }
88  
89      protected void setUp() throws Exception {
90          pipeline = new Pipeline();
91      }
92  
93  }