PageRenderTime 692ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 138 lines | 97 code | 15 blank | 26 comment | 1 complexity | 5fc4e4bbb6c614ecb306d79cd162337e MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007 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.inject.Guice;
  18. import com.google.inject.Injector;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.PrintStream;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. public class ConfigModuleTest extends AutoFieldClearTestCase {
  24. private ByteArrayOutputStream out = new ByteArrayOutputStream();
  25. private ByteArrayOutputStream err = new ByteArrayOutputStream();
  26. private PrintStream outStream = new PrintStream(out);
  27. private PrintStream errStream = new PrintStream(err);
  28. public void testParseNoArgs() {
  29. Guice.createInjector(new ConfigModule(new String[0], outStream, errStream)).
  30. getInstance(CommandLineConfig.class);
  31. String expectedStartOfError = "You must supply";
  32. assertEquals(expectedStartOfError, err.toString().substring(0, expectedStartOfError.length()));
  33. assertTrue(err.toString().indexOf("Exiting...") > -1);
  34. }
  35. public void testParseClasspathAndSingleClass() throws Exception {
  36. Injector injector = Guice.createInjector(new ConfigModule(new String[]{
  37. "-cp", "not/default/path", "com.google.TestClass"}, outStream, errStream));
  38. injector.getInstance(CommandLineConfig.class);
  39. CommandLineConfig commandLineConfig = injector.getInstance(CommandLineConfig.class);
  40. assertEquals("", err.toString());
  41. assertEquals("not/default/path", commandLineConfig.cp);
  42. List<String> expectedArgs = new ArrayList<String>();
  43. expectedArgs.add("com.google.TestClass");
  44. assertNotNull(commandLineConfig.entryList);
  45. assertEquals(expectedArgs, commandLineConfig.entryList);
  46. }
  47. public void testParseMultipleClassesAndPackages() throws Exception {
  48. Injector injector = Guice.createInjector(new ConfigModule(new String[]{
  49. "-cp", "not/default/path",
  50. "com.google.FirstClass",
  51. "com.google.second.package",
  52. "com.google.third.package"}, outStream, errStream));
  53. CommandLineConfig commandLineConfig = injector.getInstance(CommandLineConfig.class);
  54. assertEquals("", err.toString());
  55. assertEquals("not/default/path", commandLineConfig.cp);
  56. List<String> expectedArgs = new ArrayList<String>();
  57. expectedArgs.add("com.google.FirstClass");
  58. expectedArgs.add("com.google.second.package");
  59. expectedArgs.add("com.google.third.package");
  60. assertNotNull(commandLineConfig.entryList);
  61. assertEquals(expectedArgs, commandLineConfig.entryList);
  62. }
  63. /*
  64. * If the main() method is called directly by another class,
  65. * as in the case of the TestabilityTask for Ant,
  66. * multiple classpaths may be passed as a single String arg
  67. * separated by spaces (" ")
  68. */
  69. public void testParseMultipleClassesAndPackagesSingleArg() throws Exception {
  70. Injector injector = Guice.createInjector(new ConfigModule(new String[]{
  71. "-cp", "not/default/path",
  72. "com.google.FirstClass com.google.second.package com.google.third.package"},
  73. outStream, errStream));
  74. CommandLineConfig commandLineConfig = injector.getInstance(CommandLineConfig.class);
  75. // TODO(alexeagle): this test is really testing the JavaTestabilityModule
  76. new JavaTestabilityModule(commandLineConfig);
  77. assertEquals("", err.toString());
  78. assertEquals("not/default/path", commandLineConfig.cp);
  79. List<String> expectedArgs = new ArrayList<String>();
  80. expectedArgs.add("com.google.FirstClass");
  81. expectedArgs.add("com.google.second.package");
  82. expectedArgs.add("com.google.third.package");
  83. assertNotNull(commandLineConfig.entryList);
  84. assertEquals(expectedArgs, commandLineConfig.entryList);
  85. }
  86. public void testParseComplexityAndGlobal() throws Exception {
  87. Injector injector = Guice.createInjector(new ConfigModule(new String[]{
  88. "-cp", "not/default/path",
  89. "-cyclomatic", "10",
  90. "-global", "1",
  91. "com.google.TestClass"},
  92. outStream, errStream));
  93. CommandLineConfig commandLineConfig = injector.getInstance(CommandLineConfig.class);
  94. assertEquals("", err.toString());
  95. assertEquals("Classpath", "not/default/path", commandLineConfig.cp);
  96. List<String> expectedArgs = new ArrayList<String>();
  97. expectedArgs.add("com.google.TestClass");
  98. assertNotNull(commandLineConfig.entryList);
  99. assertEquals(expectedArgs, commandLineConfig.entryList);
  100. assertEquals("Cyclomatic", 10.0, commandLineConfig.cyclomaticMultiplier);
  101. assertEquals("Global", 1.0, commandLineConfig.globalMultiplier);
  102. }
  103. public void testJarFileNoClasspath() throws Exception {
  104. Guice.createInjector(new ConfigModule(new String[] {"junit.runner", "-cp"},
  105. outStream, errStream)).getInstance(CommandLineConfig.class);
  106. /**
  107. * we expect the error to say something about proper usage of the arguments.
  108. * The -cp needs a value
  109. */
  110. assertTrue(out.toString().length() == 0);
  111. assertTrue(err.toString().length() > 0);
  112. }
  113. public void testParseSrcFileUrlFlags() throws Exception {
  114. String lineUrl = "http://code.google.com/p/testability-explorer/source/browse/trunk/src/{path}#{line}";
  115. String fileUrl = "http://code.google.com/p/testability-explorer/source/browse/trunk/src/{path}";
  116. Injector injector = Guice.createInjector(new ConfigModule(new String[]{
  117. "-srcFileLineUrl", lineUrl, "-srcFileUrl", fileUrl},
  118. outStream, errStream));
  119. CommandLineConfig commandLineConfig = injector.getInstance(CommandLineConfig.class);
  120. assertEquals(lineUrl, commandLineConfig.srcFileLineUrl);
  121. assertEquals(fileUrl, commandLineConfig.srcFileUrl);
  122. }
  123. }