| /******************************************************************************* |
| * Copyright (c) 2000, 2006 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.wtp.releng.tests; |
| |
| import java.io.File; |
| import java.io.FilenameFilter; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Enumeration; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.zip.ZipException; |
| import java.util.zip.ZipFile; |
| |
| import junit.framework.TestCase; |
| |
| import org.eclipse.callisto.tools.versionchecker.VersionLister; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.equinox.app.IApplication; |
| |
| public class BuildTests extends TestCase { |
| |
| |
| |
| public class FileSuffixFilter implements FilenameFilter { |
| |
| private String suffix; |
| |
| public FileSuffixFilter(String suffix) { |
| this.suffix = suffix; |
| } |
| |
| /** |
| * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) |
| */ |
| public boolean accept(File dir, String name) { |
| int lastDot = name.lastIndexOf('.'); |
| if (lastDot == -1) { |
| return false; |
| } |
| return name.substring(lastDot).equals(suffix); |
| } |
| |
| } |
| |
| private static final String[] REQUIRED_FEATURE_FILES = {"epl-v10.html", "feature.properties", "feature.xml", "license.html"}; |
| private static final String REQUIRED_FEATURE_SUFFIX = ".jpg"; |
| |
| private static final String[] REQUIRED_PLUGIN_FILES = {"about.html", "plugin.properties", "plugin.xml"}; |
| private static final String REQUIRED_PLUGIN_SUFFIX = ".jar"; |
| |
| private static final String[] REQUIRED_FEATURE_PLUGIN_FILES = {"about.html", "about.ini", "about.mappings", "about.properties", "plugin.properties", "plugin.xml"}; |
| private static final String REQUIRED_FEATURE_PLUGIN_SUFFIX = ".gif"; |
| |
| private static final String[] REQUIRED_FRAGMENT_FILES = {"fragment.xml"}; |
| private static final String REQUIRED_FRAGMENT_SUFFIX = ""; |
| |
| private static final String[] REQUIRED_SWT_FRAGMENT_FILES = {"fragment.properties"}; |
| private static final String REQUIRED_SWT_FRAGMENT_SUFFIX = ""; |
| |
| private static final String[] REQUIRED_SOURCE_FILES = {"about.html"}; |
| private static final String REQUIRED_SOURCE_SUFFIX = ".zip"; |
| |
| private static final String[] REQUIRED_BUNDLE_FILES = {"about.html"}; |
| private static final String REQUIRED_BUNDLE_MANIFEST = "MANIFEST.MF"; |
| private static final String REQUIRED_BUNDLE_SUFFIX = ".jar"; |
| |
| private static final String[] SUFFIX_EXEMPT_LIST = {"org.eclipse.swt", "org.apache.ant"}; |
| private static final int EXPECTED_NUMBER_OF_FEATURES = 153; |
| private static final int EXPECTED_NUMBER_OF_PLUGINS = 580; |
| |
| /** |
| * Constructor for EmptyDirectoriesTest. |
| * |
| * @param arg0 |
| */ |
| public BuildTests(String arg0) { |
| super(arg0); |
| } |
| |
| |
| public void _testFeatureFiles() { |
| List result = new ArrayList(); |
| String installDir = Platform.getInstallLocation().getURL().getPath(); |
| File featureDir = new File(installDir, "features"); |
| File[] features = featureDir.listFiles(); |
| |
| for (int i = 0; i < features.length; i++) { |
| File aFeature = features[i]; |
| if (!testDirectory(aFeature, REQUIRED_FEATURE_FILES, REQUIRED_FEATURE_SUFFIX)) { |
| result.add(aFeature.getPath()); |
| } |
| |
| } |
| |
| String aString = ""; |
| if (result.size() > 0) { |
| |
| Iterator iter = result.iterator(); |
| while (iter.hasNext()) { |
| String element = (String) iter.next(); |
| aString = aString + element + "; "; |
| } |
| } |
| assertTrue("Feature directory missing required files: " + aString, result.size() == 0); |
| } |
| |
| |
| public void testNumberOfFeatures() { |
| |
| String installDir = Platform.getInstallLocation().getURL().getPath(); |
| File featureDir = new File(installDir, "features"); |
| File[] features = featureDir.listFiles(); |
| int nFeatures = features.length; |
| assertEquals("there were more or less features than expected: ", EXPECTED_NUMBER_OF_FEATURES, nFeatures); |
| // System.out.println("wtp releng tests show nFeatures=" + nFeatures); |
| |
| } |
| |
| public void _testPluginFiles() { |
| List result = new ArrayList(); |
| String installDir = Platform.getInstallLocation().getURL().getPath(); |
| File pluginDir = new File(installDir, "plugins"); |
| File[] plugins = pluginDir.listFiles(); |
| |
| |
| System.out.println("nPlugins: " + plugins.length); |
| |
| |
| for (int i = 0; i < plugins.length; i++) { |
| File aPlugin = plugins[i]; |
| if (aPlugin.getName().indexOf("test") == -1) { |
| if (!testPluginFile(aPlugin)) { |
| result.add(aPlugin.getPath()); |
| } |
| } |
| } |
| |
| String aString = ""; |
| if (result.size() > 0) { |
| |
| Iterator iter = result.iterator(); |
| while (iter.hasNext()) { |
| String element = (String) iter.next(); |
| aString = aString + element + "; "; |
| } |
| } |
| assertTrue("Plugin directory missing required files: " + aString, result.size() == 0); |
| } |
| |
| |
| public void testNumberOfPlugins() { |
| String installDir = Platform.getInstallLocation().getURL().getPath(); |
| File pluginDir = new File(installDir, "plugins"); |
| File[] plugins = pluginDir.listFiles(); |
| int nPlugins = plugins.length; |
| assertEquals("there were more or less plugins than expected: ", EXPECTED_NUMBER_OF_PLUGINS, nPlugins); |
| // System.out.println("wtp.releng tests shows nPlugins=" + nPlugins); |
| |
| |
| } |
| |
| private boolean testPluginFile(File aPlugin) { |
| |
| // Are we a doc plugin? |
| if (testDirectory(aPlugin, REQUIRED_PLUGIN_FILES, ".zip")) { |
| return true; |
| } |
| |
| // Are we a feature plugin? |
| if (testDirectory(aPlugin, REQUIRED_FEATURE_PLUGIN_FILES, REQUIRED_FEATURE_PLUGIN_SUFFIX)) { |
| return true; |
| } |
| |
| // Are we a regular plugin |
| if (testDirectory(aPlugin, REQUIRED_PLUGIN_FILES, REQUIRED_PLUGIN_SUFFIX)) { |
| return true; |
| } |
| |
| // Are we a source plugin |
| if (testSourcePlugin(aPlugin)) { |
| return true; |
| } |
| |
| // Are we a fragment |
| if ((testDirectory(aPlugin, REQUIRED_FRAGMENT_FILES, REQUIRED_FRAGMENT_SUFFIX)) || (testBundleDirectory(aPlugin, REQUIRED_BUNDLE_FILES, REQUIRED_BUNDLE_MANIFEST, REQUIRED_FRAGMENT_SUFFIX))) { |
| return true; |
| } |
| |
| // Are we an swt fragment |
| if (testDirectory(aPlugin, REQUIRED_SWT_FRAGMENT_FILES, REQUIRED_SWT_FRAGMENT_SUFFIX)) { |
| return true; |
| } |
| |
| // Are we a bundle? |
| if (testBundleDirectory(aPlugin, REQUIRED_BUNDLE_FILES, REQUIRED_BUNDLE_MANIFEST, REQUIRED_BUNDLE_SUFFIX)) { |
| return true; |
| } |
| |
| // No then we are bad |
| return false; |
| } |
| |
| private boolean testPluginJar(File aDirectory, String[] requiredFiles) { |
| ArrayList list = new ArrayList(); |
| try { |
| ZipFile jarredPlugin = new ZipFile(aDirectory); |
| Enumeration _enum = jarredPlugin.entries(); |
| while (_enum.hasMoreElements()) { |
| list.add(_enum.nextElement().toString()); |
| } |
| } |
| catch (ZipException e) { |
| e.printStackTrace(); |
| } |
| catch (IOException e) { |
| e.printStackTrace(); |
| } |
| if (!list.containsAll(Arrays.asList(requiredFiles))) { |
| return false; |
| } |
| return true; |
| } |
| |
| private boolean testDirectory(File aDirectory, String[] requiredFiles, String requiredSuffix) { |
| if (aDirectory.getName().endsWith(".jar")) { |
| return testPluginJar(aDirectory, requiredFiles); |
| } |
| else { |
| if (!Arrays.asList(aDirectory.list()).containsAll(Arrays.asList(requiredFiles))) { |
| return false; |
| } |
| |
| int index = aDirectory.getName().indexOf('_'); |
| if (index == -1) { |
| index = aDirectory.getName().length(); |
| } |
| |
| // String plainName = aDirectory.getName().substring(0, index); |
| |
| /* |
| * if (requiredSuffix.equals("") || |
| * Arrays.asList(SUFFIX_EXEMPT_LIST).contains(plainName)) { return |
| * true; } else if (aDirectory .listFiles(new |
| * FileSuffixFilter(requiredSuffix)).length == 0) { return false; } |
| */ |
| } |
| return true; |
| } |
| |
| |
| private boolean testBundleDirectory(File aDirectory, String[] requiredFiles, String manifestFile, String requiredSuffix) { |
| if (aDirectory.getName().endsWith(".jar")) { |
| return testPluginJar(aDirectory, requiredFiles); |
| } |
| else { |
| if (!Arrays.asList(aDirectory.list()).containsAll(Arrays.asList(requiredFiles))) { |
| return false; |
| } |
| |
| int index = aDirectory.getName().indexOf('_'); |
| if (index == -1) { |
| index = aDirectory.getName().length(); |
| } |
| |
| String plainName = aDirectory.getName().substring(0, index); |
| |
| File metaDir = new File(aDirectory, "META-INF"); |
| |
| String[] metaFiles = metaDir.list(); |
| if (metaFiles == null) { |
| return (false); |
| } |
| else { |
| for (int i = 0; i < metaFiles.length; i++) { |
| String filename = metaFiles[i]; |
| if (filename == manifestFile) { |
| return true; |
| } |
| } |
| } |
| |
| if (!metaDir.exists()) { |
| return false; |
| } |
| |
| if (requiredSuffix.equals("") || Arrays.asList(SUFFIX_EXEMPT_LIST).contains(plainName)) { |
| return true; |
| } |
| else if (aDirectory.listFiles(new FileSuffixFilter(requiredSuffix)).length == 0) { |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| /** |
| * Return true if the receiver is a source plugin, false otherwise A |
| * separate method because this is a little tricky. |
| * |
| * @param aPlugin |
| * @return boolean |
| */ |
| private boolean testSourcePlugin(File aPlugin) { |
| if (!testDirectory(aPlugin, REQUIRED_PLUGIN_FILES, "")) { |
| return false; |
| } |
| |
| File sourceDir = new File(aPlugin, "src"); |
| File[] sourceDirs = sourceDir.listFiles(); |
| if (sourceDirs == null) { |
| return false; |
| } |
| |
| for (int i = 0; i < sourceDirs.length; i++) { |
| File aSourceDir = sourceDirs[i]; |
| if (!testDirectory(aSourceDir, REQUIRED_SOURCE_FILES, REQUIRED_SOURCE_SUFFIX)) { |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| /** |
| * Compares the feature and plug-in versions contained in this Eclipse |
| * configuration, against a known previous Eclipse configuration. |
| * |
| * @throws Exception |
| */ |
| public void testVersionCompare() throws Exception { |
| |
| VersionLister versionLister = new VersionLister(); |
| String[] args = new String[]{"-testToReference", "WTP154Plus1Patch", "-listToReferenceFile"}; |
| Object result = versionLister.run(args); |
| boolean testok = (IApplication.EXIT_OK.equals(result)); |
| assertTrue("There is an error in versioning. See full listing for details.", testok); |
| |
| } |
| } |