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