/* | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with | |
* this work for additional information regarding copyright ownership. | |
* The ASF licenses this file to You under the Apache License, Version 2.0 | |
* (the "License"); you may not use this file except in compliance with | |
* the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
using System; | |
using System.IO; | |
using Apache.NMS.ActiveMQ.Commands; | |
using Apache.NMS.Util; | |
using NUnit.Framework; | |
namespace Apache.NMS.ActiveMQ.Test.Commands | |
{ | |
[TestFixture] | |
public class ActiveMQTextMessageTest | |
{ | |
[Test] | |
public void TestCommand() | |
{ | |
ActiveMQTextMessage message = new ActiveMQTextMessage(); | |
Assert.IsNull(message.Text); | |
// Test with ASCII Data. | |
message.Text = "Hello World"; | |
Assert.IsNotNull(message.Text); | |
Assert.AreEqual("Hello World", message.Text); | |
String unicodeString = | |
"This unicode string contains two characters " + | |
"with codes outside an 8-bit code range, " + | |
"Pi (\u03a0) and Sigma (\u03a3)."; | |
message.Text = unicodeString; | |
Assert.IsNotNull(message.Text); | |
Assert.AreEqual(unicodeString, message.Text); | |
} | |
[Test] | |
public void TestShallowCopy() | |
{ | |
ActiveMQTextMessage msg = new ActiveMQTextMessage(); | |
string testString = "str"; | |
msg.Text = testString; | |
Message copy = msg.Clone() as Message; | |
Assert.IsTrue(msg.Text == ((ActiveMQTextMessage) copy).Text); | |
} | |
[Test] | |
public void TestSetText() | |
{ | |
ActiveMQTextMessage msg = new ActiveMQTextMessage(); | |
string str = "testText"; | |
msg.Text = str; | |
Assert.AreEqual(msg.Text, str); | |
} | |
[Test] | |
public void TestGetBytes() | |
{ | |
ActiveMQTextMessage msg = new ActiveMQTextMessage(); | |
String str = "testText"; | |
msg.Text = str; | |
msg.BeforeMarshall(null); | |
byte[] bytes = msg.Content; | |
msg = new ActiveMQTextMessage(); | |
msg.Content = bytes; | |
Assert.AreEqual(msg.Text, str); | |
} | |
[Test] | |
public void TestClearBody() | |
{ | |
ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); | |
textMessage.Text = "string"; | |
textMessage.ClearBody(); | |
Assert.IsFalse(textMessage.ReadOnlyBody); | |
Assert.IsNull(textMessage.Text); | |
try | |
{ | |
textMessage.Text = "String"; | |
Assert.IsTrue(textMessage.Text.Length > 0); | |
} | |
catch(MessageNotWriteableException) | |
{ | |
Assert.Fail("should be writeable"); | |
} | |
catch(MessageNotReadableException) | |
{ | |
Assert.Fail("should be readable"); | |
} | |
} | |
[Test] | |
public void TestReadOnlyBody() | |
{ | |
ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); | |
textMessage.Text = "test"; | |
textMessage.ReadOnlyBody = true; | |
try | |
{ | |
Assert.IsTrue(textMessage.Text.Length > 0); | |
} | |
catch(MessageNotReadableException) | |
{ | |
Assert.Fail("should be readable"); | |
} | |
try | |
{ | |
textMessage.Text = "test"; | |
Assert.Fail("should throw exception"); | |
} | |
catch(MessageNotWriteableException) | |
{ | |
} | |
} | |
[Test] | |
public void TtestWriteOnlyBody() | |
{ | |
// should always be readable | |
ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); | |
textMessage.ReadOnlyBody = false; | |
try | |
{ | |
textMessage.Text = "test"; | |
Assert.IsTrue(textMessage.Text.Length > 0); | |
} | |
catch(MessageNotReadableException) | |
{ | |
Assert.Fail("should be readable"); | |
} | |
textMessage.ReadOnlyBody = true; | |
try | |
{ | |
Assert.IsTrue(textMessage.Text.Length > 0); | |
textMessage.Text = "test"; | |
Assert.Fail("should throw exception"); | |
} | |
catch(MessageNotReadableException) | |
{ | |
Assert.Fail("should be readable"); | |
} | |
catch(MessageNotWriteableException) | |
{ | |
} | |
} | |
[Test] | |
public void TestShortText() | |
{ | |
string shortText = "Content"; | |
ActiveMQTextMessage shortMessage = new ActiveMQTextMessage(); | |
SetContent(shortMessage, shortText); | |
Assert.IsTrue(shortMessage.ToString().Contains("Text = " + shortText)); | |
Assert.IsTrue(shortMessage.Text == shortText); | |
string longText = "Very very very very veeeeeeery loooooooooooooooooooooooooooooooooong text"; | |
string longExpectedText = "Very very very very veeeeeeery looooooooooooo...ooooong text"; | |
ActiveMQTextMessage longMessage = new ActiveMQTextMessage(); | |
SetContent(longMessage, longText); | |
Assert.IsTrue(longMessage.ToString().Contains("Text = " + longExpectedText)); | |
Assert.IsTrue(longMessage.Text == longText); | |
} | |
[Test] | |
public void TestNullText() | |
{ | |
ActiveMQTextMessage nullMessage = new ActiveMQTextMessage(); | |
SetContent(nullMessage, null); | |
Assert.IsNull(nullMessage.Text); | |
Assert.IsTrue(nullMessage.ToString().Contains("Text = null")); | |
} | |
protected void SetContent(Message message, String text) | |
{ | |
MemoryStream mstream = new MemoryStream(); | |
EndianBinaryWriter dataOut = new EndianBinaryWriter(mstream); | |
dataOut.WriteString32(text); | |
dataOut.Close(); | |
message.Content = mstream.ToArray(); | |
} | |
} | |
} |