Skip to content

Commit

Permalink
First tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CaelmBleidd committed Nov 1, 2023
1 parent 3b00684 commit 6c39506
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,25 @@ package org.jacodb.configuration

import kotlinx.coroutines.runBlocking
import org.jacodb.api.JcClasspath
import org.jacodb.api.ext.findClass
import org.jacodb.api.ext.methods
import org.jacodb.impl.features.classpaths.UnknownClasses
import org.jacodb.testing.BaseTest
import org.jacodb.testing.WithDB
import org.jacodb.testing.allClasspath
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class ConfigurationTest : BaseTest() {
companion object : WithDB()

override val cp: JcClasspath by lazy {
runBlocking {
val configPath = "/ourConfig.json"
val defaultConfigResource = TaintConfigurationFeature::class.java.getResourceAsStream(configPath)
?: error("No such resource found: $configPath")
val configJson = defaultConfigResource.bufferedReader().readText()
val configurationFeature = TaintConfigurationFeature.fromJson(configJson)
val features = listOf(configurationFeature, UnknownClasses)
db.classpath(allClasspath, features)
}
override val cp: JcClasspath = runBlocking {
val configPath = "/testJsonConfig.json"
val testConfig = this::class.java.getResourceAsStream(configPath)
?: error("No such resource found: $configPath")
val configJson = testConfig.bufferedReader().readText()
val configurationFeature = TaintConfigurationFeature.fromJson(configJson)
val features = listOf(configurationFeature, UnknownClasses)
db.classpath(allClasspath, features)
}

@Test
@Disabled("We must have a special file with configs for tests")
fun test() {
val feature = cp.taintConfigurationFeature()
private val taintFeature = cp.taintConfigurationFeature()

val clazz = cp.findClass<java.net.URL>()
val rules = clazz.methods.associateWith { feature.getConfigForMethod(it) }

assertTrue(rules.any { it.value.isNotEmpty() })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jacodb.configuration

import org.junit.jupiter.api.Test

class UtilTest {
@Test
fun testExtractAlternatives() {
val pkgMatcher = NamePatternMatcher("(org\\.jacodb)|.*\\.org\\.company")
val classNameMatcher = NamePatternMatcher(".*Re(quest|sponse)|ClassName|.*|Class(A)*")

val classMatcher = ClassMatcher(pkgMatcher, classNameMatcher)
val alternatives = classMatcher.extractAlternatives()

require(alternatives.size == 10)

val extractPackageMatcher = alternatives.count {
it.pkg is NameExactMatcher && (it.pkg as NameExactMatcher).name == "org.jacodb"
}
require(extractPackageMatcher == 5)

val patternPackageMatcher = alternatives.count {
it.pkg is NamePatternMatcher && (it.pkg as NamePatternMatcher).pattern == ".*\\.org\\.company"
}
require(patternPackageMatcher == 5)

val requestClassMatcher = alternatives.count {
it.classNameMatcher is NamePatternMatcher && (it.classNameMatcher as NamePatternMatcher).pattern == ".*Request"
}
require(requestClassMatcher == 2)

val responseClassMatcher = alternatives.count {
it.classNameMatcher is NamePatternMatcher && (it.classNameMatcher as NamePatternMatcher).pattern == ".*Response"
}
require(responseClassMatcher == 2)

val classNameMatcherValue = alternatives.count {
it.classNameMatcher is NameExactMatcher && (it.classNameMatcher as NameExactMatcher).name == "ClassName"
}
require(classNameMatcherValue == 2)

val allClassNamesMatcher = alternatives.count {
it.classNameMatcher is NamePatternMatcher && (it.classNameMatcher as NamePatternMatcher).pattern == ".*"
}
require(allClassNamesMatcher == 2)

val classAPattern = alternatives.count {
it.classNameMatcher is NamePatternMatcher && (it.classNameMatcher as NamePatternMatcher).pattern == "Class(A)*"
}
require(classAPattern == 2)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit 6c39506

Please sign in to comment.