Skip to content

Commit aabbbed

Browse files
authored
Merge pull request #1490 from WebFuzzing/sql-databasetype
`DatabaseType` is copied in SQL-related genes
2 parents 10609f7 + de2999f commit aabbbed

17 files changed

+221
-24
lines changed

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlBinaryStringGene.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ class SqlBinaryStringGene(
2929

3030
private val binaryArrayGene: ArrayGene<IntegerGene> = ArrayGene(name, template = IntegerGene(name, min = 0, max = 255), minSize = minSize, maxSize = maxSize),
3131

32-
val databaseType: DatabaseType = DatabaseType.POSTGRES
32+
databaseType: DatabaseType = DatabaseType.POSTGRES
3333

3434
) : CompositeFixedGene(name, mutableListOf( binaryArrayGene)) {
3535

36+
var databaseType: DatabaseType = databaseType
37+
private set
38+
3639
companion object {
3740
val log: Logger = LoggerFactory.getLogger(SqlBinaryStringGene::class.java)
3841

@@ -80,7 +83,9 @@ class SqlBinaryStringGene(
8083
if (other !is SqlBinaryStringGene) {
8184
return false
8285
}
83-
return binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)
86+
this.databaseType = other.databaseType
87+
val ok = binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)
88+
return ok
8489
}
8590

8691

@@ -99,4 +104,4 @@ class SqlBinaryStringGene(
99104
return false
100105
}
101106

102-
}
107+
}

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlMultidimensionalArrayGene.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SqlMultidimensionalArrayGene<T>(
2929
* The database type of the column where the gene is inserted.
3030
* By default, the database type is POSTGRES
3131
*/
32-
val databaseType: DatabaseType = DatabaseType.POSTGRES,
32+
databaseType: DatabaseType = DatabaseType.POSTGRES,
3333
/**
3434
* The type for this array. Every time we create a new element to add, it has to be based
3535
* on this template
@@ -46,6 +46,9 @@ class SqlMultidimensionalArrayGene<T>(
4646
private val maxDimensionSize: Int = ArrayGene.MAX_SIZE
4747
) : CompositeGene(name, mutableListOf()) where T : Gene {
4848

49+
var databaseType: DatabaseType = databaseType
50+
private set
51+
4952
/**
5053
* Stores what is the size of each dimension.
5154
* It is used to check that the multidimensional array
@@ -335,6 +338,7 @@ class SqlMultidimensionalArrayGene<T>(
335338
val elements = gene.getViewOfChildren().mapNotNull { it.copy() as? T }.toMutableList()
336339
addChildren(elements)
337340
this.dimensionSizes = gene.dimensionSizes
341+
this.databaseType = gene.databaseType
338342
return true
339343
}
340344

@@ -412,4 +416,4 @@ class SqlMultidimensionalArrayGene<T>(
412416
return false
413417
}
414418

415-
}
419+
}

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlGeometryCollectionGene.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SqlGeometryCollectionGene(
2121
/**
2222
* The database type of the source column for this gene
2323
*/
24-
val databaseType: DatabaseType = DatabaseType.H2,
24+
var databaseType: DatabaseType = DatabaseType.H2,
2525
template: Gene,
2626
val elements: ArrayGene<Gene> = ArrayGene(
2727
name = "points",
@@ -85,6 +85,7 @@ class SqlGeometryCollectionGene(
8585
if (other !is SqlGeometryCollectionGene) {
8686
return false
8787
}
88+
this.databaseType = other.databaseType
8889
return this.elements.unsafeCopyValueFrom(other.elements)
8990
}
9091

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlMultiPathGene.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ class SqlMultiPathGene(
1818
/**
1919
* The database type of the source column for this gene
2020
*/
21-
val databaseType: DatabaseType = DatabaseType.MYSQL,
21+
databaseType: DatabaseType = DatabaseType.MYSQL,
2222
val paths: ArrayGene<SqlPathGene> = ArrayGene(
2323
name = "points",
2424
minSize = 1,
2525
template = SqlPathGene("p", databaseType = databaseType)
2626
)
2727
) : CompositeFixedGene(name, mutableListOf(paths)) {
2828

29+
var databaseType: DatabaseType = databaseType
30+
private set
31+
2932
companion object {
3033
val log: Logger = LoggerFactory.getLogger(SqlMultiPathGene::class.java)
3134
}
@@ -92,6 +95,7 @@ class SqlMultiPathGene(
9295
if (other !is SqlMultiPathGene) {
9396
return false
9497
}
98+
this.databaseType = other.databaseType
9599
return this.paths.unsafeCopyValueFrom(other.paths)
96100
}
97101

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlMultiPointGene.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ class SqlMultiPointGene(
2323
/**
2424
* The database type of the source column for this gene
2525
*/
26-
val databaseType: DatabaseType = DatabaseType.H2,
26+
databaseType: DatabaseType = DatabaseType.H2,
2727
val points: ArrayGene<SqlPointGene> = ArrayGene(
2828
name = "points",
2929
minSize = 1, //looks like insertion fails if 0, due to "bad" syntax
3030
template = SqlPointGene("p", databaseType = databaseType)
3131
)
3232
) : CompositeFixedGene(name, mutableListOf(points)) {
3333

34+
var databaseType: DatabaseType = databaseType
35+
private set
36+
3437
init {
3538
if (databaseType!=DatabaseType.H2 && databaseType!=DatabaseType.MYSQL) {
3639
IllegalArgumentException("Cannot create a SqlMultiPointGene with database type ${databaseType}")
@@ -96,14 +99,15 @@ class SqlMultiPointGene(
9699
if (other !is SqlMultiPointGene) {
97100
return false
98101
}
102+
this.databaseType = other.databaseType
99103
return this.points.unsafeCopyValueFrom(other.points)
100104
}
101105

102106
override fun containsSameValueAs(other: Gene): Boolean {
103107
if (other !is SqlMultiPointGene) {
104108
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
105109
}
106-
return this.points.containsSameValueAs(other.points)
110+
return this.databaseType == other.databaseType && this.points.containsSameValueAs(other.points)
107111
}
108112

109113
override fun customShouldApplyShallowMutation(

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlMultiPolygonGene.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SqlMultiPolygonGene(
2323
/**
2424
* The database type of the source column for this gene
2525
*/
26-
val databaseType: DatabaseType = DatabaseType.H2,
26+
databaseType: DatabaseType = DatabaseType.H2,
2727
val polygons: ArrayGene<SqlPolygonGene> = ArrayGene(
2828
name = "polygons",
2929
minSize = 1,
@@ -32,6 +32,9 @@ class SqlMultiPolygonGene(
3232
databaseType = databaseType))
3333
) : CompositeFixedGene(name, mutableListOf(polygons)) {
3434

35+
var databaseType: DatabaseType = databaseType
36+
private set
37+
3538
companion object {
3639
val log: Logger = LoggerFactory.getLogger(SqlMultiPolygonGene::class.java)
3740
}
@@ -97,14 +100,15 @@ class SqlMultiPolygonGene(
97100
if (other !is SqlMultiPolygonGene) {
98101
return false
99102
}
103+
this.databaseType = other.databaseType
100104
return this.polygons.unsafeCopyValueFrom(other.polygons)
101105
}
102106

103107
override fun containsSameValueAs(other: Gene): Boolean {
104108
if (other !is SqlMultiPolygonGene) {
105109
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
106110
}
107-
return this.polygons.containsSameValueAs(other.polygons)
111+
return this.databaseType == other.databaseType && this.polygons.containsSameValueAs(other.polygons)
108112
}
109113

110114
override fun customShouldApplyShallowMutation(

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlPathGene.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ import org.slf4j.LoggerFactory
1919
*/
2020
class SqlPathGene(
2121
name: String,
22-
val databaseType: DatabaseType = DatabaseType.POSTGRES,
22+
databaseType: DatabaseType = DatabaseType.POSTGRES,
2323
val points: ArrayGene<SqlPointGene> = ArrayGene(
2424
name = "points",
2525
// paths are lists of at least 2 points
2626
minSize = 2,
2727
template = SqlPointGene("p", databaseType = databaseType))
2828
) : CompositeFixedGene(name, mutableListOf(points)) {
2929

30+
var databaseType: DatabaseType = databaseType
31+
private set
32+
3033
companion object {
3134
val log: Logger = LoggerFactory.getLogger(SqlPathGene::class.java)
3235
}
@@ -93,7 +96,9 @@ class SqlPathGene(
9396
if (other !is SqlPathGene) {
9497
return false
9598
}
96-
return this.points.unsafeCopyValueFrom(other.points)
99+
this.databaseType = other.databaseType
100+
val ok = this.points.unsafeCopyValueFrom(other.points)
101+
return ok
97102
}
98103

99104
override fun containsSameValueAs(other: Gene): Boolean {

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlPointGene.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ class SqlPointGene(
1717
name: String,
1818
val x: FloatGene = FloatGene(name = "x"),
1919
val y: FloatGene = FloatGene(name = "y"),
20-
val databaseType: DatabaseType = DatabaseType.POSTGRES
20+
databaseType: DatabaseType = DatabaseType.POSTGRES
2121
) : CompositeFixedGene(name, mutableListOf(x, y)) {
2222

23+
var databaseType: DatabaseType = databaseType
24+
private set
25+
2326
companion object {
2427
val log: Logger = LoggerFactory.getLogger(SqlPointGene::class.java)
2528
}
@@ -71,8 +74,10 @@ class SqlPointGene(
7174
if (other !is SqlPointGene) {
7275
return false
7376
}
74-
return this.x.unsafeCopyValueFrom(other.x)
77+
this.databaseType = other.databaseType
78+
val ok = this.x.unsafeCopyValueFrom(other.x)
7579
&& this.y.unsafeCopyValueFrom(other.y)
80+
return ok
7681
}
7782

7883
override fun containsSameValueAs(other: Gene): Boolean {

core/src/main/kotlin/org/evomaster/core/search/gene/sql/geometric/SqlPolygonGene.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import java.awt.geom.Line2D
2020
*/
2121
class SqlPolygonGene(
2222
name: String,
23-
val databaseType: DatabaseType = DatabaseType.POSTGRES,
23+
databaseType: DatabaseType = DatabaseType.POSTGRES,
2424
val minLengthOfPolygonRing: Int = 2,
2525
val onlyNonIntersectingPolygons: Boolean = false,
2626
val points: ArrayGene<SqlPointGene> = ArrayGene(
@@ -30,6 +30,9 @@ class SqlPolygonGene(
3030
template = SqlPointGene("p", databaseType = databaseType))
3131
) : CompositeFixedGene(name, mutableListOf(points)) {
3232

33+
var databaseType: DatabaseType = databaseType
34+
private set
35+
3336
companion object {
3437
val log: Logger = LoggerFactory.getLogger(SqlPolygonGene::class.java)
3538
}
@@ -180,7 +183,9 @@ class SqlPolygonGene(
180183
if (other !is SqlPolygonGene) {
181184
return false
182185
}
183-
return this.points.unsafeCopyValueFrom(other.points)
186+
this.databaseType = other.databaseType
187+
val ok = this.points.unsafeCopyValueFrom(other.points)
188+
return ok
184189
}
185190

186191
override fun containsSameValueAs(other: Gene): Boolean {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.evomaster.core.search.gene.sql
2+
3+
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType
4+
import org.evomaster.core.search.gene.numeric.IntegerGene
5+
import org.evomaster.core.search.gene.collection.ArrayGene
6+
import org.evomaster.core.search.gene.utils.GeneUtils.SINGLE_APOSTROPHE_PLACEHOLDER
7+
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Assertions.assertTrue
9+
import org.junit.jupiter.api.Test
10+
11+
class SqlBinaryStringGeneTest {
12+
13+
@Test
14+
fun testPrintEmptyBinaryString() {
15+
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.POSTGRES)
16+
assertEquals("\"\\x\"", gene.getValueAsPrintableString())
17+
}
18+
19+
@Test
20+
fun testPrintNonEmptyBinaryStringPostgres() {
21+
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.POSTGRES)
22+
val arrayGene = gene.getViewOfChildren()[0] as ArrayGene<IntegerGene>
23+
arrayGene.addElement(IntegerGene("g0", value = 10)) // 0a
24+
arrayGene.addElement(IntegerGene("g1", value = 255)) // ff
25+
assertEquals("\"\\x0aff\"", gene.getValueAsPrintableString())
26+
}
27+
28+
@Test
29+
fun testPrintNonEmptyBinaryStringH2() {
30+
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.H2)
31+
val arrayGene = gene.getViewOfChildren()[0] as ArrayGene<IntegerGene>
32+
arrayGene.addElement(IntegerGene("g0", value = 10)) // 0a
33+
arrayGene.addElement(IntegerGene("g1", value = 255)) // ff
34+
assertEquals("X${SINGLE_APOSTROPHE_PLACEHOLDER}0aff${SINGLE_APOSTROPHE_PLACEHOLDER}", gene.getValueAsPrintableString())
35+
}
36+
37+
@Test
38+
fun testUnsafeCopyValueFromModifiesDatabaseType() {
39+
val gene1 = SqlBinaryStringGene(
40+
name = "bin1",
41+
databaseType = DatabaseType.POSTGRES
42+
)
43+
val gene2 = SqlBinaryStringGene(
44+
name = "bin2",
45+
databaseType = DatabaseType.MYSQL
46+
)
47+
48+
val ok = gene1.unsafeCopyValueFrom(gene2)
49+
50+
assertTrue(ok)
51+
assertEquals(DatabaseType.MYSQL, gene1.databaseType)
52+
}
53+
}

0 commit comments

Comments
 (0)