|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 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.StringWriterOutputManager; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import uk.co.real_logic.sbe.Tests; |
| 23 | +import uk.co.real_logic.sbe.generation.common.PrecedenceChecks; |
| 24 | +import uk.co.real_logic.sbe.ir.Ir; |
| 25 | +import uk.co.real_logic.sbe.xml.IrGenerator; |
| 26 | +import uk.co.real_logic.sbe.xml.MessageSchema; |
| 27 | +import uk.co.real_logic.sbe.xml.ParserOptions; |
| 28 | + |
| 29 | +import java.io.InputStream; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.TreeMap; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests that generated code is deterministic when precedence checks are enabled. |
| 38 | + */ |
| 39 | +class JavaGeneratorDeterminismTest |
| 40 | +{ |
| 41 | + private static final String BUFFER_NAME = MutableDirectBuffer.class.getName(); |
| 42 | + private static final String READ_ONLY_BUFFER_NAME = DirectBuffer.class.getName(); |
| 43 | + |
| 44 | + @Test |
| 45 | + void shouldGenerateDeterministicOutputWithPrecedenceChecks() throws Exception |
| 46 | + { |
| 47 | + final Map<String, CharSequence> firstGeneration = generateWithPrecedenceChecks(); |
| 48 | + final Map<String, CharSequence> secondGeneration = generateWithPrecedenceChecks(); |
| 49 | + |
| 50 | + assertEquals(firstGeneration.keySet(), secondGeneration.keySet()); |
| 51 | + |
| 52 | + for (final String fileName : firstGeneration.keySet()) |
| 53 | + { |
| 54 | + final CharSequence firstSource = firstGeneration.get(fileName); |
| 55 | + final CharSequence secondSource = secondGeneration.get(fileName); |
| 56 | + assertEquals(firstSource, secondSource); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private Map<String, CharSequence> generateWithPrecedenceChecks() throws Exception |
| 61 | + { |
| 62 | + try (InputStream in = Tests.getLocalResource("field-order-check-schema.xml")) |
| 63 | + { |
| 64 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 65 | + final MessageSchema schema = parse(in, options); |
| 66 | + final IrGenerator irg = new IrGenerator(); |
| 67 | + final Ir ir = irg.generate(schema); |
| 68 | + final StringWriterOutputManager outputManager = new StringWriterOutputManager(); |
| 69 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 70 | + final PrecedenceChecks.Context context = new PrecedenceChecks.Context() |
| 71 | + .shouldGeneratePrecedenceChecks(true); |
| 72 | + final PrecedenceChecks precedenceChecks = PrecedenceChecks.newInstance(context); |
| 73 | + final JavaGenerator generator = new JavaGenerator( |
| 74 | + ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, false, precedenceChecks, outputManager); |
| 75 | + generator.generate(); |
| 76 | + return new TreeMap<>(outputManager.getSources()); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments