|
| 1 | +/* |
| 2 | + * Copyright 2020 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.parameter |
| 18 | + |
| 19 | +import net.kautler.command.api.Command |
| 20 | +import net.kautler.command.api.parameter.ParameterParser |
| 21 | +import net.kautler.command.integ.test.spock.AddBean |
| 22 | +import net.kautler.command.integ.test.spock.VetoBean |
| 23 | +import net.kautler.command.parameter.parser.UntypedParameterParser |
| 24 | +import net.kautler.command.parameter.parser.missingdependency.MissingDependencyParameterParser |
| 25 | +import org.javacord.api.entity.channel.ServerTextChannel |
| 26 | +import org.javacord.api.entity.message.Message |
| 27 | +import spock.lang.Specification |
| 28 | +import spock.lang.Subject |
| 29 | +import spock.util.concurrent.BlockingVariable |
| 30 | +import spock.util.concurrent.PollingConditions |
| 31 | + |
| 32 | +import javax.enterprise.context.ApplicationScoped |
| 33 | +import javax.enterprise.inject.Vetoed |
| 34 | +import javax.inject.Inject |
| 35 | + |
| 36 | +import static org.apache.logging.log4j.Level.ERROR |
| 37 | +import static org.apache.logging.log4j.test.appender.ListAppender.getListAppender |
| 38 | + |
| 39 | +@Subject(MissingDependencyParameterParser) |
| 40 | +@VetoBean(UntypedParameterParser) |
| 41 | +class MissingDependencyParameterParserIntegTest extends Specification { |
| 42 | + @AddBean(PingCommand) |
| 43 | + def 'missing dependency parameter parser should throw UnsupportedOperationException'( |
| 44 | + ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) { |
| 45 | + given: |
| 46 | + def commandReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double) |
| 47 | + |
| 48 | + and: |
| 49 | + def listenerManager = serverTextChannelAsBot.addMessageCreateListener { |
| 50 | + if ((it.message.userAuthor.orElse(null) == serverTextChannelAsUser.api.yourself) && |
| 51 | + (it.message.content == '!ping')) { |
| 52 | + commandReceived.set(true) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + when: |
| 57 | + serverTextChannelAsUser |
| 58 | + .sendMessage('!ping') |
| 59 | + .join() |
| 60 | + |
| 61 | + then: |
| 62 | + commandReceived.get() |
| 63 | + |
| 64 | + and: |
| 65 | + new PollingConditions(timeout: System.properties.testResponseTimeout as double).eventually { |
| 66 | + getListAppender('Test Appender') |
| 67 | + .events |
| 68 | + .any { |
| 69 | + (it.level == ERROR) && |
| 70 | + (it.thrown instanceof UnsupportedOperationException) && |
| 71 | + (it.thrown.message == 'ANTLR runtime is missing') |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + cleanup: |
| 76 | + listenerManager?.remove() |
| 77 | + |
| 78 | + and: |
| 79 | + getListAppender('Test Appender').@events.removeIf { |
| 80 | + (it.level == ERROR) && |
| 81 | + (it.thrown instanceof UnsupportedOperationException) && |
| 82 | + (it.thrown.message == 'ANTLR runtime is missing') |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Vetoed |
| 87 | + @ApplicationScoped |
| 88 | + static class PingCommand implements Command<Message> { |
| 89 | + @Inject |
| 90 | + ParameterParser parameterParser |
| 91 | + |
| 92 | + @Override |
| 93 | + void execute(Message incomingMessage, String prefix, String usedAlias, String parameterString) { |
| 94 | + parameterParser.toString() |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments