Skip to content
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
5 changes: 5 additions & 0 deletions src/main/java/io/mo/result/RSMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public void addMetaInfo(String name, String label, int type, int precision, int
}

public boolean equals(RSMetaData meta) {
if (this.columnCount != meta.columnCount) {
LOG.error("The column count does not equal with each other, one is "
+ this.columnCount + ", the other is " + meta.columnCount);
return false;
}
for (int i = 0; i < columnCount; i++) {
if (this.fullMetaInfo) {
String f1 = this.stringAt(i);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/mo/util/ResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ private static RSSet convertToRSSet(String rsText, String separator) {
fullMeta = true;
}
String[] cols = lines[0].split(RESULT.COLUMN_SEPARATOR_NEW, -1);
// Old space-delimited baselines can have alignment spaces after the
// last header. Drop only one resulting empty header when every data
// row proves it is padding; preserve explicit empty columns and all
// new-format metadata unchanged.
if (!rsText.contains(RESULT.ROW_SEPARATOR_NEW) && !fullMeta
&& cols.length > 1 && cols[cols.length - 1].isEmpty()
&& lines.length > 1) {
boolean padding = true;
for (int i = 1; i < lines.length; i++) {
if (lines[i].split(RESULT.COLUMN_SEPARATOR_NEW, -1).length != cols.length - 1) {
padding = false;
break;
}
}
if (padding) cols = java.util.Arrays.copyOf(cols, cols.length - 1);
}
RSSet rsSet = new RSSet();
RSMetaData meta = new RSMetaData(cols.length);
rsSet.setMeta(meta);
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/io/mo/result/RSMetaDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.mo.result;

import org.junit.Test;
import java.sql.Types;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class RSMetaDataTest {
private RSMetaData metadata(int columns, boolean full) {
RSMetaData meta = new RSMetaData(columns);
meta.setFullMetaInfo(full);
for (int i = 0; i < columns; i++) {
meta.addMetaInfo("c" + i, "c" + i, Types.INTEGER, 10, 0);
}
return meta;
}

@Test
public void extraExpectedColumnIsMismatchNotException() {
for (boolean full : new boolean[]{false, true}) {
assertFalse(metadata(8, full).equals(metadata(7, full)));
}
}

@Test
public void extraActualColumnMustNotBeSilentlyAccepted() {
for (boolean full : new boolean[]{false, true}) {
assertFalse(metadata(7, full).equals(metadata(8, full)));
}
}

@Test
public void emptyVersusNonemptyIsMismatchInBothDirections() {
for (boolean full : new boolean[]{false, true}) {
assertFalse(metadata(0, full).equals(metadata(1, full)));
assertFalse(metadata(1, full).equals(metadata(0, full)));
}
}

@Test
public void matchingMetadataStillComparesEqual() {
for (boolean full : new boolean[]{false, true}) {
assertTrue(metadata(0, full).equals(metadata(0, full)));
assertTrue(metadata(7, full).equals(metadata(7, full)));
}
}

@Test
public void labelsAndTypesStillUseExistingComparisonPolicy() {
RSMetaData changedType = new RSMetaData(1);
changedType.addMetaInfo("c0", "c0", Types.VARCHAR, 10, 0);
assertTrue(metadata(1, false).equals(changedType));
assertFalse(metadata(1, true).equals(changedType));
RSMetaData changedLabel = new RSMetaData(1);
changedLabel.addMetaInfo("other", "other", Types.INTEGER, 10, 0);
assertFalse(metadata(1, false).equals(changedLabel));
}
}
42 changes: 42 additions & 0 deletions src/test/java/io/mo/util/LegacyResultPaddingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.mo.util;

import io.mo.result.RSSet;
import org.junit.Test;

import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;

public class LegacyResultPaddingTest {
private RSSet parse(String text) throws Exception {
Method method = ResultParser.class.getDeclaredMethod("convertToRSSet", String.class, String.class);
method.setAccessible(true);
return (RSSet) method.invoke(null, text, "space");
}

@Test
public void stripsProvenHeaderPaddingWithoutChangingValues() throws Exception {
RSSet result = parse("a b \n1 2");
assertEquals(2, result.getMeta().getColumnCount());
assertEquals("2", result.getRows().get(0).getRowCellString(1));
}

@Test
public void preservesRealEmptyLastColumn() throws Exception {
RSSet result = parse("a b \n1 2 ");
assertEquals(3, result.getMeta().getColumnCount());
assertEquals("", result.getRows().get(0).getRowCellString(2));
}

@Test
public void doesNotGuessForMixedRowsOrNoRows() throws Exception {
assertEquals(3, parse("a b \n1 2\n3 4 ").getMeta().getColumnCount());
assertEquals(3, parse("a b ").getMeta().getColumnCount());
}

@Test
public void preservesNewFormat() throws Exception {
RSSet result = parse("➤ a[12,4,0] ¦ [12,4,0] 𝄀\n1 ¦ ");
assertEquals(2, result.getMeta().getColumnCount());
}
}
Loading