aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
blob: db090b1782c4db79217e4b7e1d70d57a9a051eb8 (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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/* ====================================================================
   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.ss.usermodel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
import org.junit.Test;

/**
 * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
 *
 * @author Yegor Kozlov
 */
public abstract class BaseTestNamedRange {

    private final ITestDataProvider _testDataProvider;

    protected BaseTestNamedRange(ITestDataProvider testDataProvider) {
        _testDataProvider = testDataProvider;
    }

    @Test
    public final void testCreate() throws Exception {
        // Create a new workbook
        Workbook wb = _testDataProvider.createWorkbook();
        wb.createSheet("Test1");
        wb.createSheet("Testing Named Ranges");

        Name name1 = wb.createName();
        name1.setNameName("testOne");

        //setting a duplicate name should throw IllegalArgumentException
        Name name2 = wb.createName();
        try {
            name2.setNameName("testOne");
            fail("expected exception");
        } catch (IllegalArgumentException e){
            assertEquals("The workbook already contains this name: testOne", e.getMessage());
        }
        //the check for duplicates is case-insensitive
        try {
            name2.setNameName("TESTone");
            fail("expected exception");
        } catch (IllegalArgumentException e){
            assertEquals("The workbook already contains this name: TESTone", e.getMessage());
        }

        name2.setNameName("testTwo");

        String ref1 = "Test1!$A$1:$B$1";
        name1.setRefersToFormula(ref1);
        assertEquals(ref1, name1.getRefersToFormula());
        assertEquals("Test1", name1.getSheetName());

        String ref2 = "'Testing Named Ranges'!$A$1:$B$1";
        name1.setRefersToFormula(ref2);
        assertEquals("'Testing Named Ranges'!$A$1:$B$1", name1.getRefersToFormula());
        assertEquals("Testing Named Ranges", name1.getSheetName());

        assertEquals(-1, name1.getSheetIndex());
        name1.setSheetIndex(-1);
        assertEquals(-1, name1.getSheetIndex());
        try {
            name1.setSheetIndex(2);
            fail("should throw IllegalArgumentException");
        } catch(IllegalArgumentException e){
            assertEquals("Sheet index (2) is out of range (0..1)", e.getMessage());
        }

        name1.setSheetIndex(1);
        assertEquals(1, name1.getSheetIndex());

        //-1 means the name applies to the entire workbook
        name1.setSheetIndex(-1);
        assertEquals(-1, name1.getSheetIndex());

        //names cannot be blank and must begin with a letter or underscore and not contain spaces
        String[] invalidNames = {"", "123", "1Name", "Named Range"};
        for (String name : invalidNames) {
            try {
                name1.setNameName(name);
                fail("should have thrown exceptiuon due to invalid name: " + name);
            } catch (IllegalArgumentException e) {
                // expected during successful test
            }
        }
        
        wb.close();
    }

    @Test
    public final void testAddRemove() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        assertEquals(0, wb.getNumberOfNames());
        Name name1 = wb.createName();
        name1.setNameName("name1");
        assertEquals(1, wb.getNumberOfNames());

        Name name2 = wb.createName();
        name2.setNameName("name2");
        assertEquals(2, wb.getNumberOfNames());

        Name name3 = wb.createName();
        name3.setNameName("name3");
        assertEquals(3, wb.getNumberOfNames());

        wb.removeName(wb.getName("name2"));
        assertEquals(2, wb.getNumberOfNames());

        wb.close();
    }

    @Test
    public final void testScope() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        wb.createSheet();
        wb.createSheet();

        Name name;

        name = wb.createName();
        name.setNameName("aaa");
        name = wb.createName();
        try {
            name.setNameName("aaa");
            fail("Expected exception");
        } catch(Exception e){
            assertEquals("The workbook already contains this name: aaa", e.getMessage());
        }

        name = wb.createName();
        name.setSheetIndex(0);
        name.setNameName("aaa");
        name = wb.createName();
        name.setSheetIndex(0);
        try {
            name.setNameName("aaa");
            fail("Expected exception");
        } catch(Exception e){
            assertEquals("The sheet already contains this name: aaa", e.getMessage());
        }

        name = wb.createName();
        name.setSheetIndex(1);
        name.setNameName("aaa");
        name = wb.createName();
        name.setSheetIndex(1);
        try {
            name.setNameName("aaa");
            fail("Expected exception");
        } catch(Exception e){
            assertEquals("The sheet already contains this name: aaa", e.getMessage());
        }

        assertEquals(3, wb.getNames("aaa").size());
        
        wb.close();
    }

    /**
     * Test case provided by czhang@cambian.com (Chun Zhang)
     * <p>
     * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=13775" target="_bug">#13775</a>
     */
    @Test
    public final void testMultiNamedRange() throws Exception {

         // Create a new workbook
         Workbook wb1 = _testDataProvider.createWorkbook();

         // Create a worksheet 'sheet1' in the new workbook
         wb1.createSheet ();
         wb1.setSheetName (0, "sheet1");

         // Create another worksheet 'sheet2' in the new workbook
         wb1.createSheet ();
         wb1.setSheetName (1, "sheet2");

         // Create a new named range for worksheet 'sheet1'
         Name namedRange1 = wb1.createName();

         // Set the name for the named range for worksheet 'sheet1'
         namedRange1.setNameName("RangeTest1");

         // Set the reference for the named range for worksheet 'sheet1'
         namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41");

         // Create a new named range for worksheet 'sheet2'
         Name namedRange2 = wb1.createName();

         // Set the name for the named range for worksheet 'sheet2'
         namedRange2.setNameName("RangeTest2");

         // Set the reference for the named range for worksheet 'sheet2'
         namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21");

         // Write the workbook to a file
         // Read the Excel file and verify its content
         Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
         Name nm1 = wb2.getName("RangeTest1");
        assertEquals("Name is " + nm1.getNameName(), "RangeTest1", nm1.getNameName());
        assertEquals("Reference is " + nm1.getRefersToFormula(), (wb2.getSheetName(0) + "!$A$1:$L$41"), nm1.getRefersToFormula());

         Name nm2 = wb2.getName("RangeTest2");
        assertEquals("Name is " + nm2.getNameName(), "RangeTest2", nm2.getNameName());
        assertEquals("Reference is " + nm2.getRefersToFormula(), (wb2.getSheetName(1) + "!$A$1:$O$21"), nm2.getRefersToFormula());
         
         wb2.close();
         wb1.close();
     }

    /**
     * Test to see if the print areas can be retrieved/created in memory
     */
    @Test
    public final void testSinglePrintArea() throws Exception {
        Workbook workbook = _testDataProvider.createWorkbook();
        workbook.createSheet("Test Print Area");
        String sheetName = workbook.getSheetName(0);

        String reference = "$A$1:$B$1";
        workbook.setPrintArea(0, reference);

        String retrievedPrintArea = workbook.getPrintArea(0);

        assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
        assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
        
        workbook.close();
    }

     /**
      * For Convenience, don't force sheet names to be used
      */
    @Test
     public final void testSinglePrintAreaWOSheet() throws Exception
     {
         Workbook workbook = _testDataProvider.createWorkbook();
         workbook.createSheet("Test Print Area");
         String sheetName = workbook.getSheetName(0);

         String reference = "$A$1:$B$1";
         workbook.setPrintArea(0, reference);

         String retrievedPrintArea = workbook.getPrintArea(0);

         assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
         assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea);
         
         workbook.close();
     }

     /**
      * Test to see if the print area made it to the file
      */
    @Test
    public final void testPrintAreaFile() throws Exception {
         Workbook wb1 = _testDataProvider.createWorkbook();
         wb1.createSheet("Test Print Area");
         String sheetName = wb1.getSheetName(0);


         String reference = "$A$1:$B$1";
         wb1.setPrintArea(0, reference);

         Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);

         String retrievedPrintArea = wb2.getPrintArea(0);
         assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
         assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
         
         wb2.close();
         wb1.close();
    }

    /**
     * Test to see if multiple print areas made it to the file
     */
    @Test
    public final void testMultiplePrintAreaFile() throws Exception {
        Workbook wb1 = _testDataProvider.createWorkbook();

        wb1.createSheet("Sheet1");
        wb1.createSheet("Sheet2");
        wb1.createSheet("Sheet3");
        String reference1 = "$A$1:$B$1";
        String reference2 = "$B$2:$D$5";
        String reference3 = "$D$2:$F$5";

        wb1.setPrintArea(0, reference1);
        wb1.setPrintArea(1, reference2);
        wb1.setPrintArea(2, reference3);

        //Check created print areas
        String retrievedPrintArea;

        retrievedPrintArea = wb1.getPrintArea(0);
        assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
        assertEquals("Sheet1!" + reference1, retrievedPrintArea);

        retrievedPrintArea = wb1.getPrintArea(1);
        assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
        assertEquals("Sheet2!" + reference2, retrievedPrintArea);

        retrievedPrintArea = wb1.getPrintArea(2);
        assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
        assertEquals("Sheet3!" + reference3, retrievedPrintArea);

        // Check print areas after re-reading workbook
        Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);

        retrievedPrintArea = wb2.getPrintArea(0);
        assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
        assertEquals("Sheet1!" + reference1, retrievedPrintArea);

        retrievedPrintArea = wb2.getPrintArea(1);
        assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
        assertEquals("Sheet2!" + reference2, retrievedPrintArea);

        retrievedPrintArea = wb2.getPrintArea(2);
        assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
        assertEquals("Sheet3!" + reference3, retrievedPrintArea);
        
        wb2.close();
        wb1.close();
    }

    /**
     * Tests the setting of print areas with coordinates (Row/Column designations)
     *
     */
    @Test
    public final void testPrintAreaCoords() throws Exception {
        Workbook workbook = _testDataProvider.createWorkbook();
        workbook.createSheet("Test Print Area");
        String sheetName = workbook.getSheetName(0);

        workbook.setPrintArea(0, 0, 1, 0, 0);

        String retrievedPrintArea = workbook.getPrintArea(0);

        assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
        assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
        
        workbook.close();
    }


    /**
     * Tests the parsing of union area expressions, and re-display in the presence of sheet names
     * with special characters.
     */
    @Test
    public final void testPrintAreaUnion() throws Exception {
        Workbook workbook = _testDataProvider.createWorkbook();
        workbook.createSheet("Test Print Area");

        String reference = "$A$1:$B$1,$D$1:$F$2";
        workbook.setPrintArea(0, reference);
        String retrievedPrintArea = workbook.getPrintArea(0);
        assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
        assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea);
        
        workbook.close();
    }

    /**
     * Verifies an existing print area is deleted
     *
     */
    @Test
    public final void testPrintAreaRemove() throws Exception {
        Workbook workbook = _testDataProvider.createWorkbook();
        workbook.createSheet("Test Print Area");
        workbook.getSheetName(0);

        workbook.setPrintArea(0, 0, 1, 0, 0);

        String retrievedPrintArea = workbook.getPrintArea(0);

        assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);

        workbook.removePrintArea(0);
        assertNull("PrintArea was not removed", workbook.getPrintArea(0));
        workbook.close();
    }

    /**
     * Test that multiple named ranges can be added written and read
     */
    @Test
    public final void testMultipleNamedWrite() throws Exception {
        Workbook wb1 = _testDataProvider.createWorkbook();


        wb1.createSheet("testSheet1");
        String sheetName = wb1.getSheetName(0);

        assertEquals("testSheet1", sheetName);

        //Creating new Named Range
        Name newNamedRange = wb1.createName();

        newNamedRange.setNameName("RangeTest");
        newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");

        //Creating another new Named Range
        Name newNamedRange2 = wb1.createName();

        newNamedRange2.setNameName("AnotherTest");
        newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6");

        Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
        Name nm = wb2.getName("RangeTest");
        assertEquals("Name is " + nm.getNameName(), "RangeTest", nm.getNameName());
        assertEquals("Reference is " + nm.getRefersToFormula(), (wb2.getSheetName(0) + "!$D$4:$E$8"), nm.getRefersToFormula());

        nm = wb2.getName("AnotherTest");
        assertEquals("Name is " + nm.getNameName(), "AnotherTest", nm.getNameName());
        assertEquals("Reference is " + nm.getRefersToFormula(), newNamedRange2.getRefersToFormula(), nm.getRefersToFormula());
        
        wb2.close();
        wb1.close();
    }

    /**
     * Verifies correct functioning for "single cell named range" (aka "named cell")
     */
    @Test
    public final void testNamedCell_1() throws Exception {

        // setup for this testcase
        String sheetName = "Test Named Cell";
        String cellName = "named_cell";
        String cellValue = "TEST Value";
        Workbook wb = _testDataProvider.createWorkbook();
        Sheet sheet = wb.createSheet(sheetName);
        CreationHelper factory = wb.getCreationHelper();
        sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cellValue));

        // create named range for a single cell using areareference
        Name namedCell = wb.createName();
        namedCell.setNameName(cellName);
        String reference = "'" + sheetName + "'" + "!A1:A1";
        namedCell.setRefersToFormula(reference);

        // retrieve the newly created named range
        Name aNamedCell = wb.getName(cellName);
        assertNotNull(aNamedCell);

        // retrieve the cell at the named range and test its contents
        AreaReference aref = wb.getCreationHelper().createAreaReference(aNamedCell.getRefersToFormula());
        assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());

        CellReference cref = aref.getFirstCell();
        assertNotNull(cref);
        Sheet s = wb.getSheet(cref.getSheetName());
        assertNotNull(s);
        Row r = sheet.getRow(cref.getRow());
        Cell c = r.getCell(cref.getCol());
        String contents = c.getRichStringCellValue().getString();
        assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
        wb.close();
    }

    /**
     * Verifies correct functioning for "single cell named range" (aka "named cell")
     */
    @Test
    public final void testNamedCell_2() throws Exception {

        // setup for this testcase
        String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
        Workbook wb = _testDataProvider.createWorkbook();
        CreationHelper factory = wb.getCreationHelper();
        Sheet sheet = wb.createSheet(sname);
        sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cvalue));

        // create named range for a single cell using cellreference
        Name namedCell = wb.createName();
        namedCell.setNameName(cname);
        String reference = sname+"!A1";
        namedCell.setRefersToFormula(reference);

        // retrieve the newly created named range
        Name aNamedCell = wb.getName(cname);
        assertNotNull(aNamedCell);

        // retrieve the cell at the named range and test its contents
        CellReference cref = new CellReference(aNamedCell.getRefersToFormula());
        assertNotNull(cref);
        Sheet s = wb.getSheet(cref.getSheetName());
        assertNotNull(s);
        Row r = sheet.getRow(cref.getRow());
        Cell c = r.getCell(cref.getCol());
        String contents = c.getRichStringCellValue().getString();
        assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
        
        wb.close();
    }


    /**
     * Bugzilla attachment 23444 (from bug 46973) has a NAME record with the following encoding:
     * <pre>
     * 00000000 | 18 00 17 00 00 00 00 08 00 00 00 00 00 00 00 00 | ................
     * 00000010 | 00 00 00 55 50 53 53 74 61 74 65                | ...UPSState
     * </pre>
     *
     * This caused trouble for anything that requires {@link Name#getRefersToFormula()}
     * It is easy enough to re-create the the same data (by not setting the formula). Excel
     * seems to gracefully remove this uninitialized name record.  It would be nice if POI
     * could do the same, but that would involve adjusting subsequent name indexes across
     * all formulas. <p>
     *
     * For the moment, POI has been made to behave more sensibly with uninitialized name
     * records.
     */
    @Test
    public final void testUninitialisedNameGetRefersToFormula_bug46973() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        Name n = wb.createName();
        n.setNameName("UPSState");
        String formula = n.getRefersToFormula();
        
        // bug 46973: fails here with IllegalArgumentException
        // ptgs must not be null

        assertNull(formula);
        // according to exact definition of isDeleted()
        assertFalse(n.isDeleted());
        wb.close();
    }

    @Test
    public final void testDeletedCell() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        Name n = wb.createName();
        n.setNameName("MyName");
        // contrived example to expose bug:
        n.setRefersToFormula("if(A1,\"#REF!\", \"\")");

        assertFalse("Identified bug in recoginising formulas referring to deleted cells", n.isDeleted());
        
        wb.close();
    }

    @Test
    public final void testFunctionNames() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        Name n = wb.createName();
        assertFalse(n.isFunctionName());

        n.setFunction(false);
        assertFalse(n.isFunctionName());

        n.setFunction(true);
        assertTrue(n.isFunctionName());

        n.setFunction(false);
        assertFalse(n.isFunctionName());
        
        wb.close();
    }

    @Test
    public final void testDefferedSetting() throws Exception {
        Workbook wb = _testDataProvider.createWorkbook();
        Name n1 = wb.createName();
        assertNull(n1.getRefersToFormula());
        assertEquals("", n1.getNameName());

        Name n2 = wb.createName();
        assertNull(n2.getRefersToFormula());
        assertEquals("", n2.getNameName());

        n1.setNameName("sale_1");
        n1.setRefersToFormula("10");

        n2.setNameName("sale_2");
        n2.setRefersToFormula("20");

        try {
            n2.setNameName("sale_1");
            fail("Expected exception");
        } catch(Exception e){
            assertEquals("The workbook already contains this name: sale_1", e.getMessage());
        }
        
        wb.close();
    }

    @Test
    public void testBug56930() throws IOException {
        Workbook wb = _testDataProvider.createWorkbook();

        // x1 on sheet1 defines "x=1"
        wb.createSheet("sheet1");
        Name x1 = wb.createName();

        x1.setNameName("x");
        x1.setRefersToFormula("1");
        x1.setSheetIndex(wb.getSheetIndex("sheet1"));

        // x2 on sheet2 defines "x=2"
        wb.createSheet("sheet2");
        Name x2 = wb.createName();
        x2.setNameName("x");
        x2.setRefersToFormula("2");
        x2.setSheetIndex(wb.getSheetIndex("sheet2"));

        List<? extends Name> names = wb.getNames("x");
        assertEquals("Had: " + names, 2, names.size());
        assertEquals("1", names.get(0).getRefersToFormula());
        assertEquals("2", names.get(1).getRefersToFormula());

        assertEquals("1", wb.getName("x").getRefersToFormula());
        wb.removeName(wb.getName("x"));
        assertEquals("2", wb.getName("x").getRefersToFormula());
        
        wb.close();
    }
    
    // bug 56781: name validation only checks for first character's validity and presence of spaces
    // bug 60246: validate name does not allow DOT in named ranges
    @Test
    public void testValid() throws IOException {
        Workbook wb = _testDataProvider.createWorkbook();
        
        Name name = wb.createName();
        for (String valid : Arrays.asList(
                "Hello",
                "number1",
                "_underscore",
                "underscore_",
                "p.e.r.o.i.d.s",
                "\\Backslash",
                "Backslash\\"
                )) {
            name.setNameName(valid);
        }
        
        wb.close();
    }
    
    @Test
    public void testInvalid() {
        Workbook wb = _testDataProvider.createWorkbook();
        
        Name name = wb.createName();
        try {
            name.setNameName("");
            fail("expected exception: (blank)");
        } catch (final IllegalArgumentException e) {
            assertEquals("Name cannot be blank", e.getMessage());
        }
        
        for (String invalid : Arrays.asList(
                "1number",
                "Sheet1!A1",
                "Exclamation!",
                "Has Space",
                "Colon:",
                "A-Minus",
                "A+Plus",
                "Dollar$",
                ".periodAtBeginning",
                "R", //special shorthand
                "C", //special shorthand
                "A1", // A1-style cell reference
                "R1C1", // R1C1-style cell reference
                "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
                "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
                "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
                "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
                "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters"
                )) {
            try {
                name.setNameName(invalid);
                fail("expected exception: " + invalid);
            } catch (final IllegalArgumentException e) {
                assertTrue(invalid,
                        e.getMessage().startsWith("Invalid name: '"+invalid+"'"));
            }
        }
        
    }
    
    // bug 60260: renaming a sheet with a named range referring to a unicode (non-ASCII) sheet name
    @Test
    public void renameSheetWithNamedRangeReferringToUnicodeSheetName() {
        Workbook wb = _testDataProvider.createWorkbook();
        wb.createSheet("Sheet\u30FB1");
        
        Name name = wb.createName();
        name.setNameName("test_named_range");
        name.setRefersToFormula("'Sheet\u30FB201'!A1:A6");
        
        wb.setSheetName(0, "Sheet 1");
        IOUtils.closeQuietly(wb);
    }
}