|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 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.SbeTool; |
| 23 | +import uk.co.real_logic.sbe.Tests; |
| 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 | +import uk.co.real_logic.sbe.xml.XmlSchemaParser; |
| 29 | + |
| 30 | +import java.io.InputStream; |
| 31 | + |
| 32 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | +import static org.hamcrest.core.StringContains.containsString; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.fail; |
| 36 | + |
| 37 | +class EnumTest |
| 38 | +{ |
| 39 | + private static final Class<?> BUFFER_CLASS = MutableDirectBuffer.class; |
| 40 | + private static final String BUFFER_NAME = BUFFER_CLASS.getName(); |
| 41 | + private static final Class<DirectBuffer> READ_ONLY_BUFFER_CLASS = DirectBuffer.class; |
| 42 | + private static final String READ_ONLY_BUFFER_NAME = READ_ONLY_BUFFER_CLASS.getName(); |
| 43 | + |
| 44 | + private StringWriterOutputManager outputManager; |
| 45 | + private JavaGenerator generator; |
| 46 | + |
| 47 | + private void setupGenerator(final InputStream in) throws Exception |
| 48 | + { |
| 49 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 50 | + final MessageSchema schema = XmlSchemaParser.parse(in, options); |
| 51 | + final IrGenerator irg = new IrGenerator(); |
| 52 | + final Ir ir = irg.generate(schema); |
| 53 | + |
| 54 | + outputManager = new StringWriterOutputManager(); |
| 55 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 56 | + generator = new JavaGenerator( |
| 57 | + ir, BUFFER_NAME, READ_ONLY_BUFFER_NAME, false, false, false, outputManager); |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings("checkstyle:LineLength") |
| 61 | + @Test |
| 62 | + void shouldFailOnKeywordEnumValues() throws Exception |
| 63 | + { |
| 64 | + try (InputStream in = Tests.getLocalResource("issue1007.xml")) |
| 65 | + { |
| 66 | + setupGenerator(in); |
| 67 | + |
| 68 | + try |
| 69 | + { |
| 70 | + generator.generate(); |
| 71 | + } |
| 72 | + catch (final IllegalStateException exception) |
| 73 | + { |
| 74 | + assertEquals( |
| 75 | + "Invalid property name='false' please correct the schema or consider setting system property: sbe.keyword.append.token", |
| 76 | + exception.getMessage()); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + fail("expected IllegalStateException"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void shouldAddSuffixToEnumValues() throws Exception |
| 86 | + { |
| 87 | + System.setProperty(SbeTool.KEYWORD_APPEND_TOKEN, "_"); |
| 88 | + |
| 89 | + try (InputStream in = Tests.getLocalResource("issue1007.xml")) |
| 90 | + { |
| 91 | + setupGenerator(in); |
| 92 | + |
| 93 | + generator.generate(); |
| 94 | + final String sources = outputManager.getSources().toString(); |
| 95 | + |
| 96 | + assertThat(sources, containsString("false_(")); |
| 97 | + assertThat(sources, containsString("true_(")); |
| 98 | + assertThat(sources, containsString("return false_;")); |
| 99 | + assertThat(sources, containsString("return true_;")); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments