aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java
blob: a440a39def36ab29d67879a47edf61e6f69c96b7 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hssf.usermodel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.DVRecord;
import org.apache.poi.ss.usermodel.BaseTestDataValidation;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidation.ErrorStyle;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.junit.Test;

/**
 * Class for testing Excel's data validation mechanism
 *
 * @author Dragos Buleandra ( dragos.buleandra@trade2b.ro )
 */
public final class TestDataValidation extends BaseTestDataValidation {

    public TestDataValidation(){
        super(HSSFITestDataProvider.instance);
    }


	public void assertDataValidation(Workbook wb) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream(22000);
		try {
			wb.write(baos);
			baos.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		byte[] generatedContent = baos.toByteArray();
		boolean isSame;
//		if (false) {
//			// TODO - add proof spreadsheet and compare
//			InputStream proofStream = HSSFTestDataSamples.openSampleFileStream("TestDataValidation.xls");
//			isSame = compareStreams(proofStream, generatedContent);
//		}
		isSame = true;
		
		if (isSame) {
			return;
		}
		File tempDir = new File(System.getProperty("java.io.tmpdir"));
		File generatedFile = new File(tempDir, "GeneratedTestDataValidation.xls");
		try {
			FileOutputStream fileOut = new FileOutputStream(generatedFile);
			fileOut.write(generatedContent);
			fileOut.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	
		PrintStream ps = System.out;
	
		ps.println("This test case has failed because the generated file differs from proof copy '" 
				); // TODO+ proofFile.getAbsolutePath() + "'.");
		ps.println("The cause is usually a change to this test, or some common spreadsheet generation code.  "
				+ "The developer has to decide whether the changes were wanted or unwanted.");
		ps.println("If the changes to the generated version were unwanted, "
				+ "make the fix elsewhere (do not modify this test or the proof spreadsheet to get the test working).");
		ps.println("If the changes were wanted, make sure to open the newly generated file in Excel "
				+ "and verify it manually.  The new proof file should be submitted after it is verified to be correct.");
		ps.println("");
		ps.println("One other possible (but less likely) cause of a failed test is a problem in the "
				+ "comparison logic used here. Perhaps some extra file regions need to be ignored.");
		ps.println("The generated file has been saved to '" + generatedFile.getAbsolutePath() + "' for manual inspection.");
	
		fail("Generated file differs from proof copy.  See sysout comments for details on how to fix.");
		
	}
	
//	private static boolean compareStreams(InputStream isA, byte[] generatedContent) {
//
//		InputStream isB = new ByteArrayInputStream(generatedContent);
//
//		// The allowable regions where the generated file can differ from the 
//		// proof should be small (i.e. much less than 1K)
//		int[] allowableDifferenceRegions = { 
//				0x0228, 16,  // a region of the file containing the OS username
//				0x506C, 8,   // See RootProperty (super fields _seconds_2 and _days_2)
//		};
//		int[] diffs = StreamUtility.diffStreams(isA, isB, allowableDifferenceRegions);
//		if (diffs == null) {
//			return true;
//		}
//		System.err.println("Diff from proof: ");
//		for (int i = 0; i < diffs.length; i++) {
//			System.err.println("diff at offset: 0x" + Integer.toHexString(diffs[i]));
//		}
//		return false;
//	}
  




    /* package */ static void setCellValue(HSSFCell cell, String text) {
	  cell.setCellValue(new HSSFRichTextString(text));
	  
    }
  
	@Test
    public void testAddToExistingSheet() throws Exception {

		// dvEmpty.xls is a simple one sheet workbook.  With a DataValidations header record but no 
		// DataValidations.  It's important that the example has one SHEETPROTECTION record.
		// Such a workbook can be created in Excel (2007) by adding datavalidation for one cell
		// and then deleting the row that contains the cell.
		HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("dvEmpty.xls");  
		int dvRow = 0;
		Sheet sheet = wb.getSheetAt(0);
		DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
		DataValidationConstraint dc = dataValidationHelper.createIntegerConstraint(OperatorType.EQUAL, "42", null);
		DataValidation dv = dataValidationHelper.createValidation(dc,new CellRangeAddressList(dvRow, dvRow, 0, 0));
		
		dv.setEmptyCellAllowed(false);
		dv.setErrorStyle(ErrorStyle.STOP);
		dv.setShowPromptBox(true);
		dv.createErrorBox("Xxx", "Yyy");
		dv.setSuppressDropDownArrow(true);

		sheet.addValidationData(dv);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos);
		
		byte[] wbData = baos.toByteArray();
		
//		if (false) { // TODO (Jul 2008) fix EventRecordFactory to process unknown records, (and DV records for that matter)
//
//			ERFListener erfListener = null; // new MyERFListener();
//			EventRecordFactory erf = new EventRecordFactory(erfListener, null);
//			try {
//				POIFSFileSystem fs = new POIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
//				erf.processRecords(fs.createDocumentInputStream("Workbook"));
//			} catch (RecordFormatException e) {
//				throw new RuntimeException(e);
//			} catch (IOException e) {
//				throw new RuntimeException(e);
//			}
//		}
		// else verify record ordering by navigating the raw bytes
		
		byte[] dvHeaderRecStart= { (byte)0xB2, 0x01, 0x12, 0x00, };
		int dvHeaderOffset = findIndex(wbData, dvHeaderRecStart);
		assertTrue(dvHeaderOffset > 0);
		int nextRecIndex = dvHeaderOffset + 22;
		int nextSid 
			= ((wbData[nextRecIndex + 0] << 0) & 0x00FF) 
			+ ((wbData[nextRecIndex + 1] << 8) & 0xFF00)
			;
		// nextSid should be for a DVRecord.  If anything comes between the DV header record 
		// and the DV records, Excel will not be able to open the workbook without error.
		
		if (nextSid == 0x0867) {
			fail("Identified bug 45519");
		}
		assertEquals(DVRecord.sid, nextSid);
		
		wb.close();
	}
	
	private int findIndex(byte[] largeData, byte[] searchPattern) {
		byte firstByte = searchPattern[0];
		for (int i = 0; i < largeData.length; i++) {
			if(largeData[i] != firstByte) {
				continue;
			}
			boolean match = true;
			for (int j = 1; j < searchPattern.length; j++) {
				if(searchPattern[j] != largeData[i+j]) {
					match = false;
					break;
				}
			}
			if (match) {
				return i;
			}
		}
		return -1;
	}

	@Test
    public void testGetDataValidationsAny() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(ValidationType.ANY,
                OperatorType.IGNORED, null, null);
        CellRangeAddressList addressList = new CellRangeAddressList(1, 2, 3, 4);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        validation.setEmptyCellAllowed(true);
        validation.createErrorBox("error-title", "error-text");
        validation.createPromptBox("prompt-title", "prompt-text");
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        {
            CellRangeAddressList regions = dv.getRegions();
            assertEquals(1, regions.countRanges());

            CellRangeAddress address = regions.getCellRangeAddress(0);
            assertEquals(1, address.getFirstRow());
            assertEquals(2, address.getLastRow());
            assertEquals(3, address.getFirstColumn());
            assertEquals(4, address.getLastColumn());
        }
        assertEquals(true, dv.getEmptyCellAllowed());
        assertEquals(false, dv.getSuppressDropDownArrow());
        assertEquals(true, dv.getShowErrorBox());
        assertEquals("error-title", dv.getErrorBoxTitle());
        assertEquals("error-text", dv.getErrorBoxText());
        assertEquals(true, dv.getShowPromptBox());
        assertEquals("prompt-title", dv.getPromptBoxTitle());
        assertEquals("prompt-text", dv.getPromptBoxText());

        DataValidationConstraint c = dv.getValidationConstraint();
        assertEquals(ValidationType.ANY, c.getValidationType());
        assertEquals(OperatorType.IGNORED, c.getOperator());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsIntegerFormula() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createIntegerConstraint(OperatorType.BETWEEN, "=A2",
                "=A3");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.INTEGER, c.getValidationType());
        assertEquals(OperatorType.BETWEEN, c.getOperator());
        assertEquals("A2", c.getFormula1());
        assertEquals("A3", c.getFormula2());
        assertEquals(null, c.getValue1());
        assertEquals(null, c.getValue2());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsIntegerValue() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createIntegerConstraint(OperatorType.BETWEEN, "100",
                "200");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.INTEGER, c.getValidationType());
        assertEquals(OperatorType.BETWEEN, c.getOperator());
        assertEquals(null, c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(new Double("100"), c.getValue1());
        assertEquals(new Double("200"), c.getValue2());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsDecimal() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createDecimalConstraint(OperatorType.BETWEEN, "=A2",
                "200");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.DECIMAL, c.getValidationType());
        assertEquals(OperatorType.BETWEEN, c.getOperator());
        assertEquals("A2", c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(null, c.getValue1());
        assertEquals(new Double("200"), c.getValue2());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsDate() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createDateConstraint(OperatorType.EQUAL,
                "2014/10/25", null, null);
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.DATE, c.getValidationType());
        assertEquals(OperatorType.EQUAL, c.getOperator());
        assertEquals(null, c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(DateUtil.getExcelDate(DateUtil.parseYYYYMMDDDate("2014/10/25")), c.getValue1(), 0);
        assertEquals(null, c.getValue2());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsListExplicit() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(new String[] { "aaa",
                "bbb", "ccc" });
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        validation.setSuppressDropDownArrow(true);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        assertEquals(true, dv.getSuppressDropDownArrow());

        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.LIST, c.getValidationType());
        assertEquals(null, c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(null, c.getValue1());
        assertEquals(null, c.getValue2());
        String[] values = c.getExplicitListValues();
        assertEquals(3, values.length);
        assertEquals("aaa", values[0]);
        assertEquals("bbb", values[1]);
        assertEquals("ccc", values[2]);
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsListFormula() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createFormulaListConstraint("A2");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        validation.setSuppressDropDownArrow(true);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        assertEquals(true, dv.getSuppressDropDownArrow());

        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.LIST, c.getValidationType());
        assertEquals("A2", c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(null, c.getValue1());
        assertEquals(null, c.getValue2());
        
        wb.close();
    }

	@Test
    public void testGetDataValidationsFormula() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<HSSFDataValidation> list = sheet.getDataValidations();
        assertEquals(0, list.size());

        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("A2:A3");
        CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
        DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
        sheet.addValidationData(validation);

        list = sheet.getDataValidations(); // <-- works
        assertEquals(1, list.size());

        HSSFDataValidation dv = list.get(0);
        DVConstraint c = dv.getConstraint();
        assertEquals(ValidationType.FORMULA, c.getValidationType());
        assertEquals("A2:A3", c.getFormula1());
        assertEquals(null, c.getFormula2());
        assertEquals(null, c.getValue1());
        assertEquals(null, c.getValue2());
        wb.close();
    }
}