001: package net.sf.mockcreator;
002:
003: import java.io.FileReader;
004: import java.io.IOException;
005: import java.io.LineNumberReader;
006: import java.util.ArrayList;
007: import java.util.Enumeration;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.jar.JarEntry;
011: import java.util.jar.JarFile;
012:
013: import net.sf.mockcreator.codegeneration.MockBuilder;
014: import net.sf.mockcreator.codegeneration.MockDescriptor;
015: import net.sf.mockcreator.codegeneration.Saver;
016: import net.sf.mockcreator.utils.ArgumentParser;
017:
018: public class MockCreator {
019:
020: private static ArgumentParser parser;
021:
022: private static void printBanner() {
023: System.out
024: .println("MockCreator v"
025: + MockBuilder.version
026: + " by abstrakt (www.abstrakt.de) and dozen (dozen.ru)");
027: }
028:
029: private static void printUsage() {
030: printBanner();
031:
032: System.out.println("Usage:");
033: System.out
034: .println("net.sf.mockcreator.MockCreator [options] className|@ClassListFile] [className|@ClassListFile] ...");
035: System.out
036: .println("Example: net.sf.mockcreator.MockCreator -o tests -p my.tests -f all ru.dozen.Foo @AllOtherClasses.txt");
037: ArgumentParser.help(System.out);
038: }
039:
040: public static void main(java.lang.String[] args) {
041:
042: try {
043: printBanner();
044:
045: parser = new ArgumentParser(args);
046:
047: List classNames = parser.getClassNames();
048:
049: if (classNames.size() == 0) {
050: System.err
051: .println("No interface(s) or class(es) provided");
052: System.err.println();
053: printUsage();
054: System.exit(1);
055: }
056:
057: processParameters(classNames);
058: } catch (Throwable ex) {
059: ex.printStackTrace(System.out);
060: printUsage();
061: System.exit(1);
062: }
063: }
064:
065: private static void processParameters(List classNames)
066: throws IOException, Exception {
067: Iterator it = classNames.iterator();
068:
069: while (it.hasNext()) {
070: String clsName = (String) it.next();
071:
072: if (clsName.startsWith("@")) {
073: processFileList(clsName);
074: continue;
075: }
076:
077: if (clsName.matches(".*\\.[jJ][aA][rR]$")) {
078: processJar(clsName);
079: continue;
080: }
081:
082: MockDescriptor md = Saver.configureDescriptor(clsName,
083: parser.getTargetPackage(), parser.getOutputPath(),
084: parser.getFakeDepth());
085:
086: System.out.print("mocking " + md.getSourceFQN());
087:
088: if (!parser.getSkipUptodate() || !Saver.isUpToDate(md)) {
089: if (!md.isInterface() && parser.getFakeDepth() <= 0) {
090: System.out
091: .print(" (warning: not an interface; consider -f all)");
092: }
093: Saver.saveMock(md, MockBuilder.createMock(md));
094: } else {
095: System.out.print(", up to date, skipped");
096: }
097: System.out.println();
098: }
099: }
100:
101: private static void processJar(String jarName) throws Exception {
102: List classNames = new ArrayList(1000);
103:
104: JarFile jar = new JarFile(jarName);
105: Enumeration en = jar.entries();
106: while (en.hasMoreElements()) {
107: JarEntry je = (JarEntry) en.nextElement();
108: if (je.isDirectory())
109: continue;
110:
111: String clzName = je.getName();
112: if (!clzName.matches(".*\\.[cC][lL][aA][sS][sS]$"))
113: continue;
114:
115: clzName = clzName.replace('/', '.');
116: clzName = clzName.substring(0, clzName.length()
117: - ".class".length());
118:
119: if (clzName.indexOf('$') >= 0)
120: continue; // skip inner classes: they will be generated with declaring ones
121: if (clzName.matches(".*[\\.\\$][0-9]+$"))
122: continue;
123: if (clzName.matches(".*[\\.\\$]assert$"))
124: continue; // emma-2.0
125:
126: classNames.add(clzName);
127: }
128:
129: if (classNames.size() > 0) {
130: processParameters(classNames);
131: }
132: }
133:
134: private static void processFileList(String fileName)
135: throws Exception {
136: fileName = fileName.substring(1);
137:
138: List classNames = new ArrayList();
139: LineNumberReader lnr = new LineNumberReader(new FileReader(
140: fileName));
141: while (true) {
142: String s = lnr.readLine();
143: if (s == null)
144: break;
145:
146: s = s.trim();
147: // too often people copy class names from import along with ending semicolon
148: if (s.endsWith(";"))
149: s = s.substring(0, s.length() - 1);
150: s = s.trim();
151:
152: if (s.length() == 0)
153: continue;
154: if (!Character.isJavaIdentifierStart(s.charAt(0)))
155: continue;
156:
157: classNames.add(s.trim());
158: }
159:
160: if (classNames.size() > 0) {
161: processParameters(classNames);
162: }
163: }
164: }
|