<commit_before>package com.cosylab.logging.engine.ACS;

import java.util.Vector;

import junit.framework.TestCase;

import alma.acs.logging.engine.parser.ACSLogParser;
import alma.acs.logging.engine.parser.ACSLogParserFactory;

import com.cosylab.logging.engine.log.ILogEntry;
import com.cosylab.logging.engine.log.LogTypeHelper;
import com.cosylab.logging.engine.log.ILogEntry.Field;

public class ACSLogParserTest extends TestCase {

	private ACSLogParser parser;

	private final String xmlLogInfo1 = 
		"<Info TimeStamp=\"2006-03-28T00:26:29.238\" File=\"maciContainerImpl.cpp\" Line=\"1454\" " + 
		"Routine=\"maci::ContainerImpl::initThread\" Host=\"gas\" Process=\"maciContainer\" Thread=\"ARCHIVE_BULKSENDER::actionThread\" " + 
		"Context=\"\" SourceObject=\"ARCHIVE_BULKSENDER::actionThread\">" + 
		"<![CDATA[Thread name: 'ARCHIVE_BULKSENDER::actionThread']]>" + 
		"</Info>";
	
	private final String xmlLogInfo2 = 
		"<Info TimeStamp=\"2006-03-28T00:26:29.239\" File=\"maciContainerImpl.cpp\" Line=\"1454\" " + 
		"Routine=\"maci::ContainerImpl::initThread\" Host=\"gas\" Process=\"maciContainer\" Thread=\"ARCHIVE_BULKSENDER::monitorThread\" " + 
		"Context=\"\" SourceObject=\"ARCHIVE_BULKSENDER::monitorThread\">" + 
		"<![CDATA[Thread name: 'ARCHIVE_BULKSENDER::monitorThread']]>" + 
		"</Info>";
	
	private final String xmlLogWarningWithException = 
		"<Warning TimeStamp=\"2006-03-28T00:26:46.149\" " + 
		"File=\"alma.acs.container.ContainerSealant\" Line=\"184\" Routine=\"invoke\" Host=\"gas\" " + 
		"Process=\"LoggerName: alma.acs.container.ARCHIVE/ACC/javaContainer\" SourceObject=\"ARCHIVE/ACC/javaContainer\" " + 
		"Thread=\"RequestProcessor-15\" StackId=\"unknown\" StackLevel=\"0\" LogId=\"298\">" + 
		"<![CDATA[checked exception was thrown in functional method 'ARCHIVE_CONNECTION/alma.xmlstore.Operational#retrieve':]]>" +
		"<Data Name=\"LoggedException\"><![CDATA[alma.xmlstore.OperationalPackage.NotFound: uid://X00000000000028aa/X00000002" + "\n" +
		"	at alma.archive.components.OperationalImpl.retrieve(OperationalImpl.java:450)" + "\n" +
		"	at alma.archive.components.OperationalImpl.retrieve(OperationalImpl.java:480)" + "\n" +
		"	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)" + "\n" +
		"	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)" + "\n" +
		"	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)" + "\n" +
		"	at java.lang.reflect.Method.invoke(Method.java:585)" + "\n" +
		"	at alma.acs.container.ContainerSealant.invoke(ContainerSealant.java:155)" + "\n" +
		"	at $Proxy3.retrieve(Unknown Source)" + "\n" +
		"	at alma.xmlstore.OperationalPOATie.retrieve(OperationalPOATie.java:103)" + "\n" +
		"	at alma.xmlstore.OperationalPOA._invoke(OperationalPOA.java:316)" + "\n" +
		"	at org.jacorb.poa.RequestProcessor.invokeOperation(Unknown Source)" + "\n" +
		"	at org.jacorb.poa.RequestProcessor.process(Unknown Source)" + "\n" +	
		"	at org.jacorb.poa.RequestProcessor.run(Unknown Source)" + "\n" +
		"]]></Data></Warning>";


	
	protected void setUp() throws Exception {
		super.setUp();
		parser = ACSLogParserFactory.getParser();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	
	/**
	 * Parses one log record from XML and verifies a few fields, 
	 * including the exception details that are attached as additional data.
	 * 
	 * @throws Exception
	 */
	public void testParseLogRecord() throws Exception {
		ILogEntry log = parser.parse(xmlLogWarningWithException);
		
		// verify some fields
		assertEquals("wrong typename string", "Warning", ((LogTypeHelper)log.getField(Field.ENTRYTYPE)).logEntryType);
		assertEquals("wrong type code", LogTypeHelper.WARNING.ordinal(), ((LogTypeHelper)log.getField(Field.ENTRYTYPE)).ordinal());
		
		Vector<ILogEntry.AdditionalData> datas = log.getAdditionalData();
		assertFalse("There should have been 1 piece of attached data!", datas == null || datas.size() != 1);
		Vector<ILogEntry.AdditionalData> additionalData = log.getAdditionalData();
		assertFalse("There should have been 1 piece of additional data!", additionalData == null || additionalData.size() != 1);
		ILogEntry.AdditionalData myData = additionalData.get(0);
		assertEquals("LoggedException", myData.getName());
		assertTrue(myData.getValue().startsWith("alma.xmlstore.OperationalPackage.NotFound: uid://X00000000000028aa/X00000002"));
	}
}<commit_msg>Test improved by checking all the fields read from the XML and stressing the parser<commit_after>package com.cosylab.logging.engine.ACS;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import junit.framework.TestCase;

import alma.acs.logging.engine.parser.ACSLogParser;
import alma.acs.logging.engine.parser.ACSLogParserFactory;
import alma.acs.util.IsoDateFormat;

import com.cosylab.logging.engine.log.ILogEntry;
import com.cosylab.logging.engine.log.LogTypeHelper;
import com.cosylab.logging.engine.log.ILogEntry.Field;

public class ACSLogParserTest extends TestCase {

	private ACSLogParser parser;

	private final String xmlLogInfo1 = 
		"<Info TimeStamp=\"2006-03-28T00:26:29.238\" File=\"maciContainerImpl.cpp\" Line=\"1454\" " + 
		"Routine=\"maci::ContainerImpl::initThread\" Host=\"gas\" Process=\"maciContainer\" Thread=\"ARCHIVE_BULKSENDER::actionThread\" " + 
		"Context=\"CTXT\" SourceObject=\"ARCHIVE_BULKSENDER::source\" Audience=\"Operator\" Array=\"AnArray\" Antenna=\"ThisIsTheAntenna\""+
		" StackLevel=\"10\" StackId=\"TheStackID\" Priority=\"3\">" + 
		"<![CDATA[Thread name: 'ARCHIVE_BULKSENDER::actionThread']]>" +
		"</Info>";
	
	private final String xmlLogInfo2 = 
		"<Info TimeStamp=\"2006-03-28T00:26:29.239\" File=\"maciContainerImpl.cpp\" Line=\"1454\" " + 
		"Routine=\"maci::ContainerImpl::initThread\" Host=\"gas\" Process=\"maciContainer\" Thread=\"ARCHIVE_BULKSENDER::monitorThread\" " + 
		"Context=\"\" SourceObject=\"ARCHIVE_BULKSENDER::monitorThread\">" + 
		"<![CDATA[Thread name: 'ARCHIVE_BULKSENDER::monitorThread']]>" + 
		"</Info>";
	
	private final String xmlLogWarningWithException = 
		"<Warning TimeStamp=\"2006-03-28T00:26:46.149\" " + 
		"File=\"alma.acs.container.ContainerSealant\" Line=\"184\" Routine=\"invoke\" Host=\"gas\" " + 
		"Process=\"LoggerName: alma.acs.container.ARCHIVE/ACC/javaContainer\" SourceObject=\"ARCHIVE/ACC/javaContainer\" " + 
		"Thread=\"RequestProcessor-15\" StackId=\"unknown\" StackLevel=\"0\" LogId=\"298\">" + 
		"<![CDATA[checked exception was thrown in functional method 'ARCHIVE_CONNECTION/alma.xmlstore.Operational#retrieve':]]>" +
		"<Data Name=\"LoggedException\"><![CDATA[alma.xmlstore.OperationalPackage.NotFound: uid://X00000000000028aa/X00000002" + "\n" +
		"	at alma.archive.components.OperationalImpl.retrieve(OperationalImpl.java:450)" + "\n" +
		"	at alma.archive.components.OperationalImpl.retrieve(OperationalImpl.java:480)" + "\n" +
		"	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)" + "\n" +
		"	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)" + "\n" +
		"	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)" + "\n" +
		"	at java.lang.reflect.Method.invoke(Method.java:585)" + "\n" +
		"	at alma.acs.container.ContainerSealant.invoke(ContainerSealant.java:155)" + "\n" +
		"	at $Proxy3.retrieve(Unknown Source)" + "\n" +
		"	at alma.xmlstore.OperationalPOATie.retrieve(OperationalPOATie.java:103)" + "\n" +
		"	at alma.xmlstore.OperationalPOA._invoke(OperationalPOA.java:316)" + "\n" +
		"	at org.jacorb.poa.RequestProcessor.invokeOperation(Unknown Source)" + "\n" +
		"	at org.jacorb.poa.RequestProcessor.process(Unknown Source)" + "\n" +	
		"	at org.jacorb.poa.RequestProcessor.run(Unknown Source)" + "\n" +
		"]]></Data><Data Name=\"Pippo\"><![CDATA[Pluto]]></Data></Warning>";


	
	protected void setUp() throws Exception {
		super.setUp();
		parser = ACSLogParserFactory.getParser();
		assertNotNull(parser);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	
	/**
	 * Parses one log record from XML and verifies a few fields, 
	 * including the exception details that are attached as additional data.
	 * 
	 * @throws Exception
	 */
	public void testParseLogRecord() throws Exception {
		ILogEntry log = parser.parse(xmlLogWarningWithException);
		
		// verify some fields
		assertEquals("wrong typename string", "Warning", ((LogTypeHelper)log.getField(Field.ENTRYTYPE)).logEntryType);
		assertEquals("wrong type code", LogTypeHelper.WARNING, ((LogTypeHelper)log.getField(Field.ENTRYTYPE)));
		
		Vector<ILogEntry.AdditionalData> datas = log.getAdditionalData();
		assertFalse("There should have been 2 pieces of attached data!", datas == null || datas.size() != 2);
		Vector<ILogEntry.AdditionalData> additionalData = log.getAdditionalData();
		assertFalse("There should have been 2 pieces of additional data!", additionalData == null || additionalData.size() != 2);
		ILogEntry.AdditionalData myData = additionalData.get(0);
		assertEquals("LoggedException", myData.getName());
		assertTrue(myData.getValue().startsWith("alma.xmlstore.OperationalPackage.NotFound: uid://X00000000000028aa/X00000002"));
	}
	
	/**
	 * Check if the fields are read as expected
	 * 
	 * @throws Exception
	 */
	public void testFields() throws Exception {
		ILogEntry log = parser.parse(xmlLogInfo1);
		assertNotNull(log);
		
		// Check the date
		Long logDate = (Long)log.getField(Field.TIMESTAMP);
		assertNotNull(logDate);
		Long xmlDate=null;
		SimpleDateFormat dateFormat = new IsoDateFormat();
		Date date = dateFormat.parse("2006-03-28T00:26:29.238");
		xmlDate = Long.valueOf(date.getTime());
		assertEquals("The dates differ", xmlDate,logDate);
		
		// Check the log type
		assertEquals(LogTypeHelper.INFO,(LogTypeHelper)log.getField(Field.ENTRYTYPE));
		
		// Check the file
		assertEquals("maciContainerImpl.cpp", log.getField(Field.FILE));
		
		// Check the line
		assertEquals(Integer.valueOf(1454), log.getField(Field.LINE));
		
		// Check the routine
		assertEquals("maci::ContainerImpl::initThread",log.getField(Field.ROUTINE));
		
		// Check the host
		assertEquals("gas",log.getField(Field.HOST));
		
		// Check the process
		assertEquals("maciContainer",log.getField(Field.PROCESS));
		
		// Check the thread
		assertEquals("ARCHIVE_BULKSENDER::actionThread",log.getField(Field.THREAD));
		
		// Check the Antenna
		assertEquals("CTXT", log.getField(Field.CONTEXT));
		
		// Check the source object
		assertEquals("ARCHIVE_BULKSENDER::source",log.getField(Field.SOURCEOBJECT));
		
		// Check the stack level
		assertEquals(10,log.getField(Field.STACKLEVEL));
		
		// Check the stack level
		assertEquals(3,log.getField(Field.PRIORITY));
		
		// Check the stack level
		assertEquals("TheStackID",log.getField(Field.STACKID));
		
		// Check the Audience
		assertEquals("Operator",log.getField(Field.AUDIENCE));
		
		// Check the Array
		assertEquals("AnArray", log.getField(Field.ARRAY));
		
		// Check the Antenna
		assertEquals("ThisIsTheAntenna", log.getField(Field.ANTENNA));
	}
	
	/**
	 * Call the parser several times
	 * 
	 * @throws Exception
	 */
	public void testMultipleParse() throws Exception {
		String[] logs = new String[3*1000];
		for (int t=0; t<logs.length; t+=3) {
			logs[t]=xmlLogInfo1;
			logs[t+1]=xmlLogInfo2;
			logs[t+2]=xmlLogWarningWithException;
		}
		for (String xmlLog: logs) {
			ILogEntry log = parser.parse(xmlLog);
			assertNotNull(log);
		}
	}
}
{'fraction_non_alphanumeric': 0.15746314228386984, 'fraction_numerical': 0.02835208933089069, 'mean_word_length': 5.0336842105263155, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337009', 'n_tokens_mistral': 4157, 'n_tokens_neox': 3621, 'n_words': 633}