Skip to content

Commit 197d6c4

Browse files
committed
tests and fix for #127
1 parent 27ab639 commit 197d6c4

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

jacodb-core/src/main/kotlin/org/jacodb/impl/storage/SQLitePersistenceImpl.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.jacodb.impl.storage
1818

1919
import org.jacodb.impl.FeaturesRegistry
2020
import org.jacodb.impl.fs.JavaRuntime
21+
import org.jacodb.impl.fs.logger
22+
import org.jacodb.impl.storage.jooq.tables.references.BYTECODELOCATIONS
2123
import org.jooq.DSLContext
2224
import org.jooq.SQLDialect
2325
import org.jooq.conf.Settings
@@ -60,13 +62,28 @@ class SQLitePersistenceImpl(
6062
connection = dataSource.connection
6163
jooq = DSL.using(connection, SQLDialect.SQLITE, Settings().withExecuteLogging(false))
6264
write {
63-
if (clearOnStart) {
65+
if (clearOnStart || !runtimeProcessed) {
6466
jooq.executeQueriesFrom("sqlite/drop-schema.sql")
6567
}
6668
jooq.executeQueriesFrom("sqlite/create-schema.sql")
6769
}
6870
}
6971

72+
private val runtimeProcessed: Boolean
73+
get() {
74+
try {
75+
val count = jooq.fetchCount(
76+
BYTECODELOCATIONS,
77+
BYTECODELOCATIONS.STATE.notEqual(LocationState.PROCESSED.ordinal)
78+
.and(BYTECODELOCATIONS.RUNTIME.isTrue)
79+
)
80+
return count == 0
81+
} catch (e: Exception) {
82+
logger.warn("can't check that runtime libraries is processed with", e)
83+
return false
84+
}
85+
}
86+
7087
override fun <T> write(action: (DSLContext) -> T): T = lock.withLock {
7188
action(jooq)
7289
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2022 UnitTestBot contributors (utbot.org)
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jacodb.testing.persistence
18+
19+
import kotlinx.coroutines.runBlocking
20+
import org.jacodb.impl.FeaturesRegistry
21+
import org.jacodb.impl.JcSettings
22+
import org.jacodb.impl.features.Builders
23+
import org.jacodb.impl.features.Usages
24+
import org.jacodb.impl.fs.JavaRuntime
25+
import org.jacodb.impl.jacodb
26+
import org.jacodb.impl.storage.LocationState
27+
import org.jacodb.impl.storage.SQLitePersistenceImpl
28+
import org.jacodb.impl.storage.jooq.tables.references.BYTECODELOCATIONS
29+
import org.jacodb.testing.allClasspath
30+
import org.jooq.DSLContext
31+
import org.junit.jupiter.api.Assertions.assertEquals
32+
import org.junit.jupiter.api.Assertions.assertTrue
33+
import org.junit.jupiter.api.BeforeEach
34+
import org.junit.jupiter.api.Test
35+
import java.io.File
36+
import java.nio.file.Files
37+
38+
39+
class IncompleteDataTest {
40+
41+
private val jdbcLocation = Files.createTempFile("jcdb-", null).toFile().absolutePath
42+
private lateinit var javaHome: File
43+
44+
@BeforeEach
45+
fun setupDB() {
46+
javaHome = JcSettings().useProcessJavaRuntime().jre
47+
runBlocking {
48+
newDB(true).also {
49+
it.close()
50+
}
51+
}
52+
}
53+
54+
@Test
55+
fun `if runtime is not processed schema should be dropped`() {
56+
withPersistence { jooq ->
57+
jooq.update(BYTECODELOCATIONS)
58+
.set(BYTECODELOCATIONS.STATE, LocationState.AWAITING_INDEXING.ordinal)
59+
.execute()
60+
}
61+
val db = newDB(true)
62+
db.persistence.read {
63+
val count = it.fetchCount(
64+
BYTECODELOCATIONS,
65+
BYTECODELOCATIONS.STATE.notEqual(LocationState.PROCESSED.ordinal)
66+
)
67+
assertEquals(0, count)
68+
}
69+
}
70+
71+
@Test
72+
fun `if runtime is processed unprocessed libraries should be outdated`() {
73+
val ids = arrayListOf<Long>()
74+
withPersistence { jooq ->
75+
jooq.update(BYTECODELOCATIONS)
76+
.set(BYTECODELOCATIONS.STATE, LocationState.AWAITING_INDEXING.ordinal)
77+
.where(BYTECODELOCATIONS.RUNTIME.isFalse)
78+
.execute()
79+
jooq.selectFrom(BYTECODELOCATIONS)
80+
.where(BYTECODELOCATIONS.RUNTIME.isFalse)
81+
.fetch {
82+
ids.add(it.id!!)
83+
}
84+
}
85+
val db = newDB(true)
86+
db.persistence.read {
87+
it.selectFrom(BYTECODELOCATIONS)
88+
.where(BYTECODELOCATIONS.STATE.notEqual(LocationState.PROCESSED.ordinal))
89+
.fetch {
90+
assertTrue(
91+
ids.contains(it.id!!),
92+
"expected ${it.path} to be in PROCESSED state buy is in ${LocationState.values()[it.state!!]}"
93+
)
94+
95+
}
96+
}
97+
}
98+
99+
100+
private fun withPersistence(action: (DSLContext) -> Unit) {
101+
val persistence = SQLitePersistenceImpl(
102+
JavaRuntime(javaHome), FeaturesRegistry(emptyList()), jdbcLocation, false
103+
)
104+
persistence.use {
105+
it.write {
106+
action(it)
107+
}
108+
}
109+
}
110+
111+
private fun newDB(awaitBackground: Boolean) = runBlocking {
112+
jacodb {
113+
useProcessJavaRuntime()
114+
persistent(jdbcLocation)
115+
installFeatures(Usages, Builders)
116+
loadByteCode(allClasspath)
117+
}.also {
118+
if (awaitBackground) {
119+
it.awaitBackgroundJobs()
120+
}
121+
}
122+
}
123+
124+
}

0 commit comments

Comments
 (0)