package ch.qos.logback.core.joran.event.stax;

import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.StatusChecker;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.util.CoreTestConstants;
import org.junit.Test;

import javax.xml.stream.events.Attribute;
import java.io.FileInputStream;
import java.util.List;

import static org.junit.Assert.*;

public class StaxEventRecorderTest {


  Context context = new ContextBase();
  StatusChecker statusChecker = new StatusChecker(context);


  public List<StaxEvent> doTest(String filename) throws Exception {
    StaxEventRecorder recorder = new StaxEventRecorder(context);
    FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX
            + "input/joran/" + filename);
    recorder.recordEvents(fis);
    return recorder.getEventList();
  }

  public void dump(List<StaxEvent> seList) {
    for (StaxEvent se : seList) {
      System.out.println(se);
    }
  }

  @Test
  public void testParsingOfXMLWithAttributesAndBodyText() throws Exception {
    List<StaxEvent> seList = doTest("event1.xml");
    assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
    //dump(seList);
    assertEquals(11, seList.size());
    assertEquals("test", seList.get(0).getName());
    assertEquals("badBegin", seList.get(1).getName());
    StartEvent startEvent = (StartEvent) seList.get(7);
    assertEquals("John Doe", startEvent.getAttributeByName("name").getValue());
    assertEquals("XXX&", ((BodyEvent) seList.get(8)).getText());
  }

  @Test
  public void testProcessingOfTextWithEntityCharacters() throws Exception {
    List<StaxEvent> seList = doTest("ampEvent.xml");
    StatusManager sm = context.getStatusManager();
    assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
    //dump(seList);
    assertEquals(3, seList.size());

    BodyEvent be = (BodyEvent) seList.get(1);
    assertEquals("xxx & yyy", be.getText());
  }

  @Test
  public void testAttributeProcessing() throws Exception {
    List<StaxEvent> seList = doTest("inc.xml");
    StatusManager sm = context.getStatusManager();
    assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
    assertEquals(4, seList.size());
    StartEvent se = (StartEvent) seList.get(1);
    Attribute attr = se.getAttributeByName("increment");
    assertNotNull(attr);
    assertEquals("1", attr.getValue());
  }

  @Test
  public void bodyWithSpacesAndQuotes() throws Exception {
    List<StaxEvent> seList = doTest("spacesAndQuotes.xml");
    assertEquals(3, seList.size());
    BodyEvent be = (BodyEvent) seList.get(1);
    assertEquals("[x][x] \"xyz\"%n", be.getText());
  }
}


