|
| 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.concurrent.UnsafeBuffer; |
| 21 | +import org.agrona.generation.CompilerUtil; |
| 22 | +import org.agrona.generation.StringWriterOutputManager; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import uk.co.real_logic.sbe.Tests; |
| 26 | +import uk.co.real_logic.sbe.ir.Ir; |
| 27 | +import uk.co.real_logic.sbe.xml.IrGenerator; |
| 28 | +import uk.co.real_logic.sbe.xml.MessageSchema; |
| 29 | +import uk.co.real_logic.sbe.xml.ParserOptions; |
| 30 | + |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | +import static org.hamcrest.Matchers.*; |
| 36 | +import static uk.co.real_logic.sbe.generation.java.ReflectionUtil.*; |
| 37 | +import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
| 38 | + |
| 39 | +public class FixedSizeDataGeneratorTest |
| 40 | +{ |
| 41 | + private static final Class<?> BUFFER_CLASS = MutableDirectBuffer.class; |
| 42 | + private static final String BUFFER_NAME = BUFFER_CLASS.getName(); |
| 43 | + private static final Class<DirectBuffer> READ_ONLY_BUFFER_CLASS = DirectBuffer.class; |
| 44 | + private static final String READ_ONLY_BUFFER_NAME = READ_ONLY_BUFFER_CLASS.getName(); |
| 45 | + |
| 46 | + private final StringWriterOutputManager outputManager = new StringWriterOutputManager(); |
| 47 | + |
| 48 | + private Ir ir; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setUp() throws Exception |
| 52 | + { |
| 53 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 54 | + final MessageSchema schema = parse(Tests.getLocalResource("extension-schema.xml"), options); |
| 55 | + final IrGenerator irg = new IrGenerator(); |
| 56 | + ir = irg.generate(schema); |
| 57 | + |
| 58 | + outputManager.clear(); |
| 59 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 60 | + |
| 61 | + generator().generate(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldGeneratePutAndGetByteArrayForFixedLengthBlob() throws Exception |
| 66 | + { |
| 67 | + final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]); |
| 68 | + |
| 69 | + final Object encoder = getTestMessage2Encoder(buffer); |
| 70 | + final Object decoder = getTestMessage2Decoder(buffer, encoder); |
| 71 | + |
| 72 | + final byte[] encodedData = " **DATA** ".getBytes(StandardCharsets.US_ASCII); |
| 73 | + byte[] decodedData; |
| 74 | + int decodedDataLength; |
| 75 | + |
| 76 | + // Every byte written, every byte read |
| 77 | + putByteArray(encoder, "putTag6", encodedData, 2, 8); |
| 78 | + decodedData = " ".getBytes(StandardCharsets.US_ASCII); |
| 79 | + decodedDataLength = getByteArray(decoder, "getTag6", decodedData, 1, 8); |
| 80 | + assertThat(decodedDataLength, is(8)); |
| 81 | + assertThat(new String(decodedData, StandardCharsets.US_ASCII), is(" **DATA** ")); |
| 82 | + |
| 83 | + // Every byte written, less bytes read |
| 84 | + putByteArray(encoder, "putTag6", encodedData, 2, 8); |
| 85 | + decodedData = " ".getBytes(StandardCharsets.US_ASCII); |
| 86 | + decodedDataLength = getByteArray(decoder, "getTag6", decodedData, 1, 6); |
| 87 | + assertThat(decodedDataLength, is(6)); |
| 88 | + assertThat(new String(decodedData, StandardCharsets.US_ASCII), is(" **DATA ")); |
| 89 | + |
| 90 | + // Less bytes written (padding), every byte read |
| 91 | + putByteArray(encoder, "putTag6", encodedData, 2, 6); |
| 92 | + decodedData = " ".getBytes(StandardCharsets.US_ASCII); |
| 93 | + decodedDataLength = getByteArray(decoder, "getTag6", decodedData, 1, 8); |
| 94 | + assertThat(decodedDataLength, is(8)); |
| 95 | + assertThat(new String(decodedData, StandardCharsets.US_ASCII), is(" **DATA\u0000\u0000 ")); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void shouldGeneratePutAndGetDirectBufferForFixedLengthBlob() throws Exception |
| 100 | + { |
| 101 | + final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]); |
| 102 | + |
| 103 | + final Object encoder = getTestMessage2Encoder(buffer); |
| 104 | + final Object decoder = getTestMessage2Decoder(buffer, encoder); |
| 105 | + |
| 106 | + final UnsafeBuffer encodedData = new UnsafeBuffer(" **DATA** ".getBytes(StandardCharsets.US_ASCII)); |
| 107 | + UnsafeBuffer decodedData; |
| 108 | + int decodedDataLength; |
| 109 | + |
| 110 | + // Every byte written, every byte read |
| 111 | + putDirectBuffer(encoder, "putTag6", encodedData, 2, 8); |
| 112 | + decodedData = new UnsafeBuffer(" ".getBytes(StandardCharsets.US_ASCII)); |
| 113 | + decodedDataLength = getDirectBuffer(decoder, "getTag6", decodedData, 1, 8); |
| 114 | + assertThat(decodedDataLength, is(8)); |
| 115 | + assertThat(decodedData.getStringWithoutLengthAscii(0, 12), is(" **DATA** ")); |
| 116 | + |
| 117 | + // Every byte written, less bytes read |
| 118 | + putDirectBuffer(encoder, "putTag6", encodedData, 2, 8); |
| 119 | + decodedData = new UnsafeBuffer(" ".getBytes(StandardCharsets.US_ASCII)); |
| 120 | + decodedDataLength = getDirectBuffer(decoder, "getTag6", decodedData, 1, 6); |
| 121 | + assertThat(decodedDataLength, is(6)); |
| 122 | + assertThat(decodedData.getStringWithoutLengthAscii(0, 12), is(" **DATA ")); |
| 123 | + |
| 124 | + // Less bytes written (padding), every byte read |
| 125 | + putDirectBuffer(encoder, "putTag6", encodedData, 2, 6); |
| 126 | + decodedData = new UnsafeBuffer(" ".getBytes(StandardCharsets.US_ASCII)); |
| 127 | + decodedDataLength = getDirectBuffer(decoder, "getTag6", decodedData, 1, 8); |
| 128 | + assertThat(decodedDataLength, is(8)); |
| 129 | + assertThat(decodedData.getStringWithoutLengthAscii(0, 12), is(" **DATA\u0000\u0000 ")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void shouldGenerateWrapForFixedLengthBlob() throws Exception |
| 134 | + { |
| 135 | + final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]); |
| 136 | + |
| 137 | + final Object encoder = getTestMessage2Encoder(buffer); |
| 138 | + final Object decoder = getTestMessage2Decoder(buffer, encoder); |
| 139 | + |
| 140 | + final UnsafeBuffer encodedData = new UnsafeBuffer(" **DATA** ".getBytes(StandardCharsets.US_ASCII)); |
| 141 | + putDirectBuffer(encoder, "putTag6", encodedData, 2, 8); |
| 142 | + |
| 143 | + final UnsafeBuffer decodedData = new UnsafeBuffer(); |
| 144 | + wrapDirectBuffer(decoder, "wrapTag6", decodedData); |
| 145 | + assertThat(decodedData.getStringWithoutLengthAscii(0, decodedData.capacity()), is("**DATA**")); |
| 146 | + } |
| 147 | + |
| 148 | + private JavaGenerator generator() |
| 149 | + { |
| 150 | + return new JavaGenerator(ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, outputManager); |
| 151 | + } |
| 152 | + |
| 153 | + private Class<?> compile(final String className) throws Exception |
| 154 | + { |
| 155 | + final String fqClassName = ir.applicableNamespace() + "." + className; |
| 156 | + final Map<String, CharSequence> sources = outputManager.getSources(); |
| 157 | + final Class<?> aClass = CompilerUtil.compileInMemory(fqClassName, sources); |
| 158 | + if (aClass == null) |
| 159 | + { |
| 160 | + System.out.println(sources); |
| 161 | + } |
| 162 | + |
| 163 | + return aClass; |
| 164 | + } |
| 165 | + |
| 166 | + private Object getTestMessage2Encoder(final UnsafeBuffer buffer) throws Exception |
| 167 | + { |
| 168 | + final Object encoder = compile("TestMessage2Encoder").getConstructor().newInstance(); |
| 169 | + return wrap(0, encoder, buffer, BUFFER_CLASS); |
| 170 | + } |
| 171 | + |
| 172 | + private Object getTestMessage2Decoder(final UnsafeBuffer buffer, final Object encoder) throws Exception |
| 173 | + { |
| 174 | + final Object decoder = compile("TestMessage2Decoder").getConstructor().newInstance(); |
| 175 | + return wrap(buffer, decoder, getSbeBlockLength(encoder), getSbeSchemaVersion(encoder)); |
| 176 | + } |
| 177 | + |
| 178 | + private static Object wrap( |
| 179 | + final UnsafeBuffer buffer, final Object decoder, final int blockLength, final int version) throws Exception |
| 180 | + { |
| 181 | + decoder |
| 182 | + .getClass() |
| 183 | + .getMethod("wrap", READ_ONLY_BUFFER_CLASS, int.class, int.class, int.class) |
| 184 | + .invoke(decoder, buffer, 0, blockLength, version); |
| 185 | + |
| 186 | + return decoder; |
| 187 | + } |
| 188 | + |
| 189 | + private static Object wrap( |
| 190 | + final int bufferOffset, final Object flyweight, final MutableDirectBuffer buffer, final Class<?> bufferClass) |
| 191 | + throws Exception |
| 192 | + { |
| 193 | + flyweight |
| 194 | + .getClass() |
| 195 | + .getDeclaredMethod("wrap", bufferClass, int.class) |
| 196 | + .invoke(flyweight, buffer, bufferOffset); |
| 197 | + return flyweight; |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments