KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > BaseLifeCycleCommandTest


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.cli.commands;
24
25 /**
26  * Note that this test requires resources for testing. These resources
27  * are construct4ed from the two files P1 & P2 located in the current
28  * directory. If these file names are changed then the corresponding
29  * names in this submodules build.xml file should be changed also
30  */

31 import com.sun.enterprise.cli.framework.*;
32 //import java.util.ArrayList;
33
//import java.util.Arrays;
34
//import java.util.HashMap;
35
//import java.util.List;
36
//import java.util.Properties;
37
import junit.framework.*;
38 import junit.textui.TestRunner;
39 import com.sun.enterprise.util.SystemPropertyConstants;
40 import com.sun.enterprise.admin.servermgmt.DomainConfig;
41 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
42 import java.io.File JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.io.OutputStreamWriter JavaDoc;
45 import java.io.FileOutputStream JavaDoc;
46 import java.util.Vector JavaDoc;
47 import java.util.HashMap JavaDoc;
48
49 /**
50  *
51  * @author prashanth.abbagani@sun.com
52  * @version $Revision: 1.3 $
53  */

54
55 /**
56  * Execute these tests using gmake (and Ant) by:
57  * cd <commands>
58  * gmake ANT_TARGETS=CommandTest.java
59  */

60
61 public class BaseLifeCycleCommandTest extends TestCase {
62     
63     public void testgetAdminUserWhenNotSet() throws Exception JavaDoc{
64         try{
65             String JavaDoc adminUser = testCommand.getAdminUser();
66         }catch(Exception JavaDoc e){
67             assertEquals(e.getMessage(), "CLI152 adminuser is a required option.");
68         }
69     }
70     
71     public void testgetAdminPasswordFromCommandLine() throws Exception JavaDoc{
72         testCommand.setOption("adminpassword", "test_password");
73         String JavaDoc pwd = testCommand.getAdminPassword(true);
74         assertEquals(pwd, "test_password");
75     }
76     
77     public void testgetAdminUserFromCommandLine() throws Exception JavaDoc{
78         testCommand.setOption("adminuser", "userFromCommandLine");
79         String JavaDoc adminUser = testCommand.getAdminUser();
80         assertEquals(adminUser, "userFromCommandLine");
81     }
82     
83     public void testgetAdminUserFromPrefsFile() throws Exception JavaDoc{
84         final String JavaDoc enc = "ISO-8859-1";
85         final File JavaDoc f = new File JavaDoc(System.getProperty("java.io.tmpdir"),
86                 testCommand.ASADMINPREFS);
87         f.deleteOnExit();
88         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(
89                 new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
90         pw.println("AS_ADMIN_ADMINUSER=adminuserFromPrefsFile");
91         pw.close();
92         System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
93         String JavaDoc adminUser = testCommand.getAdminUser();
94         assertEquals(adminUser, "adminuserFromPrefsFile");
95     }
96     
97     public void testgetAdminUserFromPrefsFileUsingUserOption() throws Exception JavaDoc{
98         final String JavaDoc enc = "ISO-8859-1";
99         final File JavaDoc f = new File JavaDoc(System.getProperty("java.io.tmpdir"),
100                 testCommand.ASADMINPREFS);
101         f.deleteOnExit();
102         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(
103                 new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
104         pw.println("AS_ADMIN_USER=userFromPrefsFile");
105         pw.close();
106         System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
107         String JavaDoc adminUser = testCommand.getAdminUser();
108         assertEquals(adminUser, "userFromPrefsFile");
109     }
110     
111     public void testgetDomainConfigWithVerbose() throws Exception JavaDoc{
112         testCommand.setOption("verbose", "true");
113         //assuming there is a default domain
114
DomainConfig dc = testCommand.getDomainConfig(testCommand.getDomainName());
115         assertEquals(dc.get(DomainConfig.K_VERBOSE), Boolean.TRUE);
116         assertEquals(dc.get(DomainConfig.K_DEBUG), null);
117     }
118     
119     public void testgetDomainConfigWithDebug() throws Exception JavaDoc{
120         testCommand.setOption("debug", "true");
121         //assuming there is a default domain
122
DomainConfig dc = testCommand.getDomainConfig(testCommand.getDomainName());
123         assertEquals(dc.get(DomainConfig.K_DEBUG), Boolean.TRUE);
124         assertEquals(dc.get(DomainConfig.K_VERBOSE), null);
125     }
126     
127     public void testgetDomainNameOptionFromCommandLine() throws Exception JavaDoc{
128         testCommand.setOption("domain", "test_domain");
129         //assuming there is a default domain
130
assertEquals(testCommand.getDomainName(), "test_domain");
131     }
132     
133     public void testgetDomainNameOperandFromCommandLine() throws Exception JavaDoc{
134         Vector JavaDoc operands = new Vector JavaDoc();
135         operands.add("test_domain");
136         testCommand.setOperands(operands);
137         //assuming there is a default domain
138
}
139     
140     public void testgetDomainNameWhenZero() throws Exception JavaDoc{
141         try{
142             Vector JavaDoc operands = new Vector JavaDoc();
143             operands.add("UndefinedDomain");
144             testCommand.setOperands(operands);
145             testCommand.getDomainName();
146         } catch (Exception JavaDoc e)
147         {
148             assertEquals(e.getMessage(), "CLI156 Could not start the domain UndefinedDomain.");
149         }
150     }
151
152     public void testgetDomainNameFromFileSystem() throws Exception JavaDoc{
153         String JavaDoc domainName = "test_domain";
154         String JavaDoc dirName = createDomainFileSystem("testgetDomainNameFromFileSystem", domainName);
155         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, dirName);
156         assertEquals(testCommand.getDomainName(), domainName);
157     }
158     
159     public void testgetDomainNameFromFileSystemWithMultipleDomains() throws Exception JavaDoc{
160         String JavaDoc dirName = "";
161         try{
162         String JavaDoc domainName1 = "test_domain1";
163         dirName = createDomainFileSystem("testgetDomainNameFromFileSystemWithMultipleDomains", domainName1);
164         String JavaDoc domainName2 = "test_domain2";
165         createDomainFileSystem("testgetDomainNameFromFileSystemWithMultipleDomains", domainName2);
166         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, dirName);
167         String JavaDoc domainName = testCommand.getDomainName();
168         } catch (Exception JavaDoc e)
169         {
170             assertEquals(e.getMessage(), "There is more than one domain in " +
171                             dirName +
172                             ". Please use operand to specify the domain.");
173         }
174     }
175     
176     public void testgetDomainNameFromFileSystemWhenNone() throws Exception JavaDoc{
177         String JavaDoc dirName = "domainsDirXYZ";
178         File JavaDoc domaindir = new File JavaDoc(System.getProperty("java.io.tmpdir"), dirName);
179         try{
180         domaindir.mkdir();
181         domaindir.deleteOnExit();
182         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, domaindir.getAbsolutePath());
183         String JavaDoc domainName = testCommand.getDomainName();
184         } catch (Exception JavaDoc e)
185         {
186             assertEquals(e.getMessage(), "CLI142 No domains in " +
187                                 domaindir.getAbsolutePath() + ".");
188         }
189     }
190     
191     public void testgetDomainsWhenZero() throws Exception JavaDoc{
192         String JavaDoc dirName = createDomainFileSystem("testgetDomainsWhenZero", null);
193         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, dirName);
194         String JavaDoc[] domains = testCommand.getDomains();
195         assertEquals(domains.length, 0);
196     }
197
198     public void testgetDomainsWhenOne() throws Exception JavaDoc{
199         String JavaDoc domainName = "test_domain1";
200         String JavaDoc dirName = createDomainFileSystem("testgetDomainsWhenOne", domainName);
201         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, dirName);
202         String JavaDoc[] domains = testCommand.getDomains();
203         assertEquals(domains[0], domainName);
204     }
205     
206     public void testgetDomainsWhenMultiple() throws Exception JavaDoc{
207         String JavaDoc domainName1 = "test_domain1";
208         String JavaDoc dirName = createDomainFileSystem("testgetDomainsWhenMultiple", domainName1);
209         String JavaDoc domainName2 = "test_domain2";
210         createDomainFileSystem("testgetDomainsWhenMultiple", domainName2);
211         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, dirName);
212         String JavaDoc[] domains = testCommand.getDomains();
213         assertEquals(domains[0], domainName1);
214         assertEquals(domains[1], domainName2);
215     }
216
217     private String JavaDoc createDomainFileSystem(String JavaDoc domainParent, String JavaDoc domainName) throws Exception JavaDoc{
218         final File JavaDoc domainParentDir = new File JavaDoc(System.getProperty("java.io.tmpdir"), domainParent);
219         domainParentDir.mkdir();
220         domainParentDir.deleteOnExit();
221         if (domainName == null) return domainParentDir.getPath();
222         final File JavaDoc domainDir = new File JavaDoc(domainParentDir, domainName);
223         domainDir.mkdir();
224         domainDir.deleteOnExit();
225         final File JavaDoc binDir = new File JavaDoc(domainDir, PEFileLayout.BIN_DIR);
226         binDir.mkdir();
227         binDir.deleteOnExit();
228         //System.out.println(PEFileLayout.START_SERV_OS);
229
final File JavaDoc f = new File JavaDoc(binDir, PEFileLayout.START_SERV_OS);
230         f.createNewFile();
231         f.deleteOnExit();
232         return domainParentDir.getPath();
233     }
234
235     public void testgetDomainsRootFromCommandLine() throws Exception JavaDoc{
236         testCommand.setOption("domaindir", "test_domain");
237         assertEquals(testCommand.getDomainsRoot(), "test_domain");
238     }
239     
240     public void testgetDomainsRootFromSystemProperty() throws Exception JavaDoc{
241         System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, "test_domain");
242         assertEquals(testCommand.getDomainsRoot(), "test_domain");
243     }
244     
245     /*
246     //Donno how to unset the DOMAINS_ROOT system property
247     public void testgetDomainsRootInvalid() throws Exception{
248         System.out.println("Domainroot = "+ System.getProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY));
249         assertEquals(testCommand.getDomainsRoot(), "test_domain");
250     }
251     */

252     /*
253     public void testgetDomainsRootWithoutSettingAny() throws Exception{
254         try{
255             System.setProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY, null);
256             System.out.println("Domains Root = " + testCommand.getDomainsRoot());
257         }
258         catch(CommandException ce){
259             System.out.println(ce.getMessage());
260            assertEquals(ce.getMessage(), "NoDomains");
261         }
262     }
263     */

264
265     public void testIsSpaceInPathTrue() throws Exception JavaDoc{
266         assertEquals(testCommand.isSpaceInPath("String has space"), true);
267     }
268     
269     public void testIsSpaceInPathFalse() throws Exception JavaDoc{
270         assertEquals(testCommand.isSpaceInPath("StringHasNospace"), false);
271     }
272     
273     public void testisWindows(){
274         String JavaDoc os = System.getProperty("os.name").toLowerCase();
275         assertEquals(testCommand.isWindows(), (os.indexOf("windows") != -1) ? true : false);
276     }
277     public BaseLifeCycleCommandTest(String JavaDoc name){
278         super(name);
279     }
280     
281     BaseLifeCycleCommand testCommand = null;
282     
283     protected void setUp() throws Exception JavaDoc{
284         //Properties systemProperties = new java.util.Propertis();
285
//systemProperties.put("com.sun.aas.configRoot",)
286
//String configProperty = SystemPropertyConstants.CONFIG_ROOT_PROPERTY;
287
//System.out.println(configProperty + " = " + System.getProperty(configProperty));
288
final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
289         ValidCommand validCommand = cliDescriptorsReader.getCommand(null);
290         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(
291                 CLIDescriptorsReader.getInstance().getProperties());
292         testCommand = new BaseLifeCycleCommand() {
293             public void runCommand()
294             throws CommandException, CommandValidationException {
295             }
296             public boolean validateOptions() throws CommandValidationException {
297                 return true;
298             }
299         };
300         testCommand.setName("sampleCommand");
301     }
302     
303     
304     
305     protected void tearDown() {
306     }
307     
308     private void nyi(){
309         fail("Not Yet Implemented");
310     }
311     
312     public static Test suite(){
313         TestSuite suite = new TestSuite(BackupCommandsTest.class);
314         return suite;
315     }
316     
317     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
318         final TestRunner runner= new TestRunner();
319         final TestResult result = runner.doRun(BaseLifeCycleCommandTest.suite(), false);
320         System.exit(result.errorCount() + result.failureCount());
321     }
322 }
323
324
Popular Tags