KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > beans > TestBeanCopier


1 /*
2  * Copyright 2003 The Apache Software Foundation
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.beans;
17
18 import net.sf.cglib.core.Converter;
19 import java.lang.reflect.Method JavaDoc;
20 import junit.framework.*;
21
22 /**
23  *
24  * @author baliuka
25  */

26 public class TestBeanCopier extends TestCase {
27
28     public void testSimple() {
29         BeanCopier copier = BeanCopier.create(MA.class, MA.class, false);
30         MA bean1 = new MA();
31         bean1.setIntP(42);
32         MA bean2 = new MA();
33         copier.copy(bean1, bean2, null);
34         assertTrue(bean2.getIntP() == 42);
35     }
36
37     public void testConvert() {
38         BeanCopier copier = BeanCopier.create(MA.class, MA.class, true);
39         MA bean1 = new MA();
40         bean1.setIntP(42);
41         MA bean2 = new MA();
42         copier.copy(bean1, bean2, new Converter() {
43             public Object JavaDoc convert(Object JavaDoc value, Class JavaDoc target, Object JavaDoc context) {
44                 if (target.equals(Integer.TYPE)) {
45                     return new Integer JavaDoc(((Number JavaDoc)value).intValue() + 1);
46                 }
47                 return value;
48             }
49         });
50         assertTrue(bean2.getIntP() == 43);
51     }
52     
53     public TestBeanCopier(java.lang.String JavaDoc testName) {
54         super(testName);
55     }
56     
57     public static void main(java.lang.String JavaDoc[] args) {
58         junit.textui.TestRunner.run(suite());
59     }
60     
61     public static Test suite() {
62         return new TestSuite(TestBeanCopier.class);
63     }
64 }
65
Popular Tags