blob: cd85a74af3c21ab58e9859af1bedf1881b41cc22 [file] [log] [blame]
Andreas Wundsame916d6f2013-07-30 11:33:58 -07001//:: # 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
30//:: import of_g
Yotam Harchol595c6442013-09-27 16:29:08 -070031//:: import java_gen.java_model as java_model
Andreas Wundsame916d6f2013-07-30 11:33:58 -070032//:: include('_copyright.java')
33
34//:: include('_autogen.java')
35
36package ${test.package};
37
38//:: include("_imports.java", msg=msg)
39import org.junit.Before;
40import org.junit.Test;
41import static org.junit.Assert.*;
42
43public class ${test.name} {
Yotam Harchol595c6442013-09-27 16:29:08 -070044 //:: factory = java_model.model.factory_of(test.interface)
Andreas Wundsame916d6f2013-07-30 11:33:58 -070045 //:: var_type = msg.interface.name
46 //:: var_name = msg.interface.variable_name
Yotam Harchol595c6442013-09-27 16:29:08 -070047 //:: builder_method = factory.method_name(msg.interface)
48 //:: factory_impl = java_model.model.factory_of(test.interface).of_version(test.java_class.version).name
Yotam Harchol77e6cf82013-09-27 16:36:18 -070049 ${factory.name if factory.name is not None else "OFFactory"} factory;
Andreas Wundsame916d6f2013-07-30 11:33:58 -070050
51 final static byte[] ${msg.constant_name}_SERIALIZED =
52 new byte[] { ${", ".join("%s0x%x" % (("" if ord(c)<128 else "(byte) "), ord(c)) for c in test_data["binary"] ) } };
53
54 @Before
55 public void setup() {
Yotam Harchol595c6442013-09-27 16:29:08 -070056 factory = ${factory_impl + ".INSTANCE" if factory_impl is not None else "OFFactories.getFactory(OFVersion." + version.constant_version + ")"};
Andreas Wundsame916d6f2013-07-30 11:33:58 -070057 }
58
59 //:: if "java" in test_data:
60 @Test
61 public void testWrite() {
Yotam Harchol595c6442013-09-27 16:29:08 -070062 ${var_type}.Builder builder = factory.${builder_method}();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070063 ${test_data["java"]};
Andreas Wundsame0d52be2013-08-22 07:52:13 -070064 ${var_type} ${var_name} = builder.build();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070065 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
66 ${var_name}.writeTo(bb);
67 byte[] written = new byte[bb.readableBytes()];
68 bb.readBytes(written);
69
70 assertArrayEquals(${msg.constant_name}_SERIALIZED, written);
71 }
72
73 @Test
74 public void testRead() throws Exception {
Yotam Harchol595c6442013-09-27 16:29:08 -070075 ${var_type}.Builder builder = factory.${builder_method}();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070076 ${test_data["java"]};
Andreas Wundsame0d52be2013-08-22 07:52:13 -070077 ${var_type} ${var_name}Built = builder.build();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070078
79 ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED);
80
81 // FIXME should invoke the overall reader once implemented
82 ${var_type} ${var_name}Read = ${msg.name}.READER.readFrom(input);
Andreas Wundsam45da2d42013-10-23 15:27:15 -070083 assertEquals(${msg.constant_name}_SERIALIZED.length, input.readerIndex());
Andreas Wundsame916d6f2013-07-30 11:33:58 -070084
Andreas Wundsam001b1822013-08-02 22:25:55 -070085 assertEquals(${var_name}Built, ${var_name}Read);
Andreas Wundsame916d6f2013-07-30 11:33:58 -070086 }
87 //:: else:
88 // FIXME: No java stanza in test_data for this class. Add for more comprehensive unit testing
89 //:: #endif
90
91 @Test
92 public void testReadWrite() throws Exception {
93 ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED);
94
95 // FIXME should invoke the overall reader once implemented
96 ${var_type} ${var_name} = ${msg.name}.READER.readFrom(input);
Andreas Wundsam45da2d42013-10-23 15:27:15 -070097 assertEquals(${msg.constant_name}_SERIALIZED.length, input.readerIndex());
Andreas Wundsame916d6f2013-07-30 11:33:58 -070098
99 // write message again
100 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
101 ${var_name}.writeTo(bb);
102 byte[] written = new byte[bb.readableBytes()];
103 bb.readBytes(written);
104
105 assertArrayEquals(${msg.constant_name}_SERIALIZED, written);
106 }
107
108}