クラスMobileStorageはレトロな携帯電話の受信箱の実装です。それにより、受信ボックスは、メッセージ当たり最大160文字のメッセージの予め定められた最大容量を保持することが示される。以下の操作がサポートされており、テストする必要がある。EclipseのTestNGで単体テストを書く
- saveMessage:次の自由な位置にある受信トレイに新しいテキストメッセージを格納します。 メッセージテキストが160文字を超える場合、メッセージは分割され、複数の格納場所に格納されます。
- deleteMessage:受信トレイから最も古い(最初の)モバイルメッセージを削除します。
- listMessages:現在格納されているすべてのメッセージの読み取り可能な表現を表示します。複数の部分に格納されたメッセージは、結合されて表現されます。
私が付けたこのコードでユニットテストを行う必要があります。私はTestNGと単体テストに精通していませんが、私ができるテストの例を教えてください。
mobile_storage \ SRC \メイン\のJava \ MobileMessage.java - https://pastebin.com/RxNcgnSi
/**
* Represents a mobile text message.
*/
public class MobileMessage {
//stores the content of this messages
private final String text;
//in case of multi-part-messages, stores the preceding message
//is null in case of single message
private MobileMessage predecessor;
public MobileMessage(String text, MobileMessage predecessor) {
this.text = text;
this.predecessor = predecessor;
}
public String getText() {
return text;
}
public MobileMessage getPredecessor() {
return predecessor;
}
public void setPredecessor(MobileMessage predecessor) {
this.predecessor = predecessor;
}
}
mobile_storage \ SRC \メイン\のJava \ MobileStorage.java - https://pastebin.com/wuqKgvFD
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;
/**
* Represents the message inbox of a mobile phone.
* Each storage position in the inbox can store a message with 160 characters at most.
* Messages are stored with increasing order (oldest first).
*/
public class MobileStorage {
final static int MAX_MESSAGE_LENGTH = 160;
private MobileMessage[] inbox;
private int occupied = 0;
/**
* Creates a message inbox that can store {@code storageSize} mobile messages.
* @throws IllegalArgumentException in case the passed {@code storageSize} is zero or less
*/
public MobileStorage(int storageSize) {
if(storageSize < 1) {
throw new IllegalArgumentException("Storage size must be greater than 0");
}
this.inbox = new MobileMessage[storageSize];
}
/**
* Stores a new text message to the inbox.
* In case the message text is longer than {@code MAX_MESSAGE_LENGTH}, the message is splitted and stored on multiple storage positions.
* @param message a non-empty message text
* @throws IllegalArgumentException in case the given message is empty
* @throws RuntimeException in case the available storage is too small for storing the complete message text
*/
public void saveMessage(String message) {
if(StringUtils.isBlank(message)) {
throw new IllegalArgumentException("Message cannot be null or empty");
}
int requiredStorage = (int) Math.ceil((double) message.length()/MAX_MESSAGE_LENGTH);
if(requiredStorage > inbox.length || (inbox.length - occupied) <= requiredStorage) {
throw new RuntimeException("Storage Overflow");
}
MobileMessage predecessor = null;
for(int i = 0; i < requiredStorage; i++) {
int from = i * MAX_MESSAGE_LENGTH;
int to = Math.min((i+1) * MAX_MESSAGE_LENGTH, message.length());
String messagePart = message.substring(from, to);
MobileMessage mobileMessage = new MobileMessage(messagePart, predecessor);
inbox[occupied] = mobileMessage;
occupied++;
predecessor = mobileMessage;
}
}
/**
* Returns the number of currently stored mobile messages.
*/
public int getOccupied() {
return occupied;
}
/**
* Removes the oldest (first) mobile message from the inbox.
*
* @return the deleted message
* @throws RuntimeException in case there are currently no messages stored
*/
public String deleteMessage() {
if(occupied == 0) {
throw new RuntimeException("There are no messages in the inbox");
}
MobileMessage first = inbox[0];
IntStream.range(1, occupied).forEach(index -> inbox[index-1] = inbox[index]);
inbox[occupied] = null;
inbox[0].setPredecessor(null);
occupied--;
return first.getText();
}
/**
* Returns a readable representation of all currently stored messages.
* Messages that were stored in multiple parts are joined together for representation.
* returns an empty String in case there are currently no messages stored
*/
public String listMessages() {
return Arrays.stream(inbox)
.filter(Objects::nonNull)
.collect(StringBuilder::new, MobileStorage::foldMessage, StringBuilder::append)
.toString();
}
private static void foldMessage(StringBuilder builder, MobileMessage message) {
if (message.getPredecessor() == null && builder.length() != 0) {
builder.append('\n');
}
builder.append(message.getText());
}
}
よう
あなた
test.java
見えるかもしれ何かがあなたがテストケースまたはこれらの試験例TestNGの実装を探しているお役に立てば幸いですか! – user1207289私はテストケース、およびいくつかのテストケースのコーディング例を探しています。 – Allx
@ user1207289お願いします。 – Allx