Fix Sonar issues and IDE warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1882842 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2020-10-25 16:07:30 +00:00
parent 02355346db
commit 8089682957
4 changed files with 47 additions and 57 deletions

View File

@ -71,9 +71,8 @@ public final class TestXSSFTableColumn {
assertNotNull(tableColumns.get(0).getXmlColumnPr()); assertNotNull(tableColumns.get(0).getXmlColumnPr());
assertNull(tableColumns.get(1).getXmlColumnPr()); // unmapped column assertNull(tableColumns.get(1).getXmlColumnPr()); // unmapped column
assertNotNull(tableColumns.get(2).getColumnIndex()); assertEquals(2, tableColumns.get(2).getColumnIndex());
assertNotNull(tableColumns.get(3).getColumnIndex()); assertEquals(3, tableColumns.get(3).getColumnIndex());
} }
} }
} }

View File

@ -19,7 +19,7 @@ package org.apache.poi.hslf.record;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.assertFalse;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -97,14 +97,15 @@ public final class TestCString {
ca.setText("Comments"); ca.setText("Comments");
ca.setOptions(0x10); ca.setOptions(0x10);
try { // Check bytes weren't the same
for(int i=0; i<data_a.length; i++) { boolean equals = true;
assertEquals(data_a[i],data_b[i]); for(int i=0; i<data_a.length; i++) {
if (data_a[i] != data_b[i]) {
equals = false;
break;
} }
fail();
} catch(Error e) {
// Good, they're not the same
} }
assertFalse("Arrays should not be equals", equals);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ca.writeOut(baos); ca.writeOut(baos);

View File

@ -19,9 +19,9 @@ package org.apache.poi.hslf.record;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -92,7 +92,7 @@ public final class TestComment2000 {
}; };
private static SimpleDateFormat sdf; private static SimpleDateFormat sdf;
@BeforeClass @BeforeClass
public static void initDateFormat() { public static void initDateFormat() {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ROOT); sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ROOT);
@ -102,22 +102,22 @@ public final class TestComment2000 {
@Test @Test
public void testRecordType() { public void testRecordType() {
Comment2000 ca = new Comment2000(data_a, 0, data_a.length); Comment2000 ca = new Comment2000(data_a, 0, data_a.length);
assertEquals(12000l, ca.getRecordType()); assertEquals(12000L, ca.getRecordType());
} }
@Test @Test
public void testAuthor() { public void testAuthor() {
Comment2000 ca = new Comment2000(data_a, 0, data_a.length); Comment2000 ca = new Comment2000(data_a, 0, data_a.length);
assertEquals("Dumbledore", ca.getAuthor()); assertEquals("Dumbledore", ca.getAuthor());
assertEquals("D", ca.getAuthorInitials()); assertEquals("D", ca.getAuthorInitials());
} }
@Test @Test
public void testText() { public void testText() {
Comment2000 ca = new Comment2000(data_a, 0, data_a.length); Comment2000 ca = new Comment2000(data_a, 0, data_a.length);
assertEquals("Yes, they certainly are, aren't they!", ca.getText()); assertEquals("Yes, they certainly are, aren't they!", ca.getText());
} }
@Test @Test
public void testCommentAtom() throws Exception { public void testCommentAtom() throws Exception {
Comment2000 ca = new Comment2000(data_a, 0, data_a.length); Comment2000 ca = new Comment2000(data_a, 0, data_a.length);
@ -129,7 +129,7 @@ public final class TestComment2000 {
Date exp_a = sdf.parse("2006-01-24 10:26:15.205"); Date exp_a = sdf.parse("2006-01-24 10:26:15.205");
assertEquals(exp_a, c2a.getDate()); assertEquals(exp_a, c2a.getDate());
} }
@Test @Test
public void testCommentAtomB() throws Exception { public void testCommentAtomB() throws Exception {
Comment2000 cb = new Comment2000(data_b, 0, data_b.length); Comment2000 cb = new Comment2000(data_b, 0, data_b.length);
@ -191,14 +191,14 @@ public final class TestComment2000 {
assertEquals(cn.getAuthorInitials(), cb.getAuthorInitials()); assertEquals(cn.getAuthorInitials(), cb.getAuthorInitials());
// Check bytes weren't the same // Check bytes weren't the same
try { boolean equals = true;
for(int i=0; i<data_a.length; i++) { for(int i=0; i<data_a.length; i++) {
assertEquals(data_a[i],data_b[i]); if (data_a[i] != data_b[i]) {
equals = false;
break;
} }
fail();
} catch(Error e) {
// Good, they're not the same
} }
assertFalse("Arrays should not be equals", equals);
// Check bytes are now the same // Check bytes are now the same
ByteArrayOutputStream baosa = new ByteArrayOutputStream(); ByteArrayOutputStream baosa = new ByteArrayOutputStream();

View File

@ -143,9 +143,9 @@ public final class TestStyleTextPropAtom {
StyleTextPropAtom stpa = new StyleTextPropAtom(data_a,0,data_a.length); StyleTextPropAtom stpa = new StyleTextPropAtom(data_a,0,data_a.length);
StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length);
StyleTextPropAtom stpc = new StyleTextPropAtom(data_c,0,data_c.length); StyleTextPropAtom stpc = new StyleTextPropAtom(data_c,0,data_c.length);
assertEquals(4001l, stpa.getRecordType()); assertEquals(4001L, stpa.getRecordType());
assertEquals(4001l, stpb.getRecordType()); assertEquals(4001L, stpb.getRecordType());
assertEquals(4001l, stpc.getRecordType()); assertEquals(4001L, stpc.getRecordType());
} }
@ -392,12 +392,12 @@ public final class TestStyleTextPropAtom {
TextPropCollection b_ch_2 = b_ch_l.get(1); TextPropCollection b_ch_2 = b_ch_l.get(1);
TextPropCollection b_ch_3 = b_ch_l.get(2); TextPropCollection b_ch_3 = b_ch_l.get(2);
TextPropCollection b_ch_4 = b_ch_l.get(3); TextPropCollection b_ch_4 = b_ch_l.get(3);
assertNotNull(b_p_1); assertNotNull(b_p_1);
assertNotNull(b_p_2); assertNotNull(b_p_2);
assertNotNull(b_p_3); assertNotNull(b_p_3);
assertNotNull(b_p_4); assertNotNull(b_p_4);
assertNotNull(b_ch_1); assertNotNull(b_ch_1);
assertNotNull(b_ch_2); assertNotNull(b_ch_2);
assertNotNull(b_ch_3); assertNotNull(b_ch_3);
@ -524,8 +524,7 @@ public final class TestStyleTextPropAtom {
tp = tpca.addWithName("font.size"); tp = tpca.addWithName("font.size");
tp.setValue(20); tp.setValue(20);
CharFlagsTextProp cftp = (CharFlagsTextProp) CharFlagsTextProp cftp = tpca.addWithName("char_flags");
tpca.addWithName("char_flags");
assertEquals(0, cftp.getValue()); assertEquals(0, cftp.getValue());
cftp.setSubValue(true, CharFlagsTextProp.BOLD_IDX); cftp.setSubValue(true, CharFlagsTextProp.BOLD_IDX);
assertEquals(1, cftp.getValue()); assertEquals(1, cftp.getValue());
@ -537,7 +536,7 @@ public final class TestStyleTextPropAtom {
tp.setValue(20); tp.setValue(20);
tp = tpcb.addWithName("font.color"); tp = tpcb.addWithName("font.color");
tp.setValue(0x05000000); tp.setValue(0x05000000);
cftp = (CharFlagsTextProp)tpcb.addWithName("char_flags"); cftp = tpcb.addWithName("char_flags");
cftp.setSubValue(true, CharFlagsTextProp.ITALIC_IDX); cftp.setSubValue(true, CharFlagsTextProp.ITALIC_IDX);
assertEquals(2, cftp.getValue()); assertEquals(2, cftp.getValue());
@ -556,7 +555,7 @@ public final class TestStyleTextPropAtom {
tp.setValue(24); tp.setValue(24);
tp = tpcd.addWithName("font.index"); tp = tpcd.addWithName("font.index");
tp.setValue(1); tp.setValue(1);
cftp = (CharFlagsTextProp)tpcd.addWithName("char_flags"); cftp = tpcd.addWithName("char_flags");
cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX); cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX);
assertEquals(4, cftp.getValue()); assertEquals(4, cftp.getValue());
@ -569,7 +568,7 @@ public final class TestStyleTextPropAtom {
tp.setValue(1); tp.setValue(1);
tp = tpce.addWithName("font.color"); tp = tpce.addWithName("font.color");
tp.setValue(0xfe0033ff); tp.setValue(0xfe0033ff);
cftp = (CharFlagsTextProp)tpce.addWithName("char_flags"); cftp = tpce.addWithName("char_flags");
cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX); cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX);
assertEquals(4, cftp.getValue()); assertEquals(4, cftp.getValue());
@ -646,28 +645,28 @@ public final class TestStyleTextPropAtom {
} }
@Test @Test
public void testWriteA() { public void testWriteA() throws IOException {
doReadWrite(data_a, -1); doReadWrite(data_a, -1);
} }
@Test @Test
public void testLoadWriteA() { public void testLoadWriteA() throws IOException {
doReadWrite(data_b, data_b_text_len); doReadWrite(data_b, data_b_text_len);
} }
@Test @Test
public void testWriteB() { public void testWriteB() throws IOException {
doReadWrite(data_b, -1); doReadWrite(data_b, -1);
} }
@Test @Test
public void testLoadWriteB() { public void testLoadWriteB() throws IOException {
doReadWrite(data_b, data_b_text_len); doReadWrite(data_b, data_b_text_len);
} }
@Test @Test
public void testLoadWriteC() { public void testLoadWriteC() throws IOException {
// BitMaskTextProperties will sanitize the output // BitMaskTextProperties will sanitize the output
byte[] expected = data_c.clone(); byte[] expected = data_c.clone();
expected[56] = 0; expected[56] = 0;
@ -676,33 +675,25 @@ public final class TestStyleTextPropAtom {
} }
@Test @Test
public void testLoadWriteD() { public void testLoadWriteD() throws IOException {
doReadWrite(data_d, data_d_text_len); doReadWrite(data_d, data_d_text_len);
} }
protected void doReadWrite(byte[] data, int textlen) { protected void doReadWrite(byte[] data, int textlen) throws IOException {
doReadWrite(data, data, textlen); doReadWrite(data, data, textlen);
} }
protected void doReadWrite(byte[] data, byte[] expected, int textlen) { protected void doReadWrite(byte[] data, byte[] expected, int textlen) throws IOException {
StyleTextPropAtom stpb = new StyleTextPropAtom(data, 0,data.length); StyleTextPropAtom stpb = new StyleTextPropAtom(data, 0,data.length);
if(textlen != -1) stpb.setParentTextSize(textlen); if(textlen != -1) stpb.setParentTextSize(textlen);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
try { stpb.writeOut(out);
stpb.writeOut(out);
} catch (IOException e) {
throw new RuntimeException(e);
}
byte[] bytes = out.toByteArray(); byte[] bytes = out.toByteArray();
assertEquals(expected.length, bytes.length); assertEquals(expected.length, bytes.length);
try { assertArrayEquals("Had: " + HexDump.toHex(expected) + "\nand: " + HexDump.toHex(bytes),
assertArrayEquals(expected, bytes); expected, bytes);
} catch (Throwable e){
//print hex dump if failed
assertEquals(HexDump.toHex(expected), HexDump.toHex(bytes));
}
} }
@Test @Test
@ -743,7 +734,7 @@ public final class TestStyleTextPropAtom {
* Check the test data for Bug 42677. * Check the test data for Bug 42677.
*/ */
@Test @Test
public void test42677() { public void test42677() throws IOException {
int length = 18; int length = 18;
byte[] data = { byte[] data = {
0x00, 0x00, (byte)0xA1, 0x0F, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xA1, 0x0F, 0x28, 0x00, 0x00, 0x00,
@ -769,7 +760,7 @@ public final class TestStyleTextPropAtom {
* </StyleTextPropAtom> * </StyleTextPropAtom>
*/ */
@Test @Test
public void test45815() { public void test45815() throws IOException {
int length = 19; int length = 19;
byte[] data = { byte[] data = {
0x00, 0x00, (byte)0xA1, 0x0F, 0x5E, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, (byte)0xA1, 0x0F, 0x5E, 0x00, 0x00, 0x00, 0x14, 0x00,
@ -789,8 +780,7 @@ public final class TestStyleTextPropAtom {
// the bitmask text properties will sanitize the bytes and thus the bytes differ // the bitmask text properties will sanitize the bytes and thus the bytes differ
byte[] exptected = data.clone(); byte[] exptected = data.clone();
exptected[18] = 0; exptected[18] = 0;
doReadWrite(data, exptected, length); doReadWrite(data, exptected, length);
} }
} }