|
| 1 | +/* |
| 2 | + * Copyright (c) 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 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 | +package jakarta.mail.internet; |
| 17 | + |
| 18 | +import java.util.Properties; |
| 19 | +import java.util.UUID; |
| 20 | +import jakarta.mail.Session; |
| 21 | +import org.junit.Test; |
| 22 | +import static org.junit.Assert.*; |
| 23 | + |
| 24 | +public class UniqueValueTest { |
| 25 | + |
| 26 | + private static final String MESSAGEID_KEY = "mail.mime.messageid.format"; |
| 27 | + |
| 28 | + private static final String BOUNDARY_KEY |
| 29 | + = "mail.mime.multipart.boundary.format"; |
| 30 | + |
| 31 | + public UniqueValueTest() { |
| 32 | + } |
| 33 | + |
| 34 | + @Test(expected = java.lang.IllegalAccessException.class) |
| 35 | + public void testDeclaredConstructor() throws ReflectiveOperationException { |
| 36 | + UniqueValue.class.getDeclaredConstructor().newInstance(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testDefGetUniqueBoundaryValue() { |
| 41 | + String v = System.getProperty(BOUNDARY_KEY); |
| 42 | + assertNull(v, v); |
| 43 | + expectUuidBoundary(); |
| 44 | + } |
| 45 | + |
| 46 | + private void expectUuidBoundary() { |
| 47 | + String start = "----=_Part_"; |
| 48 | + String result = UniqueValue.getUniqueBoundaryValue(); |
| 49 | + assertTrue(result, result.startsWith(start)); |
| 50 | + try { |
| 51 | + UUID.fromString(result.substring(start.length())); |
| 52 | + } catch (Throwable t) { |
| 53 | + throw new RuntimeException(result, t); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testInvalidUuidGetUniqueBoundaryValue() { |
| 59 | + System.setProperty(BOUNDARY_KEY, UniqueValueTest.class.getName()); |
| 60 | + try { |
| 61 | + expectUuidBoundary(); |
| 62 | + } finally { |
| 63 | + System.getProperties().remove(BOUNDARY_KEY); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testUuidGetUniqueBoundaryValue() { |
| 69 | + System.setProperty(BOUNDARY_KEY, "true"); |
| 70 | + try { |
| 71 | + expectUuidBoundary(); |
| 72 | + } finally { |
| 73 | + System.getProperties().remove(BOUNDARY_KEY); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testUvGetUniqueBoundaryValue() { |
| 79 | + System.setProperty(BOUNDARY_KEY, "false"); |
| 80 | + try { |
| 81 | + String start = "----=_Part_"; |
| 82 | + String result = UniqueValue.getUniqueBoundaryValue(); |
| 83 | + assertTrue(result, result.startsWith(start)); |
| 84 | + try { |
| 85 | + int s = start.length(); |
| 86 | + int n = result.indexOf('_', s); |
| 87 | + Long.parseLong(result.substring(s, n)); //id |
| 88 | + |
| 89 | + s = n + 1; |
| 90 | + n = result.indexOf('.', s); |
| 91 | + Long.parseLong(result.substring(s, n)); //hashCode |
| 92 | + |
| 93 | + Long.parseLong(result.substring(n + 1)); //millis |
| 94 | + } catch (Throwable t) { |
| 95 | + throw new RuntimeException(result, t); |
| 96 | + } |
| 97 | + } finally { |
| 98 | + System.getProperties().remove(BOUNDARY_KEY); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testDefGetUniqueMessageIDValue() { |
| 104 | + Properties p = new Properties(); |
| 105 | + String v = p.getProperty(MESSAGEID_KEY); |
| 106 | + assertNull(v, v); |
| 107 | + expectUuidMessageID(Session.getInstance(p)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * If a session is given but the value is not defined ensure code will not |
| 112 | + * fallback to using system properties. |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void testUuidNoheritGetUniqueMessageIDValue() { |
| 116 | + System.setProperty(MESSAGEID_KEY, "false"); |
| 117 | + try { |
| 118 | + Properties p = new Properties(); |
| 119 | + String v = p.getProperty(MESSAGEID_KEY); |
| 120 | + assertNull(v, v); |
| 121 | + expectUuidMessageID(Session.getInstance(p)); |
| 122 | + } finally { |
| 123 | + System.getProperties().remove(MESSAGEID_KEY); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testUvGetUniqueMessageIDValue() { |
| 130 | + Properties p = new Properties(); |
| 131 | + p.put(MESSAGEID_KEY, "false"); |
| 132 | + String result = UniqueValue.getUniqueMessageIDValue( |
| 133 | + Session.getInstance(p)); |
| 134 | + try { |
| 135 | + int s = 0; |
| 136 | + int n = result.indexOf('.', s); |
| 137 | + Long.parseLong(result.substring(s, n)); //id |
| 138 | + |
| 139 | + s = n + 1; |
| 140 | + n = result.indexOf('.', s); |
| 141 | + Long.parseLong(result.substring(s, n)); //hashCode |
| 142 | + |
| 143 | + s = n + 1; |
| 144 | + n = result.indexOf('@', s); |
| 145 | + Long.parseLong(result.substring(s, n)); //millis |
| 146 | + } catch (Throwable t) { |
| 147 | + throw new RuntimeException(result, t); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testUuidGetUniqueMessageIDValue() { |
| 153 | + Properties p = new Properties(); |
| 154 | + p.put(MESSAGEID_KEY, "true"); |
| 155 | + expectUuidMessageID(Session.getInstance(p)); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testDefSystemGetUniqueMessageIDValue() { |
| 160 | + String v = System.getProperty(MESSAGEID_KEY); |
| 161 | + assertNull(v, v); |
| 162 | + expectUuidMessageID((Session) null); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testInvalidGetUniqueMessageIDValue() { |
| 167 | + Properties p = new Properties(); |
| 168 | + p.put(MESSAGEID_KEY, UniqueValueTest.class.getName()); |
| 169 | + expectUuidMessageID(Session.getInstance(p)); |
| 170 | + } |
| 171 | + |
| 172 | + private void expectUuidMessageID(Session s) { |
| 173 | + String result = UniqueValue.getUniqueMessageIDValue(s); |
| 174 | + try { |
| 175 | + UUID.fromString(result.substring(0, result.indexOf('@'))); |
| 176 | + } catch (Throwable t) { |
| 177 | + throw new RuntimeException(result, t); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testUuidSystemGetUniqueMessageIDValue() { |
| 183 | + System.setProperty(MESSAGEID_KEY, "true"); |
| 184 | + try { |
| 185 | + expectUuidMessageID((Session) null); |
| 186 | + } finally { |
| 187 | + System.getProperties().remove(MESSAGEID_KEY); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testUvSystemGetUniqueMessageIDValue() { |
| 193 | + System.setProperty(MESSAGEID_KEY, "false"); |
| 194 | + try { |
| 195 | + String result = UniqueValue.getUniqueMessageIDValue((Session) null); |
| 196 | + try { |
| 197 | + int s = 0; |
| 198 | + int n = result.indexOf('.', s); |
| 199 | + Long.parseLong(result.substring(s, n)); //id |
| 200 | + |
| 201 | + s = n + 1; |
| 202 | + n = result.indexOf('.', s); |
| 203 | + Long.parseLong(result.substring(s, n)); //hashCode |
| 204 | + |
| 205 | + s = n + 1; |
| 206 | + n = result.indexOf('@', s); |
| 207 | + Long.parseLong(result.substring(s, n)); //millis |
| 208 | + } catch (Throwable t) { |
| 209 | + throw new RuntimeException(result, t); |
| 210 | + } |
| 211 | + } finally { |
| 212 | + System.getProperties().remove(MESSAGEID_KEY); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments