1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
package org.apache.poi.hssf.model;
import junit.framework.TestCase;
import org.apache.poi.ddf.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.util.HexDump;
/**
* @author Evgeniy Berlog
* @date 12.06.12
*/
public class TestHSSFAnchor extends TestCase {
public void testDefaultValues(){
HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
assertEquals(clientAnchor.getAnchorType(), 0);
assertEquals(clientAnchor.getCol1(), 0);
assertEquals(clientAnchor.getCol2(), 0);
assertEquals(clientAnchor.getDx1(), 0);
assertEquals(clientAnchor.getDx2(), 0);
assertEquals(clientAnchor.getDy1(), 0);
assertEquals(clientAnchor.getDy2(), 0);
assertEquals(clientAnchor.getRow1(), 0);
assertEquals(clientAnchor.getRow2(), 0);
clientAnchor = new HSSFClientAnchor(new EscherClientAnchorRecord());
assertEquals(clientAnchor.getAnchorType(), 0);
assertEquals(clientAnchor.getCol1(), 0);
assertEquals(clientAnchor.getCol2(), 0);
assertEquals(clientAnchor.getDx1(), 0);
assertEquals(clientAnchor.getDx2(), 0);
assertEquals(clientAnchor.getDy1(), 0);
assertEquals(clientAnchor.getDy2(), 0);
assertEquals(clientAnchor.getRow1(), 0);
assertEquals(clientAnchor.getRow2(), 0);
HSSFChildAnchor childAnchor = new HSSFChildAnchor();
assertEquals(childAnchor.getDx1(), 0);
assertEquals(childAnchor.getDx2(), 0);
assertEquals(childAnchor.getDy1(), 0);
assertEquals(childAnchor.getDy2(), 0);
childAnchor = new HSSFChildAnchor(new EscherChildAnchorRecord());
assertEquals(childAnchor.getDx1(), 0);
assertEquals(childAnchor.getDx2(), 0);
assertEquals(childAnchor.getDy1(), 0);
assertEquals(childAnchor.getDy2(), 0);
}
public void testCorrectOrderInSpContainer(){
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
HSSFSheet sheet = wb.getSheet("pictures");
HSSFPatriarch drawing = sheet.getDrawingPatriarch();
HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);
rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);
assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);
assertEquals(" " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + " ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);
rectangle.setAnchor(new HSSFClientAnchor());
assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);
assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);
assertEquals(" " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + " ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);
}
public void testCreateClientAnchorFromContainer(){
EscherContainerRecord container = new EscherContainerRecord();
EscherClientAnchorRecord escher = new EscherClientAnchorRecord();
escher.setFlag((short) 3);
escher.setCol1((short)11);
escher.setCol2((short)12);
escher.setRow1((short)13);
escher.setRow2((short) 14);
escher.setDx1((short) 15);
escher.setDx2((short) 16);
escher.setDy1((short) 17);
escher.setDy2((short) 18);
container.addChildRecord(escher);
HSSFClientAnchor anchor = (HSSFClientAnchor) HSSFAnchor.createAnchorFromEscher(container);
assertEquals(anchor.getCol1(), 11);
assertEquals(escher.getCol1(), 11);
assertEquals(anchor.getCol2(), 12);
assertEquals(escher.getCol2(), 12);
assertEquals(anchor.getRow1(), 13);
assertEquals(escher.getRow1(), 13);
assertEquals(anchor.getRow2(), 14);
assertEquals(escher.getRow2(), 14);
assertEquals(anchor.getDx1(), 15);
assertEquals(escher.getDx1(), 15);
assertEquals(anchor.getDx2(), 16);
assertEquals(escher.getDx2(), 16);
assertEquals(anchor.getDy1(), 17);
assertEquals(escher.getDy1(), 17);
assertEquals(anchor.getDy2(), 18);
assertEquals(escher.getDy2(), 18);
}
public void testCreateChildAnchorFromContainer(){
EscherContainerRecord container = new EscherContainerRecord();
EscherChildAnchorRecord escher = new EscherChildAnchorRecord();
escher.setDx1((short) 15);
escher.setDx2((short) 16);
escher.setDy1((short) 17);
escher.setDy2((short) 18);
container.addChildRecord(escher);
HSSFChildAnchor anchor = (HSSFChildAnchor) HSSFAnchor.createAnchorFromEscher(container);
assertEquals(anchor.getDx1(), 15);
assertEquals(escher.getDx1(), 15);
assertEquals(anchor.getDx2(), 16);
assertEquals(escher.getDx2(), 16);
assertEquals(anchor.getDy1(), 17);
assertEquals(escher.getDy1(), 17);
assertEquals(anchor.getDy2(), 18);
assertEquals(escher.getDy2(), 18);
}
public void testShapeEscherMustHaveAnchorRecord(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch drawing = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 200, 200, (short)2, 2, (short)15, 15);
anchor.setAnchorType(2);
HSSFSimpleShape rectangle = drawing.createSimpleShape(anchor);
rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
rectangle.setAnchor(anchor);
assertNotNull(anchor.getEscherAnchor());
assertNotNull(rectangle.getEscherContainer());
assertTrue(anchor.getEscherAnchor().equals(rectangle.getEscherContainer().getChildById(EscherClientAnchorRecord.RECORD_ID)));
}
public void testClientAnchorFromEscher(){
EscherClientAnchorRecord escher = new EscherClientAnchorRecord();
escher.setCol1((short)11);
escher.setCol2((short)12);
escher.setRow1((short)13);
escher.setRow2((short) 14);
escher.setDx1((short) 15);
escher.setDx2((short) 16);
escher.setDy1((short) 17);
escher.setDy2((short) 18);
HSSFClientAnchor anchor = new HSSFClientAnchor(escher);
assertEquals(anchor.getCol1(), 11);
assertEquals(escher.getCol1(), 11);
assertEquals(anchor.getCol2(), 12);
assertEquals(escher.getCol2(), 12);
assertEquals(anchor.getRow1(), 13);
assertEquals(escher.getRow1(), 13);
assertEquals(anchor.getRow2(), 14);
assertEquals(escher.getRow2(), 14);
assertEquals(anchor.getDx1(), 15);
assertEquals(escher.getDx1(), 15);
assertEquals(anchor.getDx2(), 16);
assertEquals(escher.getDx2(), 16);
assertEquals(anchor.getDy1(), 17);
assertEquals(escher.getDy1(), 17);
assertEquals(anchor.getDy2(), 18);
assertEquals(escher.getDy2(), 18);
}
public void testClientAnchorFromScratch(){
HSSFClientAnchor anchor = new HSSFClientAnchor();
EscherClientAnchorRecord escher = (EscherClientAnchorRecord) anchor.getEscherAnchor();
anchor.setAnchor((short)11, 12, 13, 14, (short)15, 16, 17, 18);
assertEquals(anchor.getCol1(), 11);
assertEquals(escher.getCol1(), 11);
assertEquals(anchor.getCol2(), 15);
assertEquals(escher.getCol2(), 15);
assertEquals(anchor.getRow1(), 12);
assertEquals(escher.getRow1(), 12);
assertEquals(anchor.getRow2(), 16);
assertEquals(escher.getRow2(), 16);
assertEquals(anchor.getDx1(), 13);
assertEquals(escher.getDx1(), 13);
assertEquals(anchor.getDx2(), 17);
assertEquals(escher.getDx2(), 17);
assertEquals(anchor.getDy1(), 14);
assertEquals(escher.getDy1(), 14);
assertEquals(anchor.getDy2(), 18);
assertEquals(escher.getDy2(), 18);
anchor.setCol1(111);
assertEquals(anchor.getCol1(), 111);
assertEquals(escher.getCol1(), 111);
anchor.setCol2(112);
assertEquals(anchor.getCol2(), 112);
assertEquals(escher.getCol2(), 112);
anchor.setRow1(113);
assertEquals(anchor.getRow1(), 113);
assertEquals(escher.getRow1(), 113);
anchor.setRow2(114);
assertEquals(anchor.getRow2(), 114);
assertEquals(escher.getRow2(), 114);
anchor.setDx1(115);
assertEquals(anchor.getDx1(), 115);
assertEquals(escher.getDx1(), 115);
anchor.setDx2(116);
assertEquals(anchor.getDx2(), 116);
assertEquals(escher.getDx2(), 116);
anchor.setDy1(117);
assertEquals(anchor.getDy1(), 117);
assertEquals(escher.getDy1(), 117);
anchor.setDy2(118);
assertEquals(anchor.getDy2(), 118);
assertEquals(escher.getDy2(), 118);
}
public void testChildAnchorFromEscher(){
EscherChildAnchorRecord escher = new EscherChildAnchorRecord();
escher.setDx1((short) 15);
escher.setDx2((short) 16);
escher.setDy1((short) 17);
escher.setDy2((short) 18);
HSSFChildAnchor anchor = new HSSFChildAnchor(escher);
assertEquals(anchor.getDx1(), 15);
assertEquals(escher.getDx1(), 15);
assertEquals(anchor.getDx2(), 16);
assertEquals(escher.getDx2(), 16);
assertEquals(anchor.getDy1(), 17);
assertEquals(escher.getDy1(), 17);
assertEquals(anchor.getDy2(), 18);
assertEquals(escher.getDy2(), 18);
}
public void testChildAnchorFromScratch(){
HSSFChildAnchor anchor = new HSSFChildAnchor();
EscherChildAnchorRecord escher = (EscherChildAnchorRecord) anchor.getEscherAnchor();
anchor.setAnchor(11, 12, 13, 14);
assertEquals(anchor.getDx1(), 11);
assertEquals(escher.getDx1(), 11);
assertEquals(anchor.getDx2(), 13);
assertEquals(escher.getDx2(), 13);
assertEquals(anchor.getDy1(), 12);
assertEquals(escher.getDy1(), 12);
assertEquals(anchor.getDy2(), 14);
assertEquals(escher.getDy2(), 14);
anchor.setDx1(115);
assertEquals(anchor.getDx1(), 115);
assertEquals(escher.getDx1(), 115);
anchor.setDx2(116);
assertEquals(anchor.getDx2(), 116);
assertEquals(escher.getDx2(), 116);
anchor.setDy1(117);
assertEquals(anchor.getDy1(), 117);
assertEquals(escher.getDy1(), 117);
anchor.setDy2(118);
assertEquals(anchor.getDy2(), 118);
assertEquals(escher.getDy2(), 118);
}
public void testEqualsToSelf(){
HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
assertEquals(clientAnchor, clientAnchor);
HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
assertEquals(childAnchor, childAnchor);
}
public void testPassIncompatibleTypeIsFalse(){
HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
assertNotSame(clientAnchor, "wrongType");
HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
assertNotSame(childAnchor, "wrongType");
}
public void testNullReferenceIsFalse() {
HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
assertFalse("Passing null to equals should return false", clientAnchor.equals(null));
HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
assertFalse("Passing null to equals should return false", childAnchor.equals(null));
}
public void testEqualsIsReflexiveIsSymmetric() {
HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
assertTrue(clientAnchor1.equals(clientAnchor2));
assertTrue(clientAnchor1.equals(clientAnchor2));
HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);
HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);
assertTrue(childAnchor1.equals(childAnchor2));
assertTrue(childAnchor2.equals(childAnchor1));
}
public void testEqualsValues(){
HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setDx1(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setDx1(0);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setDy1(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setDy1(1);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setDx2(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setDx2(2);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setDy2(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setDy2(3);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setCol1(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setCol1(4);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setRow1(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setRow1(5);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setCol2(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setCol2(6);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setRow2(10);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setRow2(7);
assertEquals(clientAnchor1, clientAnchor2);
clientAnchor2.setAnchorType(3);
assertNotSame(clientAnchor1, clientAnchor2);
clientAnchor2.setAnchorType(0);
assertEquals(clientAnchor1, clientAnchor2);
HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);
HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);
childAnchor1.setDx1(10);
assertNotSame(childAnchor1, childAnchor2);
childAnchor1.setDx1(0);
assertEquals(childAnchor1, childAnchor2);
childAnchor2.setDy1(10);
assertNotSame(childAnchor1, childAnchor2);
childAnchor2.setDy1(1);
assertEquals(childAnchor1, childAnchor2);
childAnchor2.setDx2(10);
assertNotSame(childAnchor1, childAnchor2);
childAnchor2.setDx2(2);
assertEquals(childAnchor1, childAnchor2);
childAnchor2.setDy2(10);
assertNotSame(childAnchor1, childAnchor2);
childAnchor2.setDy2(3);
assertEquals(childAnchor1, childAnchor2);
}
public void testFlipped(){
HSSFChildAnchor child = new HSSFChildAnchor(2,2,1,1);
assertEquals(child.isHorizontallyFlipped(), true);
assertEquals(child.isVerticallyFlipped(), true);
assertEquals(child.getDx1(), 1);
assertEquals(child.getDx2(), 2);
assertEquals(child.getDy1(), 1);
assertEquals(child.getDy2(), 2);
child = new HSSFChildAnchor(3,3,4,4);
assertEquals(child.isHorizontallyFlipped(), false);
assertEquals(child.isVerticallyFlipped(), false);
assertEquals(child.getDx1(), 3);
assertEquals(child.getDx2(), 4);
assertEquals(child.getDy1(), 3);
assertEquals(child.getDy2(), 4);
HSSFClientAnchor client = new HSSFClientAnchor(1,1,1,1, (short)4,4,(short)3,3);
assertEquals(client.isVerticallyFlipped(), true);
assertEquals(client.isHorizontallyFlipped(), true);
assertEquals(client.getCol1(), 3);
assertEquals(client.getCol2(), 4);
assertEquals(client.getRow1(), 3);
assertEquals(client.getRow2(), 4);
client = new HSSFClientAnchor(1,1,1,1, (short)5,5,(short)6,6);
assertEquals(client.isVerticallyFlipped(), false);
assertEquals(client.isHorizontallyFlipped(), false);
assertEquals(client.getCol1(), 5);
assertEquals(client.getCol2(), 6);
assertEquals(client.getRow1(), 5);
assertEquals(client.getRow2(), 6);
}
}
|