Skip to content

Package: MailUserBean

MailUserBean

nameinstructionbranchcomplexitylinemethod
MailUserBean()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getFolder()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHostname()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMessageCount()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPassword()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSession()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getStore()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUrl()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUsername()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isLoggedIn()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
login()
M: 73 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
login(String, String, String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
logout()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
setHostname(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setPassword(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSession(Session)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setStore(Store)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setUsername(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2001, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package demo;
12:
13: import java.util.*;
14: import jakarta.mail.*;
15: import javax.naming.*;
16:
17: /**
18: * This JavaBean is used to store mail user information.
19: */
20: public class MailUserBean {
21: private Folder folder;
22: private String hostname;
23: private String username;
24: private String password;
25: private Session session;
26: private Store store;
27: private URLName url;
28: private String protocol = "imap";
29: private String mbox = "INBOX";        
30:
31: public MailUserBean(){}
32:
33: /**
34: * Returns the jakarta.mail.Folder object.
35: */
36: public Folder getFolder() {
37:         return folder;
38: }
39:
40: /**
41: * Returns the number of messages in the folder.
42: */
43: public int getMessageCount() throws MessagingException {
44:         return folder.getMessageCount();
45: }
46:
47: /**
48: * hostname getter method.
49: */
50: public String getHostname() {
51:         return hostname;
52: }
53:
54: /**
55: * hostname setter method.
56: */
57: public void setHostname(String hostname) {
58:         this.hostname = hostname;
59: }
60:         
61: /**
62: * username getter method.
63: */
64: public String getUsername() {
65:         return username;
66: }
67:
68: /**
69: * username setter method.
70: */
71: public void setUsername(String username) {
72:         this.username = username;
73: }
74:
75: /**
76: * password getter method.
77: */
78: public String getPassword() {
79:         return password;
80: }
81:
82: /**
83: * password setter method.
84: */
85: public void setPassword(String password) {
86:         this.password = password;
87: }
88:
89: /**
90: * session getter method.
91: */
92: public Session getSession() {
93:         return session;
94: }
95:
96: /**
97: * session setter method.
98: */
99: public void setSession(Session session) {
100:         this.session = session;
101: }
102:
103: /**
104: * store getter method.
105: */
106: public Store getStore() {
107:         return store;
108: }
109:
110: /**
111: * store setter method.
112: */
113: public void setStore(Store store) {
114:         this.store = store;
115: }
116:
117: /**
118: * url getter method.
119: */
120: public URLName getUrl() {
121:         return url;
122: }
123:
124: /**
125: * Method for checking if the user is logged in.
126: */
127: public boolean isLoggedIn() {
128:         return store.isConnected();
129: }
130:
131: /**
132: * Method used to login to the mail host.
133: */
134: public void login() throws Exception {
135:         url = new URLName(protocol, getHostname(), -1, mbox,
136:                          getUsername(), getPassword());
137:         /*
138:          * First, try to get the session from JNDI,
139:          * as would be done under J2EE.
140:          */
141:         try {
142:          InitialContext ic = new InitialContext();
143:          Context ctx = (Context)ic.lookup("java:comp/env");
144:          session = (Session)ctx.lookup("MySession");
145:         } catch (Exception ex) {
146:          // ignore it
147:         }
148:
149:         // if JNDI fails, try the old way that should work everywhere
150:•        if (session == null) {
151:          Properties props = null;
152:          try {
153:                 props = System.getProperties();
154:          } catch (SecurityException sex) {
155:                 props = new Properties();
156:          }
157:          session = Session.getInstance(props, null);
158:         }
159:         store = session.getStore(url);
160:         store.connect();
161:         folder = store.getFolder(url);
162:         
163:         folder.open(Folder.READ_WRITE);
164: }
165:
166: /**
167: * Method used to login to the mail host.
168: */
169: public void login(String hostname, String username, String password)
170:         throws Exception {
171:         
172:         this.hostname = hostname;
173:         this.username = username;
174:         this.password = password;
175:         
176:         login();
177: }
178:
179: /**
180: * Method used to logout from the mail host.
181: */
182: public void logout() throws MessagingException {
183:         folder.close(false);
184:         store.close();
185:         store = null;
186:         session = null;
187: }
188: }
189: