teststyletextpropatom.java
来自「EXCEL read and write」· Java 代码 · 共 773 行 · 第 1/3 页
JAVA
773 行
tp = tppd.addWithName("alignment"); tp.setValue(0); tp = tppd.addWithName("linespacing"); tp.setValue(80); // Now do 4 character styles LinkedList cs = stpa.getCharacterStyles(); // First is 30 long, bold and font size TextPropCollection tpca = (TextPropCollection)cs.get(0); tpca.updateTextSize(30); tp = tpca.addWithName("font.size"); tp.setValue(20); CharFlagsTextProp cftp = (CharFlagsTextProp) tpca.addWithName("char_flags"); assertEquals(0, cftp.getValue()); cftp.setSubValue(true, CharFlagsTextProp.BOLD_IDX); assertEquals(1, cftp.getValue()); // Second is 28 long, blue and italic TextPropCollection tpcb = stpa.addCharacterTextPropCollection(28); tp = tpcb.addWithName("font.size"); tp.setValue(20); tp = tpcb.addWithName("font.color"); tp.setValue(0x05000000); cftp = (CharFlagsTextProp)tpcb.addWithName("char_flags"); cftp.setSubValue(true, CharFlagsTextProp.ITALIC_IDX); assertEquals(2, cftp.getValue()); // Third is 25 long and red TextPropCollection tpcc = stpa.addCharacterTextPropCollection(25); tp = tpcc.addWithName("font.size"); tp.setValue(20); tp = tpcc.addWithName("font.color"); tp.setValue(0xfe0033ff); // Fourth is 96 long, underlined and different+bigger font TextPropCollection tpcd = stpa.addCharacterTextPropCollection(96); tp = tpcd.addWithName("font.size"); tp.setValue(24); tp = tpcd.addWithName("font.index"); tp.setValue(1); cftp = (CharFlagsTextProp)tpcd.addWithName("char_flags"); cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX); assertEquals(4, cftp.getValue()); // Fifth is 1 long, underlined and different+bigger font + red TextPropCollection tpce = stpa.addCharacterTextPropCollection(1); tp = tpce.addWithName("font.size"); tp.setValue(24); tp = tpce.addWithName("font.index"); tp.setValue(1); tp = tpce.addWithName("font.color"); tp.setValue(0xfe0033ff); cftp = (CharFlagsTextProp)tpce.addWithName("char_flags"); cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX); assertEquals(4, cftp.getValue()); // Check it's as expected assertEquals(4, stpa.getParagraphStyles().size()); assertEquals(5, stpa.getCharacterStyles().size()); // Compare in detail to b StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); LinkedList psb = stpb.getParagraphStyles(); LinkedList csb = stpb.getCharacterStyles(); assertEquals(psb.size(), ps.size()); assertEquals(csb.size(), cs.size()); // Ensure Paragraph Character styles match for(int z=0; z<2; z++) { LinkedList lla = cs; LinkedList llb = csb; int upto = 5; if(z == 1) { lla = ps; llb = psb; upto = 4; } for(int i=0; i<upto; i++) { TextPropCollection ca = (TextPropCollection)lla.get(i); TextPropCollection cb = (TextPropCollection)llb.get(i); assertEquals(ca.getCharactersCovered(), cb.getCharactersCovered()); assertEquals(ca.getTextPropList().size(), cb.getTextPropList().size()); for(int j=0; j<ca.getTextPropList().size(); j++) { TextProp tpa = (TextProp)ca.getTextPropList().get(j); TextProp tpb = (TextProp)cb.getTextPropList().get(j); //System.out.println("TP " + i + " " + j + " " + tpa.getName() + "\t" + tpa.getValue() ); assertEquals(tpa.getName(), tpb.getName()); assertEquals(tpa.getMask(), tpb.getMask()); assertEquals(tpa.getWriteMask(), tpb.getWriteMask()); assertEquals(tpa.getValue(), tpb.getValue()); } ByteArrayOutputStream ba = new ByteArrayOutputStream(); ByteArrayOutputStream bb = new ByteArrayOutputStream(); ca.writeOut(ba); cb.writeOut(bb); byte[] cab = ba.toByteArray(); byte[] cbb = bb.toByteArray(); assertEquals(cbb.length, cab.length); for(int j=0; j<cab.length; j++) { //System.out.println("On tp " + z + " " + i + " " + j + "\t" + cab[j] + "\t" + cbb[j]); assertEquals(cbb[j], cab[j]); } } } // Check byte level with b ByteArrayOutputStream baos = new ByteArrayOutputStream(); stpa.writeOut(baos); byte[] b = baos.toByteArray(); assertEquals(data_b.length, b.length); for(int i=0; i<data_b.length; i++) { //System.out.println(i + "\t" + b[i] + "\t" + data_b[i] + "\t" + Integer.toHexString(b[i]) ); assertEquals(data_b[i],b[i]); } } public void testWriteA() throws Exception { doReadWrite(data_a, -1); } public void testLoadWriteA() throws Exception { doReadWrite(data_b, data_b_text_len); } public void testWriteB() throws Exception { doReadWrite(data_b, -1); } public void testLoadWriteB() throws Exception { doReadWrite(data_b, data_b_text_len); } public void testLoadWriteC() throws Exception { doReadWrite(data_c, data_c_text_len); } public void testLoadWriteD() throws Exception { doReadWrite(data_d, data_d_text_len); } protected void doReadWrite(byte[] data, int textlen) throws Exception { StyleTextPropAtom stpb = new StyleTextPropAtom(data, 0,data.length); if(textlen != -1) stpb.setParentTextSize(textlen); ByteArrayOutputStream out = new ByteArrayOutputStream(); stpb.writeOut(out); byte[] bytes = out.toByteArray(); assertEquals(data.length, bytes.length); try { assertTrue(Arrays.equals(data, bytes)); } catch (Throwable e){ //print hex dump if failed assertEquals(HexDump.toHex(data), HexDump.toHex(bytes)); } } public void testNotEnoughDataProp() throws Exception { // We don't have enough data in the record to cover // all the properties the mask says we have // Make sure we just do the best we can StyleTextPropAtom stpc = new StyleTextPropAtom(data_c,0,data_c.length); stpc.setParentTextSize(data_c_text_len); // If we get here, we didn't break } /** * Check the test data for Bug 40143. */ public void testBug40143() throws Exception { StyleTextPropAtom atom = new StyleTextPropAtom(data_d, 0, data_d.length); atom.setParentTextSize(data_d_text_len); TextPropCollection prprops = (TextPropCollection)atom.getParagraphStyles().getFirst(); assertEquals(data_d_text_len+1, prprops.getCharactersCovered()); assertEquals(1, prprops.getTextPropList().size()); //1 property found assertEquals(1, prprops.findByName("alignment").getValue()); TextPropCollection chprops = (TextPropCollection)atom.getCharacterStyles().getFirst(); assertEquals(data_d_text_len+1, chprops.getCharactersCovered()); assertEquals(5, chprops.getTextPropList().size()); //5 properties found assertEquals(1, chprops.findByName("char_flags").getValue()); assertEquals(1, chprops.findByName("font.index").getValue()); assertEquals(20, chprops.findByName("font.size").getValue()); assertEquals(0, chprops.findByName("asian.font.index").getValue()); assertEquals(1, chprops.findByName("ansi.font.index").getValue()); } /** * Check the test data for Bug 42677. */ public void test42677() throws Exception { int length = 18; byte[] data = {0x00, 0x00, (byte)0xA1, 0x0F, 0x28, 0x00, 0x00, 0x00, 0x13, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , (byte)0xF1 , 0x20 , 0x00, 0x00 , 0x00 , 0x00 , 0x22 , 0x20 , 0x00 , 0x00 , 0x64 , 0x00 , 0x00 , 0x00 , 0x00 , (byte)0xFF , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x63 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x0F , 0x00 }; doReadWrite(data, length); } /** * Bug 45815: bit mask values are not preserved on read-write * * From the test file attached to the bug: * * <StyleTextPropAtom info="0" type="4001" size="94" offset="114782" header="00 00 A1 0F 5E 00 00 00 "> * 14 00 00 00 00 00 41 00 0A 00 06 00 50 00 07 00 01 00 00 00 00 00 00 00 02 * 00 00 00 01 04 00 00 01 04 01 00 00 00 01 08 00 00 01 08 0C 00 00 00 01 0C * 00 00 01 0C 01 00 00 00 01 10 00 00 01 10 01 00 00 00 01 14 00 00 01 14 01 * 00 00 00 01 18 00 00 01 18 01 00 00 00 01 1C 00 00 01 1C * </StyleTextPropAtom> */ public void test45815() throws Exception { int length = 19; byte[] data = { 0x00, 0x00, (byte)0xA1, 0x0F, 0x5E, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x0A, 0x00, 0x06, 0x00, 0x50, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x01, 0x08, 0x0C, 0x00, 0x00, 0x00, 0x01, 0x0C, 0x00, 0x00, 0x01, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x01, 0x14, 0x01, 0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x01, 0x18, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1C, 0x00, 0x00, 0x01, 0x1C }; doReadWrite(data, length); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?