karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf git commit: [KARAF-4843] Force ascii for some tests
Date Wed, 30 Nov 2016 13:29:30 GMT
Repository: karaf
Updated Branches:
  refs/heads/master c7806957e -> edf41f7ba


[KARAF-4843] Force ascii for some tests


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/edf41f7b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/edf41f7b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/edf41f7b

Branch: refs/heads/master
Commit: edf41f7ba67d2107b0f2ed0ae876362b48f222c5
Parents: c780695
Author: Christian Schneider <chris@die-schneider.net>
Authored: Wed Nov 30 14:29:20 2016 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Wed Nov 30 14:29:20 2016 +0100

----------------------------------------------------------------------
 .../karaf/shell/support/table/ShellTable.java   | 35 ++++++++++++++------
 .../shell/support/table/ShellTableTest.java     | 14 ++++----
 2 files changed, 31 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/edf41f7b/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
b/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
index 31a9fd3..b383203 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/table/ShellTable.java
@@ -45,6 +45,7 @@ public class ShellTable {
     private String separator = DEFAULT_SEPARATOR;
     private int size;
     private String emptyTableText;
+    private boolean forceAscii;
 
     public ShellTable() {
 
@@ -81,6 +82,11 @@ public class ShellTable {
         rows.add(row);
         return row;
     }
+    
+    public ShellTable forceAscii() {
+        forceAscii = true;
+        return this;
+    }
 
     /**
      * Set text to display if there are no rows in the table.
@@ -98,15 +104,8 @@ public class ShellTable {
     }
 
     public void print(PrintStream out, boolean format)  {
-        boolean supported = false;
-        String encoding = getEncoding(out);
-        if (encoding != null) {
-            CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
-            supported = encoder.canEncode(separator)
-                    && encoder.canEncode(SEP_HORIZONTAL)
-                    && encoder.canEncode(SEP_CROSS);
-        }
-        String separator = supported ? this.separator : DEFAULT_SEPARATOR_ASCII;
+        boolean unicode = supportsUnicode(out);
+        String separator = unicode ? this.separator : DEFAULT_SEPARATOR_ASCII;
 
         // "normal" table rendering, with borders
         Row headerRow = new Row(cols);
@@ -125,9 +124,9 @@ public class ShellTable {
             int iCol = 0;
             for (Col col : cols) {
                 if (iCol++ == 0) {
-                    out.print(underline(col.getSize(), false, supported));
+                    out.print(underline(col.getSize(), false, unicode));
                 } else {
-                    out.print(underline(col.getSize() + 3, true, supported));
+                    out.print(underline(col.getSize() + 3, true, unicode));
                 }
                 iCol++;
             }
@@ -149,6 +148,20 @@ public class ShellTable {
         }
     }
 
+    private boolean supportsUnicode(PrintStream out) {
+        if (forceAscii) {
+            return false;
+        }
+        String encoding = getEncoding(out);
+        if (encoding == null) {
+            return false;
+        }
+        CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
+        return encoder.canEncode(separator) 
+            && encoder.canEncode(SEP_HORIZONTAL)
+            && encoder.canEncode(SEP_CROSS);
+    }
+
     private String getEncoding(PrintStream ps) {
         if (ps.getClass().getName().equals("org.apache.felix.gogo.runtime.threadio.ThreadPrintStream"))
{
             try {

http://git-wip-us.apache.org/repos/asf/karaf/blob/edf41f7b/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
----------------------------------------------------------------------
diff --git a/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
b/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
index 0826e88..c0d8596 100644
--- a/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
+++ b/shell/core/src/test/java/org/apache/karaf/shell/support/table/ShellTableTest.java
@@ -33,19 +33,19 @@ public class ShellTableTest {
 
     @Test
     public void testLongValueFull() {
-        ShellTable table = new ShellTable();
+        ShellTable table = new ShellTable().forceAscii();
         table.separator("|");
         table.column("col1");
         table.column("col2").maxSize(-1);
         table.addRow().addContent("my first column value", "my second column value is quite
long");
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         table.print(new PrintStream(baos), false);
-        assertEquals(String.format("%s%n","my first column value|my second column value is
quite long"), baos.toString());
+        assertEquals(String.format("%s%n","my first column value | my second column value
is quite long"), baos.toString());
     }
 
     @Test
     public void testLongValueCut() {
-        ShellTable table = new ShellTable();
+        ShellTable table = new ShellTable().forceAscii();
         table.separator("|");
         table.column("col1");
         table.column("col2").maxSize(-1);
@@ -53,12 +53,12 @@ public class ShellTableTest {
         table.size(50);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         table.print(new PrintStream(baos), false);
-        assertEquals(String.format("%s%n","my first column value|my second column value is
q"), baos.toString());
+        assertEquals(String.format("%s%n","my first column value | my second column value
is q"), baos.toString());
     }
 
     @Test
     public void testLongValueMultiline() {
-        ShellTable table = new ShellTable();
+        ShellTable table = new ShellTable().forceAscii();
         table.separator("|");
         table.column("col1");
         table.column("col2").maxSize(-1).wrap();
@@ -66,8 +66,8 @@ public class ShellTableTest {
         table.size(50);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         table.print(new PrintStream(baos), false);
-        assertEquals(String.format("%1$s\n%2$s%n","my first column value|my second column
value is",
-                "                     |quite long"), baos.toString());
+        assertEquals(String.format("%1$s\n%2$s%n","my first column value | my second column
value is",
+                "                      | quite long"), baos.toString());
     }
 
     @Test


Mime
View raw message