1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2009-2018 Oracle and/or its affiliates. All rights reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU
7  * General Public License Version 2 only ("GPL") or the Common Development
8  * and Distribution License("CDDL") (collectively, the "License").  You
9  * may not use this file except in compliance with the License.  You can
10  * obtain a copy of the License at
11  * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12  * or LICENSE.txt.  See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  * When distributing the software, include this License Header Notice in each
16  * file and include the License file at LICENSE.txt.
17  *
18  * GPL Classpath Exception:
19  * Oracle designates this particular file as subject to the "Classpath"
20  * exception as provided by Oracle in the GPL Version 2 section of the License
21  * file that accompanied this code.
22  *
23  * Modifications:
24  * If applicable, add the following below the License Header, with the fields
25  * enclosed by brackets [] replaced by your own identifying information:
26  * "Portions Copyright [year] [name of copyright owner]"
27  *
28  * Contributor(s):
29  * If you wish your version of this file to be governed by only the CDDL or
30  * only the GPL Version 2, indicate your decision by adding "[Contributor]
31  * elects to include this software in this distribution under the [CDDL or GPL
32  * Version 2] license."  If you don't indicate a single choice of license, a
33  * recipient has the option to distribute your version of this file under
34  * either the CDDL, the GPL Version 2 or to extend the choice of license to
35  * its licensees as provided above.  However, if you add GPL Version 2 code
36  * and therefore, elected the GPL Version 2 license, then the option applies
37  * only if the new code is made subject to such option by the copyright
38  * holder.
39  */
40 
41 package com.sun.mail.util;
42 
43 import java.util.Properties;
44 import javax.mail.Session;
45 
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48 import org.junit.Test;
49 
50 /**
51  * Test that the PropUtil methods return the correct values,
52  * especially when defaults and non-String values are considered.
53  */
54 public class PropUtilTest {
55     @Test
testInt()56     public void testInt() throws Exception {
57 	Properties props = new Properties();
58 	props.setProperty("test", "2");
59 	assertEquals(PropUtil.getIntProperty(props, "test", 1), 2);
60     }
61 
62     @Test
testIntDef()63     public void testIntDef() throws Exception {
64 	Properties props = new Properties();
65 	assertEquals(PropUtil.getIntProperty(props, "test", 1), 1);
66     }
67 
68     @Test
testIntDefProp()69     public void testIntDefProp() throws Exception {
70 	Properties defprops = new Properties();
71 	defprops.setProperty("test", "2");
72 	Properties props = new Properties(defprops);
73 	assertEquals(PropUtil.getIntProperty(props, "test", 1), 2);
74     }
75 
76     @Test
testInteger()77     public void testInteger() throws Exception {
78 	Properties props = new Properties();
79 	props.put("test", 2);
80 	assertEquals(PropUtil.getIntProperty(props, "test", 1), 2);
81     }
82 
83     @Test
testBool()84     public void testBool() throws Exception {
85 	Properties props = new Properties();
86 	props.setProperty("test", "true");
87 	assertTrue(PropUtil.getBooleanProperty(props, "test", false));
88     }
89 
90     @Test
testBoolDef()91     public void testBoolDef() throws Exception {
92 	Properties props = new Properties();
93 	assertTrue(PropUtil.getBooleanProperty(props, "test", true));
94     }
95 
96     @Test
testBoolDefProp()97     public void testBoolDefProp() throws Exception {
98 	Properties defprops = new Properties();
99 	defprops.setProperty("test", "true");
100 	Properties props = new Properties(defprops);
101 	assertTrue(PropUtil.getBooleanProperty(props, "test", false));
102     }
103 
104     @Test
testBoolean()105     public void testBoolean() throws Exception {
106 	Properties props = new Properties();
107 	props.put("test", true);
108 	assertTrue(PropUtil.getBooleanProperty(props, "test", false));
109     }
110 
111 
112     // the Session variants...
113 
114     @Test
115     @SuppressWarnings("deprecation")
testSessionInt()116     public void testSessionInt() throws Exception {
117 	Properties props = new Properties();
118 	props.setProperty("test", "2");
119 	Session sess = Session.getInstance(props, null);
120 	assertEquals(PropUtil.getIntSessionProperty(sess, "test", 1), 2);
121     }
122 
123     @Test
124     @SuppressWarnings("deprecation")
testSessionIntDef()125     public void testSessionIntDef() throws Exception {
126 	Properties props = new Properties();
127 	Session sess = Session.getInstance(props, null);
128 	assertEquals(PropUtil.getIntSessionProperty(sess, "test", 1), 1);
129     }
130 
131     @Test
132     @SuppressWarnings("deprecation")
testSessionIntDefProp()133     public void testSessionIntDefProp() throws Exception {
134 	Properties defprops = new Properties();
135 	defprops.setProperty("test", "2");
136 	Properties props = new Properties(defprops);
137 	Session sess = Session.getInstance(props, null);
138 	assertEquals(PropUtil.getIntSessionProperty(sess, "test", 1), 2);
139     }
140 
141     @Test
142     @SuppressWarnings("deprecation")
testSessionInteger()143     public void testSessionInteger() throws Exception {
144 	Properties props = new Properties();
145 	props.put("test", 2);
146 	Session sess = Session.getInstance(props, null);
147 	assertEquals(PropUtil.getIntSessionProperty(sess, "test", 1), 2);
148     }
149 
150     @Test
151     @SuppressWarnings("deprecation")
testSessionBool()152     public void testSessionBool() throws Exception {
153 	Properties props = new Properties();
154 	props.setProperty("test", "true");
155 	Session sess = Session.getInstance(props, null);
156 	assertTrue(PropUtil.getBooleanSessionProperty(sess, "test", false));
157     }
158 
159     @Test
160     @SuppressWarnings("deprecation")
testSessionBoolDef()161     public void testSessionBoolDef() throws Exception {
162 	Properties props = new Properties();
163 	Session sess = Session.getInstance(props, null);
164 	assertTrue(PropUtil.getBooleanSessionProperty(sess, "test", true));
165     }
166 
167     @Test
168     @SuppressWarnings("deprecation")
testSessionBoolDefProp()169     public void testSessionBoolDefProp() throws Exception {
170 	Properties defprops = new Properties();
171 	defprops.setProperty("test", "true");
172 	Properties props = new Properties(defprops);
173 	Session sess = Session.getInstance(props, null);
174 	assertTrue(PropUtil.getBooleanSessionProperty(sess, "test", false));
175     }
176 
177     @Test
178     @SuppressWarnings("deprecation")
testSessionBoolean()179     public void testSessionBoolean() throws Exception {
180 	Properties props = new Properties();
181 	props.put("test", true);
182 	Session sess = Session.getInstance(props, null);
183 	assertTrue(PropUtil.getBooleanSessionProperty(sess, "test", false));
184     }
185 
186 
187     // the System variants...
188 
189     @Test
testSystemBool()190     public void testSystemBool() throws Exception {
191 	System.setProperty("test", "true");
192 	assertTrue(PropUtil.getBooleanSystemProperty("test", false));
193     }
194 
195     @Test
testSystemBoolDef()196     public void testSystemBoolDef() throws Exception {
197 	assertTrue(PropUtil.getBooleanSystemProperty("testnotset", true));
198     }
199 
200     @Test
testSystemBoolean()201     public void testSystemBoolean() throws Exception {
202 	System.getProperties().put("testboolean", true);
203 	assertTrue(PropUtil.getBooleanSystemProperty("testboolean", false));
204     }
205 }
206