1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.message;
17  
18  import java.io.BufferedReader;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  import java.nio.ByteBuffer;
23  import java.nio.channels.FileChannel;
24  import java.nio.channels.FileChannel.MapMode;
25  
26  import junit.framework.TestCase;
27  import net.sf.beep4j.Message;
28  import net.sf.beep4j.MessageBuilder;
29  
30  public class DefaultMessageParseTest extends TestCase {
31  	
32  	private ByteBuffer readMessage(String name) throws IOException {
33  		FileInputStream is = new FileInputStream("data/plain/" + name);
34  		FileChannel channel = is.getChannel();
35  		return channel.map(MapMode.READ_ONLY, 0, channel.size());
36  	}
37  	
38  	private String getContent(Message message) throws IOException {
39  		BufferedReader reader = new BufferedReader(message.getReader());
40  		StringBuilder builder = new StringBuilder();
41  		String line;
42  		
43  		while ((line = reader.readLine()) != null) {
44  			builder.append(line).append("\r\n");
45  		}
46  		
47  		return builder.toString();
48  	}
49  	
50  	public void testParse1() throws Exception {
51  		MessageParser parser = new DefaultMessageParser();
52  		Message message = parser.parse(readMessage("greeting/i_greeting.txt"));
53  		assertEquals("application/beep+xml", message.getContentType());
54  		String content = getContent(message);
55  		assertEquals(MESSAGE_1, content);
56  	}
57  
58  	public void testParse2() throws Exception {
59  		MessageParser parser = new DefaultMessageParser();
60  		Message message = parser.parse(readMessage("greeting/l_greeting.txt"));
61  		assertEquals("application/beep+xml", message.getContentType());
62  		assertEquals("bar", message.getHeader("Foo"));
63  		String content = getContent(message);
64  		assertEquals(MESSAGE_2, content);
65  	}
66  	
67  	public void testParse3() throws Exception {
68  		MessageParser parser = new DefaultMessageParser();
69  		Message message = parser.parse(readMessage("greeting/i_greeting_no_headers.txt"));
70  		assertEquals("application/octet-stream", message.getContentType());
71  		String content = getContent(message);
72  		assertEquals(MESSAGE_3, content);
73  	}
74  	
75  	public void testParseRoundTrip() throws Exception {
76  		MessageBuilder messageBuilder = new DefaultMessageBuilder();
77  		messageBuilder.addHeader("Foo", "  Bar  ");
78  		messageBuilder.setCharsetName("UTF-8");
79  		messageBuilder.setContentType("application", "beep+xml");
80  
81  		PrintWriter writer = new PrintWriter(messageBuilder.getWriter());
82  		writer.print(MESSAGE_2);
83  		writer.close();
84  		
85  		Message outMessage = messageBuilder.getMessage();
86  		MessageParser parser = new DefaultMessageParser();
87  		Message inMessage = parser.parse(outMessage.asByteBuffer());
88  		assertEquals("application/beep+xml", inMessage.getContentType());
89  		assertEquals("Bar", inMessage.getHeader("Foo"));
90  		String content = getContent(inMessage);
91  		assertEquals(MESSAGE_2, content);
92  	}
93  
94  	private static final String MESSAGE_1 = "<greeting />\r\n";
95  
96  	private static final String MESSAGE_2 = "<greeting>\r\n"
97  		+ "   <profile uri='http://iana.org/beep/TLS' />\r\n"
98  		+ "</greeting>\r\n";
99  	
100 	private static final String MESSAGE_3 = "<greeting />\r\n";
101 
102 	
103 }