⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directorycontentstestcase.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package preverification.tests;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import eclipseme.preverifier.ClassPreverifier;
import eclipseme.preverifier.results.PreverificationResults;

/**
 * Recursively searches a set of directories (configured by
 * system property) for classes and does a preverification
 * test between EclipseME's preverifier and the WTK preverifier.
 * This test case is not a standard part of the test suite.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.3 $
 * <br>
 * $Date: 2005/12/22 02:34:50 $
 * <br>
 * @author Craig Setera
 */
public class DirectoryContentsTestCase extends BasePreverifyTestCase {
	public static final String PROPERTY = "eclipseme.preverify.source.root";
	private File sourceRoot;
	private StringBuffer expectedOutput;
	private StringBuffer actualOutput;
	
	public void testPreverify() {
		String directory = System.getProperty(PROPERTY);
		assertNotNull(PROPERTY + " must be specified", directory);
		
		sourceRoot = new File(directory);
		if (!sourceRoot.exists()) {
			fail(directory + " does not exist");
		}
		
		testRecursively(sourceRoot);
		assertEquals(expectedOutput.toString(), actualOutput.toString());
	}

	private void testRecursively(File directory) {
		File[] files = directory.listFiles(new FileFilter() {
			public boolean accept(File pathname) {
				return
					pathname.isDirectory() ||
					pathname.getName().endsWith(".class");
			}
		});
		
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				testRecursively(file);
			} else {
				testClassFile(file);
			}
		}
	}

	private void testClassFile(File file) {
		System.out.println("Testing file " + file);
		try {
			TestPreverificationPolicy policy = new CLDC_1_0_Policy();
			ClassPreverifier preverifier = new ClassPreverifier(policy);
			InputStream classStream = new FileInputStream(file);
			assertNotNull(classStream);
		
			PreverificationResults results = 
				preverifier.preverify(classStream, getClasspath(policy));
			assertTrue("No errors expected", !results.isErrorResult());
			
			byte[] preverifiedClass = results.getPreverifiedClassBytes();
			assertNotNull("Code Stream bytes should not be null", preverifiedClass);

			// Do a deeper comparison...
			String internalName = results.getPreverifiedClassNode().name;
			String className = internalName.replace('/', '.');
			
			String[] outputs = getPreverificationOutputs(policy, className, preverifiedClass);
			if (!outputs[0].equals(outputs[1])) {
				System.out.println("**** Output mismatch on class " + className);
				actualOutput.append("**** " + className + " *** begin ****\n");
				actualOutput.append(outputs[0]);
				actualOutput.append("**** " + className + " *** end ****\n");

				expectedOutput.append("**** " + className + " *** begin ****\n");
				expectedOutput.append(outputs[1]);
				expectedOutput.append("**** " + className + " *** end ****\n");
			}
			
		} catch (Exception e) {
			fail(e);
		}
	}

	protected URL[] getClasspath(TestPreverificationPolicy policy) 
		throws IOException 
	{
		URL[] baseClasspath = policy.getLibraries();
		URL[] classpath = new URL[baseClasspath.length + 2];
		System.arraycopy(baseClasspath, 0, classpath, 1, baseClasspath.length);
		classpath[0] = getEmptyApi().toURL();
		classpath[baseClasspath.length + 1] = sourceRoot.getCanonicalFile().toURL();
		
		return classpath;
	}
	
	private File getEmptyApi() {
		File inputsDirectory = super.getWTKInputDirectory();
		return new File(inputsDirectory, "../../eclipseme.core/lib/emptyapi.zip");
	}
	
	/**
	 * @return
	 */
	protected File getWTKInputDirectory() {
		return sourceRoot;
	}

	/**
	 * @see preverification.tests.BasePreverifyTestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		actualOutput = new StringBuffer();
		expectedOutput = new StringBuffer();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -