Skip to content

Commit

Permalink
Merge pull request #40 from six42/emptyRs
Browse files Browse the repository at this point in the history
Fix #39 Empty SQLCommand result ends in IndexOutOfBoundsException
  • Loading branch information
six42 authored Apr 25, 2020
2 parents db093b3 + b71c160 commit 1867dd2
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
Test
---
#Copyright (C) 2015-202 by six42, All rights reserved. Contact the author via http://github.com/six42
#
1 Empty result sets are displayed correctly
2 An exception during execution is displayed with raw result information
!include -c .FitNesse.SuiteAcceptanceTests.SuiteSlimTests.SetUp

!define TestPageName {TestPage}

!| script |
|given Jdbc Slim test page|${TestPageName}|
|and test content is |!-

1

!|SQLCommand|TestDatabase|select ID,Name from TestData where Name in('notThere', 'unknown')|SORT |
|ID?|NAME?>|
|1|notThere|
|2|unknown|

2

!|SQLCommand|TestDatabase|select ID,Name from TestData where Name in('notThere', 'unknown')|SORT dbOnlyRowCount=RowCount dbPerf=ProcessingTime|
|ID?|NAME?>|
|1|notThere|
|2|unknown|

-! |
|when page|${TestPageName}| is tested and HTML is extracted|
|then |\d | assertions pass, | 5| fail, | 0| are ignored | 1 | exceptions thrown|
|and cell |\[-\]notThere | has result | fail|
|and cell |The actual result has no column with the same name as expected column: 1.Result: \[\[RowCount, ProcessingTime\], \[0, 0.0..\]\]| has result | error|
|show Symbol |$HTML_Input|
|show Symbol |$HTML_Result|
|get collapsed executon log for page|${TestPageName}|







!include -c .FitNesse.SuiteAcceptanceTests.TearDown
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
Test
---
#Copyright (C) 2020 by six42, All rights reserved. Contact the author via http://github.com/six42
#

!3 A query can only return a header and no data in a script

!|script:SQLCommand|TestDatabase|
|run| select ID,Name from TestData where Name in('notThere', 'unknown')|
|check| raw result|[[ID, NAME]]|
|check| result sheet|[[ID, NAME]]|
|check|getRowCount|0|

!3 A query can only return a header and no data in a decision table

!|SQLCommand|TestDatabase|select ID,Name from TestData where Name in('notThere', 'unknown')|SORT|
|ID?|NAME?>|
|=~/\A\z/|=~/\A\z/|

!contents -R2 -g -p -f -h
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'signing'
apply plugin: 'eclipse'

group 'com.github.six42'
version = '1.2.4'
version = '1.2.5-snapshot'
//version = new Date().format('yyyyMMdd')

println "Building ${project.name} v${project.version}..."
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/six42/fitnesse/jdbcslim/HeaderLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static public List<HeaderCell> generateActualSortKeyList(List<HeaderCell> expect
// The actual result has no matching column
// this makes it impossible to compare results
// throw an exception
throw new RuntimeException("The actual results have no column with the same name as expected column: " + (expectedIndex.getSortIndex()-1));
throw new RuntimeException("The actual result has no column with the same name as expected column: " + (expectedIndex.getSortIndex()-1));
}
}
return result;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/six42/fitnesse/jdbcslim/SQLCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ private List<List<String>> dbExecute(String sqlCommand) throws SQLException{
getOutputParameterValues(cstmt, resultTable, outputParamterMap);

cstmt.close();

// Only empty header columns? - then remove the (top 2) empty rows
if(resultTable.get(0).isEmpty()){
if (resultTable.get(0).isEmpty()) {
resultTable.remove(0);
resultTable.remove(0);
}
return resultTable;
resultTable.remove(0);
// Only header columns? but no data columns then remove the empty data row
} else if (resultTable.get(1).isEmpty()) {
resultTable.remove(1);
}
return resultTable;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/six42/fitnesse/jdbcslim/SheetCommandBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ private void parseConfigurationString(String configurationOptions) throws FileNo


public List<?> doTable(List<List<String>> ParameterTable) {
// Always do this
return myFixture.doTable(ParameterTable);
try {
// Always do this
return myFixture.doTable(ParameterTable);
} catch (Exception e){
throw new RuntimeException("message:<<" + e.getMessage() + "\nResult: " + rawResult()+">>", e);
}
}

@Override
Expand Down Expand Up @@ -177,10 +181,9 @@ public String getColumnValueByNameFromRow(String columnName, int row){

public int getRowCount() {
List<List<String>> sheet = this.resultSheet;


// Likely extra logic required until Fitnesse#1055 is merged
// return sheet == null || sheet.isEmpty() ? 0 : sheet.size() - 1;
return sheet == null || sheet.isEmpty() || (sheet.size() == 2 && sheet.get(1).isEmpty()) ? 0 : sheet.size() - 1;
return sheet == null || sheet.isEmpty() ? 0 : sheet.size() - 1;
}

public List<String> getRowValues(int row) {
Expand Down
140 changes: 72 additions & 68 deletions src/main/java/six42/fitnesse/jdbcslim/StringListComparator.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
// Copyright (C) 2015 by six42, All rights reserved. Contact the author via http://github.com/six42
// Copyright (C) 2015-2020 by six42, All rights reserved. Contact the author via http://github.com/six42
package six42.fitnesse.jdbcslim;

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


public class StringListComparator implements Comparator<List<String>> {

private List<HeaderCell> _SortKeys1;
private List<HeaderCell> _SortKeys2;

public StringListComparator(){
_SortKeys1 = new ArrayList<HeaderCell>();
_SortKeys2 = new ArrayList<HeaderCell>();
}
public StringListComparator(List<HeaderCell> SortKeys){
_SortKeys1 = SortKeys;
_SortKeys2 = SortKeys;
}
public StringListComparator(List<HeaderCell> SortKeys1, List<HeaderCell> SortKeys2){
_SortKeys1 = SortKeys1;
_SortKeys2 = SortKeys2;
}
private List<HeaderCell> _SortKeys1;
private List<HeaderCell> _SortKeys2;

@Override
public int compare(List<String> a1, List<String> a2) {
int r =0;
for (int i=0; i < _SortKeys1.size(); i++){
String aStr1 = a1.get(_SortKeys1.get(i).getSortIndex()-1);
String aStr2 = a2.get(_SortKeys2.get(i).getSortIndex()-1);
int sortDirection = _SortKeys1.get(i).getSortDirection();
// treat two null as equal
if (aStr1 == null && aStr2 == null){
r=0;
continue;
}
// One side null
if(aStr1 == null){
r=-1*sortDirection;
break;
}
if(aStr2 == null){
r=1*sortDirection;
break;
}
if ("d".equalsIgnoreCase(_SortKeys1.get(i).getSortType())){
try{
Long aL1 = Long.parseLong(aStr1);
Long aL2 = Long.parseLong(aStr2);
r= aL1.compareTo(aL2);
}catch (NumberFormatException e){
System.out.println("Failed to convert to Long: '" + aStr1 + "' '" + aStr2 + "'");
e.printStackTrace();
r= aStr1.compareTo(aStr2);
}
}
else if ("f".equalsIgnoreCase(_SortKeys1.get(i).getSortType())){
try{
Double aD1 = Double.parseDouble(aStr1);
Double aD2 = Double.parseDouble(aStr2);
r= aD1.compareTo(aD2);
}catch (NumberFormatException e){
System.out.println("Failed to convert to Double: '" + aStr1 + "' '" + aStr2 + "'");
e.printStackTrace();
r= aStr1.compareTo(aStr2);
}
public StringListComparator() {
_SortKeys1 = new ArrayList<HeaderCell>();
_SortKeys2 = new ArrayList<HeaderCell>();
}

public StringListComparator(List<HeaderCell> SortKeys) {
_SortKeys1 = SortKeys;
_SortKeys2 = SortKeys;
}

public StringListComparator(List<HeaderCell> SortKeys1,
List<HeaderCell> SortKeys2) {
_SortKeys1 = SortKeys1;
_SortKeys2 = SortKeys2;
}

@Override
public int compare(List<String> a1, List<String> a2) {
int r = 0;
for (int i = 0; i < _SortKeys1.size(); i++) {
int sortIndex1 = _SortKeys1.get(i).getSortIndex() - 1;
int sortIndex2 = _SortKeys2.get(i).getSortIndex() - 1;
String aStr1 = sortIndex1 < a1.size() ? a1.get(sortIndex1) : null;
String aStr2 = sortIndex2 < a2.size() ? a2.get(sortIndex2) : null;
int sortDirection = _SortKeys1.get(i).getSortDirection();
// treat two null as equal
if (aStr1 == null && aStr2 == null) {
r = 0;
continue;
}
// One side null
if (aStr1 == null) {
r = -1 * sortDirection;
break;
}
if (aStr2 == null) {
r = 1 * sortDirection;
break;
}
if ("d".equalsIgnoreCase(_SortKeys1.get(i).getSortType())) {
try {
Long aL1 = Long.parseLong(aStr1);
Long aL2 = Long.parseLong(aStr2);
r = aL1.compareTo(aL2);
} catch (NumberFormatException e) {
System.out.println("Failed to convert to Long: '" + aStr1 + "' '"
+ aStr2 + "'");
e.printStackTrace();
r = aStr1.compareTo(aStr2);
}
else{
r= aStr1.compareTo(aStr2);
} else if ("f".equalsIgnoreCase(_SortKeys1.get(i).getSortType())) {
try {
Double aD1 = Double.parseDouble(aStr1);
Double aD2 = Double.parseDouble(aStr2);
r = aD1.compareTo(aD2);
} catch (NumberFormatException e) {
System.out.println("Failed to convert to Double: '" + aStr1 + "' '"
+ aStr2 + "'");
e.printStackTrace();
r = aStr1.compareTo(aStr2);
}
} else {
r = aStr1.compareTo(aStr2);
}

r = r*sortDirection;
r = r * sortDirection;

if (r != 0){
break;
}
}
return r;
}
if (r != 0) {
break;
}
}
return r;
}
}

2 comments on commit 1867dd2

@michaelklijn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear six42,

I'm currently also in the proces of setting up a FitNesse instance together with your JdbcSlim solution for database testing. However I'm getting stuck on the installation page at the end, as you state: "!2 To build your own test pages 1. include this page on the root page of your suite 2. include the SuiteSetup page or a page with similar content in the setup of your suite" What page do you mean at point 1, with 'this page'? And do you have an example (screenshot or something alike) of both point 1 and 2, the root page and the SuiteSetup page regarding the content needed?

Thanks in advance!

You can always reach me on: [email protected]

Regards,

Michael Klijn

@Unforgettable631
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @michaelklijn, you commented on a (closed) pull request. Can you please open an issue to get help from there?

Please sign in to comment.