1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.spi.beans.impl;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import org.junit.Test;
25  import org.seasar.cubby.spi.BeanDescProvider;
26  import org.seasar.cubby.spi.beans.Attribute;
27  import org.seasar.cubby.spi.beans.BeanDesc;
28  import org.seasar.cubby.spi.beans.IllegalAttributeException;
29  
30  public class DefaultBeanDescProviderPropertyTest {
31  
32  	BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
33  
34  	@Test
35  	public void invalidProperty() throws Exception {
36  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(Bean.class);
37  		assertTrue(beanDesc.hasPropertyAttribute("aaa"));
38  		assertTrue(beanDesc.hasPropertyAttribute("bbb"));
39  		assertTrue(beanDesc.hasFieldAttribute("aaa"));
40  		assertTrue(beanDesc.hasFieldAttribute("bbb"));
41  		Attribute aaa = beanDesc.getPropertyAttribute("aaa");
42  		assertTrue(aaa.isReadable());
43  		assertTrue(aaa.isWritable());
44  		Attribute bbb = beanDesc.getPropertyAttribute("bbb");
45  		assertTrue(bbb.isReadable());
46  		assertFalse(bbb.isWritable());
47  
48  		Bean bean = new Bean();
49  
50  		assertEquals("abc", aaa.getValue(bean));
51  		aaa.setValue(bean, "abcd");
52  		assertEquals("abcd", aaa.getValue(bean));
53  
54  		assertEquals("123", bbb.getValue(bean));
55  		try {
56  			bbb.setValue(bean, "1234");
57  			fail();
58  		} catch (IllegalAttributeException e) {
59  		}
60  		assertEquals("123", bbb.getValue(bean));
61  	}
62  
63  	/**
64       * 
65       */
66  	public static class Bean {
67  
68  		private String aaa = "abc";
69  
70  		private String bbb = "123";
71  
72  		public String getAaa() {
73  			return aaa;
74  		}
75  
76  		public void setAaa(String aaa) {
77  			this.aaa = aaa;
78  		}
79  
80  		public String getBbb() {
81  			return bbb;
82  		}
83  	}
84  
85  }