Skip to content

Package: MailUserBean

MailUserBean

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