1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.tools.packer;
9   
10  import junit.framework.TestCase;
11  import org.codehaus.metaclass.model.Attribute;
12  import org.codehaus.metaclass.model.ClassDescriptor;
13  import org.codehaus.metaclass.model.FieldDescriptor;
14  import org.codehaus.metaclass.model.MethodDescriptor;
15  import org.codehaus.metaclass.model.ParameterDescriptor;
16  
17  /***
18   * @author Peter Donald
19   * @version $Revision: 1.2 $ $Date: 2003/11/28 11:14:55 $
20   */
21  public class ClassDescriptorPackerTestCase
22      extends TestCase
23  {
24      public void testIsEmptyWithEmpty()
25          throws Exception
26      {
27          final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
28          final FieldDescriptor descriptor =
29              new FieldDescriptor( "x",
30                                   "int",
31                                   Attribute.EMPTY_SET,
32                                   Attribute.EMPTY_SET );
33          final boolean empty = packer.isEmpty( descriptor );
34          assertEquals( "isEmpty", true, empty );
35      }
36  
37      public void testIsEmptyWithDeclared()
38          throws Exception
39      {
40          final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
41          final FieldDescriptor descriptor =
42              new FieldDescriptor( "x",
43                                   "int",
44                                   Attribute.EMPTY_SET,
45                                   new Attribute[]{new Attribute( "blah" )} );
46          final boolean empty = packer.isEmpty( descriptor );
47          assertEquals( "isEmpty", false, empty );
48      }
49  
50      public void testIsEmptyWithInherited()
51          throws Exception
52      {
53          final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
54          final Attribute[] attributes = new Attribute[]{new Attribute( "blah" )};
55          final FieldDescriptor descriptor =
56              new FieldDescriptor( "x",
57                                   "int",
58                                   attributes,
59                                   attributes );
60          final boolean empty = packer.isEmpty( descriptor );
61          assertEquals( "isEmpty", false, empty );
62      }
63  
64      public void testPackFields()
65          throws Exception
66      {
67          final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
68          final FieldDescriptor field1 =
69              new FieldDescriptor( "x",
70                                   "int",
71                                   Attribute.EMPTY_SET,
72                                   new Attribute[]{new Attribute( "blah" )} );
73          final FieldDescriptor field2 =
74              new FieldDescriptor( "y",
75                                   "int",
76                                   Attribute.EMPTY_SET,
77                                   Attribute.EMPTY_SET );
78          final FieldDescriptor[] fields = new FieldDescriptor[]{field1, field2};
79  
80          final FieldDescriptor[] result = packer.packFields( fields );
81          assertEquals( "result.length", 1, result.length );
82          assertEquals( "result[0]", field1, result[ 0 ] );
83      }
84  
85      public void testPackMethodsWithNoKeepEmpty()
86          throws Exception
87      {
88          final ClassDescriptorPacker packer = new ClassDescriptorPacker( false );
89          final MethodDescriptor method1 =
90              new MethodDescriptor( "x",
91                                    "int",
92                                    ParameterDescriptor.EMPTY_SET,
93                                    Attribute.EMPTY_SET,
94                                    new Attribute[]{new Attribute( "blah" )} );
95          final MethodDescriptor method2 =
96              new MethodDescriptor( "y",
97                                    "int",
98                                    ParameterDescriptor.EMPTY_SET,
99                                    Attribute.EMPTY_SET,
100                                   Attribute.EMPTY_SET );
101         final MethodDescriptor[] methods = new MethodDescriptor[]{method1,
102                                                                   method2};
103 
104         final MethodDescriptor[] result = packer.packMethods( methods );
105         assertEquals( "result.length", 1, result.length );
106         assertEquals( "result[0]", method1, result[ 0 ] );
107     }
108 
109     public void testPackMethodsWithKeepEmpty()
110         throws Exception
111     {
112         final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
113         final MethodDescriptor method1 =
114             new MethodDescriptor( "x",
115                                   "int",
116                                   ParameterDescriptor.EMPTY_SET,
117                                   Attribute.EMPTY_SET,
118                                   new Attribute[]{new Attribute( "blah" )} );
119         final MethodDescriptor method2 =
120             new MethodDescriptor( "x",
121                                   "int",
122                                   ParameterDescriptor.EMPTY_SET,
123                                   Attribute.EMPTY_SET,
124                                   Attribute.EMPTY_SET );
125         final MethodDescriptor[] methods = new MethodDescriptor[]{method1,
126                                                                   method2};
127 
128         final MethodDescriptor[] result = packer.packMethods( methods );
129         assertEquals( "result.length", 2, result.length );
130         assertEquals( "result[0]", method1, result[ 0 ] );
131         assertEquals( "result[0]", method2, result[ 1 ] );
132     }
133 
134     public void testPackEmptyClass()
135         throws Exception
136     {
137         final ClassDescriptor descriptor =
138             new ClassDescriptor( "x",
139                                  Attribute.EMPTY_SET,
140                                  Attribute.EMPTY_SET,
141                                  FieldDescriptor.EMPTY_SET,
142                                  MethodDescriptor.EMPTY_SET );
143         final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
144         final ClassDescriptor result = packer.pack( descriptor );
145         assertEquals( "result", null, result );
146     }
147 
148     public void testPackNonEmptyClass()
149         throws Exception
150     {
151         final ClassDescriptor descriptor =
152             new ClassDescriptor( "x",
153                                  Attribute.EMPTY_SET,
154                                  new Attribute[]{new Attribute( "blah" )},
155                                  FieldDescriptor.EMPTY_SET,
156                                  MethodDescriptor.EMPTY_SET );
157         final ClassDescriptorPacker packer = new ClassDescriptorPacker( true );
158         final ClassDescriptor result = packer.pack( descriptor );
159         assertNotNull( "result", result );
160     }
161 }