11package cn .idev .excel .test .temp .poi ;
22
33import cn .idev .excel .test .util .TestFileUtil ;
4+ import org .apache .poi .EncryptedDocumentException ;
45import org .apache .poi .hssf .record .crypto .Biff8EncryptionKey ;
56import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
67import org .apache .poi .openxml4j .opc .OPCPackage ;
910import org .apache .poi .poifs .crypt .EncryptionMode ;
1011import org .apache .poi .poifs .crypt .Encryptor ;
1112import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
13+ import org .junit .jupiter .api .Assertions ;
1214import org .junit .jupiter .api .Test ;
15+ import org .junit .jupiter .api .io .TempDir ;
1316import org .slf4j .Logger ;
1417import org .slf4j .LoggerFactory ;
1518
1619import java .io .File ;
1720import java .io .FileOutputStream ;
1821import java .io .OutputStream ;
22+ import java .nio .file .Path ;
1923
2024/**
2125 * 测试poi
@@ -28,32 +32,32 @@ public class Poi3Test {
2832 private static final Logger LOGGER = LoggerFactory .getLogger (Poi3Test .class );
2933
3034 @ Test
31- public void Encryption () throws Exception {
32- String file = TestFileUtil .getPath () + "large" + File .separator + "large07.xlsx" ;
33- POIFSFileSystem fs = new POIFSFileSystem ();
34- EncryptionInfo info = new EncryptionInfo (EncryptionMode .agile );
35- Encryptor enc = info .getEncryptor ();
36- enc .confirmPassword ("foobaa" );
37- OPCPackage opc = OPCPackage .open (new File (file ), PackageAccess .READ_WRITE );
38- OutputStream os = enc .getDataStream (fs );
39- opc .save (os );
40- opc .close ();
41-
35+ public void Encryption (@ TempDir Path tempDir ) throws Exception {
4236 // Write out the encrypted version
43- FileOutputStream fos = new FileOutputStream ("D:\\ test\\ 99999999999.xlsx" );
44- fs .writeFilesystem (fos );
45- fos .close ();
46- fs .close ();
47-
37+ try (POIFSFileSystem fs = new POIFSFileSystem ();
38+ FileOutputStream fos = new FileOutputStream (
39+ tempDir .resolve (System .currentTimeMillis () + ".xlsx" ).toFile ());) {
40+ String file = TestFileUtil .getPath () + "large" + File .separator + "large07.xlsx" ;
41+ EncryptionInfo info = new EncryptionInfo (EncryptionMode .agile );
42+ Encryptor enc = info .getEncryptor ();
43+ enc .confirmPassword ("foobaa" );
44+ OPCPackage opc = OPCPackage .open (new File (file ), PackageAccess .READ_WRITE );
45+ OutputStream os = enc .getDataStream (fs );
46+ opc .save (os );
47+ opc .close ();
48+ fs .writeFilesystem (fos );
49+ }
4850 }
4951
5052 @ Test
5153 public void Encryption2 () throws Exception {
52- Biff8EncryptionKey .setCurrentUserPassword ("123456" );
53- POIFSFileSystem fs = new POIFSFileSystem (new File ("d:/test/simple03.xls" ), true );
54- HSSFWorkbook hwb = new HSSFWorkbook (fs .getRoot (), true );
54+ Biff8EncryptionKey .setCurrentUserPassword ("incorrect pwd" );
55+ POIFSFileSystem fs = new POIFSFileSystem (new File ("src/test/resources/demo/pwd_123.xls" ), true );
56+ Assertions .assertThrows (EncryptedDocumentException .class , () -> new HSSFWorkbook (fs .getRoot (), true ));
57+ Biff8EncryptionKey .setCurrentUserPassword ("123" );
58+ HSSFWorkbook hwb = new HSSFWorkbook (
59+ new POIFSFileSystem (new File ("src/test/resources/demo/pwd_123.xls" ), true ).getRoot (), true );
60+ Assertions .assertEquals ("Sheet1" , hwb .getSheetAt (0 ).getSheetName ());
5561 Biff8EncryptionKey .setCurrentUserPassword (null );
56- System .out .println (hwb .getSheetAt (0 ).getSheetName ());
57-
5862 }
5963}
0 commit comments