Skip to content

Commit

Permalink
Fix Sonar test coverage issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Anstis committed Nov 13, 2019
1 parent ff27209 commit 9b7dc09
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public class DecisionTableGridCellTest extends BaseHasDynamicHeightCellTest<Deci

@Override
public DecisionTableGridCell makeCell() {
return makeCell(LINE_HEIGHT);
}

@Override
protected DecisionTableGridCell makeCell(final double lineHeight) {
return new DecisionTableGridCell<>(value,
listSelector,
LINE_HEIGHT);
lineHeight);
}

@Test
Expand All @@ -49,6 +54,7 @@ public void testIsAHasDynamicHeightSubclass() {
}

@Test
@SuppressWarnings("unchecked")
public void testGetEditor() {
assertThat(cell.getEditor()).isNotEmpty();
assertThat(cell.getEditor().get()).isSameAs(listSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public class LiteralExpressionCellTest extends BaseHasDynamicHeightCellTest<Lite

@Override
public LiteralExpressionCell makeCell() {
return makeCell(LINE_HEIGHT);
}

@Override
protected LiteralExpressionCell makeCell(final double lineHeight) {
return new LiteralExpressionCell<>(value,
listSelector,
LINE_HEIGHT);
lineHeight);
}

@Test
Expand All @@ -49,6 +54,7 @@ public void testIsAHasDynamicHeightSubclass() {
}

@Test
@SuppressWarnings("unchecked")
public void testGetEditor() {
assertThat(cell.getEditor()).isNotEmpty();
assertThat(cell.getEditor().get()).isSameAs(listSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public class RelationGridCellTest extends BaseHasDynamicHeightCellTest<RelationG

@Override
public RelationGridCell makeCell() {
return makeCell(LINE_HEIGHT);
}

@Override
protected RelationGridCell makeCell(final double lineHeight) {
return new RelationGridCell<>(value,
listSelector,
LINE_HEIGHT);
lineHeight);
}

@Test
Expand All @@ -49,6 +54,7 @@ public void testIsAHasDynamicHeightSubclass() {
}

@Test
@SuppressWarnings("unchecked")
public void testGetEditor() {
assertThat(cell.getEditor()).isNotEmpty();
assertThat(cell.getEditor().get()).isSameAs(listSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class BaseHasDynamicHeightCellTest<CELL extends BaseGridCell & H

protected abstract CELL makeCell();

protected abstract CELL makeCell(final double lineHeight);

@Before
public void setup() {
this.cell = makeCell();
Expand Down Expand Up @@ -96,9 +98,46 @@ public void testMultipleLineValueWithEndingWithEmptyLine() {
assertThat(cell.getHeight()).isEqualTo(4 * LINE_HEIGHT + (RendererUtils.EXPRESSION_TEXT_PADDING * 3));
}

@Test
public void testEquals() {
final CELL sameCell = makeCell();

assertThat(cell).isEqualTo(sameCell);
assertThat(cell.hashCode()).isEqualTo(sameCell.hashCode());
}

@Test
public void testEqualsIdentity() {
assertThat(cell).isEqualTo(cell);
assertThat(cell.hashCode()).isEqualTo(cell.hashCode());
}

@Test
public void testEqualsDifferentHeight() {
final CELL differentCell = makeCell();
final GridCellValue<String> differentValue = new BaseGridCellValue<>("Hello\nWorld!");
setValue(differentCell, differentValue);

assertThat(cell).isNotEqualTo(differentCell);
assertThat(cell.hashCode()).isNotEqualTo(differentCell.hashCode());
}

@Test
public void testEqualsDifferentLineHeight() {
final CELL differentCell = makeCell(LINE_HEIGHT + 1);

assertThat(cell).isNotEqualTo(differentCell);
assertThat(cell.hashCode()).isNotEqualTo(differentCell.hashCode());
}

protected void setValue(final GridCellValue value) {
setValue(cell, value);
}

@SuppressWarnings("unchecked")
//It's not nice invoking the _setValue_ through reflection however I really don't want it public.
protected void setValue(final GridCellValue value) {
protected void setValue(final CELL cell,
final GridCellValue value) {
try {
final Method m = BaseGridCell.class.getDeclaredMethod("setValue", GridCellValue.class);
m.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

package org.kie.workbench.common.dmn.client.widgets.grid.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.ait.lienzo.test.LienzoMockitoTestRunner;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.soup.commons.util.Maps;
import org.kie.workbench.common.dmn.client.editors.expressions.types.context.ExpressionCellValue;
import org.kie.workbench.common.dmn.client.widgets.grid.BaseExpressionGrid;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.uberfire.ext.wires.core.grids.client.model.GridCell;
import org.uberfire.ext.wires.core.grids.client.model.GridRow;
import org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridCell;

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.workbench.common.dmn.client.widgets.grid.model.ExpressionEditorGridRow.DEFAULT_HEIGHT;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@RunWith(LienzoMockitoTestRunner.class)
public class ExpressionEditorGridRowTest {
Expand All @@ -41,32 +42,66 @@ public class ExpressionEditorGridRowTest {
private BaseExpressionGrid view;

@Test
public void testEmptyRow() throws Exception {
public void testEmptyRow() {
final GridRow row = new ExpressionEditorGridRow();
Assertions.assertThat(row.getHeight()).isEqualTo(DEFAULT_HEIGHT);
assertThat(row.getHeight()).isEqualTo(DEFAULT_HEIGHT);
}

@Test
public void testRowNoHigherThanDefault() throws Exception {
final GridRow row = Mockito.spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell> cells = new HashMap<Integer, GridCell>() {{
Mockito.doReturn(DEFAULT_HEIGHT - 1).when(view).getHeight();
put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))));
}};

Mockito.doReturn(cells).when(row).getCells();
Assertions.assertThat(row.getHeight()).isBetween(0D, DEFAULT_HEIGHT);
@SuppressWarnings("unchecked")
public void testRowLowerThanDefault() {
when(view.getHeight()).thenReturn(DEFAULT_HEIGHT - 1);

final GridRow row = spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell<?>> cells = new Maps.Builder<Integer, GridCell<?>>()
.put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))))
.build();

when(row.getCells()).thenReturn(cells);
assertThat(row.getHeight()).isBetween(0D, DEFAULT_HEIGHT);
}

@Test
@SuppressWarnings("unchecked")
public void testRowHigherThanDefault() {
when(view.getHeight()).thenReturn(DEFAULT_HEIGHT + 1);

final GridRow row = spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell<?>> cells = new Maps.Builder<Integer, GridCell<?>>()
.put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))))
.build();

when(row.getCells()).thenReturn(cells);
assertThat(row.getHeight()).isGreaterThan(DEFAULT_HEIGHT);
}

@Test
public void testRowHigherThanDefault() throws Exception {
final GridRow row = Mockito.spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell> cells = new HashMap<Integer, GridCell>() {{
Mockito.doReturn(DEFAULT_HEIGHT + 1).when(view).getHeight();
put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))));
}};

Mockito.doReturn(cells).when(row).getCells();
Assertions.assertThat(row.getHeight()).isGreaterThan(DEFAULT_HEIGHT);
@SuppressWarnings("unchecked")
public void testRowHigherThanDefaultWithNullCell() {
when(view.getHeight()).thenReturn(DEFAULT_HEIGHT + 1);

final GridRow row = spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell<?>> cells = new Maps.Builder<Integer, GridCell<?>>()
.put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))))
.put(1, null)
.build();

when(row.getCells()).thenReturn(cells);
assertThat(row.getHeight()).isGreaterThan(DEFAULT_HEIGHT);
}

@Test
@SuppressWarnings("unchecked")
public void testRowHigherThanDefaultWithNullCellValue() {
when(view.getHeight()).thenReturn(DEFAULT_HEIGHT + 1);

final GridRow row = spy(ExpressionEditorGridRow.class);
final Map<Integer, GridCell<?>> cells = new Maps.Builder<Integer, GridCell<?>>()
.put(0, new BaseGridCell<>(new ExpressionCellValue(Optional.of(view))))
.put(1, new BaseGridCell<>(null))
.build();

when(row.getCells()).thenReturn(cells);
assertThat(row.getHeight()).isGreaterThan(DEFAULT_HEIGHT);
}
}

0 comments on commit 9b7dc09

Please sign in to comment.