/*
 * Copyright (c) 2012, bad robot (london) ltd.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 bad.robot.excel.matchers;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.hamcrest.StringDescription;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static bad.robot.excel.WorkbookResource.*;
import static bad.robot.excel.matchers.IndividualCellMatcher.hasSameCellAs;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

public class IndividualCellMatcherTest {

    private StringDescription description = new StringDescription();

    private Row row;
    private Row secondRow;
    private Row thirdRow;
    private Row firstRowWithAlternateValues;
    private Row secondRowWithAlternateValues;
    private Row thirdRowWithAlternateValues;

    @Before
    public void loadWorkbookAndSheets() throws IOException {
        row = firstRowOf("rowWithThreeCells.xls");
        secondRow = secondRowOf("rowWithThreeCells.xls");
        thirdRow = thirdRowOf("rowWithThreeCells.xls");
        firstRowWithAlternateValues = firstRowOf("rowWithThreeCellsAlternativeValues.xls");
        secondRowWithAlternateValues = secondRowOf("rowWithThreeCellsAlternativeValues.xls");
        thirdRowWithAlternateValues = thirdRowOf("rowWithThreeCellsAlternativeValues.xls");
    }

    @Test
    public void exampleUsage() {
        assertThat(row, hasSameCellAs(createCell(0, 0, "C1, R1")));
        assertThat(row, not(hasSameCellAs(createCell(0, 0, "XXX"))));
    }

    @Test
    public void matches() {
        assertThat(hasSameCellAs(createCell(0, 1, "C2, R1")).matches(row), is(true));
    }

    @Test
    public void doesNotMatch() {
        assertThat(hasSameCellAs(createCell(0, 0, "C3, R1")).matches(row), is(false));
    }

    @Test
    public void describe() {
        hasSameCellAs(createCell(0, 0, "XXX")).describeTo(description);
        assertThat(description.toString(), is("equality of cell \"A1\""));
    }

    @Test
    public void mismatch() {
        hasSameCellAs(createCell(0, 0, "XXX")).matchesSafely(row, description);
        assertThat(description.toString(), is("cell at \"A1\" contained <\"C1, R1\"> expected <\"XXX\">"));
    }

    public static Cell createCell(int row, int column, String value) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFCell cell = sheet.createRow(row).createCell(column, CELL_TYPE_STRING);
        cell.setCellValue(value);
        return cell;
    }
}

@@ -1276,17 +1276,26 @@
 esource.
-*
+firstRowOf
 ;%0Aimport
@@ -1701,705 +1701,124 @@
 ow;%0A
-    private Row secondRow;%0A    private Row thirdRow;%0A    private Row firstRowWithAlternateValues;%0A    private Row secondRowWithAlternateValues;%0A    private Row thirdRowWithAlternateValues;%0A%0A    @Before%0A    public void loadWorkbookAndSheets() throws IOException %7B%0A        row = firstRowOf(%22rowWithThreeCells.xls%22);%0A        secondRow = secondRowOf(%22rowWithThreeCells.xls%22);%0A        thirdRow = thirdRowOf(%22rowWithThreeCells.xls%22);%0A        firstRowWithAlternateValues = firstRowOf(%22rowWithThreeCellsAlternativeValues.xls%22);%0A        secondRowWithAlternateValues = secondRowOf(%22rowWithThreeCellsAlternativeValues.xls%22);%0A        thirdRowWithAlternateValues = thirdRowOf(%22rowWithThreeCellsAlternativeValue
+%0A    @Before%0A    public void loadWorkbookAndSheets() throws IOException %7B%0A        row = firstRowOf(%22rowWithThreeCell
 s.xl

tidy up