blob: 1462465ca5b2790eb5376174be23c0fdd3ac70ff [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() {
58 ${var_type}.Builder builder = factory.create${var_type[2:]}Builder();
59 ${test_data["java"]};
60 ${var_type} ${var_name} = builder.getMessage();
61 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 {
71 ${var_type}.Builder builder = factory.create${var_type[2:]}Builder();
72 ${test_data["java"]};
73 ${var_type} ${var_name}Built = builder.getMessage();
74
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
80 assertEquals(${var_name}Read, ${var_name}Built);
81 }
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}