[6785] | 1 | /**************************************************************** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
| 3 | * or more contributor license agreements. See the NOTICE file * |
---|
| 4 | * distributed with this work for additional information * |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file * |
---|
| 6 | * to you under the Apache License, Version 2.0 (the * |
---|
| 7 | * "License"); you may not use this file except in compliance * |
---|
| 8 | * with the License. You may obtain a copy of the License at * |
---|
| 9 | * * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
| 11 | * * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, * |
---|
| 13 | * software distributed under the License is distributed on an * |
---|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
| 15 | * KIND, either express or implied. See the License for the * |
---|
| 16 | * specific language governing permissions and limitations * |
---|
| 17 | * under the License. * |
---|
| 18 | ****************************************************************/ |
---|
| 19 | |
---|
| 20 | package org.apache.james.mime4j.stream; |
---|
| 21 | |
---|
| 22 | import java.io.ByteArrayInputStream; |
---|
| 23 | import java.io.InputStream; |
---|
| 24 | |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | |
---|
| 27 | import org.apache.james.mime4j.ExampleMail; |
---|
| 28 | |
---|
| 29 | public class MimeTokenEmbeddedMessageTest extends TestCase { |
---|
| 30 | |
---|
| 31 | MimeTokenStream stream; |
---|
| 32 | |
---|
| 33 | @Override |
---|
| 34 | protected void setUp() throws Exception { |
---|
| 35 | super.setUp(); |
---|
| 36 | stream = new MimeTokenStream(); |
---|
| 37 | InputStream in = new ByteArrayInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_BYTES); |
---|
| 38 | stream.parse(in); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | @Override |
---|
| 42 | protected void tearDown() throws Exception { |
---|
| 43 | super.tearDown(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public void testWhenRecurseShouldVisitInnerMailsAndInnerMultiparts() throws Exception { |
---|
| 47 | stream.setRecursionMode(RecursionMode.M_RECURSE); |
---|
| 48 | |
---|
| 49 | nextIs(EntityState.T_START_HEADER); |
---|
| 50 | nextIs(EntityState.T_FIELD); |
---|
| 51 | nextIs(EntityState.T_FIELD); |
---|
| 52 | nextIs(EntityState.T_FIELD); |
---|
| 53 | nextIs(EntityState.T_FIELD); |
---|
| 54 | nextIs(EntityState.T_FIELD); |
---|
| 55 | nextIs(EntityState.T_END_HEADER); |
---|
| 56 | |
---|
| 57 | nextIs(EntityState.T_START_MULTIPART); |
---|
| 58 | |
---|
| 59 | nextIs(EntityState.T_PREAMBLE); |
---|
| 60 | |
---|
| 61 | // PART ONE |
---|
| 62 | nextIs(EntityState.T_START_BODYPART); |
---|
| 63 | nextIs(EntityState.T_START_HEADER); |
---|
| 64 | nextIs(EntityState.T_FIELD); |
---|
| 65 | nextIs(EntityState.T_END_HEADER); |
---|
| 66 | nextIs(EntityState.T_BODY); |
---|
| 67 | checkInputStream("Rhubarb!\r\n"); |
---|
| 68 | nextIs(EntityState.T_END_BODYPART); |
---|
| 69 | |
---|
| 70 | // PART TWO |
---|
| 71 | nextIs(EntityState.T_START_BODYPART); |
---|
| 72 | nextIs(EntityState.T_START_HEADER); |
---|
| 73 | nextIs(EntityState.T_FIELD); |
---|
| 74 | nextIs(EntityState.T_FIELD); |
---|
| 75 | nextIs(EntityState.T_END_HEADER); |
---|
| 76 | nextIs(EntityState.T_BODY); |
---|
| 77 | checkInputStream("987654321AHPLA\r\n"); |
---|
| 78 | nextIs(EntityState.T_END_BODYPART); |
---|
| 79 | |
---|
| 80 | // PART THREE |
---|
| 81 | nextIs(EntityState.T_START_BODYPART); |
---|
| 82 | nextIs(EntityState.T_START_HEADER); |
---|
| 83 | nextIs(EntityState.T_FIELD); |
---|
| 84 | nextIs(EntityState.T_END_HEADER); |
---|
| 85 | nextIs(EntityState.T_START_MESSAGE); |
---|
| 86 | nextIs(EntityState.T_START_HEADER); |
---|
| 87 | nextIs(EntityState.T_FIELD); |
---|
| 88 | nextIs(EntityState.T_FIELD); |
---|
| 89 | nextIs(EntityState.T_FIELD); |
---|
| 90 | nextIs(EntityState.T_FIELD); |
---|
| 91 | nextIs(EntityState.T_FIELD); |
---|
| 92 | nextIs(EntityState.T_END_HEADER); |
---|
| 93 | nextIs(EntityState.T_START_MULTIPART); |
---|
| 94 | nextIs(EntityState.T_PREAMBLE); |
---|
| 95 | nextIs(EntityState.T_START_BODYPART); |
---|
| 96 | nextIs(EntityState.T_START_HEADER); |
---|
| 97 | nextIs(EntityState.T_FIELD); |
---|
| 98 | nextIs(EntityState.T_END_HEADER); |
---|
| 99 | nextIs(EntityState.T_BODY); |
---|
| 100 | checkInputStream("Custard!\r\n"); |
---|
| 101 | nextIs(EntityState.T_END_BODYPART); |
---|
| 102 | nextIs(EntityState.T_START_BODYPART); |
---|
| 103 | nextIs(EntityState.T_START_HEADER); |
---|
| 104 | nextIs(EntityState.T_FIELD); |
---|
| 105 | nextIs(EntityState.T_END_HEADER); |
---|
| 106 | nextIs(EntityState.T_BODY); |
---|
| 107 | checkInputStream("CUSTARDCUSTARDCUSTARD\r\n"); |
---|
| 108 | nextIs(EntityState.T_END_BODYPART); |
---|
| 109 | nextIs(EntityState.T_END_MULTIPART); |
---|
| 110 | nextIs(EntityState.T_END_MESSAGE); |
---|
| 111 | nextIs(EntityState.T_END_BODYPART); |
---|
| 112 | |
---|
| 113 | // PART FOUR |
---|
| 114 | nextIs(EntityState.T_START_BODYPART); |
---|
| 115 | nextIs(EntityState.T_START_HEADER); |
---|
| 116 | nextIs(EntityState.T_FIELD); |
---|
| 117 | nextIs(EntityState.T_END_HEADER); |
---|
| 118 | nextIs(EntityState.T_START_MULTIPART); |
---|
| 119 | checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MULTIPART_MIXED); |
---|
| 120 | nextIs(EntityState.T_END_MULTIPART); |
---|
| 121 | nextIs(EntityState.T_END_BODYPART); |
---|
| 122 | nextIs(EntityState.T_EPILOGUE); |
---|
| 123 | nextIs(EntityState.T_END_MULTIPART); |
---|
| 124 | nextIs(EntityState.T_END_MESSAGE); |
---|
| 125 | nextIs(EntityState.T_END_OF_STREAM); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | public void testWhenFlatAtStartShouldIgnoreMultipartStructure() throws Exception { |
---|
| 130 | stream.setRecursionMode(RecursionMode.M_FLAT); |
---|
| 131 | nextIs(EntityState.T_START_HEADER); |
---|
| 132 | nextIs(EntityState.T_FIELD); |
---|
| 133 | nextIs(EntityState.T_FIELD); |
---|
| 134 | nextIs(EntityState.T_FIELD); |
---|
| 135 | nextIs(EntityState.T_FIELD); |
---|
| 136 | nextIs(EntityState.T_FIELD); |
---|
| 137 | nextIs(EntityState.T_END_HEADER); |
---|
| 138 | |
---|
| 139 | nextIs(EntityState.T_BODY); |
---|
| 140 | |
---|
| 141 | checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_BODY); |
---|
| 142 | |
---|
| 143 | nextIs(EntityState.T_END_MESSAGE); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | public void testWhenFlatShouldIgnoreInnerMailsAndInnerMultiparts() throws Exception { |
---|
| 147 | nextIs(EntityState.T_START_HEADER); |
---|
| 148 | nextIs(EntityState.T_FIELD); |
---|
| 149 | nextIs(EntityState.T_FIELD); |
---|
| 150 | nextIs(EntityState.T_FIELD); |
---|
| 151 | nextIs(EntityState.T_FIELD); |
---|
| 152 | nextIs(EntityState.T_FIELD); |
---|
| 153 | nextIs(EntityState.T_END_HEADER); |
---|
| 154 | |
---|
| 155 | nextIs(EntityState.T_START_MULTIPART); |
---|
| 156 | |
---|
| 157 | stream.setRecursionMode(RecursionMode.M_FLAT); |
---|
| 158 | nextIs(EntityState.T_PREAMBLE); |
---|
| 159 | |
---|
| 160 | // PART ONE |
---|
| 161 | nextIs(EntityState.T_START_BODYPART); |
---|
| 162 | nextIs(EntityState.T_START_HEADER); |
---|
| 163 | nextIs(EntityState.T_FIELD); |
---|
| 164 | nextIs(EntityState.T_END_HEADER); |
---|
| 165 | nextIs(EntityState.T_BODY); |
---|
| 166 | checkInputStream("Rhubarb!\r\n"); |
---|
| 167 | nextIs(EntityState.T_END_BODYPART); |
---|
| 168 | |
---|
| 169 | // PART TWO |
---|
| 170 | nextIs(EntityState.T_START_BODYPART); |
---|
| 171 | nextIs(EntityState.T_START_HEADER); |
---|
| 172 | nextIs(EntityState.T_FIELD); |
---|
| 173 | nextIs(EntityState.T_FIELD); |
---|
| 174 | nextIs(EntityState.T_END_HEADER); |
---|
| 175 | nextIs(EntityState.T_BODY); |
---|
| 176 | checkInputStream("987654321AHPLA\r\n"); |
---|
| 177 | nextIs(EntityState.T_END_BODYPART); |
---|
| 178 | |
---|
| 179 | // PART THREE |
---|
| 180 | nextIs(EntityState.T_START_BODYPART); |
---|
| 181 | nextIs(EntityState.T_START_HEADER); |
---|
| 182 | nextIs(EntityState.T_FIELD); |
---|
| 183 | nextIs(EntityState.T_END_HEADER); |
---|
| 184 | nextIs(EntityState.T_BODY); |
---|
| 185 | checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MAIL); |
---|
| 186 | nextIs(EntityState.T_END_BODYPART); |
---|
| 187 | |
---|
| 188 | // PART FOUR |
---|
| 189 | nextIs(EntityState.T_START_BODYPART); |
---|
| 190 | nextIs(EntityState.T_START_HEADER); |
---|
| 191 | nextIs(EntityState.T_FIELD); |
---|
| 192 | nextIs(EntityState.T_END_HEADER); |
---|
| 193 | nextIs(EntityState.T_BODY); |
---|
| 194 | checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MULTIPART_MIXED); |
---|
| 195 | nextIs(EntityState.T_END_BODYPART); |
---|
| 196 | nextIs(EntityState.T_EPILOGUE); |
---|
| 197 | nextIs(EntityState.T_END_MULTIPART); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | private void checkInputStream(String expected) throws Exception { |
---|
| 201 | InputStream inputStream = stream.getInputStream(); |
---|
| 202 | int next = inputStream.read(); |
---|
| 203 | int i=0; |
---|
| 204 | while (next != -1) { |
---|
| 205 | assertEquals("@" + i, expected.charAt(i++), (char) next); |
---|
| 206 | next = inputStream.read(); |
---|
| 207 | } |
---|
| 208 | assertEquals(expected.length(), i); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | private void nextIs(EntityState state) throws Exception { |
---|
| 212 | assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(stream.next())); |
---|
| 213 | } |
---|
| 214 | } |
---|