/testability-explorer/src/test/java/com/google/test/metric/CommandLineConfigTest.java

http://testability-explorer.googlecode.com/ · Java · 116 lines · 85 code · 16 blank · 15 comment · 0 complexity · a9c381fa2dd7e6b4831f023abf49a53f MD5 · raw file

  1. /*
  2. * Copyright 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric;
  17. import com.google.test.metric.ReportGeneratorProvider.ReportFormat;
  18. import org.kohsuke.args4j.CmdLineException;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.PrintStream;
  21. import java.util.Arrays;
  22. public class CommandLineConfigTest extends AutoFieldClearTestCase {
  23. private ByteArrayOutputStream out = new ByteArrayOutputStream();
  24. private ByteArrayOutputStream err = new ByteArrayOutputStream();
  25. private CommandLineConfig commandLineConfig;
  26. @Override
  27. public void setUp() {
  28. commandLineConfig = new CommandLineConfig(new PrintStream(out), new PrintStream(err));
  29. }
  30. public void testCreateSummaryReport2() throws Exception {
  31. commandLineConfig.printer = "summary";
  32. commandLineConfig.cp = "a";
  33. commandLineConfig.validate();
  34. assertEquals(ReportFormat.summary, commandLineConfig.format);
  35. }
  36. public void testCreateHtmlReport() throws Exception {
  37. commandLineConfig.printer = "html";
  38. commandLineConfig.cp = "a";
  39. commandLineConfig.validate();
  40. assertEquals(ReportFormat.html, commandLineConfig.format);
  41. }
  42. public void testCreateDetailReport() throws Exception {
  43. commandLineConfig.printer = "detail";
  44. commandLineConfig.cp = "a";
  45. commandLineConfig.validate();
  46. assertEquals(ReportFormat.detail, commandLineConfig.format);
  47. }
  48. public void testCreatePropertiesReport() throws Exception {
  49. commandLineConfig.printer = "props";
  50. commandLineConfig.cp = "a";
  51. commandLineConfig.validate();
  52. assertEquals(ReportFormat.props, commandLineConfig.format);
  53. }
  54. public void testCreateSourceReport() throws Exception {
  55. commandLineConfig.printer = "source";
  56. commandLineConfig.cp = "a";
  57. commandLineConfig.validate();
  58. assertEquals(ReportFormat.source, commandLineConfig.format);
  59. }
  60. public void testCreateXmlReport() throws Exception {
  61. commandLineConfig.printer = "xml";
  62. commandLineConfig.cp = "a";
  63. commandLineConfig.validate();
  64. JavaTestabilityModule module = new JavaTestabilityModule(commandLineConfig);
  65. assertEquals(ReportFormat.xml, module.getFormat());
  66. }
  67. public void testCreateNonexistantReportThrowsException() throws Exception {
  68. commandLineConfig.cp = "";
  69. commandLineConfig.printer = "i-dont-exist";
  70. try {
  71. commandLineConfig.validate();
  72. fail("CmdLineException exception expected but did not get thrown");
  73. } catch (CmdLineException expected) {
  74. assertTrue(expected.getMessage().startsWith("Don't understand"));
  75. }
  76. }
  77. public void testBuildTestabilityConfig() throws Exception {
  78. PrintStream errStream = new PrintStream(new ByteArrayOutputStream());
  79. commandLineConfig = new CommandLineConfig(null, errStream);
  80. commandLineConfig.entryList = Arrays.asList("com.example.io", "com.example.ext");
  81. commandLineConfig.cp = "fake/path";
  82. commandLineConfig.printDepth = 3;
  83. commandLineConfig.printer = "summary";
  84. commandLineConfig.validate();
  85. JavaTestabilityModule testabilityModule = new JavaTestabilityModule(commandLineConfig);
  86. assertEquals(2, testabilityModule.getEntryList().size());
  87. assertEquals(ReportFormat.summary, testabilityModule.getFormat());
  88. assertEquals(errStream, testabilityModule.getErr());
  89. assertEquals(3, testabilityModule.getPrintDepth());
  90. }
  91. public void testJavaWhitelistProvider() throws Exception {
  92. commandLineConfig.wl = "com.foo:org.bar";
  93. WhiteList whiteList = new JavaTestabilityModule(commandLineConfig).getWhiteList(commandLineConfig);
  94. assertTrue(whiteList.isClassWhiteListed("com.foo.Hash"));
  95. assertTrue(whiteList.isClassWhiteListed("org.bar.BiMap"));
  96. assertTrue(whiteList.isClassWhiteListed("java.lang"));
  97. assertTrue(whiteList.isClassWhiteListed("javax.swing.plaf"));
  98. assertFalse(whiteList.isClassWhiteListed("com.example"));
  99. }
  100. }