KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > CoverageInfoTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.support;
24
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanOperationInfo JavaDoc;
31
32 import com.sun.appserv.management.config.DomainConfig;
33 import com.sun.appserv.management.util.jmx.JMXUtil;
34
35 /**
36  */

37 public final class CoverageInfoTest extends junit.framework.TestCase
38 {
39         public
40     CoverageInfoTest()
41     {
42     }
43     
44     
45         public CoverageInfoImpl
46     create( final MBeanInfo JavaDoc info )
47     {
48         return new CoverageInfoImpl( info );
49     }
50     
51     
52         public void
53     testCreate()
54     {
55         CoverageInfoImpl impl = create( null );
56        
57         // verify that we fail in null MBeanInfo case
58
try { impl.getNumReadableAttributes(); assert( false ); } catch( Exception JavaDoc e ) {}
59         try { impl.getNumWriteableAttributes(); assert( false ); } catch( Exception JavaDoc e ) {}
60         try { impl.getNumWriteableAttributes(); assert( false ); } catch( Exception JavaDoc e ) {}
61         
62         impl = create( getTestMBeanInfo() );
63         assert( impl.getReadableAttributes() != null );
64         assert( impl.getWriteableAttributes() != null );
65         assert( impl.getOperations() != null );
66         assert impl.getNumReadableAttributes() != 0;
67         assert impl.getNumWriteableAttributes() != 0;
68         assert impl.getNumOperations() != 0;
69         assert impl.getAttributesRead().size() == 0;
70         assert impl.getAttributesWritten().size() == 0;
71         assert impl.getOperationsInvoked().size() == 0;
72     }
73     
74         private MBeanInfo JavaDoc
75     getTestMBeanInfo()
76     {
77         return MBeanInfoConverter.getInstance().convert( DomainConfig.class, null );
78     }
79     
80         public void
81     testAttributes()
82     {
83         final MBeanInfo JavaDoc mbeanInfo = getTestMBeanInfo();
84         
85         final CoverageInfoImpl impl = create( mbeanInfo );
86         assert( mbeanInfo == impl.getMBeanInfo() );
87         
88         final MBeanAttributeInfo JavaDoc[] attrInfos = mbeanInfo.getAttributes();
89
90         for( final MBeanAttributeInfo JavaDoc attrInfo : attrInfos )
91         {
92             final String JavaDoc name = attrInfo.getName();
93             
94             if ( attrInfo.isReadable() )
95             {
96                 assert( impl.getReadableAttributes().contains( name ) );
97                 impl.attributeWasRead( name );
98                 assert( impl.getAttributesRead().contains( name ) );
99                 assert( ! impl.getAttributesNotRead().contains( name ) );
100             }
101             
102             if ( attrInfo.isWritable() )
103             {
104                 assert( impl.getWriteableAttributes().contains( name ) );
105                 impl.attributeWasWritten( name );
106                 assert( impl.getAttributesWritten().contains( name ) );
107                 assert( ! impl.getAttributesNotWritten().contains( name ) );
108             }
109         }
110         
111         assert( impl.getAttributesNotRead().size() == 0 );
112         assert( impl.getAttributeReadCoverage() == 100 );
113         assert( impl.getAttributeGetFailures().size() == 0 );
114         
115         assert( impl.getAttributesNotWritten().size() == 0 );
116         assert( impl.getAttributeWriteCoverage() == 100 );
117         assert( impl.getAttributeSetFailures().size() == 0 );
118         
119         
120         final String JavaDoc BOGUS = "bogus";
121         impl.attributeWasRead( BOGUS );
122         impl.attributeWasWritten( BOGUS );
123         
124         assert( impl.getUnknownAttributes().keySet().contains( BOGUS ) );
125         assert( impl.getUnknownAttributes().keySet().size() == 1 );
126         assert( impl.getAttributeGetFailures().size() == 0 );
127         assert( impl.getAttributeSetFailures().size() == 0 );
128         
129         final MBeanAttributeInfo JavaDoc attr = attrInfos[ 0 ];
130         impl.attributeGetFailure( attr.getName() );
131         impl.attributeGetFailure( attr.getName() );
132         assert( impl.getAttributeGetFailures().size() == 1 );
133         impl.attributeSetFailure( attr.getName() );
134         impl.attributeSetFailure( attr.getName() );
135         assert( impl.getAttributeSetFailures().size() == 1 );
136     }
137     
138     
139         public void
140     testOperations()
141     {
142         final MBeanInfo JavaDoc mbeanInfo = getTestMBeanInfo();
143         final MBeanOperationInfo JavaDoc[] operationInfos = mbeanInfo.getOperations();
144         
145         final CoverageInfoImpl impl = create( mbeanInfo );
146         assert( mbeanInfo == impl.getMBeanInfo() );
147
148         //--------------------------------------------------------------------
149
// verify that operationWasInvoked() works
150

151         for( final MBeanOperationInfo JavaDoc operationInfo : operationInfos )
152         {
153             final String JavaDoc name = operationInfo.getName();
154             final String JavaDoc[] sig = JMXUtil.getSignature( operationInfo.getSignature() );
155             
156             impl.operationWasInvoked( name, sig );
157         }
158         
159         assert( impl.getOperationCoverage() == 100 ) :
160             "Expected coverage of 100%, got " + impl.getOperationCoverage();
161         
162         assert( impl.getUnknownOperations().size() == 0 );
163         assert( impl.getOperationsNotInvoked().size() == 0 );
164         assert( impl.getInvocationFailures().size() == 0 );
165         impl.toString( true );
166         impl.toString( false );
167         
168         
169         //--------------------------------------------------------------------
170
// verify that markAsInvoked() works
171

172         final Set JavaDoc<String JavaDoc> invoked = impl.getOperationsInvoked();
173         impl.clear();
174         for( final String JavaDoc op : invoked )
175         {
176             impl.markAsInvoked( op );
177         }
178         
179         assert( impl.getOperationCoverage() == 100 ) :
180             "Expected coverage of 100%, got " + impl.getOperationCoverage();
181         assert( impl.getUnknownOperations().size() == 0 );
182         assert( impl.getOperationsNotInvoked().size() == 0 );
183         assert( impl.getInvocationFailures().size() == 0 );
184         
185         final String JavaDoc DUMMY_OPERATION = "dummyOperationName";
186         impl.operationWasInvoked( DUMMY_OPERATION, null );
187         impl.operationWasInvoked( DUMMY_OPERATION, null );
188         assert( impl.getUnknownOperations().size() == 1 );
189         impl.toString( true );
190         impl.toString( false );
191         
192         
193         //--------------------------------------------------------------------
194
// verify that failures are tracked correctly
195
impl.clear();
196         for( final MBeanOperationInfo JavaDoc operationInfo : operationInfos )
197         {
198             final String JavaDoc name = operationInfo.getName();
199             final String JavaDoc[] sig = JMXUtil.getSignature( operationInfo.getSignature() );
200             
201             impl.operationWasInvoked( name, sig );
202             impl.operationFailed( name, sig );
203         }
204         assert( impl.getOperationCoverage() == 100 ) :
205             "Expected coverage of 100%, got " + impl.getOperationCoverage();
206         assert( impl.getUnknownOperations().size() == 0 );
207         assert( impl.getOperationsNotInvoked().size() == 0 );
208         assert( impl.getOperationsInvoked().size() == operationInfos.length );
209         assert( impl.getInvocationFailures().size() == operationInfos.length );
210         impl.toString( true );
211         impl.toString( false );
212         
213         
214         //--------------------------------------------------------------------
215
// verify that we can't call operationFailed() on an illegal operation
216
try
217         {
218             impl.operationFailed( "foo", null );
219             assert( false ) : "expected failure when calling operationFailed()";
220         }
221         catch( IllegalArgumentException JavaDoc e )
222         {
223         }
224         impl.toString( true );
225         impl.toString( false );
226         impl.toString();
227     }
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
Popular Tags