Title: [40898] trunk/hudson/plugins/serenity: Updated the accumulator to look at all the directories as well as the jars.

Diff

Modified: trunk/hudson/plugins/serenity/.classpath (40897 => 40898)


--- trunk/hudson/plugins/serenity/.classpath	2013-06-07 09:50:07 UTC (rev 40897)
+++ trunk/hudson/plugins/serenity/.classpath	2013-06-07 13:32:45 UTC (rev 40898)
@@ -1,38 +1,34 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" output="target/classes" path="src/main/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" output="target/classes" path="target/generated-sources/localizer"/>
-	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="output" path="target/classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/classes" path="target/generated-sources/localizer"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_38"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

Modified: trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/IConstants.java (40897 => 40898)


--- trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/IConstants.java	2013-06-07 09:50:07 UTC (rev 40897)
+++ trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/IConstants.java	2013-06-07 13:32:45 UTC (rev 40898)
@@ -65,6 +65,7 @@
 	public String COMPLEXITY = "complexity";
 	public String DEPENDENCY = "dependency";
 	public String PROFILING = "profiling";
+	public String DUMP = "dump";
 
 	/** The type of parameters that the {@link Collector} takes in the profiling collection method. */
 	public Type STRING_TYPE = Type.getType(String.class);

Modified: trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/Transformer.java (40897 => 40898)


--- trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/Transformer.java	2013-06-07 09:50:07 UTC (rev 40897)
+++ trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/Transformer.java	2013-06-07 13:32:45 UTC (rev 40898)
@@ -26,9 +26,9 @@
 import com.ikokoon.toolkit.Toolkit;
 
 /**
- * This class is the entry point for the Serenity code coverage/complexity/dependency/profiling functionality. This class is called by the JVM on
- * startup. The agent then has first access to the byte code for all classes that are loaded. During this loading the byte code can be enhanced.
- *
+ * This class is the entry point for the Serenity code coverage/complexity/dependency/profiling functionality. This class is called by the JVM on startup. The
+ * agent then has first access to the byte code for all classes that are loaded. During this loading the byte code can be enhanced.
+ * 
  * @author Michael Couck
  * @since 12.07.09
  * @version 01.00
@@ -46,29 +46,26 @@
 
 	/**
 	 * This method is called by the JVM at startup. This method will only be called if the command line for starting the JVM has the following on it:
-	 * -javaagent:serenity/serenity.jar. This instruction tells the JVM that there is an agent that must be used. In the META-INF directory of the jar
-	 * specified there must be a MANIFEST.MF file. In this file the instructions must be something like the following:
-	 *
+	 * -javaagent:serenity/serenity.jar. This instruction tells the JVM that there is an agent that must be used. In the META-INF directory of the jar specified
+	 * there must be a MANIFEST.MF file. In this file the instructions must be something like the following:
+	 * 
 	 * Manifest-Version: 1.0 <br>
 	 * Boot-Class-Path: asm-3.1.jar and so on..., in the case that the required libraries are not on the classpath, which they should be<br>
 	 * Premain-Class: com.ikokoon.serenity.Transformer
-	 *
+	 * 
 	 * Another line in the manifest can start an agent after the JVM has been started, but not for all JVMs. So not very useful.
-	 *
+	 * 
 	 * These instructions tell the JVM to call this method when loading class files.
-	 *
-	 * @param args
-	 *            a set of arguments that the JVM will call the method with
-	 * @param instrumentation
-	 *            the instrumentation implementation of the JVM
+	 * 
+	 * @param args a set of arguments that the JVM will call the method with
+	 * @param instrumentation the instrumentation implementation of the JVM
 	 */
 	@SuppressWarnings("unchecked")
-	public static void premain(String args, Instrumentation instrumentation) {
+	public static void premain(final String args, final Instrumentation instrumentation) {
 		if (!INITIALISED) {
 			INITIALISED = true;
 			LoggingConfigurator.configure();
-			CLASS_ADAPTER_CLASSES = Configuration.getConfiguration().classAdapters.toArray(new Class[Configuration.getConfiguration().classAdapters
-					.size()]);
+			CLASS_ADAPTER_CLASSES = Configuration.getConfiguration().classAdapters.toArray(new Class[Configuration.getConfiguration().classAdapters.size()]);
 			LOGGER = Logger.getLogger(Transformer.class);
 			LOGGER.error("Starting Serenity : ");
 			if (instrumentation != null) {
@@ -102,9 +99,8 @@
 
 	/**
 	 * This method adds the shutdown hook that will clean and accumulate the data when the Jvm shuts down.
-	 *
-	 * @param dataBase
-	 *            the database to get the data from
+	 * 
+	 * @param dataBase the database to get the data from
 	 */
 	private static void addShutdownHook(final IDataBase dataBase) {
 		shutdownHook = new Thread() {
@@ -132,6 +128,11 @@
 				dataBase.close();
 				LOGGER.warn("Close database : " + (System.currentTimeMillis() - processStart));
 
+				String dumpData = Configuration.getConfiguration().getProperty(IConstants.DUMP);
+				if (dumpData != null && "true".equals(dumpData.trim())) {
+					DataBaseToolkit.dump(dataBase, null, null);
+				}
+
 				Date end = new Date();
 				long million = 1000 * 1000;
 				long duration = end.getTime() - start.getTime();
@@ -150,14 +151,10 @@
 	/**
 	 * This method transforms the classes that are specified.
 	 */
-	public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classBytes)
-			throws IllegalClassFormatException {
-		if (Configuration.getConfiguration().excluded(className)) {
-			LOGGER.info("Excluded class : " + className);
-			return classBytes;
-		}
-		if (Configuration.getConfiguration().included(className)) {
-			LOGGER.info("Enhancing class : " + className);
+	public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain,
+			final byte[] classBytes) throws IllegalClassFormatException {
+		if (Configuration.getConfiguration().included(className) && !Configuration.getConfiguration().excluded(className)) {
+			LOGGER.error("Enhancing class : " + className);
 			ByteArrayOutputStream source = new ByteArrayOutputStream(0);
 			ClassWriter writer = (ClassWriter) VisitorFactory.getClassVisitor(CLASS_ADAPTER_CLASSES, className, classBytes, source);
 			byte[] enhancedClassBytes = writer.toByteArray();
@@ -165,22 +162,22 @@
 			if (writeClasses != null && writeClasses.equals(Boolean.TRUE.toString())) {
 				writeClass(className, enhancedClassBytes);
 			}
+			// Return the injected bytes for the class, i.e. with the coverage instructions
 			return enhancedClassBytes;
 		} else {
-			LOGGER.info("Class not included : " + className);
+			LOGGER.debug("Class not included : " + className);
 		}
+		// Return the original bytes for the class
 		return classBytes;
 	}
 
 	/**
 	 * This method writes the transformed classes to the file system so they can be viewed later.
-	 *
-	 * @param className
-	 *            the name of the class file
-	 * @param classBytes
-	 *            the bytes of byte code to write
+	 * 
+	 * @param className the name of the class file
+	 * @param classBytes the bytes of byte code to write
 	 */
-	private void writeClass(String className, byte[] classBytes) {
+	private void writeClass(final String className, final byte[] classBytes) {
 		// Write the class so we can check it with JD decompiler visually
 		String directoryPath = Toolkit.dotToSlash(Toolkit.classNameToPackageName(className));
 		String fileName = className.replaceFirst(Toolkit.classNameToPackageName(className), "") + ".class";

Modified: trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/process/Accumulator.java (40897 => 40898)


--- trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/process/Accumulator.java	2013-06-07 09:50:07 UTC (rev 40897)
+++ trunk/hudson/plugins/serenity/src/main/java/com/ikokoon/serenity/process/Accumulator.java	2013-06-07 13:32:45 UTC (rev 40898)
@@ -21,9 +21,9 @@
 import com.ikokoon.toolkit.Toolkit;
 
 /**
- * This class looks through the classpath and collects metrics on the classes that were not instantiated by the classloader during the unit tests and
- * creates a visitor chain for the class that will collect the complexity and dependency metrics for the class.
- *
+ * This class looks through the classpath and collects metrics on the classes that were not instantiated by the classloader during the unit tests and creates a
+ * visitor chain for the class that will collect the complexity and dependency metrics for the class.
+ * 
  * @author Michael Couck
  * @since 24.07.09
  * @version 01.00
@@ -41,10 +41,10 @@
 	 * Constructor takes the parent process.
 	 */
 	@SuppressWarnings("unchecked")
-	public Accumulator(IProcess parent) {
+	public Accumulator(final IProcess parent) {
 		super(parent);
-		CLASS_ADAPTER_CLASSES = Configuration.getConfiguration().classAdapters
-				.toArray(new java.lang.Class[Configuration.getConfiguration().classAdapters.size()]);
+		CLASS_ADAPTER_CLASSES = Configuration.getConfiguration().classAdapters.toArray(new java.lang.Class[Configuration.getConfiguration().classAdapters
+				.size()]);
 	}
 
 	/**
@@ -53,19 +53,18 @@
 	public void execute() {
 		super.execute();
 		String classpath = Configuration.getConfiguration().getClassPath();
-		logger.debug("Class path : " + File.pathSeparator + ", " + classpath);
+		logger.error("Class path : " + File.pathSeparator + ", " + classpath);
 		StringTokenizer stringTokenizer = new StringTokenizer(classpath, File.pathSeparator, false);
 		while (stringTokenizer.hasMoreTokens()) {
 			String token = stringTokenizer.nextToken();
 			File file = new File(token);
-			logger.debug("Processing jar : " + token + ", file : " + file.getAbsolutePath());
 			if (!file.exists() || !file.canRead()) {
 				logger.warn("Can't read file : " + file.getAbsolutePath());
 				continue;
 			}
 			if (file.isFile()) {
 				if (token.endsWith(".jar") || token.endsWith(".zip") || token.endsWith(".war") || token.endsWith(".ear")) {
-					logger.debug("Processing jar : " + file.getAbsolutePath());
+					logger.error("Processing jar : " + file.getAbsolutePath());
 					processJar(file);
 				}
 			} else if (file.isDirectory()) {
@@ -75,29 +74,26 @@
 		// Look for all jars below this directory to find some source
 		List<File> list = new ArrayList<File>();
 		File baseDirectory = new File(".");
-		logger.info("Looking for source in base directory : " + baseDirectory.getAbsolutePath());
 		Toolkit.findFiles(baseDirectory, new Toolkit.IFileFilter() {
-			public boolean matches(File file) {
+			public boolean matches(final File file) {
 				if (file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) {
 					return true;
 				}
 				return false;
 			}
 		}, list);
-		for (File file : list) {
-			logger.debug("Processing jar : " + file.getAbsolutePath());
+		for (final File file : list) {
+			logger.error("Processing jar : " + file.getAbsolutePath());
 			processJar(file);
 		}
 	}
 
 	/**
-	 * Processes a directory on a file system, looks for class files and feeds the byte code into the adapter chain for collecting the metrics for the
-	 * class.
-	 *
-	 * @param file
-	 *            the directory or file to look in for the class data
+	 * Processes a directory on a file system, looks for class files and feeds the byte code into the adapter chain for collecting the metrics for the class.
+	 * 
+	 * @param file the directory or file to look in for the class data
 	 */
-	void processDir(File file) {
+	void processDir(final File file) {
 		// Iteratively go through the directories
 		if (file == null || !file.exists() || !file.canWrite()) {
 			return;
@@ -105,8 +101,8 @@
 		if (file.isDirectory()) {
 			File files[] = file.listFiles();
 			for (int j = 0; j < files.length; j++) {
-				file = files[j];
-				processDir(file);
+				File child = files[j];
+				processDir(child);
 			}
 		} else if (file.isFile() && file.canRead()) {
 			String filePath = file.getAbsolutePath();
@@ -142,13 +138,12 @@
 	}
 
 	/**
-	 * Processes a jar or zip file or something like it, looks for class and feeds the byte code into the adapter chain for collecting the metrics for
-	 * the class.
-	 *
-	 * @param file
-	 *            the file to look in for the class data
+	 * Processes a jar or zip file or something like it, looks for class and feeds the byte code into the adapter chain for collecting the metrics for the
+	 * class.
+	 * 
+	 * @param file the file to look in for the class data
 	 */
-	private void processJar(File file) {
+	private void processJar(final File file) {
 		// Don't process the jars more than once
 		if (!jarsProcessed.add(file.getName())) {
 			return;
@@ -169,7 +164,7 @@
 			if (excluded(className)) {
 				continue;
 			}
-			logger.info("Processsing entry : " + className);
+			logger.error("Processsing entry : " + className);
 			try {
 				InputStream inputStream = jarFile.getInputStream(jarEntry);
 				byte[] classFileBytes = Toolkit.getContents(inputStream).toByteArray();
@@ -186,52 +181,71 @@
 		}
 	}
 
-	private ByteArrayOutputStream getSource(JarFile jarFile, String entryName) throws IOException {
+	protected ByteArrayOutputStream getSource(final JarFile jarFile, final String entryName) throws IOException {
 		// Look for the source
-		String javaEntryName = entryName.substring(0, entryName.lastIndexOf('.')) + ".java";
-		// LOGGER.warn("Looking for source : " + javaEntryName + ", " + entryName);
+		final String javaEntryName = entryName.substring(0, entryName.lastIndexOf('.')) + ".java";
+		logger.error("Looking for source : " + javaEntryName + ", " + entryName + ", " + jarFile.getName());
 		ZipEntry javaEntry = jarFile.getEntry(javaEntryName);
 		if (javaEntry != null) {
-			// LOGGER.warn("Got source : " + javaEntry);
+			logger.warn("Got source : " + javaEntry);
 			InputStream inputStream = jarFile.getInputStream(javaEntry);
 			return Toolkit.getContents(inputStream);
+		} else {
+			List<File> files = new ArrayList<File>();
+			// Look on the file system below the dot directory for the Java file
+			Toolkit.findFiles(new File("."), new Toolkit.IFileFilter() {
+				public boolean matches(final File file) {
+					String filePath = file.getAbsolutePath();
+					// Could be on windows
+					filePath = Toolkit.replaceAll(filePath, "\\", "/");
+					boolean isSourceFile = filePath.contains(javaEntryName);
+					if (isSourceFile) {
+						logger.error("Is source file : " + isSourceFile + ", " + filePath);
+					}
+					return isSourceFile;
+				}
+			}, files);
+			if (!files.isEmpty()) {
+				return Toolkit.getContents(files.get(0));
+			}
 		}
 		return new ByteArrayOutputStream();
 	}
 
-	private void processClass(String name, byte[] classBytes, ByteArrayOutputStream source) {
-		if (name != null && name.endsWith(".class")) {
-			name = name.substring(0, name.lastIndexOf('.'));
+	private void processClass(final String name, final byte[] classBytes, final ByteArrayOutputStream source) {
+		String strippedName = name;
+		if (strippedName != null && strippedName.endsWith(".class")) {
+			strippedName = strippedName.substring(0, strippedName.lastIndexOf('.'));
 		}
 		try {
-			logger.warn("Adding class : " + name);
-			VisitorFactory.getClassVisitor(CLASS_ADAPTER_CLASSES, name, classBytes, source);
+			logger.error("Adding class : " + strippedName);
+			VisitorFactory.getClassVisitor(CLASS_ADAPTER_CLASSES, strippedName, classBytes, source);
 		} catch (Exception e) {
 			int sourceLength = source != null ? source.size() : 0;
-			logger.warn("Class name : " + name + ", length : " + classBytes.length + ", source : " + sourceLength);
-			logger.error("Exception accululating data on class " + name, e);
+			logger.warn("Class name : " + strippedName + ", length : " + classBytes.length + ", source : " + sourceLength);
+			logger.error("Exception accululating data on class " + strippedName, e);
 		}
 	}
 
-	private boolean excluded(String name) {
+	private boolean excluded(final String name) {
 		// Don't process anything that is not a class file or a Java file
 		if (!name.endsWith(".class")) {
-			logger.info("Not processing file : " + name);
+			logger.debug("Not processing file : " + name);
 			return true;
 		}
 		// Check that the class is included in the included packages
 		if (!Configuration.getConfiguration().included(name)) {
-			logger.info("File not included : " + name);
+			logger.debug("File not included : " + name);
 			return true;
 		}
 		// Check that the class is not excluded in the excluded packages
 		if (Configuration.getConfiguration().excluded(name)) {
-			logger.info("Excluded file : " + name);
+			logger.debug("Excluded file : " + name);
 			return true;
 		}
 		// Don't process the same class twice
 		if (!filesProcessed.add(name)) {
-			logger.info("Already done file : " + name);
+			logger.debug("Already done file : " + name);
 			return true;
 		}
 		return false;

Modified: trunk/hudson/plugins/serenity/src/test/java/com/ikokoon/serenity/process/AccumulatorTest.java (40897 => 40898)


--- trunk/hudson/plugins/serenity/src/test/java/com/ikokoon/serenity/process/AccumulatorTest.java	2013-06-07 09:50:07 UTC (rev 40897)
+++ trunk/hudson/plugins/serenity/src/test/java/com/ikokoon/serenity/process/AccumulatorTest.java	2013-06-07 13:32:45 UTC (rev 40898)
@@ -4,7 +4,7 @@
 import static org.junit.Assert.assertNull;
 
 import java.io.File;
-import java.util.StringTokenizer;
+import java.util.jar.JarFile;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -33,10 +33,6 @@
 		classPath = Toolkit.replaceAll(classPath, "\\.\\", "\\");
 		classPath = Toolkit.replaceAll(classPath, "/./", "/");
 		System.setProperty("java.class.path", classPath);
-		StringTokenizer stringTokenizer = new StringTokenizer(classPath, ";");
-		while (stringTokenizer.hasMoreTokens()) {
-			LOGGER.warn(stringTokenizer.nextToken());
-		}
 	}
 
 	@Test
@@ -51,4 +47,13 @@
 		assertNull(targetConsumerClass);
 	}
 
+	@Test
+	public void getSource() throws Exception {
+		Accumulator accumulator = new Accumulator(null);
+		JarFile jarFile = new JarFile(new File("src/test/resources/serenity.jar"));
+		String source = accumulator.getSource(jarFile, Toolkit.dotToSlash(Accumulator.class.getName()) + ".java").toString();
+		LOGGER.info("Source : " + source);
+		assertNotNull(source);
+	}
+
 }

Added: trunk/hudson/plugins/serenity/src/test/resources/serenity.jar


(Binary files differ)
Property changes on: trunk/hudson/plugins/serenity/src/test/resources/serenity.jar ___________________________________________________________________

Added: svn:mime-type

--
You received this message because you are subscribed to the Google Groups "Jenkins Commits" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply via email to