1   /*
2    * Copyright 2002-2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.form.builder.support;
17  
18  import java.util.Arrays;
19  
20  import junit.framework.TestCase;
21  
22  import org.springframework.binding.form.FormModel;
23  import org.springframework.binding.form.support.DefaultFormModel;
24  import org.springframework.binding.value.swing.TestableFormComponentInterceptor;
25  import org.springframework.richclient.form.builder.FormComponentInterceptor;
26  
27  /**
28   * Tests for <code>ConfigurableFormComponentInterceptorFactory</code>.
29   * 
30   * @author Peter De Bruycker
31   */
32  public class ConfigurableFormComponentInterceptorFactoryTests extends TestCase {
33      public void testSettingBothIncludedAndExcludedFormModelIdsMustFail() throws Exception {
34          TestableConfigurableFormComponentInterceptorFactory factory = new TestableConfigurableFormComponentInterceptorFactory();
35  
36          factory.setIncludedFormModelIds( new String[] { "included-0", "included-1" } );
37          factory.setExcludedFormModelIds( new String[] { "excluded-0", "excluded-1" } );
38  
39          try {
40              factory.afterPropertiesSet();
41              fail( "Should throw IllegalStateException" );
42          } catch( IllegalStateException e ) {
43              // test passes;
44          }
45      }
46  
47      public void testSetIncludedFormModelIds() throws Exception {
48          TestableConfigurableFormComponentInterceptorFactory factory = new TestableConfigurableFormComponentInterceptorFactory();
49          factory.setCreateThis( new TestableFormComponentInterceptor() );
50  
51          factory.setIncludedFormModelIds( new String[] { "included-0", "included-1" } );
52          factory.afterPropertiesSet();
53  
54          assertTrue( Arrays.equals( new String[] { "included-0", "included-1" }, factory.getIncludedFormModelIds() ) );
55  
56          DefaultFormModel included = new DefaultFormModel();
57          included.setId( "included-0" );
58          DefaultFormModel excluded = new DefaultFormModel();
59          excluded.setId( "excluded-0" );
60  
61          assertNotNull( "FormModel should be included", factory.getInterceptor( included ) );
62          assertNull( "FormModel is not included", factory.getInterceptor( excluded ) );
63      }
64  
65      public void testSetExcludedFormModelIds() throws Exception {
66          TestableConfigurableFormComponentInterceptorFactory factory = new TestableConfigurableFormComponentInterceptorFactory();
67          factory.setCreateThis( new TestableFormComponentInterceptor() );
68  
69          factory.setExcludedFormModelIds( new String[] { "excluded-0", "excluded-1" } );
70          factory.afterPropertiesSet();
71  
72          assertTrue( Arrays.equals( new String[] { "excluded-0", "excluded-1" }, factory.getExcludedFormModelIds() ) );
73  
74          DefaultFormModel included = new DefaultFormModel();
75          included.setId( "included-0" );
76          DefaultFormModel excluded = new DefaultFormModel();
77          excluded.setId( "excluded-0" );
78  
79          assertNotNull( "FormModel should be included", factory.getInterceptor( included ) );
80          assertNull( "FormModel is not included", factory.getInterceptor( excluded ) );
81      }
82      
83      private class TestableConfigurableFormComponentInterceptorFactory extends ConfigurableFormComponentInterceptorFactory {
84          private FormComponentInterceptor createThis;
85          private FormModel lastFormModel;
86  
87          protected FormComponentInterceptor createInterceptor( FormModel formModel ) {
88              lastFormModel = formModel;
89              return createThis;
90          }
91  
92          public void reset() {
93              createThis = null;
94              lastFormModel = null;
95          }
96  
97          public void setCreateThis( FormComponentInterceptor createThis ) {
98              this.createThis = createThis;
99          }
100 
101         public void setLastFormModel( FormModel lastFormModel ) {
102             this.lastFormModel = lastFormModel;
103         }
104 
105         public FormModel getLastFormModel() {
106             return lastFormModel;
107         }
108     }
109 }