diff options
author | Igor Fedorenko | 2012-01-11 15:20:54 +0000 |
---|---|---|
committer | Igor Fedorenko | 2012-01-11 15:20:54 +0000 |
commit | 3fb9523fe4e80096f92aaca46fcf2b67ec0ea7f1 (patch) | |
tree | 6ce199a614212adc074a8f070edc2296ce113391 /org.eclipse.m2e.tests.common | |
parent | 9a29b43091d60947277b2fa3f61ae17ecac06fcc (diff) | |
download | m2e-core-3fb9523fe4e80096f92aaca46fcf2b67ec0ea7f1.tar.gz m2e-core-3fb9523fe4e80096f92aaca46fcf2b67ec0ea7f1.tar.xz m2e-core-3fb9523fe4e80096f92aaca46fcf2b67ec0ea7f1.zip |
introduced new ClasspathHelpers common test helper
Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
Diffstat (limited to 'org.eclipse.m2e.tests.common')
-rw-r--r-- | org.eclipse.m2e.tests.common/META-INF/MANIFEST.MF | 2 | ||||
-rw-r--r-- | org.eclipse.m2e.tests.common/src/org/eclipse/m2e/tests/common/ClasspathHelpers.java | 107 |
2 files changed, 108 insertions, 1 deletions
diff --git a/org.eclipse.m2e.tests.common/META-INF/MANIFEST.MF b/org.eclipse.m2e.tests.common/META-INF/MANIFEST.MF index de2dbb19..d33711f2 100644 --- a/org.eclipse.m2e.tests.common/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.tests.common/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.m2e.tests.common;singleton:=true Bundle-Version: 1.1.0.qualifier -Require-Bundle: org.junit, +Require-Bundle: org.junit;bundle-version="4.0.0", org.eclipse.m2e.core;bundle-version="[1.1.0,1.2.0)", org.eclipse.m2e.maven.runtime;bundle-version="[1.1.0,1.2.0)", org.eclipse.m2e.jdt;bundle-version="[1.1.0,1.2.0)", diff --git a/org.eclipse.m2e.tests.common/src/org/eclipse/m2e/tests/common/ClasspathHelpers.java b/org.eclipse.m2e.tests.common/src/org/eclipse/m2e/tests/common/ClasspathHelpers.java new file mode 100644 index 00000000..c8d3c9fc --- /dev/null +++ b/org.eclipse.m2e.tests.common/src/org/eclipse/m2e/tests/common/ClasspathHelpers.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2012 Sonatype, Inc. + * 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: + * Sonatype, Inc. - initial API and implementation + *******************************************************************************/ + +package org.eclipse.m2e.tests.common; + +import java.util.Arrays; +import java.util.regex.Pattern; + +import org.junit.Assert; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.IClasspathEntry; + + +/** + * @since 1.1 + */ +public class ClasspathHelpers { + + /** + * Returns classpath entry with given path. Throws AssertionError if no such entry. + */ + public static IClasspathEntry getClasspathEntry(IClasspathEntry[] cp, IPath path) { + for(IClasspathEntry cpe : cp) { + if(path.equals(cpe.getPath())) { + return cpe; + } + } + Assert.fail("Missing classpath entry " + path); + return null; + } + + /** + * Asserts that classpath has one and only one entry with given first path segments. + */ + public static void assertClasspathEntry(IClasspathEntry[] cp, String... segments) { + int count = 0; + for(IClasspathEntry cpe : cp) { + if(startsWith(cpe.getPath(), segments)) { + count++ ; + } + } + Assert.assertEquals("Unexpected classpath with prefix " + Arrays.toString(segments), 1, count); + } + + private static boolean startsWith(IPath path, String[] segments) { + if(path.segmentCount() < segments.length) { + return false; + } + for(int i = 0; i < segments.length; i++ ) { + if(!segments[i].equals(path.segment(i))) { + return false; + } + } + return true; + } + + /** + * Asserts that classpath has one and only one entry with given path. + */ + public static void assertClasspathEntry(IClasspathEntry[] cp, Path path) { + int count = 0; + for(IClasspathEntry cpe : cp) { + if(cpe.getPath().equals(path)) { + count++ ; + } + } + Assert.assertEquals("Unexpected classpath with path " + path, 1, count); + } + + /** + * Asserts that classpath matches specified path regex patterns. + */ + public static void assertClasspath(String[] expectedPathPatterns, IClasspathEntry[] cp) { + boolean matches = false; + if(expectedPathPatterns.length == cp.length) { + matches = true; + for(int i = 0; i < expectedPathPatterns.length; i++ ) { + if(!Pattern.matches(expectedPathPatterns[i], cp[i].getPath().toPortableString())) { + matches = false; + break; + } + } + } + if(!matches) { + // pretty format and fail + StringBuilder sb_expected = new StringBuilder(); + for(String expected : expectedPathPatterns) { + sb_expected.append(expected).append("\n"); + } + StringBuilder sb_actual = new StringBuilder(); + for(IClasspathEntry cpe : cp) { + sb_actual.append(cpe.getPath().toPortableString()).append("\n"); + } + Assert.assertEquals("Unexpected classpath", sb_expected.toString(), sb_actual.toString()); + } + } +} |