You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2021/03/22 22:55:25 UTC
[geode] 01/04: First pass
This is an automated email from the ASF dual-hosted git repository.
klund pushed a commit to branch GEODE-6143-offheap-02
in repository https://gitbox.apache.org/repos/asf/geode.git
commit a86fdf069173ae2d10f0d9daf8d15947e073d0d6
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Thu Mar 18 16:57:24 2021 -0700
First pass
---
.../org/apache/geode/internal/util/ArrayUtils.java | 16 +
geode-gfsh/build.gradle | 2 -
.../result/model/ResultModelIntegrationTest.java | 48 ++-
.../internal/cli/result/TableBuilder.java | 39 ++-
.../internal/cli/result/FileResultTest.java | 11 +-
.../internal/cli/result/TableBuilderJUnitTest.java | 250 ---------------
.../internal/cli/result/TableBuilderTest.java | 335 +++++++++++++++++++++
.../internal/cli/result/model/ResultModelTest.java | 19 --
8 files changed, 416 insertions(+), 304 deletions(-)
diff --git a/geode-core/src/main/java/org/apache/geode/internal/util/ArrayUtils.java b/geode-core/src/main/java/org/apache/geode/internal/util/ArrayUtils.java
index 9160d26..ddc3a1d 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/util/ArrayUtils.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/util/ArrayUtils.java
@@ -386,4 +386,20 @@ public abstract class ArrayUtils {
return new ArrayList<>(Arrays.asList(array));
}
+ /**
+ * Assigns the specified typed reference to each element of the specified
+ * typed array.
+ *
+ * @param array the array to be filled
+ * @param value the value to be stored in all elements of the array
+ * @return the specified typed array filled with the specified typed reference
+ * @throws ArrayStoreException if the specified value is not of a runtime type that can be stored
+ * in the specified array
+ */
+ public static <T> T[] fill(T[] array, T value) {
+ for (int i = 0, length = array.length; i < length; i++) {
+ array[i] = value;
+ }
+ return array;
+ }
}
diff --git a/geode-gfsh/build.gradle b/geode-gfsh/build.gradle
index 038fb24..03eb564 100644
--- a/geode-gfsh/build.gradle
+++ b/geode-gfsh/build.gradle
@@ -38,8 +38,6 @@ dependencies {
// //Find bugs is used in multiple places in the code to suppress findbugs warnings
testImplementation('com.github.stephenc.findbugs:findbugs-annotations')
testImplementation('org.springframework:spring-test')
- integrationTestImplementation('org.powermock:powermock-module-junit4')
- integrationTestImplementation('org.powermock:powermock-api-mockito2')
testImplementation(project(':geode-junit'))
integrationTestImplementation(project(':geode-dunit'))
diff --git a/geode-gfsh/src/integrationTest/java/org/apache/geode/management/internal/cli/result/model/ResultModelIntegrationTest.java b/geode-gfsh/src/integrationTest/java/org/apache/geode/management/internal/cli/result/model/ResultModelIntegrationTest.java
index 2025643..f0a93c1 100644
--- a/geode-gfsh/src/integrationTest/java/org/apache/geode/management/internal/cli/result/model/ResultModelIntegrationTest.java
+++ b/geode-gfsh/src/integrationTest/java/org/apache/geode/management/internal/cli/result/model/ResultModelIntegrationTest.java
@@ -22,13 +22,16 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.apache.geode.management.cli.Result;
import org.apache.geode.management.internal.cli.result.CommandResult;
import org.apache.geode.test.junit.assertions.ResultModelAssert;
+import org.apache.geode.util.internal.GeodeJsonMapper;
public class ResultModelIntegrationTest {
@@ -48,18 +51,24 @@ public class ResultModelIntegrationTest {
public void emptyFileSizeDoesNothing() throws IOException {
ResultModel emptyFileResult = new ResultModel();
result.saveFileTo(temporaryFolder.newFolder());
+
assertThat(emptyFileResult.getInfoSections()).hasSize(0);
}
@Test
public void savesToNullThrowException() {
- assertThatThrownBy(() -> result.saveFileTo(null)).isInstanceOf(NullPointerException.class);
+ assertThatThrownBy(() -> result.saveFileTo(null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
public void notADirectory() throws IOException {
result.saveFileTo(temporaryFolder.newFile());
- assertThis(result).hasInfoResultModel("fileSave").hasOutput().contains("is not a directory");
+
+ assertThis(result)
+ .hasInfoResultModel("fileSave")
+ .hasOutput()
+ .contains("is not a directory");
}
@Test
@@ -68,12 +77,19 @@ public class ResultModelIntegrationTest {
Files.delete(dir.toPath());
result.saveFileTo(dir);
- assertThat(dir).exists();
+ assertThat(dir)
+ .exists();
+
File file1 = new File(dir, "test1.txt");
File file2 = new File(dir, "test2.txt");
- assertThat(dir.listFiles()).contains(file1, file2);
- assertThis(result).hasInfoResultModel("fileSave").hasLines().hasSize(2)
+ assertThat(dir.listFiles())
+ .contains(file1, file2);
+
+ assertThis(result)
+ .hasInfoResultModel("fileSave")
+ .hasLines()
+ .hasSize(2)
.containsExactlyInAnyOrder(
"File saved to " + file1.getAbsolutePath(),
"File saved to " + file2.getAbsolutePath());
@@ -83,8 +99,26 @@ public class ResultModelIntegrationTest {
@SuppressWarnings("deprecation")
public void modelCommandResultShouldNotDealWithFiles() throws IOException {
result.saveFileTo(temporaryFolder.newFolder("test"));
- CommandResult commandResult = new CommandResult(result);
- assertThat(commandResult.hasIncomingFiles()).isFalse();
+ Result commandResult = new CommandResult(result);
+
+ assertThat(commandResult.hasIncomingFiles())
+ .isFalse();
+ }
+
+ @Test
+ public void serializeFileToDownload() throws Exception {
+ File file = temporaryFolder.newFile("test.log");
+ ResultModel result = new ResultModel();
+ result.addFile(file, FileResultModel.FILE_TYPE_FILE);
+ ObjectMapper mapper = GeodeJsonMapper.getMapper();
+ String json = mapper.writeValueAsString(result);
+ System.out.println(json);
+ ResultModel resultModel = mapper.readValue(json, ResultModel.class);
+
+ File value = resultModel.getFileToDownload().toFile();
+
+ assertThat(value)
+ .isEqualTo(file);
}
public static ResultModelAssert assertThis(ResultModel model) {
diff --git a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/result/TableBuilder.java b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/result/TableBuilder.java
index ca23502..078e008 100644
--- a/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/result/TableBuilder.java
+++ b/geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/result/TableBuilder.java
@@ -29,37 +29,36 @@ import org.apache.geode.management.internal.cli.shell.Gfsh;
*
* Sample usage:
*
- * <code>
- * public final Table createTable() {
- * Table resultTable = TableBuilder.newTable();
- * resultTable.setColumnSeparator(" | ");
+ * <pre>
+ * public final Table createTable() {
+ * Table resultTable = TableBuilder.newTable();
+ * resultTable.setColumnSeparator(" | ");
*
- * resultTable.newBlankRow();
- * resultTable.newRow().newLeftCol("Displaying all fields for member: " + memberName);
- * resultTable.newBlankRow();
- * RowGroup rowGroup = resultTable.newRowGroup();
- * rowGroup.newRow().newCenterCol("FIELD1").newCenterCol("FIELD2");
- * rowGroup.newRowSeparator('-');
- * for (int i = 0; i < counter; i++) {
- * rowGroup.newRow().newLeftCol(myFirstField[i]).newLeftCol(mySecondField[i]);
- * }
- * resultTable.newBlankRow();
+ * resultTable.newBlankRow();
+ * resultTable.newRow().newLeftCol("Displaying all fields for member: " + memberName);
+ * resultTable.newBlankRow();
+ * RowGroup rowGroup = resultTable.newRowGroup();
+ * rowGroup.newRow().newCenterCol("FIELD1").newCenterCol("FIELD2");
+ * rowGroup.newRowSeparator('-');
+ * for (int i = 0; i < counter; i++) {
+ * rowGroup.newRow().newLeftCol(myFirstField[i]).newLeftCol(mySecondField[i]);
+ * }
+ * resultTable.newBlankRow();
*
- * return resultTable;
- * }
- * </code>
+ * return resultTable;
+ * }
+ * </pre>
*
* Will result in this:
*
- * <literal>
+ * <pre>
*
* Displaying all fields for member: Member1
*
* FIELD1 | FIELD2 -------------- | --------------- My First Field | My Second Field Another Fld1 |
* Another Fld2 Last Fld1 | Last Fld2
*
- * </literal>
- *
+ * </pre>
*
* @since GemFire 7.0
*/
diff --git a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/FileResultTest.java b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/FileResultTest.java
index 903232a..e36ce36 100644
--- a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/FileResultTest.java
+++ b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/FileResultTest.java
@@ -25,31 +25,30 @@ import org.junit.Test;
import org.apache.geode.management.internal.cli.result.model.FileResultModel;
import org.apache.geode.management.internal.cli.result.model.ResultModel;
-
public class FileResultTest {
private ResultModel fileResult;
@Before
- public void before() {
+ public void setUp() {
fileResult = new ResultModel();
}
@Test
- public void getFormattedFileList() {
+ public void getFormattedFileListReturnsCommaDelimitedStringOfFiles() {
fileResult.addFile(new File("file1.txt"), FileResultModel.FILE_TYPE_FILE);
fileResult.addFile(new File("file2.txt"), FileResultModel.FILE_TYPE_FILE);
+
assertThat(fileResult.getFormattedFileList()).isEqualTo("file1.txt, file2.txt");
}
@Test
- public void getFiles() {
- assertThat(fileResult.getFiles()).isEmpty();
-
+ public void getFileListReturnsListOfFilesInAnyOrder() {
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
fileResult.addFile(file1, FileResultModel.FILE_TYPE_FILE);
fileResult.addFile(file2, FileResultModel.FILE_TYPE_FILE);
+
assertThat(fileResult.getFileList()).containsExactlyInAnyOrder(file1, file2);
}
}
diff --git a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderJUnitTest.java b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderJUnitTest.java
deleted file mode 100644
index 337b5a3..0000000
--- a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderJUnitTest.java
+++ /dev/null
@@ -1,250 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
- * agreements. See the NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.management.internal.cli.result;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.spy;
-
-import java.util.Arrays;
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TestName;
-
-import org.apache.geode.management.internal.cli.GfshParser;
-import org.apache.geode.management.internal.cli.result.TableBuilder.Row;
-import org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup;
-import org.apache.geode.management.internal.cli.result.TableBuilder.Table;
-
-public class TableBuilderJUnitTest {
-
- @Rule
- public TestName testName = new TestName();
-
- private TableBuilder builder;
- private final int screenWidth = 40;
-
- @Before
- public void setUp() {
- builder = spy(new TableBuilder());
- doReturn(screenWidth).when(builder).getScreenWidth();
- doReturn(true).when(builder).shouldTrimColumns();
- }
-
- private Table createTableStructure(int cols, String separator) {
- String[] colNames = new String[cols];
- for (int i = 0; i < cols; i++) {
- colNames[i] = "Field";
- }
- return createTableStructure(cols, separator, colNames);
- }
-
- private Table createTableStructure(int cols, String separator, String... colNames) {
- Table resultTable = builder.newTable();
- resultTable.setTabularResult(true);
- resultTable.setColumnSeparator(separator);
-
- resultTable.newBlankRow();
- RowGroup rowGroup = resultTable.newRowGroup();
- Row row = rowGroup.newRow();
- for (int colIndex = 0; colIndex < cols; colIndex++) {
- row.newCenterCol(colNames[colIndex] + colIndex);
- }
-
- rowGroup.newRowSeparator('-', false);
-
- return resultTable;
- }
-
- private List<String> validateTable(Table table, boolean shouldTrim) {
- String st = table.buildTable();
- System.out.println(st);
-
- List<String> lines = Arrays.asList(st.split(GfshParser.LINE_SEPARATOR));
-
- int line = 0;
- for (String s : lines) {
- System.out.println("For line " + line++ + " length is " + s.length() + " isWider = "
- + (s.length() > screenWidth));
-
- if (shouldTrim) {
- assertThat(s.length()).isLessThanOrEqualTo(screenWidth);
- } else {
- assertThat(s.length()).satisfiesAnyOf(
- length -> assertThat(length).isZero(),
- length -> assertThat(length).isGreaterThan(screenWidth));
- }
- }
-
- return lines;
- }
-
- /**
- * Test Variations table-wide separator true false
- */
- @Test
- public void testSanity() {
- Table table = createTableStructure(3, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("1").newLeftCol("1");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 |1 |1");
- }
-
- @Test
- public void testLastColumnTruncated() {
- Table table = createTableStructure(4, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-")
- .newLeftCol("123456789-123456789-12345");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 |123456789-|123456789-|123456789..");
- }
-
- @Test
- public void testLongestColumnFirstTruncated() {
- Table table = createTableStructure(4, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("123456789-123456789-").newLeftCol("123456789-12345").newLeftCol("123456789-")
- .newLeftCol("1");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1234..|123456789-12345|123456789-|1");
- }
-
- @Test
- public void testMultipleColumnsTruncated() {
- Table table = createTableStructure(4, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-123456789-123456789-")
- .newLeftCol("123456789-123456789-12345");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 |123456789-|123456789..|1234567..");
- }
-
- @Test
- public void testMultipleColumnsTruncatedLongestFirst() {
- Table table = createTableStructure(4, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("123456789-123456789-123456789-").newLeftCol("123456789-123456789-12345")
- .newLeftCol("1").newLeftCol("123456789-");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("123456789..|1234567..|1 |123456789-");
- }
-
- @Test
- public void testColumnsWithShortNames() {
- doReturn(9).when(builder).getScreenWidth();
-
- Table table = createTableStructure(3, "|", "A", "A", "A");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("123").newLeftCol("123").newLeftCol("123");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("..|..|..");
- }
-
- @Test
- public void testExceptionTooSmallWidth() {
- doReturn(7).when(builder).getScreenWidth();
-
- Table table = createTableStructure(3, "|", "A", "A", "A");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("12").newLeftCol("12").newLeftCol("12");
-
- // This should throw an exception
- assertThatThrownBy(() -> validateTable(table, true))
- .isInstanceOf(TableBuilder.TooManyColumnsException.class);
- }
-
- @Test
- public void testTooLittleSpaceOnNextToLastColumn() {
- Table table = createTableStructure(4, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-123456789-123456789-")
- .newLeftCol("123456789-");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 |123456789-|123456789..|123456789-");
- }
-
- @Test
- public void testSeparatorWithMultipleChars() {
- Table table = createTableStructure(4, "<|>");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-")
- .newLeftCol("123456789-123456789-12345");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 <|>123456789-<|>123456789-<|>123..");
- }
-
- /**
- * multiple columns upto 8 : done
- */
- @Test
- public void testManyColumns() {
- Table table = createTableStructure(8, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("123456789-").newLeftCol("123456789-").newLeftCol("123456789-")
- .newLeftCol("123456789-").newLeftCol("123456789-").newLeftCol("123456789-")
- .newLeftCol("123456789-").newLeftCol("123456789-");
-
- List<String> result = validateTable(table, true);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("123456789-|123456789-|..|..|..|..|..|..");
- }
-
- @Test
- public void testDisableColumnAdjustment() {
- doReturn(false).when(builder).shouldTrimColumns();
-
- Table table = createTableStructure(5, "|");
- RowGroup rowGroup = table.getLastRowGroup();
- Row row1 = rowGroup.newRow();
- row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-")
- .newLeftCol("123456789-123456789-12345").newLeftCol("1");
-
- List<String> result = validateTable(table, false);
- // Check the last line
- assertThat(result.get(3)).isEqualTo("1 |123456789-|123456789-|123456789-123456789-12345|1");
- }
-}
diff --git a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderTest.java b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderTest.java
new file mode 100644
index 0000000..e679eed
--- /dev/null
+++ b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/TableBuilderTest.java
@@ -0,0 +1,335 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.management.internal.cli.result;
+
+import static java.util.Arrays.asList;
+import static org.apache.geode.internal.util.ArrayUtils.fill;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.geode.management.internal.cli.GfshParser;
+import org.apache.geode.management.internal.cli.result.TableBuilder.Row;
+import org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup;
+import org.apache.geode.management.internal.cli.result.TableBuilder.Table;
+
+public class TableBuilderTest {
+
+ private static final int SCREEN_WIDTH = 40;
+
+ private TableBuilder tableBuilder;
+
+ @Before
+ public void setUp() {
+ tableBuilder = spy(new TableBuilder());
+
+ doReturn(SCREEN_WIDTH).when(tableBuilder).getScreenWidth();
+ doReturn(true).when(tableBuilder).shouldTrimColumns();
+ }
+
+ /**
+ * Test Variations table-wide separator true false
+ */
+ @Test
+ public void testSanity() {
+ Table table = createTableStructure(3, "|", fill(new String[3], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("1")
+ .newLeftCol("1");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0|Field1|Field2",
+ "------|------|------",
+ "1 |1 |1");
+ }
+
+ @Test
+ public void testLastColumnTruncated() {
+ Table table = createTableStructure(4, "|", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-123456789-12345");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0| Field1 | Field2 |Field3",
+ "------|----------|----------|-----------",
+ "1 |123456789-|123456789-|123456789..");
+ }
+
+ @Test
+ public void testLongestColumnFirstTruncated() {
+ Table table = createTableStructure(4, "|", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("123456789-123456789-")
+ .newLeftCol("123456789-12345")
+ .newLeftCol("123456789-")
+ .newLeftCol("1");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0| Field1 | Field2 |Field3",
+ "------|---------------|----------|------",
+ "1234..|123456789-12345|123456789-|1");
+ }
+
+ @Test
+ public void testMultipleColumnsTruncated() {
+ Table table = createTableStructure(4, "|", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-123456789-123456789-")
+ .newLeftCol("123456789-123456789-12345");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0| Field1 | Field2 |Field3",
+ "------|----------|-----------|---------",
+ "1 |123456789-|123456789..|1234567..");
+ }
+
+ @Test
+ public void testMultipleColumnsTruncatedLongestFirst() {
+ Table table = createTableStructure(4, "|", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("123456789-123456789-123456789-")
+ .newLeftCol("123456789-123456789-12345")
+ .newLeftCol("1")
+ .newLeftCol("123456789-");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ " Field0 | Field1 |Field2|Field3",
+ "-----------|---------|------|----------",
+ "123456789..|1234567..|1 |123456789-");
+ }
+
+ @Test
+ public void testColumnsWithShortNames() {
+ doReturn(9).when(tableBuilder).getScreenWidth();
+
+ Table table = createTableStructure(3, "|", "A", "A", "A");
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("123")
+ .newLeftCol("123")
+ .newLeftCol("123");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "A0|A1|A2",
+ "--|--|--",
+ "..|..|..");
+ }
+
+ @Test
+ public void testExceptionTooSmallWidth() {
+ doReturn(7).when(tableBuilder).getScreenWidth();
+
+ Table table = createTableStructure(3, "|", "A", "A", "A");
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("12")
+ .newLeftCol("12")
+ .newLeftCol("12");
+
+ // This should throw an exception
+ assertThatThrownBy(() -> validateTable(table, true))
+ .isInstanceOf(TableBuilder.TooManyColumnsException.class);
+ }
+
+ @Test
+ public void testTooLittleSpaceOnNextToLastColumn() {
+ Table table = createTableStructure(4, "|", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-123456789-123456789-")
+ .newLeftCol("123456789-");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0| Field1 | Field2 |Field3",
+ "------|----------|-----------|----------",
+ "1 |123456789-|123456789..|123456789-");
+ }
+
+ @Test
+ public void testSeparatorWithMultipleChars() {
+ Table table = createTableStructure(4, "<|>", fill(new String[4], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-123456789-12345");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0<|> Field1 <|> Field2 <|>Fie..",
+ "------<|>----------<|>----------<|>-----",
+ "1 <|>123456789-<|>123456789-<|>123..");
+ }
+
+ @Test
+ public void testManyColumns() {
+ Table table = createTableStructure(8, "|", fill(new String[8], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-");
+
+ List<String> result = validateTable(table, true);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ " Field0 | Field1 |..|..|..|..|..|..",
+ "----------|----------|--|--|--|--|--|--",
+ "123456789-|123456789-|..|..|..|..|..|..");
+ }
+
+ @Test
+ public void testDisableColumnAdjustment() {
+ doReturn(false).when(tableBuilder).shouldTrimColumns();
+
+ Table table = createTableStructure(5, "|", fill(new String[5], "Field"));
+ RowGroup rowGroup = table.getLastRowGroup();
+ Row row = rowGroup.newRow();
+ row
+ .newLeftCol("1")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-")
+ .newLeftCol("123456789-123456789-12345")
+ .newLeftCol("1");
+
+ List<String> result = validateTable(table, false);
+
+ assertThat(result)
+ .hasSize(4)
+ .containsExactly(
+ "",
+ "Field0| Field1 | Field2 | Field3 |Field4",
+ "------|----------|----------|-------------------------|------",
+ "1 |123456789-|123456789-|123456789-123456789-12345|1");
+ }
+
+ private Table createTableStructure(int columnCount, String separator, String... columnNames) {
+ Table resultTable = tableBuilder.newTable();
+ resultTable.setTabularResult(true);
+ resultTable.setColumnSeparator(separator);
+
+ resultTable.newBlankRow();
+ RowGroup rowGroup = resultTable.newRowGroup();
+ Row row = rowGroup.newRow();
+
+ for (int column = 0; column < columnCount; column++) {
+ row.newCenterCol(columnNames[column] + column);
+ }
+
+ rowGroup.newRowSeparator('-', false);
+
+ return resultTable;
+ }
+
+ private List<String> validateTable(Table table, boolean shouldTrim) {
+ String tableAsString = table.buildTable();
+ System.out.println(tableAsString);
+
+ List<String> lines = asList(tableAsString.split(GfshParser.LINE_SEPARATOR));
+
+ int lineCount = 0;
+ for (String line : lines) {
+ System.out.println("Line #" + lineCount++ + ": length = " + line.length() + ", isWider = "
+ + (line.length() > SCREEN_WIDTH));
+
+ if (shouldTrim) {
+ assertThat(line.length()).isLessThanOrEqualTo(SCREEN_WIDTH);
+ } else {
+ assertThat(line.length()).satisfiesAnyOf(
+ length -> assertThat(length).isZero(),
+ length -> assertThat(length).isGreaterThan(SCREEN_WIDTH));
+ }
+ }
+
+ return lines;
+ }
+}
diff --git a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/model/ResultModelTest.java b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/model/ResultModelTest.java
index 64af19e..a4c85fe 100644
--- a/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/model/ResultModelTest.java
+++ b/geode-gfsh/src/test/java/org/apache/geode/management/internal/cli/result/model/ResultModelTest.java
@@ -12,7 +12,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
-
package org.apache.geode.management.internal.cli.result.model;
import static org.apache.geode.management.internal.functions.CliFunctionResult.StatusState.ERROR;
@@ -20,29 +19,22 @@ import static org.apache.geode.management.internal.functions.CliFunctionResult.S
import static org.apache.geode.management.internal.functions.CliFunctionResult.StatusState.OK;
import static org.assertj.core.api.Assertions.assertThat;
-import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.internal.functions.CliFunctionResult;
import org.apache.geode.util.internal.GeodeJsonMapper;
-
public class ResultModelTest {
private ResultModel result;
private TabularResultModel table;
- @Rule
- public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
@Before
public void setUp() throws Exception {
result = new ResultModel();
@@ -172,17 +164,6 @@ public class ResultModelTest {
}
@Test
- public void serializeFileToDownload() throws Exception {
- File file = temporaryFolder.newFile("test.log");
- result.addFile(file, FileResultModel.FILE_TYPE_FILE);
- ObjectMapper mapper = GeodeJsonMapper.getMapper();
- String json = mapper.writeValueAsString(result);
- System.out.println(json);
- ResultModel resultModel = mapper.readValue(json, ResultModel.class);
- assertThat(resultModel.getFileToDownload()).isEqualTo(file.toPath());
- }
-
- @Test
public void serializeInfoResult() throws Exception {
InfoResultModel info = result.addInfo();
info.addLine("line2");