001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    
017    package org.springframework.binding.support;
018    
019    import java.util.Map;
020    
021    import org.springframework.binding.PropertyMetadataAccessStrategy;
022    import org.springframework.binding.form.FormModel;
023    import org.springframework.binding.form.FieldMetadata;
024    import org.springframework.binding.form.support.DefaultFormModel;
025    import org.springframework.richclient.test.SpringRichTestCase;
026    
027    /**
028     * @author andy
029     * @since May 8, 2006 5:20:39 PM
030     */
031    public class AnnotationAwareBeanPropertyAccessStrategyTests extends SpringRichTestCase {
032        public void testBeanWithNoAnnotations() {
033            final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new NoAnnotationTestBean());
034            final PropertyMetadataAccessStrategy mas = pas.getMetadataAccessStrategy();
035            
036            Map<String,Object> um = mas.getAllUserMetadata("name");
037            assertTrue(um == null || um.size() == 0);
038            um = mas.getAllUserMetadata("age");
039            assertTrue(um == null || um.size() == 0);
040            um = mas.getAllUserMetadata("rank");
041            assertTrue(um == null || um.size() == 0);
042        }
043        
044        public void testBeanWithAnnotations() throws Exception {
045            final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new TestBean());
046            final PropertyMetadataAccessStrategy mas = pas.getMetadataAccessStrategy();
047            
048            Map<String,Object> um = mas.getAllUserMetadata("name");
049            assertNotNull(um);
050            assertEquals(2, um.size());
051            assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
052            NoValueAnnotation nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
053            assertNotNull(nva);
054            
055            um = mas.getAllUserMetadata("age");
056            assertNotNull(um);
057            assertEquals(4, um.size());
058            assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
059            assertEquals("The Age Method", um.get("org.springframework.binding.support.SingleValueAnnotation"));
060            nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
061            assertNotNull(nva);
062            SingleValueAnnotation sva = (SingleValueAnnotation)um.get("@" + SingleValueAnnotation.class.getName());
063            assertNotNull(sva);
064            assertEquals("The Age Method", sva.value());
065            
066            um = mas.getAllUserMetadata("rank");
067            assertNotNull(um);
068            assertEquals(9, um.size());
069            assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.NoValueAnnotation"));
070            assertEquals("The Rank Method", um.get("org.springframework.binding.support.SingleValueAnnotation"));
071            assertEquals(Boolean.TRUE, um.get("org.springframework.binding.support.MultiValueAnnotation"));
072            assertEquals("First Test Name", um.get("org.springframework.binding.support.MultiValueAnnotation.name"));
073            assertEquals(24.5, um.get("org.springframework.binding.support.MultiValueAnnotation.age"));
074            assertEquals(10, um.get("org.springframework.binding.support.MultiValueAnnotation.rank"));
075            nva = (NoValueAnnotation)um.get("@" + NoValueAnnotation.class.getName());
076            assertNotNull(nva);
077            sva = (SingleValueAnnotation)um.get("@" + SingleValueAnnotation.class.getName());
078            assertNotNull(sva);
079            assertEquals("The Rank Method", sva.value());
080            MultiValueAnnotation mva = (MultiValueAnnotation)um.get("@" + MultiValueAnnotation.class.getName());
081            assertNotNull(mva);
082            assertEquals("First Test Name", mva.name());
083            assertEquals(24.5, mva.age());
084            assertEquals(10, mva.rank());
085            
086            um = mas.getAllUserMetadata("description");
087            assertTrue(um == null || um.size() == 0);
088        }
089        
090        public void testInFormModel() {
091            final AnnotationAwareBeanPropertyAccessStrategy pas = new AnnotationAwareBeanPropertyAccessStrategy(new TestBean());
092            final FormModel fm = new DefaultFormModel(pas);
093            
094            FieldMetadata pm = fm.getFieldMetadata("name"); 
095            assertNotNull(pm);
096            assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
097            NoValueAnnotation nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
098            assertNotNull(nva);
099            assertEquals(2, pm.getAllUserMetadata().size());
100            
101            pm = fm.getFieldMetadata("age");
102            assertNotNull(pm);
103            assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
104            assertEquals("The Age Method", pm.getUserMetadata("org.springframework.binding.support.SingleValueAnnotation"));
105            nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
106            assertNotNull(nva);
107            SingleValueAnnotation sva = (SingleValueAnnotation)pm.getUserMetadata("@" + SingleValueAnnotation.class.getName());
108            assertNotNull(sva);
109            assertEquals("The Age Method", sva.value());
110            assertEquals(4, pm.getAllUserMetadata().size());
111            
112            pm = fm.getFieldMetadata("rank");
113            assertNotNull(pm);
114            assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.NoValueAnnotation"));
115            assertEquals("The Rank Method", pm.getUserMetadata("org.springframework.binding.support.SingleValueAnnotation"));
116            assertEquals(Boolean.TRUE, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation"));
117            assertEquals("First Test Name", pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.name"));
118            assertEquals(24.5, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.age"));
119            assertEquals(10, pm.getUserMetadata("org.springframework.binding.support.MultiValueAnnotation.rank"));
120            nva = (NoValueAnnotation)pm.getUserMetadata("@" + NoValueAnnotation.class.getName());
121            assertNotNull(nva);
122            sva = (SingleValueAnnotation)pm.getUserMetadata("@" + SingleValueAnnotation.class.getName());
123            assertNotNull(sva);
124            assertEquals("The Rank Method", sva.value());
125            MultiValueAnnotation mva = (MultiValueAnnotation)pm.getUserMetadata("@" + MultiValueAnnotation.class.getName());
126            assertNotNull(mva);
127            assertEquals("First Test Name", mva.name());
128            assertEquals(24.5, mva.age());
129            assertEquals(10, mva.rank());
130            assertEquals(9, pm.getAllUserMetadata().size());
131            
132            pm = fm.getFieldMetadata("description");
133            assertNotNull(pm);
134            assertTrue(pm.getAllUserMetadata() == null || pm.getAllUserMetadata().size() == 0);
135        }
136        
137        
138        
139        
140        
141        
142        
143        public static class NoAnnotationTestBean {
144            private String name;
145            private double age;
146            private int rank;
147    
148            public String getName() {
149                return name;
150            }
151    
152            public void setName(final String name) {
153                this.name = name;
154            }
155    
156            public double getAge() {
157                return age;
158            }
159    
160            public void setAge(final double age) {
161                this.age = age;
162            }
163    
164            public int getRank() {
165                return rank;
166            }
167    
168            public void setRank(final int rank) {
169                this.rank = rank;
170            }
171        }
172        
173        
174        
175        
176        
177        
178        public static class TestBean {
179            private String name;
180            private double age;
181            private int rank;
182            private String description;
183            
184            @NoValueAnnotation
185            public String getName() {
186                return name;
187            }
188            
189            public void setName(final String name) {
190                this.name = name;
191            }
192            
193            @SingleValueAnnotation("The Age Method")
194            public double getAge() {
195                return age;
196            }
197    
198            @NoValueAnnotation
199            public void setAge(final double age) {
200                this.age = age;
201            }
202    
203            @MultiValueAnnotation(name="Second Test Name", age=30, rank=5)
204            @SingleValueAnnotation("The Rank Method")
205            public void setRank(final int rank) {
206                this.rank = rank;
207            }
208    
209            @MultiValueAnnotation(name="First Test Name", age=24.5, rank=10)
210            @NoValueAnnotation
211            public int getRank() {
212                return rank;
213            }
214    
215            public String getDescription() {
216                return description;
217            }
218    
219            public void setDescription(final String description) {
220                this.description = description;
221            }
222        }
223    }