Skip to content

Commit

Permalink
avoid crashing if not handled regex in SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuri82 committed Sep 26, 2023
1 parent e138079 commit acdd23d
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions core/src/main/kotlin/org/evomaster/core/sql/SqlActionGeneBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.evomaster.core.sql

import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType
import org.evomaster.client.java.instrumentation.shared.RegexSharedUtils
import org.evomaster.core.logging.LoggingUtil
import org.evomaster.core.sql.schema.Column
import org.evomaster.core.sql.schema.ColumnDataType
import org.evomaster.core.sql.schema.ForeignKey
Expand Down Expand Up @@ -680,33 +681,50 @@ class SqlActionGeneBuilder {
/**
* TODO Handle a conjunction of Regex patterns
*/
log.warn("Handling only a regex pattern for (${column.name}). Using like pattern: ${likePattern}}")
LoggingUtil.uniqueWarn(log, "Handling only a regex pattern for (${column.name}). Using like pattern: ${likePattern}}")
}
buildLikeRegexGene(columnName, likePattern, databaseType = column.databaseType)
} else if (column.javaRegExPattern != null) {
buildJavaRegexGene(column.name, column.javaRegExPattern)
} else {
val columnMinLength = if (isFixedLength) {
column.size
} else {
if (column.minSize !=null && column.minSize>0) {
column.minSize
} else if (column.isNotBlank==true) {
1
} else {
0
}
}
val columnMaxLength = if (column.maxSize!=null) {
minOf(column.maxSize, column.size)
} else {
column.size

try {
buildJavaRegexGene(column.name, column.javaRegExPattern)
} catch (e: Exception){
LoggingUtil.uniqueWarn(log, "Failed to handle regex: ${column.javaRegExPattern}")
buildStringGene(isFixedLength, column)
}
StringGene(name = column.name, minLength = columnMinLength, maxLength = columnMaxLength)
/*
TODO in those cases of regex, shouldn't still check for size constraints?
*/

} else {
buildStringGene(isFixedLength, column)
}
}
}

private fun buildStringGene(
isFixedLength: Boolean,
column: Column,
): StringGene {
val columnMinLength = if (isFixedLength) {
column.size
} else {
if (column.minSize != null && column.minSize > 0) {
column.minSize
} else if (column.isNotBlank == true) {
1
} else {
0
}
}
val columnMaxLength = if (column.maxSize != null) {
minOf(column.maxSize, column.size)
} else {
column.size
}
return StringGene(name = column.name, minLength = columnMinLength, maxLength = columnMaxLength)
}

private fun buildJavaRegexGene(name: String, javaRegExPattern: String): RegexGene {
val fullMatchRegex = RegexSharedUtils.forceFullMatch(javaRegExPattern)
val disjunctionRxGenes = RegexHandler.createGeneForJVM(fullMatchRegex).disjunctions
Expand Down

0 comments on commit acdd23d

Please sign in to comment.