Update of /cvsroot/mocklib/gwtmocklib/input/javasrc/biz/xsoftware/manifest
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv23230/input/javasrc/biz/xsoftware/manifest
Added Files:
ManifestUtilImpl.java ManifestInfo.java TestManifestInfo.java
Log Message:
first commit of gwtmocklib module.
--- NEW FILE: ManifestUtilImpl.java ---
/*
* Created on Sep 19, 2004
*
* FIXME To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package biz.xsoftware.manifest;
import java.io.File;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.logging.Logger;
/**
* @author Dean Hiller
*
* FIXME To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ManifestUtilImpl implements ManifestInfo.ManifestUtil {
private static final Logger log = Logger.getLogger(ManifestUtilImpl.class.getName());
public File getFile(URL url) {
log.finest("url="+url);
String filePart = url.getFile();
log.finest("filePart from url="+filePart);
String nameWithClass = filePart.substring(5, filePart.length());
log.finest("nameWithClass="+nameWithClass);
int index = nameWithClass.indexOf("!");
log.finest("index="+index);
String fileName = nameWithClass.substring(0, index);
log.finest("fileName of buildtemplate="+fileName);
File f = new File(fileName);
if(!f.exists()) {
log.warning("Bug, Exiting System. Could not find file="+fileName);
System.exit(1);
}
log.finer("returning file="+f.getAbsolutePath());
return f;
}
/* (non-Javadoc)
* @see biz.xsoftware.manifest.ManifestInfo.SystemInterface#exit(int)
*/
public void exit(int code) {
System.exit(code);
}
public String getMainClass(Manifest manifest) {
Attributes attr = manifest.getMainAttributes();
String s = attr.getValue("SubMain-Class");
return s.trim();
}
//dummy main for testing purposes!!!
public static void main(String[] args) {
log.fine("just throwing an exception for testing purposes");
throw new RuntimeException("Just for testing only");
}
}
--- NEW FILE: TestManifestInfo.java ---
/*
* Created on Sep 11, 2004
*
*/
package biz.xsoftware.manifest;
import java.io.File;
import java.net.URL;
import java.util.jar.Manifest;
import junit.framework.TestCase;
/**
* This is purely so emma always always reports code coverage
* numbers on everything
*/
public class TestManifestInfo extends TestCase {
private static final String DUMMY = "dummy";
private File jarFile;
/**
* @param arg0
*/
public TestManifestInfo(String arg0) {
super(arg0);
}
public void setUp() {
String jarLoc = System.getProperty("jar.name");
if(jarLoc != null) //for tests run from ant
jarFile = new File(jarLoc);
else //for tests run from eclipse
jarFile = new File("output/jardist/projectname.jar");
}
public void testManifestInfo() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, "should.not.matter.class");
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[] {"-version"});
ManifestInfo.main(new String[] {"-manifest"});
String version = new ManifestInfo().getVersion();
assertEquals("version from build.xml should equal version in jar",
System.getProperty("version"), version);
}
public void testRunMainClass() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, TestManifestInfo.class.getName());
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[] {DUMMY});
assertTrue("should have one argument", argsLen == 1);
assertEquals("variable should have been passed through", DUMMY, dummyArg);
}
public void testExceptionFromMainClass() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, ManifestUtilImpl.class.getName());
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[0]);
}
public void testClassNotFound() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, "this.class.is.not.found");
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[0]);
}
public void testTOOLSJAVAMain() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, " TOOLS.JAVA.Main ");
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[0]);
}
public void testNullClassName() throws Throwable {
FakeJarLocator mock = new FakeJarLocator(jarFile, null);
ManifestInfo.setJarLocator(mock);
ManifestInfo.main(new String[0]);
}
public void testGetMainClass() {
Manifest mf = new Manifest();
mf.getMainAttributes().putValue("SubMain-Class", " xx ");
ManifestUtilImpl util = new ManifestUtilImpl();
String s = util.getMainClass(mf);
assertEquals("should have trimmed the value", "xx", s);
}
public void testGetFile() throws Exception {
File f = jarFile;
assertTrue("file should exist before we test this", f.exists());
URL url = f.toURL();
URL urlToClassFile = new URL("file", null, -1, url+"!/biz/xsoftware/buildtemplate/Util.class");
File tmp = new ManifestUtilImpl().getFile(urlToClassFile);
assertNotNull("Should return a real file", tmp);
assertTrue("file should still exist", tmp.exists());
}
private static class FakeJarLocator implements ManifestInfo.ManifestUtil {
private File file;
private String mainClass;
public FakeJarLocator(File f, String mainClass) {
this.file = f;
this.mainClass = mainClass;
}
public File getFile(URL url) {
return file;
}
public void exit(int code) {
//do nothing!!!
}
public String getMainClass(Manifest manifest) {
return mainClass;
}
}
//dummy main method for testing!!!
public static void main(String[] args) {
argsLen = args.length;
dummyArg = args[0];
}
private static int argsLen;
private static String dummyArg;
}
--- NEW FILE: ManifestInfo.java ---
/*
* Created on Jul 9, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.manifest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Miscellaneous class that just prints the version of the mock object jar
* getting it from the manifest file.
*
* @author Dean Hiller
*/
public class ManifestInfo {
private static final Logger log = Logger.getLogger(ManifestInfo.class.getName());
private static ManifestUtil util = new ManifestUtilImpl();
//private Package thePackage;
private Manifest manifest;
/**
* The main program for Version that prints the version info from
* the manifest file.
*
* java -jar xxx.jar
* 1. runs SubMain-Class in manifest file
* 2. If SubMain-Class is default value or "", prints usage info
* for -manifest and -version
*
* java -jar xxx.jar -version
* 1. prints version info
*
* java -jar xxx.jar -manifest
* 1. prints all manifest contents.
*
* @param args Ignores all arguments.
*/
public static void main(String[] args) throws Throwable {
try {
run(args);
} catch(Throwable e) {
log.log(Level.WARNING, "Exception occurred", e);
throw e;
}
}
public static void run(String[] args) throws IOException {
ManifestInfo manifestInfo = new ManifestInfo();
if(args.length > 0) {
if("-manifest".equals(args[0])) {
System.out.println(""+manifestInfo);
return;
} else if("-version".equals(args[0])) {
System.out.println(manifestInfo.getFullVersionInfo());
return;
}
}
String className = util.getMainClass(manifestInfo.getManifest());
if(className == null)
className = "";
if("TOOLS.JAVA.Main".equals(className.trim()) || "".equals(className)) {
System.err.println("Usage:");
System.err.println("1. java -jar <jarfile> -version");
System.err.println("2. java -jar <jarfile> -manifest");
} else {
runProgram(className, args);
}
util.exit(1);
}
static void runProgram(String className, String[] args) {
String msg = "";
ClassLoader cl = ManifestInfo.class.getClassLoader();
try {
Class c = cl.loadClass(className);
log.finest("class="+c);
Method m = c.getMethod("main", new Class[] {String[].class});
m.invoke(null, new Object[] { args });
} catch(ClassNotFoundException e) {
msg = "Class in manifest not found in classpath\n"
+"Fix the ant.properties file to refer to the\n"
+"main class or refer to nothing\n"
+"class="+className;
System.out.println(msg);
} catch(NoSuchMethodException e) {
msg = "You have specified a class that doesn't"
+"have a main method in ant.properties file."
+"class="+className;
System.out.println(msg);
} catch(Exception e) {
msg = "\n\n2. Unknown failure. Contact buildtemplate owner";
log.log(Level.WARNING, "Exception occurred", e);
System.out.println(msg);
}
util.exit(1);
}
public static void setJarLocator(ManifestUtil l) {
util = l;
}
/**
* Constructor that takes a class to get the version information
* from out of the manifest. Uses the class's package to retrieve
* the manifest version info.
* @param c The Class on whose package to use to get version info.
*/
public ManifestInfo() throws IOException {
URL url = ManifestInfo.class.getResource("ManifestInfo.class");
//set manifest from jar file
File f = util.getFile(url);
JarFile jarFile = new JarFile(f);
manifest = jarFile.getManifest();
//set the package of this guy(not really needed as we could get all this info
//directly from manifest)
//String name = ManifestInfo.class.getName();
//int index = name.lastIndexOf(".");
//String packageName = name.substring(0, index);
//thePackage = Package.getPackage(packageName);
}
private Manifest getManifest() {
return manifest;
}
public String getVersion() {
Attributes attr = manifest.getMainAttributes();
String version = attr.getValue("Implementation-Version");
version = version.trim();
int index = version.indexOf(" ");
version = version.substring(0, index);
return version;
}
public String getFullVersionInfo() {
Attributes attr = manifest.getMainAttributes();
String retVal = attr.getValue("Implementation-Title")+" information...";
retVal += "\nwebsite= "+attr.getValue("Implementation-Vendor");
retVal += "\nbuilt by= "+attr.getValue("Built-By");
retVal += "\nclasspath= "+attr.getValue("Class-Path");
retVal += "\nversion= "+attr.getValue("Implementation-Version")+"\n";
return retVal;
}
/**
* Prints the version info the Version represents.
*
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer manifestInfo = new StringBuffer();
Attributes attr = manifest.getMainAttributes();
Set entries = attr.entrySet();
Iterator iter = entries.iterator();
while(iter.hasNext()) {
Entry entry = (Entry)iter.next();
manifestInfo.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
}
return manifestInfo+"";
}
public interface ManifestUtil {
public File getFile(URL url);
/**
* @param manifest
* @return
*/
public String getMainClass(Manifest manifest);
public void exit(int code);
}
}
|