/*
 * Copyright 2013-2021 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.sbe.generation.csharp;

import org.agrona.generation.StringWriterOutputManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import uk.co.real_logic.sbe.Tests;
import uk.co.real_logic.sbe.ir.Ir;
import uk.co.real_logic.sbe.xml.IrGenerator;
import uk.co.real_logic.sbe.xml.MessageSchema;
import uk.co.real_logic.sbe.xml.ParserOptions;

import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;

@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_17)
public class Issue567GroupSizeTest
{
    public static final String ERR_MSG =
        "WARNING: at <sbe:message name=\"issue567\"> <group name=\"group\"> \"numInGroup\" should be UINT8 or UINT16";

    private final PrintStream mockErr = mock(PrintStream.class);
    private PrintStream err;

    @BeforeEach
    public void before()
    {
        err = System.err;
        System.setErr(mockErr);
    }

    @AfterEach
    public void after()
    {
        System.setErr(err);
        verify(mockErr).println(ERR_MSG);
    }

    @Test
    public void shouldThrowWhenUsingATypeThatIsNotConstrainedToFitInAnIntAsTheGroupSize()
    {
        final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
        assertThrows(IllegalArgumentException.class,
            () -> parse(Tests.getLocalResource("issue567-invalid.xml"), options));
    }

    @Test
    public void shouldGenerateWhenUsingATypeThatIsConstrainedToFitInAnIntAsTheGroupSize() throws Exception
    {
        final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
        final MessageSchema schema = parse(Tests.getLocalResource("issue567-valid.xml"), options);
        final IrGenerator irg = new IrGenerator();
        final Ir ir = irg.generate(schema);

        final StringWriterOutputManager outputManager = new StringWriterOutputManager();
        outputManager.setPackageName(ir.applicableNamespace());
        final CSharpGenerator generator = new CSharpGenerator(ir, outputManager);

        // Act + Assert (no exception)
        generator.generate();
    }
}

@@ -834,98 +834,27 @@
 org.
-junit.jupiter.api.condition.EnabledForJreRange;%0Aimport org.junit.jupiter.api.condition.JRE
+xml.sax.InputSource
 ;%0Aim
@@ -1061,16 +1061,72 @@
 tions;%0A%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0A
 import j
@@ -1356,65 +1356,8 @@
 e;%0A%0A
-@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_17)%0A
 publ
@@ -1978,16 +1978,35 @@
 upSize()
+ throws IOException
 %0A    %7B%0A 
@@ -2085,32 +2085,197 @@
 (true).build();%0A
+        final InputStream in = Tests.getLocalResource(%22issue567-invalid.xml%22);%0A        final InputSource is = new InputSource(in);%0A        is.setEncoding(%22UTF-8%22);%0A%0A
         assertTh
@@ -2314,20 +2314,8 @@
 ass,
-%0A           
  () 
@@ -2327,54 +2327,10 @@
 rse(
-Tests.getLocalResource(%22issue567-invalid.xml%22)
+is
 , op
@@ -2575,37 +2575,25 @@
 nal 
-MessageSchema schema = parse(
+InputStream in = 
 Test
@@ -2632,16 +2632,149 @@
 id.xml%22)
+;%0A        final InputSource is = new InputSource(in);%0A        is.setEncoding(%22UTF-8%22);%0A%0A        final MessageSchema schema = parse(is
 , option

Set encoding type for test to see if it works for charset changes in Java 18.