KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > TargetTest


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
24 /*
25  * $Id: TargetTest.java,v 1.3 2005/12/25 03:43:14 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 //jdk imports
31
import java.io.File JavaDoc;
32
33 //junit imports
34
import junit.framework.*;
35 import junit.textui.TestRunner;
36
37 //jmx imports
38
import javax.management.ObjectName JavaDoc;
39
40 //config imports
41
import com.sun.enterprise.config.ConfigFactory;
42 import com.sun.enterprise.config.ConfigContext;
43 import com.sun.enterprise.config.ConfigException;
44
45 public class TargetTest extends TestCase
46 {
47     public void testTarget() throws Exception JavaDoc
48     {
49         testTarget(getTargetData(null, TargetType.SERVER, getConfigRef(),
50                     false, "server", true));
51         testTarget(getTargetData("server", TargetType.SERVER, getConfigRef(),
52                     false, "server", true));
53         testTarget(getTargetData("domain", TargetType.DOMAIN, null, false,
54                     "domain", false));
55         testTarget(getTargetData(getConfigRef(), TargetType.CONFIG,
56                     getConfigRef(), false, getConfigRef(), true));
57     }
58
59     public void testUnknownTarget()
60     {
61         try
62         {
63             Target target = TargetBuilder.INSTANCE.createTarget(
64                 "abcd", domainContext);
65             Assert.assertTrue(false);
66         }
67         catch (Exception JavaDoc e) {}
68         try
69         {
70             Target target = TargetBuilder.INSTANCE.createTarget(null, null);
71             Assert.assertTrue(false);
72         }
73         catch (Exception JavaDoc e) {}
74     }
75
76     void testTarget(TargetData td) throws Exception JavaDoc
77     {
78         Target target = TargetBuilder.INSTANCE.createTarget(
79                                     td.name, domainContext);
80         Assert.assertTrue(target != null);
81         Assert.assertEquals(td.type, target.getType());
82         Assert.assertEquals(getTargetObjectName(td.target, td.type),
83             target.getTargetObjectName(new String JavaDoc[] { getDomainName() }));
84         Assert.assertEquals(td.configRef ,target.getConfigRef());
85         if (td.checkConfigTarget)
86         {
87             final ConfigTarget configTarget = target.getConfigTarget();
88             Assert.assertTrue(configTarget != null);
89             Assert.assertEquals(td.is1ToN, configTarget.is1ToN(target));
90         }
91     }
92
93     public TargetTest(String JavaDoc name) throws Exception JavaDoc
94     {
95         super(name);
96     }
97
98     protected void setUp()
99     {
100         try
101         {
102             domainContext = getConfigContext();
103         }
104         catch (Exception JavaDoc e)
105         {
106             throw new RuntimeException JavaDoc(e.getMessage());
107         }
108     }
109
110     protected void tearDown()
111     {
112         domainContext = null;
113     }
114
115     public static junit.framework.Test suite()
116     {
117         TestSuite suite = new TestSuite(TargetTest.class);
118         return suite;
119     }
120
121     private ConfigContext domainContext;
122     private static File JavaDoc domainXml;
123
124     public static void setDomainXml(File JavaDoc xml)
125     {
126         domainXml = xml;
127     }
128
129     public static void main(String JavaDoc args[]) throws Exception JavaDoc
130     {
131         final TestRunner runner= new TestRunner();
132         setDomainXml(new File JavaDoc(args[0]));
133         final TestResult result = runner.doRun(TargetTest.suite(), false);
134         System.exit(result.errorCount() + result.failureCount());
135     }
136
137     private ConfigContext getConfigContext() throws ConfigException
138     {
139         return ConfigFactory.createConfigContext(domainXml.getAbsolutePath());
140     }
141
142     private String JavaDoc getDomainName()
143     {
144         return "testdomain";
145     }
146
147     private String JavaDoc getTargetObjectName(String JavaDoc name, TargetType type)
148         throws Exception JavaDoc
149     {
150         String JavaDoc on = null;
151         if (type.equals(TargetType.DOMAIN))
152         {
153             on = getDomainName() + ":type=domain,category=config";
154         }
155         else if (type.equals(TargetType.CONFIG))
156         {
157             on = getDomainName() + ":type=config,category=config,name=" + name;
158         }
159         else if (type.equals(TargetType.SERVER))
160         {
161             on = getDomainName() + ":type=server,category=config,name=" + name;
162         }
163         return on;
164     }
165
166     private String JavaDoc getConfigRef()
167     {
168         return "server-config";
169     }
170
171     private TargetData getTargetData(String JavaDoc name,
172                                      TargetType type,
173                                      String JavaDoc configRef,
174                                      boolean is1ToN,
175                                      String JavaDoc target,
176                                      boolean checkConfigTarget)
177     {
178         TargetData td = new TargetData();
179         td.name = name;
180         td.type = type;
181         td.configRef = configRef;
182         td.is1ToN = is1ToN;
183         td.target = target;
184         td.checkConfigTarget = checkConfigTarget;
185         return td;
186     }
187
188     private class TargetData
189     {
190         public String JavaDoc name;
191         public TargetType type;
192         public String JavaDoc configRef;
193         public boolean is1ToN;
194         public String JavaDoc target;
195         public boolean checkConfigTarget;
196     }
197 }
198
Popular Tags