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