[Change] Framework-API 为 CharSequenceMessage 构造中添加非空检查, 添加完整的对应单元测试类;

[Change] CharSequenceMessage 添加对 content 为 null 的检查, 调整文档;
[Change] CharSequenceMessageTest 添加针对 CharSequenceMessage 的完整单元测试类;
This commit is contained in:
LamGC 2020-11-20 23:16:14 +08:00
parent 7fb4f6d3df
commit 320858c4d0
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 82 additions and 2 deletions

View File

@ -17,18 +17,28 @@
package net.lamgc.cgj.bot.framework.message; package net.lamgc.cgj.bot.framework.message;
import java.util.Objects;
/** /**
* 字符序列消息对象. * 字符序列消息对象.
* <p> 仅包含了一段字符. * <p> 仅包含了一段字符序列形式的内容.
* @author LamGC * @author LamGC
*/ */
public final class CharSequenceMessage implements Message { public final class CharSequenceMessage implements Message {
private final CharSequence content; private final CharSequence content;
/**
* 构造一个 CharSequenceMessage.
* @param content 字符序列形式的内容.
* @throws NullPointerException content {@code null},
* content {@link Message} {@link Message#contentToString()} 返回 {@code null} 时抛出.
*/
public CharSequenceMessage(CharSequence content) { public CharSequenceMessage(CharSequence content) {
Objects.requireNonNull(content);
if (content instanceof Message) { if (content instanceof Message) {
this.content = ((Message) content).contentToString(); this.content = Objects.requireNonNull(((Message) content).contentToString());
} else { } else {
this.content = content; this.content = content;
} }

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.framework.message;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Field;
/**
* @see CharSequenceMessage
*/
public class CharSequenceMessageTest {
@Test
public void buildTest() {
final String contentStr = "test";
Assert.assertEquals(contentStr, new CharSequenceMessage(contentStr).contentToString());
Assert.assertEquals(contentStr, new CharSequenceMessage(new StringBuilder(contentStr)).contentToString());
}
@Test(expected = NullPointerException.class)
public void nullContentTest() {
new CharSequenceMessage(null);
}
@Test
public void buildWithMessageTest() throws NoSuchFieldException, IllegalAccessException {
final String testContent = "test123";
class TestMessage implements Message {
@Override
public String contentToString() {
return testContent;
}
}
CharSequenceMessage charSequenceMessage = new CharSequenceMessage(new TestMessage());
Assert.assertEquals(testContent, charSequenceMessage.contentToString());
Field contentField = CharSequenceMessage.class.getDeclaredField("content");
contentField.setAccessible(true);
Assert.assertFalse(contentField.get(charSequenceMessage) instanceof Message);
}
@Test
public void toStringTest() {
final String testContent = "test";
Assert.assertEquals("CharSequenceMessage{content='" + testContent + "'}",
new CharSequenceMessage(testContent).toString());
}
}