1   /*
2    * Copyright 2004-2009 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  package org.seasar.cubby.spi.beans.impl;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Test;
24  import org.seasar.cubby.spi.BeanDescProvider;
25  import org.seasar.cubby.spi.beans.Attribute;
26  import org.seasar.cubby.spi.beans.BeanDesc;
27  import org.seasar.cubby.spi.beans.IllegalAttributeException;
28  
29  public class DefaultBeanDescProviderFieldTest {
30  
31  	BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
32  
33  	@Test
34  	public void invalidProperty() throws Exception {
35  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(Bean.class);
36  		assertFalse(beanDesc.hasPropertyAttribute("aaa"));
37  		assertFalse(beanDesc.hasPropertyAttribute("bbb"));
38  		assertTrue(beanDesc.hasFieldAttribute("aaa"));
39  		assertTrue(beanDesc.hasFieldAttribute("bbb"));
40  		Attribute aaa = beanDesc.getFieldAttribute("aaa");
41  		assertTrue(aaa.isReadable());
42  		assertTrue(aaa.isWritable());
43  		Attribute bbb = beanDesc.getFieldAttribute("bbb");
44  		assertTrue(bbb.isReadable());
45  		assertFalse(bbb.isWritable());
46  
47  		Bean bean = new Bean();
48  
49  		assertEquals("abc", aaa.getValue(bean));
50  		aaa.setValue(bean, "abcd");
51  		assertEquals("abcd", aaa.getValue(bean));
52  
53  		assertEquals("123", bbb.getValue(bean));
54  		try {
55  			bbb.setValue(bean, "1234");
56  			fail();
57  		} catch (IllegalAttributeException e) {
58  		}
59  		assertEquals("123", bbb.getValue(bean));
60  	}
61  
62  	/**
63       * 
64       */
65  	public static class Bean {
66  		@SuppressWarnings("unused")
67  		private String aaa = "abc";
68  
69  		@SuppressWarnings("unused")
70  		private final String bbb = "123";
71  	}
72  
73  }