|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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 Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package jakarta.mail.internet; |
| 18 | + |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import java.util.Properties; |
| 23 | +import jakarta.mail.Session; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Test the properties that control strict address parsing and UTF-8 support |
| 28 | + */ |
| 29 | +public class MimeMessageTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testDefaultProperties() throws Exception { |
| 33 | + Session session = Session.getInstance(new Properties()); |
| 34 | + MimeMessage message = new MimeMessage(session); |
| 35 | + assertEquals(true, message.isStrict()); |
| 36 | + assertEquals(false, message.isAllowutf8()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testSystemAndSession() throws Exception { |
| 41 | + try { |
| 42 | + System.setProperty("mail.mime.address.strict", Boolean.TRUE.toString()); |
| 43 | + System.setProperty("mail.mime.allowutf8", Boolean.FALSE.toString()); |
| 44 | + Properties properties = new Properties(); |
| 45 | + properties.setProperty("mail.mime.address.strict", Boolean.FALSE.toString()); |
| 46 | + properties.setProperty("mail.mime.allowutf8", Boolean.TRUE.toString()); |
| 47 | + Session session = Session.getInstance(properties); |
| 48 | + MimeMessage message = new MimeMessage(session); |
| 49 | + assertEquals(false, message.isStrict()); |
| 50 | + assertEquals(true, message.isAllowutf8()); |
| 51 | + } finally { |
| 52 | + System.clearProperty("mail.mime.address.strict"); |
| 53 | + System.clearProperty("mail.mime.allowutf8"); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testSystemPropertiesNoSession() throws Exception { |
| 59 | + try { |
| 60 | + System.setProperty("mail.mime.address.strict", Boolean.FALSE.toString()); |
| 61 | + System.setProperty("mail.mime.allowutf8", Boolean.TRUE.toString()); |
| 62 | + MimeMessage message = new MimeMessage((Session) null); |
| 63 | + assertEquals(false, message.isStrict()); |
| 64 | + assertEquals(true, message.isAllowutf8()); |
| 65 | + } finally { |
| 66 | + System.clearProperty("mail.mime.address.strict"); |
| 67 | + System.clearProperty("mail.mime.allowutf8"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testSystemPropertiesAndSessionWithNoProperties() throws Exception { |
| 73 | + try { |
| 74 | + System.setProperty("mail.mime.address.strict", Boolean.FALSE.toString()); |
| 75 | + System.setProperty("mail.mime.allowutf8", Boolean.TRUE.toString()); |
| 76 | + Session session = Session.getInstance(new Properties()); |
| 77 | + MimeMessage message = new MimeMessage(session); |
| 78 | + // System properties are ignored when Session is present (even if empty) |
| 79 | + assertEquals(true, message.isStrict()); |
| 80 | + assertEquals(false, message.isAllowutf8()); |
| 81 | + } finally { |
| 82 | + System.clearProperty("mail.mime.address.strict"); |
| 83 | + System.clearProperty("mail.mime.allowutf8"); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments