| 1 |
/** |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.apache.activemq.protobuf; |
| 18 |
|
| 19 |
import junit.framework.TestCase; |
| 20 |
|
| 21 |
import org.apache.activemq.protobuf.DeferredUnmarshal.Bar; |
| 22 |
import org.apache.activemq.protobuf.DeferredUnmarshal.Foo; |
| 23 |
|
| 24 |
public class DeferredUnmarshalTest extends TestCase { |
| 25 |
|
| 26 |
public void testDeferredDecoding() throws InvalidProtocolBufferException { |
| 27 |
|
| 28 |
Foo foo = new Foo(); |
| 29 |
foo.setField1(5); |
| 30 |
foo.setField2(20); |
| 31 |
|
| 32 |
Bar bar = new Bar(); |
| 33 |
|
| 34 |
// There is no decoding pending so its' considered decoded. |
| 35 |
assertTrue(bar.isDecoded()); |
| 36 |
|
| 37 |
bar.setField1(25); |
| 38 |
bar.setField2(220); |
| 39 |
bar.setField3(foo); |
| 40 |
|
| 41 |
// The message should not be encoded yet. |
| 42 |
assertFalse(bar.isEncoded()); |
| 43 |
|
| 44 |
// The message should be encoded now.. |
| 45 |
byte[] encodedForm = bar.toUnframedByteArray(); |
| 46 |
assertTrue(bar.isEncoded()); |
| 47 |
|
| 48 |
// Repeated encoding operations should just give back the same byte[] |
| 49 |
assertTrue(encodedForm == bar.toUnframedByteArray()); |
| 50 |
|
| 51 |
// Decoding does not occur until a field is accessed. The new message should still be considered encoded. |
| 52 |
Bar bar2 = Bar.parseUnframed(encodedForm); |
| 53 |
assertTrue(bar2.isEncoded()); |
| 54 |
assertFalse(bar2.isDecoded()); |
| 55 |
|
| 56 |
// This should now decode the message. |
| 57 |
assertEquals(25, bar2.getField1()); |
| 58 |
assertTrue(bar2.isDecoded()); |
| 59 |
|
| 60 |
// Since bar2 still has not been modified it should still spit out the same byte[] |
| 61 |
assertTrue(encodedForm == bar2.toUnframedByteArray()); |
| 62 |
|
| 63 |
// Nested messages should remain un-decoded. |
| 64 |
assertFalse( bar2.getField3().isDecoded() ); |
| 65 |
|
| 66 |
// Changing a field should remove the encoding. |
| 67 |
bar2.setField1(35); |
| 68 |
assertFalse(bar2.isEncoded()); |
| 69 |
assertTrue(bar2.isDecoded()); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
} |