private String[] determineImportsFor(Map entries) { // get contained packages to do matching on the test hierarchy Collection containedPackages = jarCreator.getContainedPackages(); Set cumulatedPackages = new LinkedHashSet(); // make sure the collection package is valid boolean validPackageCollection = !containedPackages.isEmpty(); boolean trace = logger.isTraceEnabled(); for (Iterator iterator = entries.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry entry = (Map.Entry) iterator.next(); String resourceName = (String) entry.getKey(); // filter out the test hierarchy if (resourceName.endsWith(ClassUtils.CLASS_FILE_SUFFIX)) { if (trace) logger.trace("Analyze imports for test bundle resource " + resourceName); String classFileName = StringUtils.getFilename(resourceName); String className = classFileName.substring( 0, classFileName.length() - ClassUtils.CLASS_FILE_SUFFIX.length()); String classPkg = resourceName .substring(0, resourceName.length() - classFileName.length()) .replace('/', '.'); if (classPkg.startsWith(".")) classPkg = classPkg.substring(1); if (classPkg.endsWith(".")) classPkg = classPkg.substring(0, classPkg.length() - 1); // if we don't have the package, add it if (validPackageCollection && StringUtils.hasText(classPkg) && !containedPackages.contains(classPkg)) { logger.trace( "Package [" + classPkg + "] is NOT part of the test archive; adding an import for it"); cumulatedPackages.add(classPkg); } // otherwise parse the class byte-code else { if (trace) logger.trace( "Package [" + classPkg + "] is part of the test archive; parsing " + className + " bytecode to determine imports..."); cumulatedPackages.addAll( determineImportsForClass(className, (Resource) entry.getValue())); } } } return (String[]) cumulatedPackages.toArray(new String[cumulatedPackages.size()]); }
protected void postProcessBundleContext(BundleContext context) throws Exception { logger.debug("Post processing: creating test bundle"); Resource jar; Manifest mf = getManifest(); // if the jar content hasn't been discovered yet (while creating the manifest) // do so now if (jarEntries == null) { // set root path jarCreator.setRootPath(getRootPath()); // add the content pattern jarCreator.setContentPattern(getBundleContentPattern()); // use jar creator for pattern discovery jar = jarCreator.createJar(mf); } // otherwise use the cached resources else { jar = jarCreator.createJar(mf, jarEntries); } try { installAndStartBundle(context, jar); } catch (Exception e) { IllegalStateException ise = new IllegalStateException("Unable to dynamically start generated unit test bundle"); ise.initCause(e); throw ise; } // now do the delegation super.postProcessBundleContext(context); }
/** * Returns the current test bundle manifest. The method tries to read the manifest from the given * location; in case the location is <code>null</code> (default), it will search for <code> * META-INF/MANIFEST.MF</code> file in jar content (as specified through the patterns) and, if it * cannot find the file, <em>automatically</em> create a <code>Manifest</code> object containing * default entries. * * <p>Subclasses can override this method to enhance the returned Manifest. * * @return Manifest used for this test suite. * @see #createDefaultManifest() */ protected Manifest getManifest() { // return cached manifest if (manifest != null) return manifest; String manifestLocation = getManifestLocation(); if (StringUtils.hasText(manifestLocation)) { logger.info("Using Manifest from specified location=[" + getManifestLocation() + "]"); DefaultResourceLoader loader = new DefaultResourceLoader(); manifest = createManifestFrom(loader.getResource(manifestLocation)); } else { // set root path jarCreator.setRootPath(getRootPath()); // add the content pattern jarCreator.setContentPattern(getBundleContentPattern()); // see if the manifest already exists in the classpath // to resolve the patterns jarEntries = jarCreator.resolveContent(); for (Iterator iterator = jarEntries.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry entry = (Map.Entry) iterator.next(); if (META_INF_JAR_LOCATION.equals(entry.getKey())) { logger.info("Using Manifest from the test bundle content=[/META-INF/MANIFEST.MF]"); manifest = createManifestFrom((Resource) entry.getValue()); } } // fallback to default manifest creation if (manifest == null) { logger.info("Automatically creating Manifest for the test bundle"); manifest = createDefaultManifest(); } } return manifest; }
/** * Eliminates imports for packages already included in the bundle. Works only if the jar content * is known (variable 'jarEntries' set). * * @param imports * @return */ private Collection eliminatePackagesAvailableInTheJar(Collection imports) { // no jar entry present, bail out. if (jarEntries == null || jarEntries.isEmpty()) return imports; Set filteredImports = new LinkedHashSet(imports.size()); Collection eliminatedImports = new LinkedHashSet(2); Collection jarPackages = jarCreator.getContainedPackages(); for (Iterator iterator = imports.iterator(); iterator.hasNext(); ) { String pckg = (String) iterator.next(); if (jarPackages.contains(pckg)) eliminatedImports.add(pckg); else filteredImports.add(pckg); } if (!eliminatedImports.isEmpty() && logger.isTraceEnabled()) logger.trace("Eliminated packages already present in the bundle " + eliminatedImports); return filteredImports; }