Skip to content

Commit

Permalink
fix borders
Browse files Browse the repository at this point in the history
  • Loading branch information
arksap2002 committed Aug 21, 2023
1 parent ed689f6 commit 4e0544d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,24 @@ class Workspace(private val project: Project) : Disposable {
var projectClassPath: String? = null
var testResultDirectory: String? = null
var testResultName: String? = null

// The path to save the generated test results.
var resultPath: String? = null

// The base directory of the project.
var baseDir: String? = null
var vFile: VirtualFile? = null

// The URL of the file being tested.
var fileUrl: String? = null

// The modification stamp of the file being tested.
var modificationStamp: Long? = null
var cutPsiClass: PsiClass? = null

// The module to cut.
var cutModule: Module? = null

// The fully qualified name of the class being tested.
var classFQN: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.OpenFileDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
Expand All @@ -46,7 +45,6 @@ import org.jetbrains.research.testspark.data.Report
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.tools.evosuite.validation.Validator
import org.jetbrains.research.testspark.tools.getBuildPath
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Dimension
Expand Down Expand Up @@ -837,8 +835,8 @@ class TestCaseDisplayService(private val project: Project) {
textFieldEditor.editor!!.markupModel.removeAllHighlighters()

resetButton.isEnabled = document.text != testCase.testCode
resetToLastRunButton.isEnabled = document.text != lastRunCode && document.text != testCase.testCode
runTestButton.isEnabled = document.text != lastRunCode
resetToLastRunButton.isEnabled = document.text != lastRunCode
runTestButton.isEnabled = document.text != lastRunCode && document.text != testCase.testCode

textFieldEditor.border =
when (document.text) {
Expand Down Expand Up @@ -868,10 +866,15 @@ class TestCaseDisplayService(private val project: Project) {
resetButton.addActionListener {
WriteCommandAction.runWriteCommandAction(project) {
document.setText(testCase.testCode)
project.service<Workspace>().updateTestCase(testCase)
resetButton.isEnabled = false
resetToLastRunButton.isEnabled = false
runTestButton.isEnabled = false
project.service<Workspace>().updateTestCase(testCase)
if ((initialBorder as MatteBorder).matteColor == JBColor.GREEN) {
project.service<TestsExecutionResultService>().addPassingTest(testCase.testName)
} else {
project.service<TestsExecutionResultService>().removeFromPassingTest(testCase.testName)
}
textFieldEditor.border = initialBorder
textFieldEditor.editor!!.markupModel.removeAllHighlighters()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.io.FileUtilRt
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.tools.getBuildPath
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.util.*
import java.util.UUID
import kotlin.collections.ArrayList
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
Expand Down Expand Up @@ -163,6 +162,10 @@ class TestCoverageCollectorService(private val project: Project) {
val jacocoCLIDir = project.service<TestCoverageCollectorService>().getLibrary("jacococli.jar")
val sourceRoots = ModuleRootManager.getInstance(project.service<Workspace>().cutModule!!).getSourceRoots(false)

// unique name
var name = if (generatedTestPackage.isEmpty()) "" else "$generatedTestPackage."
name += "$className#$testCaseName"

// run the test method with jacoco agent
val testExecutionError = runCommandLine(
arrayListOf(
Expand All @@ -173,7 +176,7 @@ class TestCoverageCollectorService(private val project: Project) {
project.service<TestCoverageCollectorService>().getPath(projectBuildPath)
}${project.service<TestCoverageCollectorService>().getLibrary("JUnitRunner.jar")}:$resultPath",
"org.jetbrains.research.SingleJUnitTestRunner",
"$generatedTestPackage.$className#$testCaseName",
name,
),
)

Expand Down Expand Up @@ -293,11 +296,20 @@ class TestCoverageCollectorService(private val project: Project) {
return result
}

/**
* Update the code of the test.
*
* @param testCode new code of test
* @param testName the name of the test
*/
fun updateTestCode(testCode: String, testName: String) {
// generate the fileName
// TODO check for unique name
val fileName: String = ('A'..'Z').toList().random().toString() +
(List(20) { ('a'..'z').toList().random() }.joinToString("")) +
".java"
(List(20) { ('a'..'z').toList().random() }.joinToString("")) +
".java"

// generate code from document
val code = project.service<JavaClassBuilderService>().generateCode(
fileName.split(".")[0],
testCode,
Expand All @@ -307,29 +319,27 @@ class TestCoverageCollectorService(private val project: Project) {
project.service<Workspace>().testGenerationData.otherInfo,
)

// get buildPath
var buildPath: String = ProjectRootManager.getInstance(project).contentRoots.first().path
if (project.service<SettingsProjectService>().state.buildPath.isEmpty()) {
// User did not set own path
buildPath = getBuildPath(project)
}

// save new test to file
val generatedTestPath: String = project.service<TestCoverageCollectorService>().saveGeneratedTests(
project.service<Workspace>().testGenerationData.packageLine,
code,
project.service<Workspace>().resultPath!!,
fileName,
)

// compilation checking
if (!project.service<TestCoverageCollectorService>().compileCode(generatedTestPath, buildPath).first) {
project.service<TestsExecutionResultService>().removeFromPassingTest(testName)
} else {
val dataFileName = "${project.service<Workspace>().resultPath!!}/jacoco-${
(
List(20) {
('a'..'z').toList().random()
}.joinToString("")
)
}"
// TODO check for unique name
val dataFileName = "${project.service<Workspace>().resultPath!!}/jacoco-${(List(20) { ('a'..'z').toList().random() }.joinToString(""))}"

val testExecutionError = project.service<TestCoverageCollectorService>().createXmlFromJacoco(
fileName.split(".")[0],
Expand All @@ -353,7 +363,9 @@ class TestCoverageCollectorService(private val project: Project) {
),
)
}
return
}
}
project.service<Workspace>().updateTestCase(TestCase(testName, testCode, setOf(), setOf(), setOf()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fun getImportsCodeFromTestSuiteCode(testSuiteCode: String?, classFQN: String): M
// get package from a generated code
fun getPackageFromTestSuiteCode(testSuiteCode: String?): String {
testSuiteCode ?: return ""
if (!testSuiteCode.contains("package")) return ""
val result = testSuiteCode.replace("\r\n", "\n").split("\n")
.filter { it.contains("^package".toRegex()) }.joinToString("").split("package ")[1].split(";")[0]
if (result.isBlank()) return ""
Expand All @@ -46,9 +47,7 @@ fun getPackageFromTestSuiteCode(testSuiteCode: String?): String {
* Saves the data related to test generation in the specified project's workspace.
*
* @param project The project in which the test generation data will be saved.
* @param report The report object to be added to the test generation result list.
* @param resultName The name of the test generation result.
* @param fileUrl The URL of the file where the test generation data will be saved.
* @param report The report object to be added to the test generation result list.\
* @param packageLine The package declaration line of the test generation data.
* @param importsCode The import statements code of the test generation data.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package org.jetbrains.research.testspark.tools.evosuite.generation

import com.google.gson.Gson
import com.google.gson.stream.JsonReader
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import org.evosuite.utils.CompactReport
import org.jetbrains.research.testspark.data.Report
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.TestCoverageCollectorService
import org.jetbrains.research.testspark.tools.getImportsCodeFromTestSuiteCode
import org.jetbrains.research.testspark.tools.getPackageFromTestSuiteCode
import org.jetbrains.research.testspark.tools.saveData
Expand Down Expand Up @@ -45,7 +42,7 @@ class ResultWatcher(
project,
Report(testGenerationResult),
getPackageFromTestSuiteCode(testGenerationResult.testSuiteCode),
getImportsCodeFromTestSuiteCode(testGenerationResult.testSuiteCode, classFQN)
getImportsCodeFromTestSuiteCode(testGenerationResult.testSuiteCode, classFQN),
)
}
}

0 comments on commit 4e0544d

Please sign in to comment.