GridTest.java 8.75 KB
package kernel.test;

import kernel.Cell;
import kernel.Grid;
import kernel.LanguageEnum;
import kernel.exception.CannotDeleteCellException;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import kernel.function.Average;
import kernel.function.Sum;
import kernel.operation.Addition;
import kernel.operation.Multiplication;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

public class GridTest {
	
	private Grid grid;
	
	@Before
	public void initData() {
		Grid.language = LanguageEnum.EN;
		this.grid = new Grid();
	}
	
	@Test
	public void testCreateCellWithValue() throws InvalidIntervalException {
		assertEquals(0, this.grid.getCells().size());
		
		this.grid.createCell("A", 1, 10.);
		
		assertEquals(1, this.grid.getCells().size());
	}
	
	@Test
	public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithValue();
		assertEquals(3, this.grid.getCells().size());
		
		List<Cell> cellsFormula = new ArrayList<>();
		cellsFormula.add(this.grid.getCell("A", 1));
		cellsFormula.add(this.grid.getCell("A", 2));
		
		this.grid.createCell("B", 1, new Sum(cellsFormula));
		
		assertEquals(4, this.grid.getCells().size());
	}
	
	@Test(expected = InvalidIntervalException.class)
	public void testCreateCellWithInvalidInterval() throws InvalidIntervalException {
		this.grid.createCell("AZ", 120, 0.);
	}
	
	@Test(expected = CellNotFoundException.class)
	public void testGetUnknownCell() throws CellNotFoundException, InvalidIntervalException {
		this.createCellsWithValue();
		
		this.grid.getCell("B", 5);
	}
	
	@Test
	public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithFormula();
		
		assertEquals(10, this.grid.getValue("A", 1), 0);
		assertEquals(0, this.grid.getValue("A", 2), 0);
		assertEquals(2, this.grid.getValue("A", 3), 0);
		assertEquals(10, this.grid.getValue("B", 1), 0);
		assertEquals(12, this.grid.getValue("B", 2), 0);
		assertEquals(24, this.grid.getValue("B", 3), 0);
	}
	
	@Test
	public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		Grid.language = LanguageEnum.EN;
		this.createCellsWithFormula();
		
		assertEquals("A1", this.grid.getFormulaAsString("A", 1));
		assertEquals("A2", this.grid.getFormulaAsString("A", 2));
		assertEquals("A3", this.grid.getFormulaAsString("A", 3));
		assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1));
		assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2));
		assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3));
	}
	
	@Test
	public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithFormula();
		
		assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
	}
	
	@Test
	public void testSetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithFormula();
		
		assertEquals(10, this.grid.getValue("A", 1), 0);
		assertEquals(12, this.grid.getValue("B", 2), 0);
		assertEquals(24, this.grid.getValue("B", 3), 0);
		
		this.grid.setValue("A", 1, 15.);
		
		assertEquals(15, this.grid.getValue("A", 1), 0);
		assertEquals(17, this.grid.getValue("B", 2), 0);
		assertEquals(34, this.grid.getValue("B", 3), 0);
		assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
		
		this.grid.setValue("B", 2, 20.);
		
		assertEquals(40, this.grid.getValue("B", 3), 0);
		assertEquals("(A3*B2)", this.grid.getDevelopedFormula("B", 3));
	}
	
	@Test
	public void testSetFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithFormula();
		
		assertEquals(12, this.grid.getValue("B", 2), 0);
		assertEquals(24, this.grid.getValue("B", 3), 0);
		assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2));
		assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
		
		List<Cell> sumList = new ArrayList<>();
		sumList.add(this.grid.getCell("A", 1));
		sumList.add(this.grid.getCell("A", 3));
		sumList.add(this.grid.getCell("B", 1));
		
		this.grid.setFormula("B", 2, new Sum(sumList));
		
		// B2
		assertEquals(22, this.grid.getValue("B", 2), 0);
		assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
		assertEquals("SUM(A1,A3,(A1+A2))", this.grid.getDevelopedFormula("B", 2));
		// B3
		assertEquals(44, this.grid.getValue("B", 3), 0);
		assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3));
		assertEquals("(A3*SUM(A1,A3,(A1+A2)))", this.grid.getDevelopedFormula("B", 3));
		
		this.grid.createCell("C", 1, 10.);
		this.grid.createCell("C", 2, 20.);
		
		List<Cell> averageList = new ArrayList<>();
		averageList.add(this.grid.getCell("C", 1));
		averageList.add(this.grid.getCell("C", 2));
		
		this.grid.setFormula("A", 1, new Average(averageList));
		
		// A1
		assertEquals(15, this.grid.getValue("A", 1), 0);
		assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1));
		// B1
		assertEquals(15, this.grid.getValue("B", 1), 0);
		assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1));
		assertEquals("(AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 1));
		// B2
		assertEquals(32, this.grid.getValue("B", 2), 0);
		assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
		assertEquals("SUM(AVERAGE(C1,C2),A3,(AVERAGE(C1,C2)+A2))", this.grid.getDevelopedFormula("B", 2));
	}
	
	@Test
	public void testGetCell() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithFormula();
		
		Cell cell = this.grid.getCell("A", 1);
		
		assertEquals("A1", cell.getId());
		assertEquals(10, cell.getValue(), 0);
		assertEquals("A1", cell.toString());
		assertEquals("A1", cell.getDevelopedFormula());
		assertFalse(cell.containFormula());
		
		cell = this.grid.getCell("B", 2);
		
		assertEquals("B2", cell.getId());
		assertEquals(12, cell.getValue(), 0);
		assertEquals("SUM(A1,A2,A3)", cell.toString());
		assertEquals("SUM(A1,A2,A3)", cell.getDevelopedFormula());
		assertTrue(cell.containFormula());
		
		cell = this.grid.getCell("B", 3);
		
		assertEquals("B3", cell.getId());
		assertEquals(24, cell.getValue(), 0);
		assertEquals("(A3*B2)", cell.toString());
		assertEquals("(A3*SUM(A1,A2,A3))", cell.getDevelopedFormula());
		assertTrue(cell.containFormula());
	}
	
	@Test(expected = CellNotFoundException.class)
	public void testGetCellNotFound() throws CellNotFoundException {
		Cell cell = this.grid.getCell("A", 1);
	}
	
	@Test
	public void testGetCells() throws InvalidIntervalException {
		assertEquals(0, this.grid.getCells().size());
		
		this.createCellsWithValue();
		
		assertEquals(3, this.grid.getCells().size());
	}
	
	@Test
	public void testSave() throws InvalidIntervalException, IOException, CellNotFoundException, ClassNotFoundException,
			CreateCycleException {
		this.createCellsWithFormula();
		
		this.grid.save();
		
		Grid g = Grid.load();
		
		Cell cell = g.getCell("A", 1);
		
		assertEquals("A1", cell.getId());
		assertEquals(10, cell.getValue(), 0);
		
		cell = g.getCell("B", 1);
		
		assertEquals("B1", cell.getId());
		assertTrue(cell.containFormula());
		assertEquals(10, cell.getValue(), 0);
	}
	
	@Test
	public void testDeleteCell() throws CannotDeleteCellException, InvalidIntervalException {
		this.createCellsWithValue();
		
		Cell cell = this.grid.getCell("A2");
		
		assertNotNull(cell);
		assertEquals(0, cell.getValue(), 0);
		assertFalse(cell.containFormula());
		
		this.grid.deleteCell("A", 2);
		
		cell = this.grid.getCell("A2");
		
		assertNull(cell);
	}
	
	@Test(expected = CannotDeleteCellException.class)
	public void testDeleteCellUsedInAnother() throws CannotDeleteCellException, InvalidIntervalException,
			CellNotFoundException, CreateCycleException {
		this.createCellsWithFormula();
		
		this.grid.deleteCell("A", 1);
	}
	
	private void createCellsWithValue() throws InvalidIntervalException {
		this.grid.createCell("A", 1, 10.);
		this.grid.createCell("A", 2, 0.);
		this.grid.createCell("A", 3, 2.);
	}
	
	private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
		this.createCellsWithValue();
		
		Cell a1 = this.grid.getCell("A", 1);
		Cell a2 = this.grid.getCell("A", 2);
		Cell a3 = this.grid.getCell("A", 3);
		
		List<Cell> sumList = new ArrayList<>();
		sumList.add(a1);
		sumList.add(a2);
		sumList.add(a3);
		
		// A1+A2
		this.grid.createCell("B", 1, new Addition(a1, a2));
		// SUM(A1+A2+A3)
		this.grid.createCell("B", 2, new Sum(sumList));
		// A3*B2
		this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2)));
	}
}