exporttestbuilder.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 341 行
SVN-BASE
341 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.javatest;import com.sun.tck.cldc.javatest.TestBuilder;import com.sun.tck.j2me.utils.FileUtils;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.Enumeration;import java.util.HashSet;import java.util.Hashtable;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.jar.JarEntry;import java.util.jar.JarFile;/** * Test Builder for Test Export mode. Implements generic functionality for * exporting test classes and sources. */public class ExportTestBuilder extends TestBuilder { /** * Returns the name of the agent class used in this test run. * * @return Classname of the agent. */ protected String getAppMainClassName() { return appMainClassName; } /** * Returns <code>TestExportInfo</code> instance used in this test run. */ protected TestExportInfo getTestExportInfo() { return testExportInfo; } /** * Creates a new instance of ExportTestBuilder. * * @param aDestDir Pathname of a directory, where the exported JARs * and JADs will be created. * * @param aMaxSize Maximum size of a JAR file in bytes. * * @param testExportInfo TestExportInfo instance containing test export * specific parameters. * @param appMainClassName Classname of the agent for all tests. */ public ExportTestBuilder(String aDestDir, int aMaxSize, String classpath, TestExportInfo testExportInfo, String appMainClassName) { super(aDestDir, aMaxSize, classpath); this.testExportInfo = testExportInfo; this.appMainClassName = appMainClassName; bundleExtraJavaSourcesMap = new Hashtable<String, Set<String>>(); bundleClassesMap = new Hashtable<String, Set<String>>(); currentBundleExtraJavaSources = new HashSet<String>(); currentBundleClasses = new HashSet<String>(); urlPrefix = testExportInfo.getJarURLPrefix(); if ((urlPrefix.length() > 0) && !urlPrefix.endsWith("/")) { urlPrefix += "/"; } } /** * This method overrides the <code>TestBuilder.addFiles(String[] names) * </code> method. It invokes the super implementation (which is supposed to * add the specified resources to JAR file) and then copies the same files * to <code>classes</code> directory in the export output directory so that * the copied files can be used to rebuild the exported tests. */ public synchronized boolean addTest(String testID) { boolean status = super.addTest(testID); String[] files = this.getTestFiles(testID); String[] jars = this.getTestJars(testID); if (files != null) { File classesDir = new File(getTestExportInfo().getClassesDirName()); if (!classesDir.exists()) { classesDir.mkdirs(); } for (String file : files) { InputStream in = pathReader.getFileDataAsStream(file); if (in == null) { continue; } copyToDir(in, file, classesDir); currentBundleClasses.add(file); tryToAddSourceForClass(file); } } if (jars != null) { File libDir = new File(getTestExportInfo().getExportDirName() + File.separator + "lib"); if (!libDir.exists()) { libDir.mkdirs(); } for (int i = 0; i < jars.length; i++) { File srcFile = new File(jars[i]); try { InputStream in = new FileInputStream(srcFile); String jarName = srcFile.getName(); copyToDir(in, jarName, libDir); } catch (FileNotFoundException e) { throw new RuntimeException("Cannot find library JAR file: " + jars[i], e); } } } return status; } /** * Returns the set of extra Java sources for the specified bundle. Extra * Java sources are the sources not listed in the test descriptions, but * identified by <code>MidExportBuilder</code> as sources of the classes * contained in the bundle. * * @param name Name of the test bundle (like <code>test1.jar</code>. * * @return Set containing the pathnames of the Java source files. */ public Set<String> getExtraJavaSources(String name) { return bundleExtraJavaSourcesMap.get(name); } /** * Invokes the super implementation and collects the information about * classes contained in the specified JAR file and their Java sources. */ public boolean addJarFileContent(String fname) { JarFile jarFile = null; try { jarFile = new JarFile(fname); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().endsWith(".class")) { String srcPathName = getSourceForClass(entry.getName()); if (srcPathName != null) { currentBundleExtraJavaSources.add(srcPathName); } currentBundleClasses.add(entry.getName()); } } } catch (IOException e) { throw new RuntimeException("Failed to copy source for JAR file: " + fname, e); } finally { if (jarFile != null) { try { jarFile.close(); } catch (IOException e) { throw new RuntimeException("Unexpected I/O error while " + "closing JAR file: " + jarFile.getName(), e); } } } return super.addJarFileContent(fname); } public synchronized void checkOut(String name) { super.checkOut(name); /* * "agent_client.jar" comes from TestBundle class. It's not a test * bundle and it doesn't need JAD */ if (!name.equals("agent_client.jar") && (this instanceof ExportTestBuilder)) { bundleClassesMap.put(name, currentBundleClasses); bundleExtraJavaSourcesMap.put(name, currentBundleExtraJavaSources); createApplicationDescriptor(name); } currentBundleExtraJavaSources = new HashSet<String>(); currentBundleClasses = new HashSet<String>(); } /** * Returns the set of names of classes contained in the specified bundle. * * @param name Name of the test bundle (like <code>test1.jar</code>. * * @return Set of class file names. */ Set<String> getClassesForBundle(String name) { return bundleClassesMap.get(name); } /** * Creates application descriptor for specified JAR file in the export * directory. * * @param jarName Name of JAR file relative to the export directory. */ protected void createApplicationDescriptor(String jarName) { FileOutputStream fos = null; String jamName = jarName.replace(".jar", ".jam"); File jarFile = new File(getTestExportInfo().getExportDirName(), jarName); File jamFile = new File(getTestExportInfo().getExportDirName(), jamName); try { if (!jamFile.createNewFile()) { throw new RuntimeException("Cannot create JAM file: " + jamFile.getPath()); } fos = new FileOutputStream(jamFile); PrintWriter pw = new PrintWriter(fos); String bundleId = jarName.substring(0, jarName.lastIndexOf('.')); pw.println("JAR-File-URL: " + urlPrefix + bundleId + "/" + jarName); pw.println("JAR-File-Size: " + jarFile.length()); pw.println("Main-Class: " + getAppMainClassName()); pw.println("Application-Name: Test Suite"); pw.println("Use-Once: yes"); pw.flush(); } catch (IOException e) { throw new RuntimeException("Error during creation of JAM file", e); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) {} } } } /** * If the specified name is the name of a class file, this method tries * to find a Java source file for it. Otherwise, does nothing. */ private void tryToAddSourceForClass(String name) { if (name.endsWith(".class")) { String srcPathName = getSourceForClass(name); if (srcPathName != null) { currentBundleExtraJavaSources.add(srcPathName); } } } /** * Searches the Java source file for the specified class. Returns pathname * of the found Java source file or <code>null</code> if nothing found. * * @param classFileName Name of class for which to search the source. * * @return Pathname of a Java source file or <code>null</code> */ private String getSourceForClass(String classFileName) { String javaFileName = classFileName.replace(".class", ".java"); for (String srcRoot : testExportInfo.getSrcRoots()) { File javaFile = new File(srcRoot, javaFileName); if (javaFile.exists()) { return javaFile.getPath(); } } return null; } /** * Mapping from the bundle IDs to the sets of extra Java sources. Extra * Java sources are the sources not listed in the test descriptions, but * identified by the MidExportBuilder as Java sources of classes from * the test bundles. */ private Map<String, Set<String>> bundleExtraJavaSourcesMap; /** * Mapping from the bundle IDs to the sets of class file names. */ private Map<String, Set<String>> bundleClassesMap; /** * Set of extra Java sources of currently processes test bundle. */ private Set<String> currentBundleExtraJavaSources; /** * Set of class files contained in currently processed test bundle. */ private Set<String> currentBundleClasses; private void copyToDir(InputStream in, String relativeName, File rootDir) { File destFile = new File(rootDir, relativeName); if (destFile.exists()) { return; } File destDir = destFile.getParentFile(); if (destDir != null && !destDir.exists()) { destDir.mkdirs(); } try { FileUtils.copyStreamToFile(in, destFile, true); } catch (Exception e) { throw new RuntimeException("Cannot copy resource file: " + relativeName, e); } } private String urlPrefix; private TestExportInfo testExportInfo; private String appMainClassName;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?