KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > test > MailResourceTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * MailResourceTest.java
21  *
22  * Created on March 15, 2006, 1:49 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.j2ee.sun.test;
29
30 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.junit.NbTestSuite;
35 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
36 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
37 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
38 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
39 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
40 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
41 import org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader.SunResourceDataObject;
42 import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData;
43 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
44 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 /**
48  *
49  * @author Amanpreet Kaur
50  */

51 public class MailResourceTest extends NbTestCase implements WizardConstants{
52     
53     private static String JavaDoc MAIL_RESOURCE_NAME = "mailResourceTest";
54     private static String JavaDoc MAIL_USER= "default";
55     private static String JavaDoc MAIL_FROM = "default@sun.com";
56     
57     /** Creates a new instance of MailResourcesTest */
58     public MailResourceTest(String JavaDoc testName) {
59         super(testName);
60     }
61     
62     public void registerMailResource() {
63         try {
64             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
65
ResourceConfigData mrdata = new ResourceConfigData();
66             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
67             //Java Mail Resource Setting
68
NameValuePair v =new NameValuePair();
69             v.setParamName("imqDestination");
70             v.setParamValue("dont know");
71             v.setParamDescription("test property");
72             mrdata.addProperty(v);
73             mrdata.setString(__JndiName, MAIL_RESOURCE_NAME);
74             mrdata.setString(__StoreProtocol, "imap");
75             mrdata.setString(__StoreProtocolClass, "com.sun.mail.imap.IMAPStore");
76             mrdata.setString(__TransportProtocol, "smtp");
77             mrdata.setString(__TransportProtocolClass, "com.sun.mail.smtp.SMTPTransport");
78             mrdata.setString(__Host, "mail-apac.sun.com");
79             mrdata.setString(__MailUser, MAIL_USER);
80             mrdata.setString(__Debug, "false");
81             mrdata.setString(__From,MAIL_FROM);
82             mrdata.setString(__Enabled,"true");
83             //mrdata.setTargetFileObject(project.getProjectDirectory());
84
File JavaDoc fpf = File.createTempFile("falseProject","");
85             fpf.delete();
86             FileObject falseProject = FileUtil.createFolder(fpf);
87             falseProject.createFolder("setup");
88             mrdata.setTargetFileObject(falseProject);
89             mrdata.setTargetFile("mailResourceTest");
90             ResourceUtils.saveMailResourceDatatoXml(mrdata);
91             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/mailResourceTest.sun-resource"));
92             Resources res = Util.getResourcesObject(resourceObj);
93             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
94             ResourceUtils.register(res.getMailResource(0), mejb, false);
95             resourceObj.delete();
96             falseProject.delete();
97             //Util.closeProject(Util.WEB_PROJECT_NAME);
98
Util.sleep(5000);
99             String JavaDoc[] mailResource=Util.getResourcesNames("getMailResource","jndi-name",mejb);
100             for(int i=0;i<mailResource.length;i++) {
101                 if(mailResource[i].equals(MAIL_RESOURCE_NAME))
102                     return;
103             }
104             throw new Exception JavaDoc("Java Mail Resource hasn't been created !");
105         } catch(Exception JavaDoc e) {
106             fail(e.getMessage());
107         }
108     }
109     public void unregisterMailResource() {
110         try {
111             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
112             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
113             String JavaDoc[] command = new String JavaDoc[] {"delete-javamail-resource", "--user", "admin", MAIL_RESOURCE_NAME};
114             Process JavaDoc p=Util.runAsadmin(command);
115             Util.sleep(Util.SLEEP);
116             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
117             String JavaDoc errorMess = error.readLine();
118             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
119             String JavaDoc output=input.readLine();
120             if(errorMess!=null)
121                 throw new Exception JavaDoc(errorMess+"\n"+output);
122             System.out.println(output);
123             Util.closeProject(Util.WEB_PROJECT_NAME);
124             Util.sleep(5000);
125             String JavaDoc[] mailResource = Util.getResourcesNames("getMailResource", "jndi-name", mejb);
126             for(int i=0;i<mailResource.length;i++) {
127                 if(mailResource[i].equals(MAIL_RESOURCE_NAME))
128                     throw new Exception JavaDoc("Java Mail Resource hasn't been removed !");
129             }
130         } catch(Exception JavaDoc e) {
131             fail(e.getMessage());
132         }
133     }
134     public static NbTestSuite suite() {
135         NbTestSuite suite = new NbTestSuite("MailResourcesTest");
136         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
137         suite.addTest(new StartStopServerTest("startServer"));
138         suite.addTest(new MailResourceTest("registerMailResource"));
139         suite.addTest(new MailResourceTest("unregisterMailResource"));
140         suite.addTest(new StartStopServerTest("stopServer"));
141         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
142         return suite;
143     }
144 }
145
Popular Tags