mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Change] Framework-API 为 CharSequenceMessage 构造中添加非空检查, 添加完整的对应单元测试类;
[Change] CharSequenceMessage 添加对 content 为 null 的检查, 调整文档; [Change] CharSequenceMessageTest 添加针对 CharSequenceMessage 的完整单元测试类;
This commit is contained in:
parent
7fb4f6d3df
commit
320858c4d0
@ -17,18 +17,28 @@
|
||||
|
||||
package net.lamgc.cgj.bot.framework.message;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 字符序列消息对象.
|
||||
* <p> 仅包含了一段字符串.
|
||||
* <p> 仅包含了一段字符序列形式的内容.
|
||||
* @author LamGC
|
||||
*/
|
||||
public final class CharSequenceMessage implements Message {
|
||||
|
||||
private final CharSequence content;
|
||||
|
||||
/**
|
||||
* 构造一个 CharSequenceMessage.
|
||||
* @param content 字符序列形式的内容.
|
||||
* @throws NullPointerException 当 content 为 {@code null},
|
||||
* 或 content 为 {@link Message} 且 {@link Message#contentToString()} 返回 {@code null} 时抛出.
|
||||
*/
|
||||
public CharSequenceMessage(CharSequence content) {
|
||||
Objects.requireNonNull(content);
|
||||
|
||||
if (content instanceof Message) {
|
||||
this.content = ((Message) content).contentToString();
|
||||
this.content = Objects.requireNonNull(((Message) content).contentToString());
|
||||
} else {
|
||||
this.content = content;
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user