Skip to content

Commit e888741

Browse files
committed
Port framework independent tests also to JDA integ tests
1 parent db9c5c0 commit e888741

4 files changed

Lines changed: 190 additions & 4 deletions

File tree

config/codenarc/codenarc.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ ruleset {
5858
EmptyIfStatement
5959
EmptyInstanceInitializer
6060
EmptyMethod {
61-
doNotApplyToClassNames = 'net.kautler.command.integ.test.VersionIntegTest$VersionHolder'
61+
doNotApplyToClassNames = [
62+
'net.kautler.command.integ.test.javacord.VersionIntegTest$VersionHolder',
63+
'net.kautler.command.integ.test.jda.VersionIntegTest$VersionHolder'
64+
].join(', ')
6265
}
6366
EmptyStaticInitializer
6467
EmptySwitchStatement
@@ -571,10 +574,11 @@ ruleset {
571574
UnusedArray
572575
UnusedMethodParameter {
573576
doNotApplyToClassNames = [
574-
'net.kautler.command.integ.test.VersionIntegTest$VersionHolder',
575577
'net.kautler.command.integ.test.javacord.PingSlashIntegTest$SlashCommandRegisterer',
578+
'net.kautler.command.integ.test.javacord.VersionIntegTest$VersionHolder',
576579
'net.kautler.command.integ.test.javacord.event.CommandNotFoundEventJavacordSlashIntegTest$SlashCommandRegisterer',
577580
'net.kautler.command.integ.test.jda.PingSlashIntegTest$SlashCommandRegisterer',
581+
'net.kautler.command.integ.test.jda.VersionIntegTest$VersionHolder',
578582
'net.kautler.command.integ.test.jda.event.CommandNotFoundEventJdaSlashIntegTest$SlashCommandRegisterer'
579583
].join(', ')
580584
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2020-2025 Björn Kautler
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 net.kautler.command.integ.test.javacord
18+
19+
import jakarta.enterprise.context.ApplicationScoped
20+
import jakarta.enterprise.context.Initialized
21+
import jakarta.enterprise.event.Observes
22+
import jakarta.enterprise.inject.Vetoed
23+
import jakarta.inject.Inject
24+
import net.kautler.command.api.Version
25+
import net.kautler.command.integ.test.spock.AddBean
26+
import spock.lang.ResourceLock
27+
import spock.lang.Specification
28+
import spock.lang.Subject
29+
30+
@Subject(Version)
31+
class VersionIntegTest extends Specification {
32+
@AddBean(VersionHolder)
33+
@ResourceLock('net.kautler.command.integ.test.VersionIntegTest.VersionHolder.version')
34+
def 'version should be known'() {
35+
expect:
36+
VersionHolder.version.version != '<unknown>'
37+
}
38+
39+
@AddBean(VersionHolder)
40+
@ResourceLock('net.kautler.command.integ.test.VersionIntegTest.VersionHolder.version')
41+
def 'build timestamp should be known'() {
42+
expect:
43+
VersionHolder.version.buildTimestamp
44+
}
45+
46+
@AddBean(VersionHolder)
47+
@ResourceLock('net.kautler.command.integ.test.VersionIntegTest.VersionHolder.version')
48+
def 'display version should be equal to version if and only if the version is not a snapshot version'() {
49+
expect:
50+
with(VersionHolder.version) {
51+
(displayVersion == version) != (version.endsWith('-SNAPSHOT') || version == '<unknown>')
52+
}
53+
}
54+
55+
@Vetoed
56+
@ApplicationScoped
57+
static class VersionHolder {
58+
static Version version
59+
60+
@Inject
61+
def setVersion(Version version) {
62+
VersionHolder.version = version
63+
}
64+
65+
def ensureInitializationAtStartup(@Observes @Initialized(ApplicationScoped) Object event) {
66+
// just ensure initialization at startup
67+
}
68+
}
69+
}

src/javacordIntegTest/groovy/net/kautler/command/integ/test/VersionIntegTest.groovy renamed to src/jdaIntegTest/groovy/net/kautler/command/integ/test/jda/VersionIntegTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 Björn Kautler
2+
* Copyright 2025 Björn Kautler
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package net.kautler.command.integ.test
17+
package net.kautler.command.integ.test.jda
1818

1919
import jakarta.enterprise.context.ApplicationScoped
2020
import jakarta.enterprise.context.Initialized
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2025 Björn Kautler
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 net.kautler.command.integ.test.jda.restriction
18+
19+
import jakarta.enterprise.context.ApplicationScoped
20+
import jakarta.enterprise.event.ObservesAsync
21+
import jakarta.enterprise.inject.Produces
22+
import jakarta.enterprise.inject.Vetoed
23+
import net.dv8tion.jda.api.entities.Message
24+
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
25+
import net.kautler.command.api.CommandContext
26+
import net.kautler.command.api.CommandContextTransformer
27+
import net.kautler.command.api.CommandContextTransformer.InPhase
28+
import net.kautler.command.api.CommandHandler
29+
import net.kautler.command.api.annotation.RestrictedTo
30+
import net.kautler.command.api.event.jda.CommandNotAllowedEventJda
31+
import net.kautler.command.api.restriction.Restriction
32+
import net.kautler.command.integ.test.jda.PingIntegTest
33+
import net.kautler.command.integ.test.spock.AddBean
34+
import spock.lang.ResourceLock
35+
import spock.lang.Specification
36+
import spock.lang.Subject
37+
import spock.util.concurrent.BlockingVariable
38+
39+
import static java.util.UUID.randomUUID
40+
import static net.kautler.command.api.CommandContextTransformer.Phase.BEFORE_PREFIX_COMPUTATION
41+
42+
@Subject(CommandHandler)
43+
class ProducedRestrictionIntegTest extends Specification {
44+
@AddBean(RestrictionProducer)
45+
@AddBean(PingCommand)
46+
@AddBean(IgnoreOtherTestsTransformer)
47+
@ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.PingCommand.alias')
48+
@ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.PingCommand.commandNotAllowedEventReceived')
49+
@ResourceLock('net.kautler.command.integ.test.jda.restriction.ProducedRestrictionIntegTest.IgnoreOtherTestsTransformer.expectedContent')
50+
def 'produced restrictions should be usable'(TextChannel textChannelAsUser) {
51+
given:
52+
PingCommand.alias = "ping_${randomUUID()}"
53+
IgnoreOtherTestsTransformer.expectedContent = "!${PingCommand.alias}"
54+
55+
and:
56+
def commandNotAllowedEventReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double)
57+
PingCommand.commandNotAllowedEventReceived = commandNotAllowedEventReceived
58+
59+
when:
60+
textChannelAsUser
61+
.sendMessage(IgnoreOtherTestsTransformer.expectedContent)
62+
.complete()
63+
64+
then:
65+
commandNotAllowedEventReceived.get()
66+
}
67+
68+
@Vetoed
69+
@ApplicationScoped
70+
@RestrictedTo(Restriction1)
71+
static class PingCommand extends PingIntegTest.PingCommand {
72+
static volatile String alias
73+
static volatile commandNotAllowedEventReceived
74+
75+
@Override
76+
List<String> getAliases() {
77+
[alias]
78+
}
79+
80+
void handleCommandNotAllowedEvent(@ObservesAsync CommandNotAllowedEventJda commandNotAllowedEvent) {
81+
commandNotAllowedEventReceived?.set(commandNotAllowedEvent)
82+
}
83+
}
84+
85+
static class Restriction1 implements Restriction<Message> {
86+
@Override
87+
boolean allowCommand(CommandContext<? extends Message> commandContext) {
88+
false
89+
}
90+
}
91+
92+
@Vetoed
93+
@ApplicationScoped
94+
static class RestrictionProducer {
95+
@Produces
96+
@ApplicationScoped
97+
Restriction<Message> restriction = new Restriction1()
98+
}
99+
100+
@Vetoed
101+
@ApplicationScoped
102+
@InPhase(BEFORE_PREFIX_COMPUTATION)
103+
static class IgnoreOtherTestsTransformer implements CommandContextTransformer<Object> {
104+
static volatile expectedContent
105+
106+
@Override
107+
<T> CommandContext<T> transform(CommandContext<T> commandContext, Phase phase) {
108+
(commandContext.messageContent == expectedContent)
109+
? commandContext
110+
: commandContext.withPrefix('<do not match>').build()
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)