From a682e3b5341e5cf21e84c1dbbb2bc98743674392 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 16 Sep 2013 14:31:37 +0300 Subject: Refactored build scripts to support TB2, TB3 + integration tests (#12572) * Main build configuration triggers unit tests for all modules and uitest/build.xml testbench tests in parallel * uitest/build.xml triggers Jetty startup and integration (server) tests in parallel. After the server has started, TB2 and TB3 tests are run in parallel. * Server integration tests for servlet containers are run using TB3 and the com.vaadin.tests.tb3.ServletIntegrationTests test suite. * Portlet integration tests are still run using TB2 test scripts Change-Id: Ie6bffd4e68b4889074e9c470faa3c65f923e55c4 --- uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java | 223 ++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java (limited to 'uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java') diff --git a/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java b/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java new file mode 100644 index 0000000000..dc187000d2 --- /dev/null +++ b/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java @@ -0,0 +1,223 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.tb3; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Modifier; +import java.net.JarURLConnection; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; + +import org.junit.runners.Suite; +import org.junit.runners.model.InitializationError; + +/** + * Test suite which consists of all the TB3 tests passed in the constructor. + * Runs the tests in parallel using a {@link ParallelScheduler} + * + * @author Vaadin Ltd + */ +public class TB3TestSuite extends Suite { + + public TB3TestSuite(Class klass, + Class baseClass, String basePackage, + String[] ignorePackages) throws InitializationError { + super(klass, findTests(baseClass, basePackage, ignorePackages)); + setScheduler(new ParallelScheduler()); + } + + /** + * Traverses the directory on the classpath (inside or outside a Jar file) + * specified by 'basePackage'. Collects all classes inside the location + * which can be assigned to 'baseClass' except for classes inside packages + * listed in 'ignoredPackages'. + * + * @param baseClass + * @param basePackage + * @param ignorePackages + * @return + */ + private static Class[] findTests( + Class baseClass, String basePackage, + String[] ignorePackages) { + try { + List l = findClasses(baseClass, basePackage, ignorePackages); + return l.toArray(new Class[] {}); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + /** + * Traverses the directory on the classpath (inside or outside a Jar file) + * specified by 'basePackage'. Collects all classes inside the location + * which can be assigned to 'baseClass' except for classes inside packages + * listed in 'ignoredPackages'. + * + * @param baseClass + * @param basePackage + * @param ignoredPackages + * @return + * @throws IOException + */ + private static List> findClasses(Class baseClass, + String basePackage, String[] ignoredPackages) throws IOException { + List> classes = new ArrayList>(); + String basePackageDirName = "/" + basePackage.replace('.', '/'); + URL location = baseClass.getResource(basePackageDirName); + if (location.getProtocol().equals("file")) { + try { + File f = new File(location.toURI()); + if (!f.exists()) { + throw new IOException("Directory " + f.toString() + + " does not exist"); + } + findPackages(f, basePackage, baseClass, classes, + ignoredPackages); + } catch (URISyntaxException e) { + throw new IOException(e.getMessage()); + } + } else if (location.getProtocol().equals("jar")) { + JarURLConnection juc = (JarURLConnection) location.openConnection(); + findClassesInJar(juc, basePackage, baseClass, classes); + } + + Collections.sort(classes, new Comparator>() { + + @Override + public int compare(Class o1, Class o2) { + return o1.getName().compareTo(o2.getName()); + } + + }); + return classes; + } + + /** + * Traverses the given directory and collects all classes which are inside + * the given 'javaPackage' and can be assigned to the given 'baseClass'. The + * found classes are added to 'result'. + * + * @param parent + * The directory to traverse + * @param javaPackage + * The java package which 'parent' contains + * @param baseClass + * The class which the target classes extend + * @param result + * The collection to which found classes are added + * @param ignoredPackages + * A collection of packages (including sub packages) to ignore + */ + private static void findPackages(File parent, String javaPackage, + Class baseClass, Collection> result, + String[] ignoredPackages) { + for (String ignoredPackage : ignoredPackages) { + if (javaPackage.equals(ignoredPackage)) { + return; + } + } + + for (File file : parent.listFiles()) { + if (file.isDirectory()) { + findPackages(file, javaPackage + "." + file.getName(), + baseClass, result, ignoredPackages); + } else if (file.getName().endsWith(".class")) { + String fullyQualifiedClassName = javaPackage + "." + + file.getName().replace(".class", ""); + addClassIfMatches(result, fullyQualifiedClassName, baseClass); + } + } + + } + + /** + * Traverses a Jar file using the given connection and collects all classes + * which are inside the given 'javaPackage' and can be assigned to the given + * 'baseClass'. The found classes are added to 'result'. + * + * @param javaPackage + * The java package containing the classes (classes may be in a + * sub package) + * @param baseClass + * The class which the target classes extend + * @param result + * The collection to which found classes are added + * @throws IOException + */ + private static void findClassesInJar(JarURLConnection juc, + String javaPackage, Class baseClass, + Collection> result) throws IOException { + String javaPackageDir = javaPackage.replace('.', '/'); + Enumeration ent = juc.getJarFile().entries(); + while (ent.hasMoreElements()) { + JarEntry e = ent.nextElement(); + if (e.getName().endsWith(".class") + && e.getName().startsWith(javaPackageDir)) { + String fullyQualifiedClassName = e.getName().replace('/', '.') + .replace(".class", ""); + addClassIfMatches(result, fullyQualifiedClassName, baseClass); + } + } + } + + /** + * Verifies that the class represented by 'fullyQualifiedClassName' can be + * loaded, assigned to 'baseClass' and is not an abstract or anonymous + * class. + * + * @param result + * The collection to add to + * @param fullyQualifiedClassName + * The candidate class + * @param baseClass + * The class 'fullyQualifiedClassName' should be assignable to + */ + @SuppressWarnings("unchecked") + private static void addClassIfMatches( + Collection> result, + String fullyQualifiedClassName, Class baseClass) { + try { + // Try to load the class + + Class c = Class.forName(fullyQualifiedClassName); + if (!baseClass.isAssignableFrom(c)) { + return; + } + if (!Modifier.isAbstract(c.getModifiers()) && !c.isAnonymousClass()) { + result.add((Class) c); + } + } catch (Exception e) { + // Could ignore that class cannot be loaded + e.printStackTrace(); + } catch (LinkageError e) { + // Ignore. Client side classes will at least throw LinkageErrors + } + + } + +} \ No newline at end of file -- cgit v1.2.3 From bd0ae0581f265be57374b236cfe1b71043ab69aa Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 24 Sep 2013 18:19:45 +0300 Subject: Limit the number of tests run concurrently (#12572) Change-Id: I015e92ccc9be963543032c14dd9d051bcba58e53 --- uitest/src/com/vaadin/tests/tb3/ParallelScheduler.java | 13 +++++++++++-- uitest/src/com/vaadin/tests/tb3/TB3Runner.java | 16 +++++++++++++++- uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) (limited to 'uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java') diff --git a/uitest/src/com/vaadin/tests/tb3/ParallelScheduler.java b/uitest/src/com/vaadin/tests/tb3/ParallelScheduler.java index f8013169fa..912d7d010e 100644 --- a/uitest/src/com/vaadin/tests/tb3/ParallelScheduler.java +++ b/uitest/src/com/vaadin/tests/tb3/ParallelScheduler.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.runners.model.RunnerScheduler; @@ -33,8 +32,18 @@ import org.junit.runners.model.RunnerScheduler; */ public class ParallelScheduler implements RunnerScheduler { private final List> fResults = new ArrayList>(); + private ExecutorService fService; - private final ExecutorService fService = Executors.newCachedThreadPool(); + /** + * Creates a parallel scheduler which will use the given executor service + * when submitting test jobs. + * + * @param service + * The service to use for tests + */ + public ParallelScheduler(ExecutorService service) { + fService = service; + } @Override public void schedule(final Runnable childStatement) { diff --git a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java index 5860ac42c0..b612b17caa 100644 --- a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java +++ b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java @@ -19,6 +19,8 @@ package com.vaadin.tests.tb3; import java.lang.reflect.Method; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.junit.Test; import org.junit.runners.BlockJUnit4ClassRunner; @@ -40,9 +42,21 @@ import com.vaadin.tests.tb3.AbstractTB3Test.BrowserUtil; */ public class TB3Runner extends BlockJUnit4ClassRunner { + /** + * This is the total limit of actual JUnit test instances run in parallel + */ + private static final int MAX_CONCURRENT_TESTS = 50; + + /** + * This is static so it is shared by all tests running concurrently on the + * same machine and thus can limit the number of threads in use. + */ + private static final ExecutorService service = Executors + .newFixedThreadPool(MAX_CONCURRENT_TESTS); + public TB3Runner(Class klass) throws InitializationError { super(klass); - setScheduler(new ParallelScheduler()); + setScheduler(new ParallelScheduler(service)); } @Override diff --git a/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java b/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java index dc187000d2..e1c8edfd60 100644 --- a/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java +++ b/uitest/src/com/vaadin/tests/tb3/TB3TestSuite.java @@ -28,6 +28,8 @@ import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.jar.JarEntry; import org.junit.runners.Suite; @@ -41,11 +43,24 @@ import org.junit.runners.model.InitializationError; */ public class TB3TestSuite extends Suite { + /** + * This only restricts the number of test suites running concurrently. The + * number of tests to run concurrently are configured in {@link TB3Runner}. + */ + private static final int MAX_CONCURRENT_TEST_SUITES = 20; + + /** + * This is static so it is shared by all test suites running concurrently on + * the same machine and thus can limit the number of threads in use. + */ + private final ExecutorService service = Executors + .newFixedThreadPool(MAX_CONCURRENT_TEST_SUITES); + public TB3TestSuite(Class klass, Class baseClass, String basePackage, String[] ignorePackages) throws InitializationError { super(klass, findTests(baseClass, basePackage, ignorePackages)); - setScheduler(new ParallelScheduler()); + setScheduler(new ParallelScheduler(service)); } /** -- cgit v1.2.3