You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestVBAMacroReader.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.macros;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertTrue;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.util.Map;
  23. import org.apache.poi.hssf.HSSFTestDataSamples;
  24. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  25. import org.apache.poi.util.IOUtils;
  26. import org.apache.poi.util.StringUtil;
  27. import org.junit.Test;
  28. public class TestVBAMacroReader {
  29. private final String testMacroContents;
  30. private final String testMacroNoSub;
  31. public TestVBAMacroReader() throws Exception {
  32. File macro = HSSFTestDataSamples.getSampleFile("SimpleMacro.vba");
  33. testMacroContents = new String(
  34. IOUtils.toByteArray(new FileInputStream(macro)),
  35. StringUtil.UTF8
  36. );
  37. if (! testMacroContents.startsWith("Sub ")) {
  38. throw new IllegalArgumentException("Not a macro");
  39. }
  40. testMacroNoSub = testMacroContents.substring(testMacroContents.indexOf("()")+3);
  41. }
  42. @Test
  43. public void fromStream() throws Exception {
  44. VBAMacroReader r;
  45. r = new VBAMacroReader(HSSFTestDataSamples.openSampleFileStream("SimpleMacro.xls"));
  46. assertMacroContents(r);
  47. r.close();
  48. r = new VBAMacroReader(HSSFTestDataSamples.openSampleFileStream("SimpleMacro.xlsm"));
  49. assertMacroContents(r);
  50. r.close();
  51. }
  52. @Test
  53. public void fromFile() throws Exception {
  54. VBAMacroReader r;
  55. r = new VBAMacroReader(HSSFTestDataSamples.getSampleFile("SimpleMacro.xls"));
  56. assertMacroContents(r);
  57. r.close();
  58. r = new VBAMacroReader(HSSFTestDataSamples.getSampleFile("SimpleMacro.xlsm"));
  59. assertMacroContents(r);
  60. r.close();
  61. }
  62. @Test
  63. public void fromNPOIFS() throws Exception {
  64. NPOIFSFileSystem fs = new NPOIFSFileSystem(
  65. HSSFTestDataSamples.getSampleFile("SimpleMacro.xls"));
  66. VBAMacroReader r = new VBAMacroReader(fs);
  67. assertMacroContents(r);
  68. r.close();
  69. }
  70. protected void assertMacroContents(VBAMacroReader r) throws Exception {
  71. Map<String,String> contents = r.readMacros();
  72. assertFalse(contents.isEmpty());
  73. assertEquals(5, contents.size());
  74. // Check the ones without scripts
  75. String[] noScripts = new String[] { "ThisWorkbook",
  76. "Sheet1", "Sheet2", "Sheet3" };
  77. for (String entry : noScripts) {
  78. assertTrue(entry, contents.containsKey(entry));
  79. String content = contents.get(entry);
  80. assertContains(content, "Attribute VB_Exposed = True");
  81. assertContains(content, "Attribute VB_Customizable = True");
  82. assertContains(content, "Attribute VB_TemplateDerived = False");
  83. assertContains(content, "Attribute VB_GlobalNameSpace = False");
  84. assertContains(content, "Attribute VB_Exposed = True");
  85. }
  86. // Check the script one
  87. String content = contents.get("Module1");
  88. assertContains(content, "Attribute VB_Name = \"Module1\"");
  89. assertContains(content, "Attribute TestMacro.VB_Description = \"This is a test macro\"");
  90. // And the macro itself
  91. assertContains(content, testMacroNoSub);
  92. }
  93. }