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.FileInputStream;
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.nio.ByteBuffer;
22  import java.nio.channels.FileChannel;
23  import java.nio.channels.FileChannel.MapMode;
24  
25  import junit.framework.TestCase;
26  import net.sf.beep4j.Message;
27  import net.sf.beep4j.MessageBuilder;
28  
29  public class DefaultMessageBuilderTest extends TestCase {
30  	
31  	private ByteBuffer getMessage(String name) throws IOException {
32  		FileInputStream fis = new FileInputStream("data/plain/" + name);
33  		FileChannel channel = fis.getChannel();
34  		ByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
35  		return buffer;
36  	}
37  	
38  	public void testBuild() throws Exception {
39  		MessageBuilder builder = new DefaultMessageBuilder();
40  		builder.setContentType("application", "beep+xml");
41  		builder.setCharsetName("UTF-8");
42  		
43  		PrintWriter writer = new PrintWriter(builder.getWriter());
44  		writer.print("<greeting />\r\n");
45  		writer.close();
46  		
47  		Message message = builder.getMessage();
48  		ByteBuffer buffer = message.asByteBuffer();
49  		assertEquals(getMessage("greeting/i_greeting.txt"), buffer);
50  	}
51  	
52  }