package de.tudresden.inf.lat.tabula.main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

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

import de.tudresden.inf.lat.tabula.datatype.CompositeType;
import de.tudresden.inf.lat.tabula.datatype.CompositeTypeImpl;
import de.tudresden.inf.lat.tabula.datatype.StringValue;
import de.tudresden.inf.lat.tabula.parser.SimpleFormatParser;
import de.tudresden.inf.lat.tabula.renderer.SimpleFormatRenderer;
import de.tudresden.inf.lat.tabula.table.Table;
import de.tudresden.inf.lat.tabula.table.TableImpl;
import de.tudresden.inf.lat.tabula.table.TableMap;
import de.tudresden.inf.lat.tabula.table.TableMapImpl;

/**
 * This is a test of modification of a Tabula file.
 */
public class MainTest {

	public static final String INPUT_FILE_NAME = "src/test/resources/example.properties";
	public static final String EXPECTED_OUTPUT_FILE_NAME = "src/test/resources/example-modified.properties";

	public static final String FIELD_AUTHORS = "authors";
	public static final String FIELD_RECORD = "record";
	public static final String FIELD_NUMBER_OF_AUTHORS = "numberOfAuthors";
	public static final String TYPE_OF_NUMBER_OF_AUTHORS = "String";
	public static final String NEWLINE = "\n";

	@Test
	public void addNewFieldOldTest() throws IOException {

		// This is an example of source code where the number of authors is
		// a computed value

		// Read the table map
		TableMap oldTableMap = new SimpleFormatParser(new FileReader(INPUT_FILE_NAME)).parse();

		// Make a copy of the tableMap
		TableMapImpl newTableMap = new TableMapImpl();
		oldTableMap.getTableIds().forEach(tableId -> newTableMap.put(tableId, oldTableMap.getTable(tableId)));

		// Get the main table
		Table table = newTableMap.getTable(FIELD_RECORD);

		// Make a copy of the main table
		TableImpl newTable = new TableImpl(table);

		// Add the new table to the new table map
		newTableMap.put(FIELD_RECORD, newTable);

		// Get type of main table
		CompositeType oldType = table.getType();

		// Make a copy of type
		CompositeTypeImpl newType = new CompositeTypeImpl();
		oldType.getFields().forEach(field -> newType.declareField(field, oldType.getFieldType(field)));

		// Add new declaration with number of authors
		if (!newType.getFields().contains(FIELD_NUMBER_OF_AUTHORS)) {
			newType.declareField(FIELD_NUMBER_OF_AUTHORS, TYPE_OF_NUMBER_OF_AUTHORS);
		}

		// Update type of table
		newTable.setType(newType);

		// Compute the number of authors for each record
		table.getRecords().forEach(record -> {
			List<String> authors = record.get(FIELD_AUTHORS).renderAsList();
			int numberOfAuthors = authors.size();
			record.set(FIELD_NUMBER_OF_AUTHORS, new StringValue("" + numberOfAuthors));
		});

		// Store the new table map
		StringWriter writer = new StringWriter();
		SimpleFormatRenderer renderer = new SimpleFormatRenderer(writer);
		renderer.render(newTableMap);

		// Read the expected output
		StringBuffer sbuf = new StringBuffer();
		BufferedReader reader = new BufferedReader(new FileReader(EXPECTED_OUTPUT_FILE_NAME));
		reader.lines().forEach(line -> sbuf.append(line + NEWLINE));
		reader.close();

		// Compare the expected output with the actual output
		Assert.assertEquals(sbuf.toString(), writer.toString());
	}

}

@@ -1291,24 +1291,25 @@
 l String NEW
+_
 LINE = %22%5Cn%22;
@@ -3193,16 +3193,17 @@
 ne + NEW
+_
 LINE));%0A

Make minor change