    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.fail;
    20
    21  import java.math.BigDecimal;
    22  import java.net.URL;
    23  import java.sql.Timestamp;
    24  import java.util.Calendar;
    25
    26  import org.junit.Test;
    27  import org.seasar.cubby.spi.BeanDescProvider;
    28  import org.seasar.cubby.spi.beans.Attribute;
    29  import org.seasar.cubby.spi.beans.BeanDesc;
    30  import org.seasar.cubby.spi.beans.IllegalAttributeException;
    31
    32  public class DefaultBeanDescProviderAttributeTest {
    33
    34  	BeanDescProvider beanDescProvider = new DefaultBeanDescProvider();
    35
    36  	@Test
    37  	public void setValue() throws Exception {
    38  		MyBean myBean = new MyBean();
    39  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    40  		Attribute propDesc = beanDesc.getPropertyAttribute("fff");
    41  		propDesc.setValue(myBean, 2);
    42  		assertEquals(2, myBean.getFff());
    43  	}
    44
    45  	@Test
    46  	public void setValue_null() throws Exception {
    47  		MyBean myBean = new MyBean();
    48  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    49  		Attribute propDesc = beanDesc.getPropertyAttribute("fff");
    50  		propDesc.setValue(myBean, null);
    51  		assertEquals(0, myBean.getFff());
    52  	}
    53
    54  	@Test
    55  	public void setValue_notWritable() throws Exception {
    56  		MyBean myBean = new MyBean();
    57  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    58  		Attribute propDesc = beanDesc.getPropertyAttribute("aaa");
    59  		try {
    60  			propDesc.setValue(myBean, null);
    61  			fail();
    62  		} catch (IllegalAttributeException e) {
    63  			System.out.println(e);
    64  		}
    65  	}
    66
    67  	@Test
    68  	public void setValue_notWritableWithField() throws Exception {
    69  		MyBean myBean = new MyBean();
    70  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    71  		Attribute propDesc = beanDesc.getPropertyAttribute("jjj");
    72  		try {
    73  			propDesc.setValue(myBean, null);
    74  			fail();
    75  		} catch (IllegalAttributeException e) {
    76  			System.out.println(e);
    77  		}
    78  	}
    79
    80  	@Test
    81  	public void getValue_notReable() throws Exception {
    82  		MyBean myBean = new MyBean();
    83  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    84  		Attribute propDesc = beanDesc.getPropertyAttribute("iii");
    85  		try {
    86  			propDesc.getValue(myBean);
    87  			fail();
    88  		} catch (IllegalAttributeException e) {
    89  			System.out.println(e);
    90  		}
    91  	}
    92
    93  	@Test
    94  	public void getValue_notReableWithField() throws Exception {
    95  		MyBean myBean = new MyBean();
    96  		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    97  		Attribute propDesc = beanDesc.getPropertyAttribute("kkk");
    98  		try {
    99  			propDesc.getValue(myBean);
    100 			fail();
    101 		} catch (IllegalAttributeException e) {
    102 			System.out.println(e);
    103 		}
    104 	}
    105
    106 	@Test
    107 	public void setIllegalValue() throws Exception {
    108 		MyBean myBean = new MyBean();
    109 		BeanDesc beanDesc = beanDescProvider.getBeanDesc(MyBean.class);
    110 		Attribute propDesc = beanDesc.getPropertyAttribute("fff");
    111 		try {
    112 			propDesc.setValue(myBean, "hoge");
    113 			fail("1");
    114 		} catch (IllegalAttributeException ex) {
    115 			System.out.println(ex);
    116 		}
    117 	}
    118
    119 	public static class MyBean {
    120
    121 		private int fff_;
    122
    123 		private BigDecimal ggg_;
    124
    125 		private Timestamp hhh_;
    126
    127 		private String jjj;
    128
    129 		String kkk;
    130
    131 		private URL url_;
    132
    133 		private Calendar cal;
    134
    135 		/**
    136          *
    137          */
    138 		public String str;
    139
    140 		/**
    141 		 * @return
    142 		 */
    143 		public String getAaa() {
    144 			return null;
    145 		}
    146
    147 		/**
    148 		 * @param a
    149 		 * @return
    150 		 */
    151 		public String getBbb(Object a) {
    152 			return null;
    153 		}
    154
    155 		/**
    156 		 * @return
    157 		 */
    158 		public boolean isCCC() {
    159 			return true;
    160 		}
    161
    162 		/**
    163 		 * @return
    164 		 */
    165 		public Object isDdd() {
    166 			return null;
    167 		}
    168
    169 		/**
    170 		 * @return
    171 		 */
    172 		public String getEee() {
    173 			return null;
    174 		}
    175
    176 		/**
    177 		 * @param eee
    178 		 */
    179 		public void setEee(String eee) {
    180 		}
    181
    182 		/**
    183 		 * @return
    184 		 */
    185 		public int getFff() {
    186 			return fff_;
    187 		}
    188
    189 		/**
    190 		 * @param fff
    191 		 */
    192 		public void setFff(int fff) {
    193 			fff_ = fff;
    194 		}
    195
    196 		/**
    197 		 * @return
    198 		 */
    199 		public String getJjj() {
    200 			return jjj;
    201 		}
    202
    203 		/**
    204 		 * @param kkk
    205 		 */
    206 		public void setKkk(String kkk) {
    207 			this.kkk = kkk;
    208 		}
    209
    210 		/**
    211 		 * @param arg1
    212 		 * @param arg2
    213 		 * @return
    214 		 */
    215 		public Number add(Number arg1, Number arg2) {
    216 			return Integer.valueOf(3);
    217 		}
    218
    219 		/**
    220 		 * @return
    221 		 */
    222 		public BigDecimal getGgg() {
    223 			return ggg_;
    224 		}
    225
    226 		/**
    227 		 * @param ggg
    228 		 */
    229 		public void setGgg(BigDecimal ggg) {
    230 			this.ggg_ = ggg;
    231 		}
    232
    233 		/**
    234 		 * @return
    235 		 */
    236 		public Timestamp getHhh() {
    237 			return hhh_;
    238 		}
    239
    240 		/**
    241 		 * @param hhh
    242 		 */
    243 		public void setHhh(Timestamp hhh) {
    244 			this.hhh_ = hhh;
    245 		}
    246
    247 		/**
    248 		 * @param iii
    249 		 */
    250 		public void setIii(String iii) {
    251 		}
    252
    253 		/**
    254 		 * @return
    255 		 */
    256 		public URL getURL() {
    257 			return url_;
    258 		}
    259
    260 		/**
    261 		 * @param url
    262 		 */
    263 		public void setURL(URL url) {
    264 			url_ = url;
    265 		}
    266
    267 		/**
    268 		 * @return Returns the cal.
    269 		 */
    270 		public Calendar getCal() {
    271 			return cal;
    272 		}
    273
    274 		/**
    275 		 * @param cal
    276 		 *            The cal to set.
    277 		 */
    278 		public void setCal(Calendar cal) {
    279 			this.cal = cal;
    280 		}
    281 	}
    282
    283 }