Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null check in DelegatingBlockReader#toString() and unit tests #18677

Open
wants to merge 1 commit into
base: master-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public String getLocation() {

@Override
public String toString() {
if (mBlockReader == null) {
return "DelegatingBlockReader not fully initialized.";
}
return mBlockReader.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package alluxio.worker.block.io;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.Closeable;
import java.io.IOException;

/**
* Tests for the {@link DelegatingBlockReader} class.
*/
public class DelegatingBlockReaderTest {

private DelegatingBlockReader mReader;

/** Rule to create a new temporary folder during each test. */
@Rule
public TemporaryFolder mFolder = new TemporaryFolder();

/**
* Test the {@link DelegatingBlockReader#toString()} method when the underlying BlockReader is null.
*/
@Test
public void toStringWithNullBlockReader() throws Exception {
mReader = new DelegatingBlockReader(null, new Closeable() {
@Override
public void close() throws IOException {
// Empty close implementation for testing
}
});

String result = mReader.toString();
Assert.assertEquals("DelegatingBlockReader not fully initialized.", result);
}

/**
* Test the {@link DelegatingBlockReader#toString()} method when the underlying BlockReader is valid.
*/
@Test
public void toStringWithValidBlockReader() throws Exception {
byte[] data = "test data".getBytes();
MockBlockReader mockBlockReader = new MockBlockReader(data);

mReader = new DelegatingBlockReader(mockBlockReader, new Closeable() {
@Override
public void close() throws IOException {
// Empty close implementation for testing
}
});

String result = mReader.toString();
Assert.assertEquals(mockBlockReader.toString(), result);
}
}