// Could be replaced with org.apache.commons.lang3.StringUtils#join
@Internal
public static String join(Object[] array, String separator) {
- if (array.length == 0) return "";
+ if (array == null || array.length == 0) return "";
StringBuilder sb = new StringBuilder();
sb.append(array[0]);
for (int i=1; i<array.length; i++) {
return sb.toString();
}
+ @Internal
+ public static String join(Object[] array) {
+ if (array == null) return "";
+ StringBuilder sb = new StringBuilder();
+ for (Object o : array) {
+ sb.append(o);
+ }
+ return sb.toString();
+ }
+
@Internal
public static String join(String separator, Object... array) {
return join(array, separator);
int pos = 0;
int lastUEPos = -1;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof PersistPtrHolder) {
- pp.put(Integer.valueOf(pos), r[i]);
+ for (final Record rec : r) {
+ if(rec instanceof PersistPtrHolder) {
+ pp.put(Integer.valueOf(pos), rec);
}
- if(r[i] instanceof UserEditAtom) {
- ue.put(Integer.valueOf(pos), r[i]);
+ if(rec instanceof UserEditAtom) {
+ ue.put(Integer.valueOf(pos), rec);
lastUEPos = pos;
}
ByteArrayOutputStream bc = new ByteArrayOutputStream();
- r[i].writeOut(bc);
+ rec.writeOut(bc);
pos += bc.size();
}
// Check that the UserEditAtom's point to right stuff
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof UserEditAtom) {
- UserEditAtom uea = (UserEditAtom)r[i];
+ for (final Record rec : r) {
+ if(rec instanceof UserEditAtom) {
+ UserEditAtom uea = (UserEditAtom)rec;
int luPos = uea.getLastUserEditAtomOffset();
int ppPos = uea.getPersistPointersOffset();
Record[] r = ss.getRecords();
int count = 0;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof Slide) {
+ for (final Record rec : r) {
+ if(rec instanceof Slide) {
count++;
}
}
Record[] r = ss.getRecords();
int count = 0;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof Notes &&
- r[i].getRecordType() == 1008l) {
+ for (final Record rec : r) {
+ if (rec instanceof Notes && rec.getRecordType() == 1008l) {
count++;
}
}
Record[] r = rt[0].getChildRecords();
int count = 0;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof SlideListWithText &&
- r[i].getRecordType() == 4080l) {
+ for (final Record rec : r) {
+ if (rec instanceof SlideListWithText && rec.getRecordType() == 4080l) {
count++;
}
}
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
+import org.apache.poi.util.StringUtil;
/**
* Tests that the QuickButCruddyTextExtractor works correctly
String foundText = te.getTextAsString();
// Turn the string array into a single string
- StringBuffer expectTextSB = new StringBuffer();
- for(int i=0; i<allTheText.length; i++) {
- expectTextSB.append(allTheText[i]);
- expectTextSB.append('\n');
- }
- String expectText = expectTextSB.toString();
+ String expectText = StringUtil.join(allTheText, "\n") + "\n";
// Ensure they match
assertEquals(expectText,foundText);
Document doc = ss.getDocumentRecord();
// Get the ExObjList
ExObjList exObjList = null;
- for(int i=0; i<doc._children.length; i++) {
- if(doc._children[i] instanceof ExObjList) {
- exObjList = (ExObjList)doc._children[i];
+ for (final Record rec : doc._children) {
+ if(rec instanceof ExObjList) {
+ exObjList = (ExObjList)rec;
}
}
if (exObjList == null) {
HSLFSlideShowImpl hss = new HSLFSlideShowImpl(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
Record[] r = hss.getRecords();
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof RecordContainer) {
- recordContainer = (RecordContainer)r[i];
+ for (Record rec : r) {
+ if(rec instanceof RecordContainer) {
+ recordContainer = (RecordContainer)rec;
return;
}
}
Document doc = ppt.getDocumentRecord();
SoundCollection soundCollection = null;
Record[] doc_ch = doc.getChildRecords();
- for (int i = 0; i < doc_ch.length; i++) {
- if (doc_ch[i] instanceof SoundCollection) {
- soundCollection = (SoundCollection) doc_ch[i];
+ for (Record rec : doc_ch) {
+ if (rec instanceof SoundCollection) {
+ soundCollection = (SoundCollection) rec;
break;
}
}
Sound sound = null;
Record[] sound_ch = soundCollection.getChildRecords();
int k = 0;
- for (int i = 0; i < sound_ch.length; i++) {
- if (sound_ch[i] instanceof Sound) {
- sound = (Sound) sound_ch[i];
+ for (Record rec : sound_ch) {
+ if (rec instanceof Sound) {
+ sound = (Sound) rec;
k++;
}
}
public void testDefaultStyles() {
TxMasterStyleAtom[] txmaster = getMasterStyles();
- for (int i = 0; i < txmaster.length; i++) {
- int txtype = txmaster[i].getTextType();
+ for (final TxMasterStyleAtom atom : txmaster) {
+ final int txtype = atom.getTextType();
switch (txtype){
case TextHeaderAtom.TITLE_TYPE:
- checkTitleType(txmaster[i]);
+ checkTitleType(atom);
break;
case TextHeaderAtom.BODY_TYPE:
- checkBodyType(txmaster[i]);
+ checkBodyType(atom);
break;
case TextHeaderAtom.NOTES_TYPE:
- checkNotesType(txmaster[i]);
+ checkNotesType(atom);
break;
case TextHeaderAtom.OTHER_TYPE:
- checkOtherType(txmaster[i]);
+ checkOtherType(atom);
break;
case TextHeaderAtom.CENTRE_BODY_TYPE:
break;
List<TxMasterStyleAtom> lst = new ArrayList<TxMasterStyleAtom>();
Record[] coreRecs = _ppt.getMostRecentCoreRecords();
- for (int i = 0; i < coreRecs.length; i++) {
- Record coreRec = coreRecs[i];
+ for (final Record coreRec : coreRecs) {
if(coreRec.getRecordType() == RecordTypes.MainMaster.typeID){
Record[] recs = coreRec.getChildRecords();
int cnt = 0;
- for (int j = 0; j < recs.length; j++) {
- Record rec = recs[j];
+ for (final Record rec : recs) {
if (rec instanceof TxMasterStyleAtom) {
lst.add((TxMasterStyleAtom) rec);
cnt++;
TxMasterStyleAtom txstyle = null;
Document doc = (Document)coreRec;
Record[] rec = doc.getEnvironment().getChildRecords();
- for (int j = 0; j < rec.length; j++) {
- if (rec[j] instanceof TxMasterStyleAtom) {
+ for (final Record atom : rec) {
+ if (atom instanceof TxMasterStyleAtom) {
if (txstyle != null) fail("Document.Environment must contain 1 TxMasterStyleAtom");
- txstyle = (TxMasterStyleAtom)rec[j];
+ txstyle = (TxMasterStyleAtom)atom;
}
}
if (txstyle == null) {
if(r instanceof RecordContainer) {
RecordContainer rc = (RecordContainer)r;
Record[] children = rc.getChildRecords();
- for(int i=0; i<children.length; i++) {
- ensureParentAware(children[i], rc);
+ for (Record rec : children) {
+ ensureParentAware(rec, rc);
}
}
}
package org.apache.poi.hwpf.extractor;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.junit.Test;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
+import org.apache.poi.util.StringUtil;
+import org.junit.After;
+import org.junit.Before;
/**
* Test the different routes to extracting text
*
* @author Nick Burch (nick at torchbox dot com)
*/
-public final class TestDifferentRoutes extends TestCase {
- private String[] p_text = new String[] {
+public final class TestDifferentRoutes {
+ private static final String[] p_text = new String[] {
"This is a simple word document\r",
"\r",
"It has a number of paragraphs in it\r",
private HWPFDocument doc;
- @Override
- protected void setUp() {
+ @Before
+ public void setUp() {
doc = HWPFTestDataSamples.openSampleFile("test2.doc");
}
+
+ @After
+ public void tearDown() throws IOException {
+ doc.close();
+ }
/**
* Test model based extraction
*/
+ @Test
public void testExtractFromModel() {
Range r = doc.getRange();
text[i] = p.text();
}
- assertEquals(p_text.length, text.length);
- for (int i = 0; i < p_text.length; i++) {
- assertEquals(p_text[i], text[i]);
- }
+ assertArrayEquals(p_text, text);
}
/**
* Test textPieces based extraction
*/
+ @Test
public void testExtractFromTextPieces() throws Exception {
- StringBuffer exp = new StringBuffer();
- for (int i = 0; i < p_text.length; i++) {
- exp.append(p_text[i]);
- }
- assertEquals(exp.toString(), doc.getDocumentText());
+ String expected = StringUtil.join(p_text, "");
+ assertEquals(expected, doc.getDocumentText());
}
}
package org.apache.poi.hwpf.extractor;
+import static org.apache.poi.POITestCase.assertContains;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.StringUtil;
import java.io.IOException;
import java.io.InputStream;
.replaceAll( "\r", "\n" ).trim();
TestCase.assertEquals( newExpected, newActual );
}
+
+ private static void assertExtractedContains(String[] extracted, String needle) {
+ String endnote = StringUtil.join(extracted, "");
+ assertContains(endnote, needle);
+ }
- private String[] p_text1 = new String[] {
+ private final String[] p_text1 = new String[] {
"This is a simple word document\r\n",
"\r\n",
"It has a number of paragraphs in it\r\n",
extractor2 = new WordExtractor(docTests.openResourceAsStream(filename2));
// Build splat'd out text version
- for(int i=0; i<p_text1.length; i++) {
- p_text1_block += p_text1[i];
- }
+ p_text1_block = StringUtil.join(p_text1, "");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ if (extractor != null) extractor.close();
+ if (extractor2 != null) extractor2.close();
}
/**
extractor = new WordExtractor(doc);
assertEquals("First header column!\tMid header Right header!\n", extractor.getHeaderText());
-
- String text = extractor.getText();
- assertTrue(text.indexOf("First header column!") > -1);
+ assertContains(extractor.getText(), "First header column!");
// Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5);
extractor = new WordExtractor(doc);
- assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor
- .getHeaderText());
- text = extractor.getText();
- assertTrue(text.indexOf("This is a simple header") > -1);
+ assertEquals("This is a simple header, with a \u20ac euro symbol in it.\n\n", extractor.getHeaderText());
+ assertContains(extractor.getText(), "This is a simple header");
}
public void testWithFooter() {
extractor = new WordExtractor(doc);
assertEquals("Footer Left\tFooter Middle Footer Right\n", extractor.getFooterText());
-
- String text = extractor.getText();
- assertTrue(text.indexOf("Footer Left") > -1);
+ assertContains(extractor.getText(), "Footer Left");
// Unicode
doc = HWPFTestDataSamples.openSampleFile(filename5);
extractor = new WordExtractor(doc);
- assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor
- .getFooterText());
- text = extractor.getText();
- assertTrue(text.indexOf("The footer, with") > -1);
+ assertEquals("The footer, with Moli\u00e8re, has Unicode in it.\n", extractor.getFooterText());
+ assertContains(extractor.getText(), "The footer, with");
}
public void testFootnote() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
- String[] text = extractor.getFootnoteText();
- StringBuffer b = new StringBuffer();
- for (int i = 0; i < text.length; i++) {
- b.append(text[i]);
- }
-
- assertTrue(b.toString().contains("TestFootnote"));
+ assertExtractedContains(extractor.getFootnoteText(), "TestFootnote");
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteNumberingFormat()); // msonfcArabic
assertEquals(0x00, doc.getRange().getSection(0).getFootnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getFootnoteNumberingOffset());
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
- String[] text = extractor.getEndnoteText();
- StringBuffer b = new StringBuffer();
- for (int i = 0; i < text.length; i++) {
- b.append(text[i]);
- }
-
- assertTrue(b.toString().contains("TestEndnote"));
+ assertExtractedContains(extractor.getEndnoteText(), "TestEndnote");
assertEquals(0x02, doc.getRange().getSection(0).getEndnoteNumberingFormat()); // msonfcLCRoman
assertEquals(0x00, doc.getRange().getSection(0).getEndnoteRestartQualifier()); // rncCont
assertEquals(0, doc.getRange().getSection(0).getEndnoteNumberingOffset());
HWPFDocument doc = HWPFTestDataSamples.openSampleFile(filename6);
extractor = new WordExtractor(doc);
- String[] text = extractor.getCommentsText();
- StringBuffer b = new StringBuffer();
- for (int i = 0; i < text.length; i++) {
- b.append(text[i]);
- }
-
- assertTrue(b.toString().contains("TestComment"));
+ assertExtractedContains(extractor.getCommentsText(), "TestComment");
}
public void testWord95() throws Exception {
String text = extractor.getText();
assertTrue(text.contains("\u0425\u0425\u0425\u0425\u0425"));
assertTrue(text.contains("\u0423\u0423\u0423\u0423\u0423"));
+
+ extractor.close();
}
public void testFirstParagraphFix() throws Exception {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
- files[0] = (new POIFSFileSystem(docTests.openResourceAsStream("test2.doc"))).getRoot();
+ POIFSFileSystem poifs = new POIFSFileSystem(docTests.openResourceAsStream("test2.doc"));
+ files[0] = poifs.getRoot();
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(docTests.getFile("test2.doc"));
files[1] = npoifsFileSystem.getRoot();
assertEquals(p_text1_block, extractor.getText());
}
+ poifs.close();
npoifsFileSystem.close();
}
try {
// Now overall
String text = ext.getText();
- assertTrue(text.indexOf("TEMPLATE = Normal") > -1);
- assertTrue(text.indexOf("SUBJECT = sample subject") > -1);
- assertTrue(text.indexOf("MANAGER = sample manager") > -1);
- assertTrue(text.indexOf("COMPANY = sample company") > -1);
+ assertContains(text, "TEMPLATE = Normal");
+ assertContains(text, "SUBJECT = sample subject");
+ assertContains(text, "MANAGER = sample manager");
+ assertContains(text, "COMPANY = sample company");
} finally {
ext.close();
}
return f.getName().startsWith("Test") && TestReadAllFiles.checkExclude(f);
}
});
- for (int i = 0; i < fileList.length; i++) {
+ for (final File file : fileList) {
try {
- testRecreate(fileList[i]);
+ testRecreate(file);
} catch (Exception e) {
- throw new IOException("While handling file " + fileList[i], e);
+ throw new IOException("While handling file " + file, e);
}
}
}
copy.deleteOnExit();
final OutputStream out = new FileOutputStream(copy);
final POIFSFileSystem poiFs = new POIFSFileSystem();
- for (int i = 0; i < psf1.length; i++)
- {
+ for (POIFile file : psf1) {
final InputStream in =
- new ByteArrayInputStream(psf1[i].getBytes());
+ new ByteArrayInputStream(file.getBytes());
final PropertySet psIn = PropertySetFactory.create(in);
final MutablePropertySet psOut = new MutablePropertySet(psIn);
final ByteArrayOutputStream psStream =
psStream.close();
final byte[] streamData = psStream.toByteArray();
poiFs.createDocument(new ByteArrayInputStream(streamData),
- psf1[i].getName());
+ file.getName());
poiFs.writeFilesystem(out);
}
poiFs.close();
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
{
final Properties p = System.getProperties();
final List<String> names = new LinkedList<String>();
- for (Iterator<String> i = p.stringPropertyNames().iterator(); i.hasNext();)
- names.add(i.next());
+ for (String name : p.stringPropertyNames())
+ names.add(name);
Collections.sort(names);
for (String name : names) {
String value = p.getProperty(name);
// Check the numbers of the last seen columns
LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[24];
int lrscount = 0;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof LastCellOfRowDummyRecord) {
- lrs[lrscount] = (LastCellOfRowDummyRecord)r[i];
+ for (final Record rec : r) {
+ if(rec instanceof LastCellOfRowDummyRecord) {
+ lrs[lrscount] = (LastCellOfRowDummyRecord)rec;
lrscount++;
}
}
readRecords("MRExtraLines.xls");
int rowCount=0;
- for(int i=0; i<r.length; i++) {
- if(r[i] instanceof LastCellOfRowDummyRecord) {
- LastCellOfRowDummyRecord eor = (LastCellOfRowDummyRecord) r[i];
+ for (Record rec : r) {
+ if(rec instanceof LastCellOfRowDummyRecord) {
+ LastCellOfRowDummyRecord eor = (LastCellOfRowDummyRecord) rec;
assertEquals(rowCount, eor.getRow());
rowCount++;
}
Record[] rr = r;
int eorCount=0;
int sfrCount=0;
- for (int i = 0; i < rr.length; i++) {
- Record record = rr[i];
+ for (Record record : rr) {
if (record instanceof SharedFormulaRecord) {
sfrCount++;
}
@Test
public void testClone() {
byte[][] data = {data1, data2, data3, data4};
- for (int i = 0; i < data.length; i++) {
- RecordInputStream is = TestcaseRecordInputStream.create(HyperlinkRecord.sid, data[i]);
+ for (final byte[] d : data) {
+ RecordInputStream is = TestcaseRecordInputStream.create(HyperlinkRecord.sid, d);
HyperlinkRecord link = new HyperlinkRecord(is);
HyperlinkRecord clone = link.clone();
assertArrayEquals(link.serialize(), clone.serialize());
* Test that TextObjectRecord serializes logs records properly.
*/
public void testLongRecords() {
- int[] length = {1024, 2048, 4096, 8192, 16384}; //test against strings of different length
- for (int i = 0; i < length.length; i++) {
- StringBuffer buff = new StringBuffer(length[i]);
- for (int j = 0; j < length[i]; j++) {
+ int[] lengths = {1024, 2048, 4096, 8192, 16384}; //test against strings of different length
+ for (int length : lengths) {
+ StringBuffer buff = new StringBuffer(length);
+ for (int j = 0; j < length; j++) {
buff.append("x");
}
HSSFRichTextString str = new HSSFRichTextString(buff.toString());
String[] operation = new String[] {
"+", "-", "*", "/", "^", "&"
};
- for (int k = 0; k < operation.length; k++) {
- operationRefTest(operation[k]);
+ for (final String op : operation) {
+ operationRefTest(op);
}
}
* (Bug 42999 reported that dx1 and dx2 are swapped if dx1>dx2. It doesn't make sense for client anchors.)
*/
public void testConvertAnchor() {
- HSSFClientAnchor[] anchor = {
+ HSSFClientAnchor[] anchors = {
new HSSFClientAnchor( 0 , 0 , 0 , 0 ,(short)0, 1,(short)1,3),
new HSSFClientAnchor( 100 , 0 , 900 , 255 ,(short)0, 1,(short)1,3),
new HSSFClientAnchor( 900 , 0 , 100 , 255 ,(short)0, 1,(short)1,3)
};
- for (int i = 0; i < anchor.length; i++) {
- EscherClientAnchorRecord record = (EscherClientAnchorRecord)ConvertAnchor.createAnchor(anchor[i]);
- assertEquals(anchor[i].getDx1(), record.getDx1());
- assertEquals(anchor[i].getDx2(), record.getDx2());
- assertEquals(anchor[i].getDy1(), record.getDy1());
- assertEquals(anchor[i].getDy2(), record.getDy2());
- assertEquals(anchor[i].getCol1(), record.getCol1());
- assertEquals(anchor[i].getCol2(), record.getCol2());
- assertEquals(anchor[i].getRow1(), record.getRow1());
- assertEquals(anchor[i].getRow2(), record.getRow2());
+ for (HSSFClientAnchor anchor : anchors) {
+ EscherClientAnchorRecord record = (EscherClientAnchorRecord)ConvertAnchor.createAnchor(anchor);
+ assertEquals(anchor.getDx1(), record.getDx1());
+ assertEquals(anchor.getDx2(), record.getDx2());
+ assertEquals(anchor.getDy1(), record.getDy1());
+ assertEquals(anchor.getDy2(), record.getDy2());
+ assertEquals(anchor.getCol1(), record.getCol1());
+ assertEquals(anchor.getCol2(), record.getCol2());
+ assertEquals(anchor.getRow1(), record.getRow1());
+ assertEquals(anchor.getRow2(), record.getRow2());
}
}
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
List<HSSFPictureData> lst = (List<HSSFPictureData>)(List<?>)wb.getAllPictures();
//assertEquals(2, lst.size());
- for (Iterator it = lst.iterator(); it.hasNext(); ) {
- HSSFPictureData pict = (HSSFPictureData)it.next();
+ for (final HSSFPictureData pict : lst) {
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("jpeg")){
while (stream.available() >= buffer.length)
{
assertEquals(_buffer_size, stream.read(buffer));
- for (int j = 0; j < buffer.length; j++)
- {
+ for (byte data : buffer) {
assertEquals("in main loop, byte " + offset,
- _workbook_data[ offset ], buffer[ j ]);
+ _workbook_data[ offset ], data);
offset++;
}
assertEquals("offset " + offset, _workbook_size - offset,
assertEquals("testing block at offset " + offset, 64,
out_data.length);
- for (int j = 0; j < out_data.length; j++)
- {
+ for (byte b : out_data) {
assertEquals("testing byte at offset " + offset,
- data[ offset ], out_data[ j ]);
+ data[ offset ], b);
offset++;
}
}
// as expected
}
- for (int j = 0; j < _test_array.length; j++)
- {
+ for (byte b : _test_array) {
array = new byte[ 1 ];
- new ByteField(0, _test_array[ j ], array);
- assertEquals(_test_array[ j ], new ByteField(0, array).get());
+ new ByteField(0, b, array);
+ assertEquals(b, new ByteField(0, array).get());
}
}
ByteField field = new ByteField(0);
byte[] array = new byte[ 1 ];
- for (int j = 0; j < _test_array.length; j++)
- {
- field.set(_test_array[ j ]);
+ for (byte b : _test_array) {
+ field.set(b);
field.writeToBytes(array);
- assertEquals("testing ", _test_array[ j ], array[ 0 ]);
+ assertEquals("testing ", b, array[ 0 ]);
}
}
}
0, 1, 2, 3, 5
};
- for (int j = 0; j < testArray.length; j++)
- {
- list.add(testArray[ j ]);
+ for (int element : testArray) {
+ list.add(element);
}
for (int j = 0; j < testArray.length; j++)
{
// as expected
}
- for (int j = 0; j < _test_array.length; j++)
- {
+ for (int element : _test_array) {
array = new byte[ 4 ];
- new IntegerField(0, _test_array[ j ], array);
- assertEquals(_test_array[ j ], new IntegerField(0, array).get());
+ new IntegerField(0, element, array);
+ assertEquals(element, new IntegerField(0, array).get());
}
}
IntegerField field = new IntegerField(0);
byte[] array = new byte[ 4 ];
- for (int j = 0; j < _test_array.length; j++)
- {
- field.set(_test_array[ j ]);
+ for (int b : _test_array) {
+ field.set(b);
field.writeToBytes(array);
int val = array[ 3 ] << 24;
val += (array[ 2 ] << 16) & 0x00FF0000;
val += (array[ 1 ] << 8) & 0x0000FF00;
val += (array[ 0 ] & 0x000000FF);
- assertEquals("testing ", _test_array[ j ], val);
+ assertEquals("testing ", b, val);
}
}
}
// as expected
}
- for (int j = 0; j < _test_array.length; j++)
- {
+ for (long element : _test_array) {
array = new byte[ 8 ];
- new LongField(0, _test_array[ j ], array);
- assertEquals(_test_array[ j ], new LongField(0, array).get());
+ new LongField(0, element, array);
+ assertEquals(element, new LongField(0, array).get());
}
}
LongField field = new LongField(0);
byte[] array = new byte[ 8 ];
- for (int j = 0; j < _test_array.length; j++)
- {
- field.set(_test_array[ j ]);
+ for (long element : _test_array) {
+ field.set(element);
field.writeToBytes(array);
long val = (( long ) array[ 7 ]) << 56;
val += ((( long ) array[ 2 ]) << 16) & 0x0000000000FF0000L;
val += ((( long ) array[ 1 ]) << 8) & 0x000000000000FF00L;
val += (array[ 0 ] & 0x00000000000000FFL);
- assertEquals("testing ", _test_array[ j ], val);
+ assertEquals("testing ", element, val);
}
}
}
// as expected
}
- for (int j = 0; j < _test_array.length; j++)
- {
+ for (short element : _test_array) {
array = new byte[ 2 ];
- new ShortField(0, _test_array[ j ], array);
- assertEquals(_test_array[ j ], new ShortField(0, array).get());
+ new ShortField(0, element, array);
+ assertEquals(element, new ShortField(0, array).get());
}
}
ShortField field = new ShortField(0);
byte[] array = new byte[ 2 ];
- for (int j = 0; j < _test_array.length; j++)
- {
- field.set(_test_array[ j ]);
+ for (short element : _test_array) {
+ field.set(element);
field.writeToBytes(array);
short val = ( short ) (array[ 1 ] << 8);
val &= ( short ) 0xFF00;
val += ( short ) (array[ 0 ] & 0x00FF);
- assertEquals("testing ", _test_array[ j ], val);
+ assertEquals("testing ", element, val);
}
}
}
assertEquals("abc", StringUtil.join(",", "abc")); // degenerate case: one thing to join, no trailing comma
assertEquals("abc|def|ghi", StringUtil.join("|", "abc", "def", "ghi"));
assertEquals("5|8.5|true|string", StringUtil.join("|", 5, 8.5, true, "string")); //assumes Locale prints number decimal point as a period rather than a comma
+
+ String[] arr = new String[] { "Apache", "POI", "project" };
+ assertEquals("no separator", "ApachePOIproject", StringUtil.join(arr));
+ assertEquals("separator", "Apache POI project", StringUtil.join(arr, " "));
}
@Test