Skip to content

Commit

Permalink
#16 do not enforce not using the nullable flag when there is an @Elem…
Browse files Browse the repository at this point in the history
…entCollection annotation (#18)
  • Loading branch information
klu2 authored Sep 21, 2022
1 parent 5fb6a12 commit 8c7f3cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields
import io.cloudflight.cleancode.archunit.ArchRuleWithId.Companion.archRuleWithId
import io.cloudflight.cleancode.archunit.utils.AreKotlinClasses
import javax.persistence.Column
import javax.persistence.ElementCollection
import kotlin.reflect.KProperty1

class JpaRuleSet {
Expand All @@ -18,7 +19,9 @@ class JpaRuleSet {
val do_not_use_column_nullable =
archRuleWithId(
"jpa.entities-not-null-instead-of-nullable",
fields().should(NotUseTheAnnotationField(Column::nullable))
fields().that()
.areNotAnnotatedWith(ElementCollection::class.java)
.should(NotUseTheAnnotationField(Column::nullable))
)

@ArchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.tngtech.archunit.core.importer.ClassFileImporter
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.time.OffsetDateTime
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.ManyToOne
import javax.persistence.*
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

Expand Down Expand Up @@ -72,6 +69,12 @@ class JpaRuleSetTest {
rules.nullable_flag_of_kotlin_needs_to_match_jpa_specification.check(importer)
}
}

@Test
fun `nullable column with element collection`() {
val importer = ClassFileImporter().importClasses(EntityWithFiles::class.java)
rules.do_not_use_column_nullable.check(importer)
}
}

@Entity
Expand Down Expand Up @@ -148,4 +151,19 @@ class EntityWithJoinToOtherOptionalErronousEntity(
val id: Long = 0,
@ManyToOne(optional = false)
var someEntity: EntityWithColumnLength?
)
)


class EntityWithFiles {
@ElementCollection
@CollectionTable(
name = "some_entity_file",
joinColumns = [JoinColumn(name = "some_entity_id")],
uniqueConstraints = [UniqueConstraint(name = "unique_some_entity_file_id", columnNames = ["file_id"])]
)
@Column(name = "file_id", nullable = false)
var someFiles = mutableSetOf<FileReference>()
}

@Entity
class FileReference

0 comments on commit 8c7f3cf

Please sign in to comment.