blob: 47dcc2463014c0767a55c380361813d830e9e0dc [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
31//:: include('_copyright.java')
32
33//:: include('_autogen.java')
34
35package ${test.package};
36
37//:: include("_imports.java", msg=msg)
38import org.junit.Before;
39import org.junit.Test;
40import static org.junit.Assert.*;
41
42public class ${test.name} {
43 //:: var_type = msg.interface.name
44 //:: var_name = msg.interface.variable_name
45 OFFactory factory;
46
47 final static byte[] ${msg.constant_name}_SERIALIZED =
48 new byte[] { ${", ".join("%s0x%x" % (("" if ord(c)<128 else "(byte) "), ord(c)) for c in test_data["binary"] ) } };
49
50 @Before
51 public void setup() {
52 factory = OFFactories.getFactory(OFVersion.${version.constant_version});
53 }
54
55 //:: if "java" in test_data:
56 @Test
57 public void testWrite() {
Andreas Wundsame0d52be2013-08-22 07:52:13 -070058 ${var_type}.Builder builder = factory.build${var_type[2:]}();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070059 ${test_data["java"]};
Andreas Wundsame0d52be2013-08-22 07:52:13 -070060 ${var_type} ${var_name} = builder.build();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070061 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
62 ${var_name}.writeTo(bb);
63 byte[] written = new byte[bb.readableBytes()];
64 bb.readBytes(written);
65
66 assertArrayEquals(${msg.constant_name}_SERIALIZED, written);
67 }
68
69 @Test
70 public void testRead() throws Exception {
Andreas Wundsame0d52be2013-08-22 07:52:13 -070071 ${var_type}.Builder builder = factory.build${var_type[2:]}();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070072 ${test_data["java"]};
Andreas Wundsame0d52be2013-08-22 07:52:13 -070073 ${var_type} ${var_name}Built = builder.build();
Andreas Wundsame916d6f2013-07-30 11:33:58 -070074
75 ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED);
76
77 // FIXME should invoke the overall reader once implemented
78 ${var_type} ${var_name}Read = ${msg.name}.READER.readFrom(input);
79
Andreas Wundsam001b1822013-08-02 22:25:55 -070080 assertEquals(${var_name}Built, ${var_name}Read);
Andreas Wundsame916d6f2013-07-30 11:33:58 -070081 }
82 //:: else:
83 // FIXME: No java stanza in test_data for this class. Add for more comprehensive unit testing
84 //:: #endif
85
86 @Test
87 public void testReadWrite() throws Exception {
88 ChannelBuffer input = ChannelBuffers.copiedBuffer(${msg.constant_name}_SERIALIZED);
89
90 // FIXME should invoke the overall reader once implemented
91 ${var_type} ${var_name} = ${msg.name}.READER.readFrom(input);
92
93 // write message again
94 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
95 ${var_name}.writeTo(bb);
96 byte[] written = new byte[bb.readableBytes()];
97 bb.readBytes(written);
98
99 assertArrayEquals(${msg.constant_name}_SERIALIZED, written);
100 }
101
102}