Skip to content

Commit

Permalink
Create StringAppenderTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jul 10, 2024
1 parent 7950a46 commit 7cab685
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
20 changes: 14 additions & 6 deletions kala-base/src/main/java/kala/text/StringAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.io.Writer;
import java.io.*;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;


public class StringAppender extends Writer implements Serializable {
@SuppressWarnings("resource")
public class StringAppender extends Writer implements Externalizable {
@Serial
private static final long serialVersionUID = 0L;

Expand Down Expand Up @@ -448,6 +445,17 @@ private void beforeAppend(int n) {
builder.ensureCapacity(builder.length() + n);
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(builder.toString());
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.builder.setLength(0);
this.builder.append((String) in.readObject());
}

@Override
public String toString() {
return builder.toString();
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/kala/text/StringAppenderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.text;

import kala.SerializationUtils;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StringAppenderTest {

@Test
public void serializeTest() throws Exception {
assertEquals("", SerializationUtils.writeAndRead(new StringAppender()).toString());
assertEquals("str", SerializationUtils.writeAndRead(new StringAppender("str")).toString());
}
}

0 comments on commit 7cab685

Please sign in to comment.