Skip to content

Commit

Permalink
add basic test coverage for column diffs (apache#820)
Browse files Browse the repository at this point in the history
... and also improve a bit the BRL column diffs
  • Loading branch information
zkrejcov authored and csadilek committed Jun 27, 2016
1 parent b9ca44e commit 88e5334
Show file tree
Hide file tree
Showing 21 changed files with 1,915 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,15 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
}

/**
* Clones this metadata column instance.
* Clones this attribute column instance.
* @return The cloned instance.
*/
public AttributeCol52 cloneColumn() {
AttributeCol52 cloned = new AttributeCol52();
cloned.setAttribute( getAttribute() );
cloned.setReverseOrder( isReverseOrder() );
cloned.setUseRowNumber( isUseRowNumber() );
cloned.setWidth( getWidth() );
cloned.setHideColumn( isHideColumn() );
cloned.setHeader( getHeader() );
cloned.setDefaultValue( getDefaultValue() != null ? getDefaultValue().cloneDefaultValueCell() : null );
cloned.cloneCommonColumnConfigFrom( this );
return cloned;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.drools.workbench.models.guided.dtable.shared.model;

import static java.lang.Math.min;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -60,14 +62,36 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
// Field: childColumns.
if ( !isEqualOrNull( this.getChildColumns(),
other.getChildColumns() ) ) {
result.add( new BaseColumnFieldDiffImpl( FIELD_CHILD_COLUMNS,
this.getChildColumns(),
other.getChildColumns() ) );
result.addAll( getColumnDiffs( other.getChildColumns() ) );
}

return result;
}

private List<BaseColumnFieldDiff> getColumnDiffs( List<BRLActionVariableColumn> otherChildColumns ) {
int commonLength = min( this.childColumns.size(), otherChildColumns.size() );
List<BaseColumnFieldDiff> result = new ArrayList<>();
for ( int i = 0; i < commonLength; i ++ ) {
result.addAll( this.childColumns.get( i ).diff( otherChildColumns.get( i ) ) );
}
result.addAll( getDiffsForUnpairedColumns( this.childColumns, commonLength, false ) );
result.addAll( getDiffsForUnpairedColumns( otherChildColumns, commonLength, true ) );
return result;
}

private List<BaseColumnFieldDiff> getDiffsForUnpairedColumns( List<BRLActionVariableColumn> addedChildColumns,
int commonLength,
boolean added ) {
List<BaseColumnFieldDiff> result = new ArrayList<>();
if ( addedChildColumns.size() > commonLength ) {
for ( BRLActionVariableColumn column : addedChildColumns.subList( commonLength, addedChildColumns.size() ) )
result.add( new BaseColumnFieldDiffImpl( FIELD_CHILD_COLUMNS,
(added) ? null : column,
(added) ? column : null ) );
}
return result;
}

public List<IAction> getDefinition() {
return this.definition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.drools.workbench.models.guided.dtable.shared.model;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
return null;
}

List<BaseColumnFieldDiff> result = super.diff( otherColumn );
List<BaseColumnFieldDiff> result = new ArrayList<>();
BRLActionVariableColumn other = (BRLActionVariableColumn) otherColumn;

// Field: varName.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.drools.workbench.models.guided.dtable.shared.model;

import static java.lang.Math.min;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -60,14 +62,36 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
// Field: childColumns.
if ( !isEqualOrNull( this.getChildColumns(),
other.getChildColumns() ) ) {
result.add( new BaseColumnFieldDiffImpl( FIELD_CHILD_COLUMNS,
this.getChildColumns(),
other.getChildColumns() ) );
result.addAll( getColumnDiffs( other.getChildColumns() ) );
}

return result;
}

private List<BaseColumnFieldDiff> getColumnDiffs( List<BRLConditionVariableColumn> otherChildColumns ) {
int commonLength = min( this.childColumns.size(), otherChildColumns.size() );
List<BaseColumnFieldDiff> result = new ArrayList<>();
for ( int i = 0; i < commonLength; i ++ ) {
result.addAll( this.childColumns.get( i ).diff( otherChildColumns.get( i ) ) );
}
result.addAll( getDiffsForUnpairedColumns( this.childColumns, commonLength, false ) );
result.addAll( getDiffsForUnpairedColumns( otherChildColumns, commonLength, true ) );
return result;
}

private List<BaseColumnFieldDiff> getDiffsForUnpairedColumns( List<BRLConditionVariableColumn> addedChildColumns,
int commonLength,
boolean added ) {
List<BaseColumnFieldDiff> result = new ArrayList<>();
if ( addedChildColumns.size() > commonLength ) {
for ( BRLConditionVariableColumn column : addedChildColumns.subList( commonLength, addedChildColumns.size() ) )
result.add( new BaseColumnFieldDiffImpl( FIELD_CHILD_COLUMNS,
(added) ? null : column,
(added) ? column : null ) );
}
return result;
}

public List<IPattern> getDefinition() {
return this.definition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
return null;
}

List<BaseColumnFieldDiff> result = super.diff( otherColumn );
List<BaseColumnFieldDiff> result = conditionCol52SpecificDiff( otherColumn );
BRLConditionVariableColumn other = (BRLConditionVariableColumn) otherColumn;

// Field: varName.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.drools.workbench.models.guided.dtable.shared.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -82,6 +83,17 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
}

List<BaseColumnFieldDiff> result = super.diff( otherColumn );
result.addAll( conditionCol52SpecificDiff( otherColumn ) );

return result;
}

protected List<BaseColumnFieldDiff> conditionCol52SpecificDiff( BaseColumn otherColumn ) {
if ( otherColumn == null ) {
return null;
}

List<BaseColumnFieldDiff> result = new ArrayList<>();
ConditionCol52 other = (ConditionCol52) otherColumn;

// Field: factField.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
return result;
}

/**
* Clones the configuration (width, header, hide, default value) of the argument into this instance.
* @param model column instance to be cloned
*/
public void cloneCommonColumnConfigFrom( DTColumnConfig52 model ) {
setWidth( model.getWidth() );
setHideColumn( model.isHideColumn() );
setHeader( model.getHeader() );
setDefaultValue( model.getDefaultValue() != null ? new DTCellValue52( model.getDefaultValue() ) : null );
}

protected Object extractDefaultValue( final DTCellValue52 dcv ) {
switch ( dcv.getDataType() ) {
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public List<BaseColumnFieldDiff> diff( BaseColumn otherColumn ) {
public MetadataCol52 cloneColumn() {
MetadataCol52 cloned = new MetadataCol52();
cloned.setMetadata( getMetadata() );
cloned.setWidth( getWidth() );
cloned.setHideColumn( isHideColumn() );
cloned.setHeader( getHeader() );
cloned.setDefaultValue( getDefaultValue() != null ? getDefaultValue().cloneDefaultValueCell() : null );
cloned.cloneCommonColumnConfigFrom( this );
return cloned;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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 org.drools.workbench.models.guided.dtable.shared.model;

import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_BOUND_NAME;
import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_FACT_FIELD;
import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_FACT_TYPE;
import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_IS_INSERT_LOGICAL;
import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_TYPE;
import static org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52.FIELD_VALUE_LIST;
import static org.drools.workbench.models.guided.dtable.shared.model.DTColumnConfig52.FIELD_DEFAULT_VALUE;
import static org.drools.workbench.models.guided.dtable.shared.model.DTColumnConfig52.FIELD_HEADER;
import static org.drools.workbench.models.guided.dtable.shared.model.DTColumnConfig52.FIELD_HIDE_COLUMN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

public class ActionInsertFactCol52Test extends ColumnTestBase {

private ActionInsertFactCol52 column1;
private ActionInsertFactCol52 column2;

@Before
public void setup() {
column1 = new ActionInsertFactCol52();
column1.setFactType("FactType");
column1.setBoundName("$var");
column1.setFactField("field");
column1.setType("Type");
column1.setValueList("a,b,c");
column1.setInsertLogical(false);
column1.setHeader("header");
column1.setHideColumn(false);
column1.setDefaultValue(new DTCellValue52("default"));

column2 = new ActionInsertFactCol52();
column2.setFactType("FactType");
column2.setBoundName("$var");
column2.setFactField("field");
column2.setType("Type");
column2.setValueList("a,b,c");
column2.setInsertLogical(false);
column2.setHeader("header");
column2.setHideColumn(false);
column2.setDefaultValue(new DTCellValue52("default"));
}

@Test
public void testDiffEmpty() {
checkDiffEmpty(column1, column2);
}

@Test
public void testDiffFactType() {
column1.setFactType("FactType1");
column2.setFactType("FactType2");

checkSingleDiff(FIELD_FACT_TYPE, "FactType1", "FactType2", column1, column2);
}

@Test
public void testDiffBoundName() {
column1.setBoundName("$var1");
column2.setBoundName("$var2");

checkSingleDiff(FIELD_BOUND_NAME, "$var1", "$var2", column1, column2);
}

@Test
public void testDiffFactField() {
column1.setFactField("field1");
column2.setFactField("field2");

checkSingleDiff(FIELD_FACT_FIELD, "field1", "field2", column1, column2);
}

@Test
public void testDiffType() {
column1.setType("Type1");
column2.setType("Type2");

checkSingleDiff(FIELD_TYPE, "Type1", "Type2", column1, column2);
}

@Test
public void testDiffValueList() {
column1.setValueList("a,b");
column2.setValueList("b,c");

checkSingleDiff(FIELD_VALUE_LIST, "a,b", "b,c", column1, column2);
}

@Test
public void testDiffInsert() {
column1.setInsertLogical(false);
column2.setInsertLogical(true);

checkSingleDiff(FIELD_IS_INSERT_LOGICAL, false, true, column1, column2);
}

@Test
public void testDiffAll() {
column1.setFactType("FactType1");
column1.setBoundName("$var1");
column1.setFactField("field1");
column1.setType("Type1");
column1.setValueList("a,b");
column1.setInsertLogical(false);
column1.setHeader("header1");
column1.setHideColumn(false);
column1.setDefaultValue(new DTCellValue52("default1"));

column2.setFactType("FactType2");
column2.setBoundName("$var2");
column2.setFactField("field2");
column2.setType("Type2");
column2.setValueList("b,c");
column2.setInsertLogical(true);
column2.setHeader("header2");
column2.setHideColumn(true);
column2.setDefaultValue(new DTCellValue52("default2"));

List<BaseColumnFieldDiff> diff = column1.diff(column2);
assertNotNull(diff);
assertEquals(9, diff.size());
assertEquals(FIELD_HIDE_COLUMN, diff.get(0).getFieldName());
assertEquals(false, diff.get(0).getOldValue());
assertEquals(true, diff.get(0).getValue());
assertEquals(FIELD_DEFAULT_VALUE, diff.get(1).getFieldName());
assertEquals("default1", diff.get(1).getOldValue());
assertEquals("default2", diff.get(1).getValue());
assertEquals(FIELD_HEADER, diff.get(2).getFieldName());
assertEquals("header1", diff.get(2).getOldValue());
assertEquals("header2", diff.get(2).getValue());
assertEquals(FIELD_FACT_TYPE, diff.get(3).getFieldName());
assertEquals("FactType1", diff.get(3).getOldValue());
assertEquals("FactType2", diff.get(3).getValue());
assertEquals(FIELD_BOUND_NAME, diff.get(4).getFieldName());
assertEquals("$var1", diff.get(4).getOldValue());
assertEquals("$var2", diff.get(4).getValue());
assertEquals(FIELD_FACT_FIELD, diff.get(5).getFieldName());
assertEquals("field1", diff.get(5).getOldValue());
assertEquals("field2", diff.get(5).getValue());
assertEquals(FIELD_TYPE, diff.get(6).getFieldName());
assertEquals("Type1", diff.get(6).getOldValue());
assertEquals("Type2", diff.get(6).getValue());
assertEquals(FIELD_VALUE_LIST, diff.get(7).getFieldName());
assertEquals("a,b", diff.get(7).getOldValue());
assertEquals("b,c", diff.get(7).getValue());
assertEquals(FIELD_IS_INSERT_LOGICAL, diff.get(8).getFieldName());
assertEquals(false, diff.get(8).getOldValue());
assertEquals(true, diff.get(8).getValue());
}
}
Loading

0 comments on commit 88e5334

Please sign in to comment.