Skip to content

Commit 4da0878

Browse files
merge
1 parent 28e6636 commit 4da0878

File tree

227 files changed

+20618
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+20618
-15
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import java.nio.file.Path
4040
import java.nio.file.Paths
4141
import java.time.LocalDateTime
4242
import java.time.temporal.ChronoUnit
43+
import org.utbot.engine.greyboxfuzzer.util.CustomClassLoader
4344

4445
private const val LONG_GENERATION_TIMEOUT = 1_200_000L
4546

@@ -144,8 +145,16 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
144145
protected fun getWorkingDirectory(classFqn: String): Path? {
145146
val classRelativePath = classFqnToPath(classFqn) + ".class"
146147
val classAbsoluteURL = classLoader.getResource(classRelativePath) ?: return null
147-
val classAbsolutePath = replaceSeparator(classAbsoluteURL.toPath().toString())
148-
.removeSuffix(classRelativePath)
148+
val classAbsolutePath =
149+
if (classAbsoluteURL.toURI().scheme == "jar") {
150+
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
151+
.removeSuffix(classRelativePath)
152+
.removeSuffix("/")
153+
.removeSuffix("!")
154+
} else {
155+
replaceSeparator(classAbsoluteURL.toPath().toString())
156+
.removeSuffix(classRelativePath)
157+
}
149158
return Paths.get(classAbsolutePath)
150159
}
151160

@@ -155,15 +164,17 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
155164
sourceCodeFile: Path? = null,
156165
searchDirectory: Path,
157166
chosenClassesToMockAlways: Set<ClassId>
158-
): List<UtMethodTestSet> =
159-
testCaseGenerator.generate(
167+
): List<UtMethodTestSet> {
168+
CustomClassLoader.classLoader = classLoader
169+
return testCaseGenerator.generate(
160170
targetMethods,
161171
mockStrategy,
162172
chosenClassesToMockAlways,
163173
generationTimeout
164174
).map {
165175
if (sourceCodeFile != null) it.summarize(searchDirectory, sourceCodeFile.toFile()) else it
166176
}
177+
}
167178

168179

169180
protected fun withLogger(targetClassFqn: String, block: Runnable) {

utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ object FileUtil {
9090

9191
for (clazz in classes) {
9292
val path = clazz.toClassFilePath()
93-
val resource = clazz.classLoader.getResource(path) ?: error("No such file: $path")
93+
val resource =
94+
clazz.classLoader.getResource(path)
95+
?: ClassLoader.getSystemClassLoader().getResource(path)
96+
?: error("No such file: $path")
9497

9598
if (resource.toURI().scheme == "jar") {
9699
val jarLocation = resource.toURI().extractJarName()

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
240240
/**
241241
* Set to true to start fuzzing if symbolic execution haven't return anything
242242
*/
243-
var useFuzzing: Boolean by getBooleanProperty(true)
243+
var useFuzzing: Boolean by getBooleanProperty(false)
244+
245+
/**
246+
* Set to true to start grey-box fuzzing
247+
*/
248+
var useGreyBoxFuzzing: Boolean by getBooleanProperty(true)
244249

245250
/**
246251
* Set the total attempts to improve coverage by fuzzer.

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,13 @@ data class UtArrayModel(
480480
* @param instantiationCall is an [UtExecutableCallModel] to instantiate represented object. It **must** not return `null`.
481481
* @param modificationsChain is a chain of [UtStatementModel] to construct object state.
482482
*/
483-
data class UtAssembleModel private constructor(
483+
data class UtAssembleModel constructor(
484484
override val id: Int?,
485485
override val classId: ClassId,
486486
override val modelName: String,
487487
val instantiationCall: UtExecutableCallModel,
488-
val modificationsChain: List<UtStatementModel>,
488+
//TODO: get rid of var
489+
var modificationsChain: List<UtStatementModel>,
489490
val origin: UtCompositeModel?
490491
) : UtReferenceModel(id, classId, modelName) {
491492

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/UtExecutionResult.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.framework.plugin.api
22

3+
import org.utbot.framework.plugin.api.util.objectClassId
34
import org.utbot.framework.plugin.api.visible.UtStreamConsumingException
45
import java.io.File
56
import java.util.LinkedList
@@ -21,6 +22,10 @@ sealed class UtExecutionFailure : UtExecutionResult() {
2122
get() = exception
2223
}
2324

25+
data class UtExecutionSuccessConcrete(val result: Any?) : UtExecutionResult() {
26+
override fun toString() = "$result"
27+
}
28+
2429
data class UtOverflowFailure(
2530
override val exception: Throwable,
2631
) : UtExecutionFailure()
@@ -102,7 +107,14 @@ inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit):
102107
return this
103108
}
104109

110+
fun UtExecutionResult.getOrThrow(): UtModel = when (this) {
111+
is UtExecutionSuccess -> model
112+
is UtExecutionFailure -> throw exception
113+
is UtExecutionSuccessConcrete -> UtNullModel(objectClassId)
114+
}
115+
105116
fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) {
106117
is UtExecutionFailure -> rootCauseException
107118
is UtExecutionSuccess -> null
119+
is UtExecutionSuccessConcrete -> null
108120
}

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/constructor/ValueConstructor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.utbot.framework.plugin.api.UtExecution
2626
import org.utbot.framework.plugin.api.UtExecutionFailure
2727
import org.utbot.framework.plugin.api.UtExecutionResult
2828
import org.utbot.framework.plugin.api.UtExecutionSuccess
29+
import org.utbot.framework.plugin.api.UtExecutionSuccessConcrete
2930
import org.utbot.framework.plugin.api.UtLambdaModel
3031
import org.utbot.framework.plugin.api.UtMockValue
3132
import org.utbot.framework.plugin.api.UtModel
@@ -42,6 +43,7 @@ import org.utbot.framework.plugin.api.util.anyInstance
4243
import org.utbot.framework.plugin.api.util.constructLambda
4344
import org.utbot.framework.plugin.api.util.constructStaticLambda
4445
import org.utbot.framework.plugin.api.util.constructor
46+
import org.utbot.framework.plugin.api.util.id
4547
import org.utbot.framework.plugin.api.util.isStatic
4648
import org.utbot.framework.plugin.api.util.jClass
4749
import org.utbot.framework.plugin.api.util.jField
@@ -492,6 +494,7 @@ class ValueConstructor {
492494
private fun <R> UtExecutionResult.map(transform: (model: UtModel) -> R): Result<R> = when (this) {
493495
is UtExecutionSuccess -> Result.success(transform(model))
494496
is UtExecutionFailure -> Result.failure(exception)
497+
is UtExecutionSuccessConcrete -> Result.success(transform(UtNullModel(Any::class.java.id)))
495498
}
496499

497500
/**

utbot-framework/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ dependencies {
3434
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlinLoggingVersion
3535
implementation group: 'org.jacoco', name: 'org.jacoco.report', version: jacocoVersion
3636
implementation group: 'org.apache.commons', name: 'commons-text', version: apacheCommonsTextVersion
37+
implementation "org.javaruntype:javaruntype:1.3"
38+
implementation "ru.vyarus:generics-resolver:3.0.3"
39+
implementation "ognl:ognl:3.3.2"
40+
3741
// we need this for construction mocks from composite models
3842
implementation group: 'org.mockito', name: 'mockito-core', version: '4.2.0'
3943

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Produced;
29+
import org.utbot.quickcheck.Property;
30+
import org.utbot.quickcheck.generator.Generator;
31+
32+
import java.lang.annotation.Repeatable;
33+
import java.lang.annotation.Retention;
34+
import java.lang.annotation.Target;
35+
36+
import static java.lang.annotation.ElementType.*;
37+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
38+
39+
/**
40+
* <p>Mark a parameter of a {@link Property} method with this annotation to
41+
* have random values supplied to it via the specified
42+
* {@link Generator}.</p>
43+
*
44+
* <p>You may specify as many of these annotations as as you wish on a given
45+
* parameter. On a given generation, one of the specified generators will be
46+
* chosen at random with probability in proportion to {@link #frequency()}.</p>
47+
*
48+
* <p>If any such generator produces values of a type incompatible with the
49+
* type of the marked parameter, {@link IllegalArgumentException} is
50+
* raised.</p>
51+
*/
52+
@Target({ PARAMETER, FIELD, ANNOTATION_TYPE, TYPE_USE })
53+
@Retention(RUNTIME)
54+
@Repeatable(Produced.class)
55+
public @interface From {
56+
/**
57+
* @return the generator to be used for the annotated property parameter
58+
*/
59+
Class<? extends Generator> value();
60+
61+
/**
62+
* @return a weight to influence how often the generator is chosen
63+
*/
64+
int frequency() default 1;
65+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Property;
29+
30+
/**
31+
* <p>Allows access to an actual failing example.</p>
32+
*
33+
* <p>This may be useful if any of the objects' {@link Object#toString()}
34+
* representations is difficult to understand.</p>
35+
*
36+
* @see Property#onMinimalCounterexample()
37+
*/
38+
public interface MinimalCounterexampleHook {
39+
/**
40+
* @param counterexample the minimal counterexample (after shrinking)
41+
* @param action work to perform with the minimal counterexample;
42+
* for example, this could repeat the test using the same inputs.
43+
* This action should be safely callable multiple times.
44+
*/
45+
void handle(Object[] counterexample, Runnable action);
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Property;
29+
import org.utbot.quickcheck.internal.ParameterSampler;
30+
import org.utbot.quickcheck.internal.sampling.ExhaustiveParameterSampler;
31+
import org.utbot.quickcheck.internal.sampling.TupleParameterSampler;
32+
33+
/**
34+
* Represents different modes of execution of property-based tests.
35+
*
36+
* @see org.utbot.quickcheck.generator.Only
37+
* @see org.utbot.quickcheck.generator.Also
38+
*/
39+
public enum Mode {
40+
/**
41+
* Verify {@link org.utbot.quickcheck.Property#trials()} tuples of arguments for a property's
42+
* parameters.
43+
*/
44+
SAMPLING {
45+
@Override ParameterSampler sampler(int defaultSampleSize) {
46+
return new TupleParameterSampler(defaultSampleSize);
47+
}
48+
},
49+
50+
/**
51+
* Generate {@link Property#trials()} arguments for each parameter
52+
* property, and verify the cross-products of those sets of arguments.
53+
* This behavior mirrors that of the JUnit
54+
* {@link org.junit.experimental.theories.Theories} runner.
55+
*/
56+
EXHAUSTIVE {
57+
@Override ParameterSampler sampler(int defaultSampleSize) {
58+
return new ExhaustiveParameterSampler(defaultSampleSize);
59+
}
60+
};
61+
62+
abstract ParameterSampler sampler(int defaultSampleSize);
63+
}

0 commit comments

Comments
 (0)