Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
replace filters (one-way conversion) with converters (two-way convers…
Browse files Browse the repository at this point in the history
…ion)
  • Loading branch information
nkoudelia committed Nov 29, 2015
1 parent 78f496f commit fd6616d
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 29 deletions.
Binary file modified easyFit/install/easyfit.jar
Binary file not shown.
12 changes: 12 additions & 0 deletions easyFit/src/main/scala/easyfit/CellFactory.scala
Expand Up @@ -74,6 +74,18 @@ object CellFactory
cellArray
}

def splitConverterHeader(header: String): (String, String) =
{
val tokens = header.split(":")

if (tokens.length == 2)
{
return (tokens(0), tokens(1))
}

(null, header)
}

private def createRowCell(header: Header, cellValue: String): RowCell =
{
new RowCell(cellValue, header.fetchConverter())
Expand Down
21 changes: 5 additions & 16 deletions easyFit/src/main/scala/easyfit/cells/Header.scala
Expand Up @@ -14,6 +14,7 @@ import easyfit.{Store, StopTestException}
import easyfit.Strings.InvalidColumnName
import easyfit.Strings.UndefinedConverter
import easyfit.IConverter
import easyfit.CellFactory

/**
* Represents a header cell in Row and Query tables.
Expand Down Expand Up @@ -43,18 +44,6 @@ class Header(value: String)
value.endsWith("?")
}

def converterName(): String =
{
val tokens = value.split(":")

if (tokens.length == 2)
{
return tokens(0)
}

""
}

def sutInput(): String =
{
var sInput = value
Expand Down Expand Up @@ -96,15 +85,15 @@ class Header(value: String)
return converter
}

val fName = converterName()
val (converterName, _) = CellFactory.splitConverterHeader(value)

if (fName != "")
if (converterName != null)
{
converter = Store.getConverter(fName)
converter = Store.getConverter(converterName)

if (converter == null)
{
throw new StopTestException(UndefinedConverter + ": " + fName)
throw new StopTestException(UndefinedConverter + ": " + converterName)
}
}

Expand Down
40 changes: 35 additions & 5 deletions easyFit/src/main/scala/easyfit/tables/Map.scala
Expand Up @@ -15,8 +15,10 @@ import easyfit.Strings.MapTableMissingRows
import easyfit.Strings.MapTableInvalidColumns
import easyfit.Strings.MapTableEmptyKey
import easyfit.Strings.UndefinedVariable
import easyfit.Strings.UndefinedConverter
import easyfit.StopTestException
import easyfit.Store
import easyfit.CellFactory

import scala.collection.immutable

Expand Down Expand Up @@ -83,8 +85,10 @@ class Map(mapId: String)
map
}

private def getPair(key: String, value: String, result: java.util.List[java.util.List[String]]): (String, String) =
private def getPair(header: String, value: String, result: java.util.List[java.util.List[String]]): (String, String) =
{
val (converterName, key) = CellFactory.splitConverterHeader(header)

if (key == null || key == "")
{
throw new StopTestException(MapTableEmptyKey)
Expand All @@ -102,20 +106,46 @@ class Map(mapId: String)
}
else
{
val converted = applyConverter(converterName, varValue, true)

row.add("pass")
row.add(s"pass: $value [$varValue]")
row.add(s"pass: $value [$converted]")

result.add(row)

return key -> varValue
return key -> applyConverter(converterName, varValue, false)
}
}

val converted = applyConverter(converterName, value, true)

row.add("pass")
row.add("pass")
row.add(s"pass: $converted")

result.add(row)

key -> value
key -> applyConverter(converterName, value, false)
}

private def applyConverter(converterName: String, value: String, bothConversions: Boolean): String =
{
if (converterName == null)
{
return value
}

var converter = Store.getConverter(converterName)

if (converter == null)
{
throw new StopTestException(UndefinedConverter + ": " + converterName)
}

if(bothConversions)
{
return converter.convertActual(converter.convertExpected(value))
}

converter.convertExpected(value)
}
}
34 changes: 29 additions & 5 deletions easyFit/src/test/scala/easyfit.test/ConverterTests.scala
Expand Up @@ -12,28 +12,30 @@ package easyfit.test

import easyfit._
import easyfit.tables.Converter
import easyfit.tables.Map
import easyfit.Strings._
import easyfit.cells.{RowCell, Header}
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import scala.collection.JavaConversions.seqAsJavaList

@RunWith(classOf[JUnitRunner])
class ConverterTests extends EasyTest
{
"A Converter table" should "add converter objects" in
{
val converterTable = new Converter()
val className = converterTable.getClass.getName
val adderConverterName = "ac"
val adderConverterClassName = classOf[easyfit.test.AdderConverter].getName

val in = Seq(Seq(className, "easyfit.test.AdderConverter"))
val in = Seq(Seq(adderConverterName, adderConverterClassName))

val tableResult = converterTable.doTable(asJavaArrayList(in))
val tableResult = new Converter().doTable(asJavaArrayList(in))

val expectedResult = Seq(Seq("pass", "pass"))

compareTables(tableResult, asJavaArrayList(expectedResult))

val converter = Store.getConverter(className)
val converter = Store.getConverter(adderConverterName)

converter should not be null

Expand Down Expand Up @@ -159,6 +161,28 @@ class ConverterTests extends EasyTest
failMessage(f) should be(UndefinedConverter + ": " + "u-converter")
}

"A Map table" should "apply converters correctly" in
{
Store.setConverter("ac", new AdderConverter(" + 1", " + 2"))

val row1 = seqAsJavaList(Seq("ac:key1", "value"))
val row2 = seqAsJavaList(Seq("ac:key2", "$value"))
val table = seqAsJavaList(Seq(row1, row2))

Store.setVariable("$value", "123")

val results = new Map("test_map").doTable(table)

val map = Store.getMap("test_map")

map should not be null
map("key1") should be("value + 1")
map("key2") should be("123 + 1")

results.get(0).get(1) should be("pass: value + 1 + 2")
results.get(1).get(1) should be("pass: $value [123 + 1 + 2]")
}

private def getTestData(converterName: String, strToExpected: String, strToActual: String): (Seq[Seq[String]], Seq[Seq[String]], Seq[Seq[String]]) =
{
val header = Seq(s"$converterName:C1")
Expand Down
2 changes: 1 addition & 1 deletion easyFit/src/test/scala/easyfit.test/MapTests.scala
Expand Up @@ -72,7 +72,7 @@ class MapTests extends EasyTest
map("key1") should be("value")
map("key2") should be("123")

results.get(0).get(1) should be("pass")
results.get(0).get(1) should be("pass: value")
results.get(1).get(1) should be("pass: $value [123]")
}
}
Binary file modified filters/install/filters.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion testWebService/install/FitNesseRoot/FrontPage/content.txt
Expand Up @@ -10,7 +10,7 @@
|baseURL |http://localhost:48080/(controller).json|
|logFilePath|.\easyFit.log |

!|Table:ActualFilter |
!|Table:Converter |
|df|filter.DateFormatter|

!|Table:Row|CreateCar |
Expand Down
@@ -1 +1 @@
|FrontPage||22:27:17 ma, marraskuuta 16, 2015|
|FrontPage||14:58:52 su, marraskuuta 29, 2015|
6 changes: 6 additions & 0 deletions testWebService/install/easyFit.log
Expand Up @@ -4,3 +4,9 @@ POST http://localhost:48080/CreateCar.json: {"id":null,"make":"VW","model":"Jett
POST http://localhost:48080/CreateCar.json: {"id":null,"make":"Toyota","model":"Corolla","color":"green","registrationDate":"2012-01-11","mileage":"64200","fuel":"Petrol"} -> {"id":4,"make":"Toyota","model":"Corolla","color":"green","registrationDate":1326240000000,"mileage":64200,"fuel":"Petrol"}
GET http://localhost:48080/GetCars.json: [{"id":1,"make":"VW","model":"Passat","color":"red","registrationDate":1290643200000,"mileage":117000,"fuel":"Diesel"},{"id":2,"make":"VW","model":"Bora","color":"red","registrationDate":1080864000000,"mileage":176000,"fuel":"Petrol"},{"id":3,"make":"VW","model":"Jetta","color":"blue","registrationDate":1185062400000,"mileage":123400,"fuel":"Petrol"},{"id":4,"make":"Toyota","model":"Corolla","color":"green","registrationDate":1326240000000,"mileage":64200,"fuel":"Petrol"}]
GET http://localhost:48080/GetCars.json?make=VW&color=red: [{"id":1,"make":"VW","model":"Passat","color":"red","registrationDate":1290643200000,"mileage":117000,"fuel":"Diesel"},{"id":2,"make":"VW","model":"Bora","color":"red","registrationDate":1080864000000,"mileage":176000,"fuel":"Petrol"}]
POST http://localhost:48080/CreateCar.json: {"id":null,"make":"VW","model":"Passat","color":"red","registrationDate":"2010-11-25","mileage":"117000","fuel":"Diesel"} -> {"id":1,"make":"VW","model":"Passat","color":"red","registrationDate":1290643200000,"mileage":117000,"fuel":"Diesel"}
POST http://localhost:48080/CreateCar.json: {"id":null,"make":"VW","model":"Bora","color":"red","registrationDate":"2004-04-02","mileage":"176000","fuel":"Petrol"} -> {"id":2,"make":"VW","model":"Bora","color":"red","registrationDate":1080864000000,"mileage":176000,"fuel":"Petrol"}
POST http://localhost:48080/CreateCar.json: {"id":null,"make":"VW","model":"Jetta","color":"blue","registrationDate":"2007-07-22","mileage":"123400","fuel":"Petrol"} -> {"id":3,"make":"VW","model":"Jetta","color":"blue","registrationDate":1185062400000,"mileage":123400,"fuel":"Petrol"}
POST http://localhost:48080/CreateCar.json: {"id":null,"make":"Toyota","model":"Corolla","color":"green","registrationDate":"2012-01-11","mileage":"64200","fuel":"Petrol"} -> {"id":4,"make":"Toyota","model":"Corolla","color":"green","registrationDate":1326240000000,"mileage":64200,"fuel":"Petrol"}
GET http://localhost:48080/GetCars.json: [{"id":1,"make":"VW","model":"Passat","color":"red","registrationDate":1290643200000,"mileage":117000,"fuel":"Diesel"},{"id":2,"make":"VW","model":"Bora","color":"red","registrationDate":1080864000000,"mileage":176000,"fuel":"Petrol"},{"id":3,"make":"VW","model":"Jetta","color":"blue","registrationDate":1185062400000,"mileage":123400,"fuel":"Petrol"},{"id":4,"make":"Toyota","model":"Corolla","color":"green","registrationDate":1326240000000,"mileage":64200,"fuel":"Petrol"}]
GET http://localhost:48080/GetCars.json?make=VW&color=red: [{"id":1,"make":"VW","model":"Passat","color":"red","registrationDate":1290643200000,"mileage":117000,"fuel":"Diesel"},{"id":2,"make":"VW","model":"Bora","color":"red","registrationDate":1080864000000,"mileage":176000,"fuel":"Petrol"}]
Binary file modified testWebService/install/testWebService.jar
Binary file not shown.

0 comments on commit fd6616d

Please sign in to comment.