|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 1997-2016 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/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 packager/legal/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 javax.mail.internet; |
| 42 | + |
| 43 | +import java.io.*; |
| 44 | +import java.util.*; |
| 45 | +import javax.mail.internet.InternetAddress; |
| 46 | + |
| 47 | +import org.junit.Test; |
| 48 | +import org.junit.Assert; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | +import org.junit.runners.Parameterized; |
| 51 | +import org.junit.runners.Parameterized.Parameters; |
| 52 | + |
| 53 | +/** |
| 54 | + * Test InternetAddress folding. |
| 55 | + * |
| 56 | + * @author Bill Shannon |
| 57 | + */ |
| 58 | + |
| 59 | +@RunWith(Parameterized.class) |
| 60 | +public class InternetAddressFoldTest { |
| 61 | + private InternetAddress[] orig; |
| 62 | + private String expect; |
| 63 | + |
| 64 | + private static List<Object[]> testData; |
| 65 | + |
| 66 | + public InternetAddressFoldTest(InternetAddress[] orig, String expect) { |
| 67 | + this.orig = orig; |
| 68 | + this.expect = expect; |
| 69 | + } |
| 70 | + |
| 71 | + @Parameters |
| 72 | + public static Collection<Object[]> data() throws Exception { |
| 73 | + testData = new ArrayList<Object[]>(); |
| 74 | + parse(new BufferedReader(new InputStreamReader( |
| 75 | + InternetAddressFoldTest.class.getResourceAsStream("addrfolddata")))); |
| 76 | + return testData; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Read the data from the test file. Format is: |
| 81 | + * |
| 82 | + * FOLD N |
| 83 | + * address1$ |
| 84 | + * ... |
| 85 | + * addressN$ |
| 86 | + * EXPECT |
| 87 | + * address1, ..., addressN$ |
| 88 | + */ |
| 89 | + private static void parse(BufferedReader in) throws Exception { |
| 90 | + String line; |
| 91 | + while ((line = in.readLine()) != null) { |
| 92 | + if (line.startsWith("#") || line.length() == 0) |
| 93 | + continue; |
| 94 | + if (!line.startsWith("FOLD")) |
| 95 | + throw new IOException("TEST DATA FORMAT ERROR, MISSING FOLD"); |
| 96 | + int count = Integer.parseInt(line.substring(5)); |
| 97 | + InternetAddress[] orig = new InternetAddress[count]; |
| 98 | + for (int i = 0; i < count; i++) |
| 99 | + orig[i] = new InternetAddress(readString(in)); |
| 100 | + String e = in.readLine(); |
| 101 | + if (!e.equals("EXPECT")) |
| 102 | + throw new IOException("TEST DATA FORMAT ERROR, MISSING EXPECT"); |
| 103 | + String expect = readString(in); |
| 104 | + testData.add(new Object[] { orig, expect }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Read a string that ends with '$', preserving all characters, |
| 110 | + * especially including CR and LF. |
| 111 | + */ |
| 112 | + private static String readString(BufferedReader in) throws IOException { |
| 113 | + StringBuffer sb = new StringBuffer(); |
| 114 | + int c; |
| 115 | + while ((c = in.read()) != '$') |
| 116 | + sb.append((char)c); |
| 117 | + in.readLine(); // throw away rest of line |
| 118 | + return sb.toString(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testFold() { |
| 123 | + Assert.assertEquals("Fold", expect, InternetAddress.toString(orig, 0)); |
| 124 | + } |
| 125 | +} |
0 commit comments