From c9e81d2fa074f58d4ed1d511bfa71e091d7bca70 Mon Sep 17 00:00:00 2001 From: reger Date: Sun, 6 Nov 2016 03:34:24 +0100 Subject: [PATCH] fix Column parsing from celldefinition string, without cellwidth def. (outofbound exception) --- source/net/yacy/kelondro/index/Column.java | 9 ++- .../net/yacy/kelondro/index/ColumnTest.java | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 test/java/net/yacy/kelondro/index/ColumnTest.java diff --git a/source/net/yacy/kelondro/index/Column.java b/source/net/yacy/kelondro/index/Column.java index 09dd1015d..892d3fb26 100644 --- a/source/net/yacy/kelondro/index/Column.java +++ b/source/net/yacy/kelondro/index/Column.java @@ -62,6 +62,11 @@ public final class Column implements Cloneable, Serializable { this.description = description; } + /** + * Create column from definiton string + * + * @param celldef syntax: "celltype cellname-cellwidth {encoder} \"descripton\"" + */ public Column(String celldef) { // define column with column syntax // example: @@ -127,8 +132,8 @@ public final class Column implements Cloneable, Serializable { this.nickname = celldef; celldef = ""; } else { - this.nickname = celldef.substring(0, p); - celldef = celldef.substring(q + 1); + this.nickname = celldef.substring(0, q); + celldef = celldef.substring(q + 1).trim(); } } else { this.nickname = celldef.substring(0, p); diff --git a/test/java/net/yacy/kelondro/index/ColumnTest.java b/test/java/net/yacy/kelondro/index/ColumnTest.java new file mode 100644 index 000000000..e0cedcfb5 --- /dev/null +++ b/test/java/net/yacy/kelondro/index/ColumnTest.java @@ -0,0 +1,55 @@ +/** + * ColumnTest.java + * part of YaCy + * Copyright 2016 by reger24; https://github.com/reger24 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program in the file lgpl21.txt + * If not, see . + */ +package net.yacy.kelondro.index; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * Unit tests for ColumnTest class. + */ +public class ColumnTest { + + + /** + * Test of cell definition parsing and cellwidth assignment, of class Column. + */ + @Test + public void testCellWidth() { + // test cell definition + // key=definition string, value= expected cellwidth + Map testcelldefs = new HashMap(); + testcelldefs.put("long countA {b256}", 8); // simple + testcelldefs.put("long countB-8 {b256}",8); // redundant cellwidth + testcelldefs.put("long countC {b256}", 8); // spaces between definition + testcelldefs.put("long countD {b256} \"Description\"", 8); + testcelldefs.put("", 8); + testcelldefs.put("int icountA {b256}", 4); + + for (String celldef:testcelldefs.keySet()) { + Column col = new Column(celldef); + Integer expectedCellWidth = testcelldefs.get(celldef); + assertEquals (celldef,expectedCellWidth.intValue(), col.cellwidth); + } + } + +}