KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > pe > PEDomainsManagerTest


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 package com.sun.enterprise.admin.servermgmt.pe;
25
26 import java.io.*;
27 import java.util.*;
28 import javax.xml.parsers.*;
29 import org.xml.sax.*;
30 import junit.framework.TestResult;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33 import junit.textui.TestRunner;
34 import com.sun.enterprise.admin.servermgmt.*;
35
36 public class PEDomainsManagerTest extends TestCase
37 {
38     public void testCreate()
39     {
40         assertNotNull("Couldnt create PEDomainManager",
41                     new PEDomainsManager());
42     }
43
44     public void testValidate() throws Exception JavaDoc
45     {
46         final Map domainConfig = getDomainConfig();
47         final PEDomainsManager mgr = new PEDomainsManager();
48         mgr.validate(getDomainName(), domainConfig);
49     }
50
51     public void testCreateFileLayout()
52     {
53         final Map domainConfig = getDomainConfig();
54         final PEDomainsManager mgr = new PEDomainsManager();
55         mgr.createFileLayout(getDomainName(), domainConfig);
56
57         final String JavaDoc domainRoot =
58             (String JavaDoc)domainConfig.get(DomainConfig.K_DOMAINS_ROOT);
59         final String JavaDoc domainName = getDomainName();
60         assertTrue("Domain dir not created",
61             new File(domainRoot, domainName).exists());
62
63         final File repositoryRoot =
64             getFileLayout().getRepositoryRoot(domainName, getInstanceName());
65         assertTrue("Repository dir not created", repositoryRoot.exists());
66
67         final File repositoryBackup =
68             getFileLayout().getRepositoryBackupRoot(domainName,
69                                                     getInstanceName());
70         assertTrue("Repository backup dir not created",
71             repositoryBackup.exists());
72     }
73
74     public void testCreateDomainXml() throws Exception JavaDoc
75     {
76         final PEDomainsManager mgr = new PEDomainsManager();
77         mgr.createDomainXml(getDomainName(), getDomainConfig());
78         final PEFileLayout layout = getFileLayout();
79         final String JavaDoc domainName = getDomainName();
80         final File domainXml = layout.getDomainConfigFile(domainName,
81                                                           getInstanceName());
82         final File domainXmlBackup =
83             layout.getDomainConfigBackupFile(domainName, getInstanceName());
84         assertTrue("Domain xml not created", domainXml.exists());
85         assertTrue("Domain xml backup not created", domainXmlBackup.exists());
86 // assertTrue("Invalid domain xml", isValidXml(domainXml));
87
}
88
89     private boolean isValidXml(File xml)
90     {
91         boolean isValid = true;
92         try
93         {
94             InputSource is = new InputSource(new FileInputStream(xml));
95             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
96             dbf.setValidating(true);
97             DocumentBuilder db = dbf.newDocumentBuilder();
98             db.parse(is);
99         }
100         catch (Exception JavaDoc e) { e.printStackTrace(); isValid = false; }
101         return isValid;
102     }
103
104     public void testCreateScripts() throws Exception JavaDoc
105     {
106         final PEDomainsManager mgr = new PEDomainsManager();
107         mgr.createScripts(getDomainName(), getDomainConfig());
108         final PEFileLayout layout = getFileLayout();
109         final File startServ = layout.getStartServ(getDomainName(),
110                                                    getInstanceName());
111         final File stopServ = layout.getStopServ(getDomainName(),
112                                                  getInstanceName());
113         assertTrue("startserv not created", startServ.exists());
114         assertTrue("stopserv not created", stopServ.exists());
115     }
116
117     public void testCreateServerPolicyFile() throws Exception JavaDoc
118     {
119         final PEDomainsManager mgr = new PEDomainsManager();
120         mgr.createServerPolicyFile(getDomainName(), getDomainConfig());
121         assertTrue("Policy file not created",
122             getFileLayout().getPolicyFile(getDomainName(),
123                                           getInstanceName()).exists());
124     }
125
126     public void testCreateAccXml() throws Exception JavaDoc
127     {
128         final PEDomainsManager mgr = new PEDomainsManager();
129         mgr.createAppClientContainerXml(getDomainName(), getDomainConfig());
130         assertTrue("Acc Xml file not created",
131             getFileLayout().getAppClientContainerXml(getDomainName(),
132                                             getInstanceName()).exists());
133     }
134
135     public void testCreateDefaultWebXml() throws Exception JavaDoc
136     {
137         final PEDomainsManager mgr = new PEDomainsManager();
138         mgr.createDefaultWebXml(getDomainName(), getDomainConfig());
139         assertTrue("Default Web Xml file not created",
140             getFileLayout().getDefaultWebXml(getDomainName(),
141                                             getInstanceName()).exists());
142     }
143
144     private String JavaDoc getDomainName()
145     {
146         return "domain5";
147     }
148
149     private String JavaDoc getInstanceName()
150     {
151         return "server";
152     }
153
154     private PEFileLayout getFileLayout()
155     {
156         return new PEFileLayout(getInstallRoot(), getDomainRoot(),
157             getDomainName());
158     }
159
160     private static String JavaDoc getInstallRoot()
161     {
162         File f = new File(System.getProperty("java.io.tmpdir"),
163                           System.getProperty("user.name", "install"));
164         f.mkdir();
165         f.deleteOnExit();
166         return f.getAbsolutePath();
167     }
168
169     private String JavaDoc getDomainRoot()
170     {
171         File f = new File(getInstallRoot(), "domains");
172         f.mkdir();
173         f.deleteOnExit();
174         return f.getAbsolutePath();
175     }
176
177     private Map getDomainConfig()
178     {
179         final Map domainConfig = new HashMap();
180         domainConfig.put(DomainConfig.K_INSTALL_ROOT, getInstallRoot());
181         domainConfig.put(DomainConfig.K_DOMAINS_ROOT, getDomainRoot());
182         domainConfig.put(DomainConfig.K_HOST_NAME, "surya10");
183         domainConfig.put(DomainConfig.K_ADMIN_PORT, new Integer JavaDoc(8888));
184         domainConfig.put(DomainConfig.K_INSTANCE_PORT, new Integer JavaDoc(8889));
185         domainConfig.put(DomainConfig.K_ORB_LISTENER_PORT, new Integer JavaDoc(1025));
186         domainConfig.put(DomainConfig.K_JAVA_HOME, getDomainRoot());
187         domainConfig.put(DomainConfig.K_JMS_PASSWORD, "admin1");
188         domainConfig.put(DomainConfig.K_JMS_USER, "admin1");
189         domainConfig.put(DomainConfig.K_JMS_PORT, new Integer JavaDoc(7677));
190         domainConfig.put(DomainConfig.K_HTTP_SSL_PORT, new Integer JavaDoc(8181));
191         domainConfig.put(DomainConfig.K_IIOP_SSL_PORT, new Integer JavaDoc(1060));
192         domainConfig.put(DomainConfig.K_IIOP_MUTUALAUTH_PORT, new Integer JavaDoc(1061));
193
194         return domainConfig;
195     }
196
197     public PEDomainsManagerTest(String JavaDoc name) throws Exception JavaDoc {
198         super(name);
199         String JavaDoc templatesDir = System.getProperty("com.sun.aas.templatesDir");
200         if (templatesDir == null)
201         {
202             throw new Exception JavaDoc("com.sun.aas.templatesDir is null");
203         }
204         if (!new File(templatesDir).exists())
205         {
206             throw new Exception JavaDoc(templatesDir + " Doesnot exist");
207         }
208         File dest = getFileLayout().getTemplatesDir();
209         createClean(dest);
210         copy(new File(templatesDir), dest);
211     }
212
213     protected void setUp() {
214     }
215
216     protected void tearDown() {
217     }
218
219     private void nyi() {
220         fail("Not yet implemented");
221     }
222
223     public static junit.framework.Test suite(){
224         TestSuite suite = new TestSuite(PEDomainsManagerTest.class);
225         return suite;
226     }
227
228     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
229         final TestRunner runner= new TestRunner();
230         final TestResult result = runner.doRun(PEDomainsManagerTest.suite(), false);
231         System.exit(result.errorCount() + result.failureCount());
232     }
233
234     static void createClean(File f) throws IOException
235     {
236         f.delete();
237         f.mkdirs();
238         if (!f.exists())
239         {
240             throw new IOException("Could not create " + f.getAbsolutePath());
241         }
242     }
243
244     static void copy(File templatesDir, File dest) throws IOException
245     {
246         File[] fa = templatesDir.listFiles();
247         for (int i = 0; i < fa.length; i++)
248         {
249             copyFile(fa[i], dest);
250         }
251     }
252
253     static void copyFile(File src, File destDir) throws IOException
254     {
255         FileInputStream inStream = new FileInputStream(src);
256         FileOutputStream outStream = new FileOutputStream(
257             new File(destDir, src.getName()));
258         try
259         {
260             byte[] buf = new byte[1024];
261             int len = 0;
262             while (len != -1)
263             {
264                 len = inStream.read(buf, 0, buf.length);
265                 if (len == -1) { break; }
266                 outStream.write(buf, 0, len);
267             }
268         }
269         finally
270         {
271             inStream.close();
272             outStream.close();
273         }
274     }
275 }
276
Popular Tags