/***************************************************************************
 * Copyright 2012 Kieker Project (http://kieker-monitoring.net)
 *
 * 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
 *
 *     http://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 kieker.test.analysis.junit.plugin.filter.forward;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import kieker.analysis.AnalysisController;
import kieker.analysis.exception.AnalysisConfigurationException;
import kieker.analysis.plugin.filter.forward.ListCollectionFilter;
import kieker.analysis.plugin.filter.forward.StringBufferFilter;
import kieker.analysis.plugin.reader.list.ListReader;
import kieker.common.configuration.Configuration;
import kieker.common.record.IMonitoringRecord;
import kieker.common.record.misc.EmptyRecord;

/**
 * Andre van Hoorn
 */
public class TestStringBufferFilter {
	@Test
	public void testLoggingTimestampRemains() throws IllegalStateException, AnalysisConfigurationException {
		final AnalysisController analysisController = new AnalysisController();

		final long timestamp = 3268936l;
		final IMonitoringRecord recordIn = new EmptyRecord();
		recordIn.setLoggingTimestamp(timestamp);

		final ListReader<IMonitoringRecord> reader = new ListReader<IMonitoringRecord>(new Configuration());
		reader.addObject(recordIn);
		analysisController.registerReader(reader);

		final StringBufferFilter stringBufferFilter = new StringBufferFilter(new Configuration());
		analysisController.registerFilter(stringBufferFilter);
		analysisController.connect(reader, ListReader.OUTPUT_PORT_NAME, stringBufferFilter, StringBufferFilter.INPUT_PORT_NAME_EVENTS);

		final ListCollectionFilter<IMonitoringRecord> collectionFilter = new ListCollectionFilter<IMonitoringRecord>(new Configuration());
		analysisController.registerFilter(collectionFilter);
		analysisController.connect(stringBufferFilter, StringBufferFilter.OUTPUT_PORT_NAME_RELAYED_EVENTS, collectionFilter, ListCollectionFilter.INPUT_PORT_NAME);

		analysisController.run();

		final List<IMonitoringRecord> records = collectionFilter.getList();
		Assert.assertEquals("Unexpected number of records", 1, records.size());
		final IMonitoringRecord recordOut = records.get(0);
		Assert.assertEquals("Unexpected logging timestamp", timestamp, recordOut.getLoggingTimestamp());
	}
}

@@ -1326,18 +1326,38 @@
 ord.
-misc.Empty
+controlflow.OperationExecution
 Reco
@@ -1616,22 +1616,16 @@
 r();%0A%0A%09%09
-final 
 long tim
@@ -1681,60 +1681,171 @@
 rdIn
+1
  = 
-new EmptyRecord();%0A%09%09recordIn.setLoggingTimestamp
+TestStringBufferFilter.createOperationExecutionRecord(timestamp++);%0A%09%09final IMonitoringRecord recordIn2 = TestStringBufferFilter.createOperationExecutionRecord
 (tim
@@ -1850,16 +1850,18 @@
 imestamp
+++
 );%0A%0A%09%09fi
@@ -1986,16 +1986,48 @@
 recordIn
+1);%0A%09%09reader.addObject(recordIn2
 );%0A%09%09ana
@@ -2848,17 +2848,17 @@
 cords%22, 
-1
+2
 , record
@@ -2907,68 +2907,705 @@
 dOut
- = records.get(0);%0A%09%09Assert.assertEquals(%22Unexpected
+1 = records.get(0);%0A%09%09final IMonitoringRecord recordOut2 = records.get(0);%0A%09%09Assert.assertNotSame(%22First output record same as first input record%22, recordIn2, recordOut2); // includes String, hence NOT %22as-is%22%0A%09%09Assert.assertEquals(%22First output record doesn't equal first input record%22, recordIn1, recordOut1); // ... but must be equal%0A%09%09Assert.assertNotSame(%22Second output record same as second input record%22, recordIn2, recordOut2); // includes String, hence NOT %22as-is%22%0A%09%09Assert.assertEquals(%22Second output record doesn't equal second input record%22, recordIn2, recordOut2); // ... but must be equal%0A%09%7D%0A%0A%09private static OperationExecutionRecord createOperationExecutionRecord(final long
  logging
  tim
@@ -3600,26 +3600,25 @@
  logging
- t
+T
 imestamp
 %22, times
@@ -3613,54 +3613,459 @@
 tamp
-%22, timestamp, recordOut.getLoggingTimestamp())
+) %7B%0A%09%09final OperationExecutionRecord opRec = new OperationExecutionRecord(%22mySignatureStringWhichShouldBeCached%22, OperationExecutionRecord.NO_SESSION_ID,%0A%09%09%09%09OperationExecutionRecord.NO_TRACEID, OperationExecutionRecord.NO_TIMESTAMP, OperationExecutionRecord.NO_TIMESTAMP,%0A%09%09%09%09OperationExecutionRecord.NO_HOSTNAME, OperationExecutionRecord.NO_EOI_ESS, OperationExecutionRecord.NO_EOI_ESS);%0A%09%09opRec.setLoggingTimestamp(loggingTimestamp);%0A%09%09return opRec
 ;%0A%09%7D

test to reproduce unset logging timestamps by StringBufferFilter