diff --git beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java new file mode 100644 index 0000000..1dbfa63 --- /dev/null +++ beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java @@ -0,0 +1,50 @@ +package org.apache.hive.beeline; + +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +/** + * Unit test for Beeline output format. + */ +public class TestTableOutputFormat { + + /** + * This case is mainly focusing on the handling of ColorBuffer when dealing with line wrapping. + * And it will also cover the print row when meeting line wrapping case. + * @throws UnsupportedEncodingException + */ + @Test + public void testTruncateTableOptionInLineWrapping() throws UnsupportedEncodingException { + // set output stream + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + + // initialize the beeline with truncate options + BeeLine beeLine = new BeeLine(); + String args[] = new String[]{"--truncateTable", "--maxWidth=24"}; + Assert.assertEquals(0, beeLine.initArgs(args)); + beeLine.setOutputStream(ps); + + TableOutputFormat tableOutputFormat = new TableOutputFormat(beeLine); + + // parsing by the table output format + String separator = System.getProperty("line.separator"); + String str = String.format("There is line break %s abOVE", separator); + ColorBuffer buffer = new ColorBuffer(str, false); + + if (beeLine.getOpts().getTruncateTable()) { + buffer = buffer.truncate(beeLine.getOpts().getMaxWidth()); + } + + // expected result + String expected = String.format("| There is line break %s ab |%s", separator, separator); + + tableOutputFormat.printRow(buffer, false); + Assert.assertEquals(expected, baos.toString("UTF8")); + } +}