Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1 | //:: # Copyright 2013, Big Switch Networks, Inc. |
| 2 | //:: # |
| 3 | //:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | //:: # the following special exception: |
| 5 | //:: # |
| 6 | //:: # LOXI Exception |
| 7 | //:: # |
| 8 | //:: # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | //:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | //:: # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | //:: # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | //:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | //:: # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | //:: # |
| 15 | //:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | //:: # |
| 17 | //:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | //:: # a copy of the EPL at: |
| 19 | //:: # |
| 20 | //:: # http::: #www.eclipse.org/legal/epl-v10.html |
| 21 | //:: # |
| 22 | //:: # Unless required by applicable law or agreed to in writing, software |
| 23 | //:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | //:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | //:: # EPL for the specific language governing permissions and limitations |
| 26 | //:: # under the EPL. |
| 27 | //:: |
| 28 | //:: from loxi_ir import * |
| 29 | //:: import itertools |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 30 | //:: import java_gen.java_model as java_model |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 31 | //:: include('_copyright.java') |
| 32 | |
| 33 | //:: include('_autogen.java') |
| 34 | |
| 35 | package ${test.package}; |
| 36 | |
| 37 | //:: include("_imports.java", msg=msg) |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import static org.junit.Assert.*; |
| 41 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 42 | import org.hamcrest.CoreMatchers; |
| 43 | |
| 44 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 45 | public class ${test.name} { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 46 | //:: factory = java_model.model.factory_of(test.interface) |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 47 | //:: var_type = msg.interface.name |
| 48 | //:: var_name = msg.interface.variable_name |
Rich Lane | 0240860 | 2013-11-20 11:46:37 -0800 | [diff] [blame] | 49 | //:: use_builder = len(msg.data_members) > 0 |
| 50 | //:: factory_method = factory.method_name(msg.interface, builder=use_builder) |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 51 | //:: factory_impl = java_model.model.factory_of(test.interface).of_version(test.java_class.version).name |
Yotam Harchol | 77e6cf8 | 2013-09-27 16:36:18 -0700 | [diff] [blame] | 52 | ${factory.name if factory.name is not None else "OFFactory"} factory; |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 53 | |
| 54 | final static byte[] ${msg.constant_name}_SERIALIZED = |
| 55 | new byte[] { ${", ".join("%s0x%x" % (("" if ord(c)<128 else "(byte) "), ord(c)) for c in test_data["binary"] ) } }; |
| 56 | |
| 57 | @Before |
| 58 | public void setup() { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 59 | factory = ${factory_impl + ".INSTANCE" if factory_impl is not None else "OFFactories.getFactory(OFVersion." + version.constant_version + ")"}; |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | //:: if "java" in test_data: |
| 63 | @Test |
| 64 | public void testWrite() { |
Rich Lane | 0240860 | 2013-11-20 11:46:37 -0800 | [diff] [blame] | 65 | //:: if use_builder: |
| 66 | ${var_type}.Builder builder = factory.${factory_method}(); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 67 | ${test_data["java"]}; |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 68 | ${var_type} ${var_name} = builder.build(); |
Rich Lane | 0240860 | 2013-11-20 11:46:37 -0800 | [diff] [blame] | 69 | //:: else: |
| 70 | ${var_type} ${var_name} = factory.${factory_method}(); |
| 71 | //:: #endif |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 72 | ChannelBuffer bb = ChannelBuffers.dynamicBuffer(); |
| 73 | ${var_name}.writeTo(bb); |
| 74 | byte[] written = new byte[bb.readableBytes()]; |
| 75 | bb.readBytes(written); |
| 76 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 77 | assertThat(written, CoreMatchers.equalTo(${msg.constant_name}_SERIALIZED)); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testRead() throws Exception { |
Rich Lane | 0240860 | 2013-11-20 11:46:37 -0800 | [diff] [blame] | 82 | //:: if use_builder: |
| 83 | ${var_type}.Builder builder = factory.${factory_method}(); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 84 | ${test_data["java"]}; |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 85 | ${var_type} ${var_name}Built = builder.build(); |
Rich Lane | 0240860 | 2013-11-20 11:46:37 -0800 | [diff] [blame] | 86 | //:: else: |
| 87 | ${var_type} ${var_name}Built = factory.${factory_method}(); |
| 88 | //:: #endif |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 89 | |
| 90 | ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED); |
| 91 | |
| 92 | // FIXME should invoke the overall reader once implemented |
| 93 | ${var_type} ${var_name}Read = ${msg.name}.READER.readFrom(input); |
Andreas Wundsam | 45da2d4 | 2013-10-23 15:27:15 -0700 | [diff] [blame] | 94 | assertEquals(${msg.constant_name}_SERIALIZED.length, input.readerIndex()); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 95 | |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 96 | assertEquals(${var_name}Built, ${var_name}Read); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 97 | } |
| 98 | //:: else: |
| 99 | // FIXME: No java stanza in test_data for this class. Add for more comprehensive unit testing |
| 100 | //:: #endif |
| 101 | |
| 102 | @Test |
| 103 | public void testReadWrite() throws Exception { |
| 104 | ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED); |
| 105 | |
| 106 | // FIXME should invoke the overall reader once implemented |
| 107 | ${var_type} ${var_name} = ${msg.name}.READER.readFrom(input); |
Andreas Wundsam | 45da2d4 | 2013-10-23 15:27:15 -0700 | [diff] [blame] | 108 | assertEquals(${msg.constant_name}_SERIALIZED.length, input.readerIndex()); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 109 | |
| 110 | // write message again |
| 111 | ChannelBuffer bb = ChannelBuffers.dynamicBuffer(); |
| 112 | ${var_name}.writeTo(bb); |
| 113 | byte[] written = new byte[bb.readableBytes()]; |
| 114 | bb.readBytes(written); |
| 115 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 116 | assertThat(written, CoreMatchers.equalTo(${msg.constant_name}_SERIALIZED)); |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } |