|
| 1 | +/* |
| 2 | + * Copyright 2013-2020 Real Logic Limited. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package uk.co.real_logic.sbe.generation.java; |
| 17 | + |
| 18 | +import org.agrona.DirectBuffer; |
| 19 | +import org.agrona.MutableDirectBuffer; |
| 20 | +import org.agrona.generation.CompilerUtil; |
| 21 | +import org.agrona.generation.StringWriterOutputManager; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import uk.co.real_logic.sbe.SbeTool; |
| 24 | +import uk.co.real_logic.sbe.Tests; |
| 25 | +import uk.co.real_logic.sbe.ir.*; |
| 26 | +import uk.co.real_logic.sbe.xml.*; |
| 27 | + |
| 28 | +import java.nio.ByteBuffer; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
| 34 | + |
| 35 | +public class GenerateIlinkBinaryTest |
| 36 | +{ |
| 37 | + private static final Class<?> BUFFER_CLASS = MutableDirectBuffer.class; |
| 38 | + private static final String BUFFER_NAME = BUFFER_CLASS.getName(); |
| 39 | + private static final Class<DirectBuffer> READ_ONLY_BUFFER_CLASS = DirectBuffer.class; |
| 40 | + private static final String READ_ONLY_BUFFER_NAME = READ_ONLY_BUFFER_CLASS.getName(); |
| 41 | + |
| 42 | + private final StringWriterOutputManager outputManager = new StringWriterOutputManager(); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldGenerateValidJava() throws Exception |
| 46 | + { |
| 47 | + System.setProperty(SbeTool.KEYWORD_APPEND_TOKEN, "_"); |
| 48 | + |
| 49 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 50 | + final MessageSchema schema = parse(Tests.getLocalResource("ilinkbinary.xml"), options); |
| 51 | + final IrGenerator irg = new IrGenerator(); |
| 52 | + final Ir ir = irg.generate(schema); |
| 53 | + final JavaGenerator generator = new JavaGenerator( |
| 54 | + ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, outputManager); |
| 55 | + |
| 56 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 57 | + generator.generateMessageHeaderStub(); |
| 58 | + generator.generateTypeStubs(); |
| 59 | + generator.generate(); |
| 60 | + |
| 61 | + final Map<String, CharSequence> sources = outputManager.getSources(); |
| 62 | + |
| 63 | + { |
| 64 | + final String className = "NewOrderSingle514Encoder"; |
| 65 | + final String fqClassName = ir.applicableNamespace() + "." + className; |
| 66 | + final Class<?> aClass = CompilerUtil.compileInMemory(fqClassName, sources); |
| 67 | + assertNotNull(aClass); |
| 68 | + } |
| 69 | + |
| 70 | + { |
| 71 | + final String className = "NewOrderSingle514Decoder"; |
| 72 | + final String fqClassName = ir.applicableNamespace() + "." + className; |
| 73 | + final Class<?> aClass = CompilerUtil.compileInMemory(fqClassName, sources); |
| 74 | + assertNotNull(aClass); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldGenerateAndEncodeIr() throws Exception |
| 80 | + { |
| 81 | + System.setProperty(SbeTool.KEYWORD_APPEND_TOKEN, "_"); |
| 82 | + |
| 83 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 84 | + final MessageSchema schema = parse(Tests.getLocalResource("ilinkbinary.xml"), options); |
| 85 | + final IrGenerator irg = new IrGenerator(); |
| 86 | + final Ir ir = irg.generate(schema); |
| 87 | + final ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); |
| 88 | + |
| 89 | + final IrEncoder irEncoder = new IrEncoder(buffer, ir); |
| 90 | + irEncoder.encode(); |
| 91 | + |
| 92 | + buffer.flip(); |
| 93 | + final IrDecoder irDecoder = new IrDecoder(buffer); |
| 94 | + final Ir decodedIr = irDecoder.decode(); |
| 95 | + |
| 96 | + assertEquals(ir.id(), decodedIr.id()); |
| 97 | + assertEquals(ir.version(), decodedIr.version()); |
| 98 | + assertEquals(ir.byteOrder(), decodedIr.byteOrder()); |
| 99 | + assertEquals(ir.applicableNamespace(), decodedIr.applicableNamespace()); |
| 100 | + assertEquals(ir.packageName(), decodedIr.packageName()); |
| 101 | + assertEquals(ir.types().size(), decodedIr.types().size()); |
| 102 | + assertEquals(ir.messages().size(), decodedIr.messages().size()); |
| 103 | + } |
| 104 | +} |
0 commit comments