From abfb4ed803bb1c5e006dbafb400b8c9a4ea33a7e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 20 Aug 2013 16:29:00 +0300 Subject: [PATCH] Add integration test suite --- .../vaadin/tests/tb3/AllIntegrationTests.java | 33 +++ .../src/com/vaadin/tests/tb3/AllTB3Tests.java | 178 +--------------- .../src/com/vaadin/tests/tb3/TB3Runner.java | 2 +- .../com/vaadin/tests/tb3/TB3TestFinder.java | 200 ++++++++++++++++++ 4 files changed, 240 insertions(+), 173 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/tb3/AllIntegrationTests.java create mode 100644 uitest/src/com/vaadin/tests/tb3/TB3TestFinder.java diff --git a/uitest/src/com/vaadin/tests/tb3/AllIntegrationTests.java b/uitest/src/com/vaadin/tests/tb3/AllIntegrationTests.java new file mode 100644 index 0000000000..b02c5f0670 --- /dev/null +++ b/uitest/src/com/vaadin/tests/tb3/AllIntegrationTests.java @@ -0,0 +1,33 @@ +/* + * 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 org.junit.runner.RunWith; +import org.junit.runners.model.InitializationError; + +import com.vaadin.tests.tb3.AllIntegrationTests.AllIntegrationTestsFinder; + +@RunWith(AllIntegrationTestsFinder.class) +public class AllIntegrationTests { + public static class AllIntegrationTestsFinder extends TB3TestFinder { + public AllIntegrationTestsFinder(Class klass) + throws InitializationError { + super(klass, AbstractTB3Test.class, "com.vaadin.tests.integration", + new String[] {}); + } + } +} diff --git a/uitest/src/com/vaadin/tests/tb3/AllTB3Tests.java b/uitest/src/com/vaadin/tests/tb3/AllTB3Tests.java index 5ce3c6e00e..d2322d6f58 100644 --- a/uitest/src/com/vaadin/tests/tb3/AllTB3Tests.java +++ b/uitest/src/com/vaadin/tests/tb3/AllTB3Tests.java @@ -19,194 +19,28 @@ */ 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.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.jar.JarEntry; - import org.junit.runner.RunWith; -import org.junit.runners.Suite; import org.junit.runners.model.InitializationError; -import org.junit.runners.model.RunnerScheduler; -import com.vaadin.tests.tb3.AllTB3Tests.TB3TestFinder; +import com.vaadin.tests.tb3.AllTB3Tests.AllTestsFinder; /** * * @since * @author Vaadin Ltd */ -@RunWith(TB3TestFinder.class) +@RunWith(AllTestsFinder.class) public class AllTB3Tests { - public static class ParallelScheduler implements RunnerScheduler { - private final List> fResults = new ArrayList>(); - - private final ExecutorService fService = Executors - .newCachedThreadPool(); - - @Override - public void schedule(final Runnable childStatement) { - fResults.add(fService.submit(new Callable() { - @Override - public Object call() throws Exception { - childStatement.run(); - return null; - } - })); - } - - @Override - public void finished() { - for (Future each : fResults) { - try { - each.get(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - } - - public static class TB3TestFinder extends Suite { + public static class AllTestsFinder extends TB3TestFinder { /** * @param klass - * @param suiteClasses * @throws InitializationError */ - public TB3TestFinder(Class klass) throws InitializationError { - super(klass, getAllTB3Tests()); - setScheduler(new ParallelScheduler()); - } - - /** - * @since - * @return - */ - private static Class[] getAllTB3Tests() { - try { - List> l = findClasses( - AbstractTB3Test.class, "com.vaadin", - new String[] { "com.vaadin.tests.integration" }); - return l.toArray(new Class[] {}); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } - - 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(); - findPackages(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; - } - - 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); - } - } - - } - - private static void findPackages(JarURLConnection juc, - String javaPackage, Class baseClass, - Collection> result) throws IOException { - String prefix = "com/vaadin/ui"; - Enumeration ent = juc.getJarFile().entries(); - while (ent.hasMoreElements()) { - JarEntry e = ent.nextElement(); - if (e.getName().endsWith(".class") - && e.getName().startsWith(prefix)) { - String fullyQualifiedClassName = e.getName() - .replace('/', '.').replace(".class", ""); - addClassIfMatches(result, fullyQualifiedClassName, - baseClass); - } - } - } - - @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 - } - + public AllTestsFinder(Class klass) throws InitializationError { + super(klass, AbstractTB3Test.class, "com.vaadin", + new String[] { "com.vaadin.tests.integration" }); } } diff --git a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java index 8424abf2c9..dcfb8b6973 100644 --- a/uitest/src/com/vaadin/tests/tb3/TB3Runner.java +++ b/uitest/src/com/vaadin/tests/tb3/TB3Runner.java @@ -33,7 +33,7 @@ import org.junit.runners.model.Statement; import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.DesiredCapabilities; -import com.vaadin.tests.tb3.AllTB3Tests.ParallelScheduler; +import com.vaadin.tests.tb3.TB3TestFinder.ParallelScheduler; /** * This runner is loosely based on FactoryTestRunner by Ted Young diff --git a/uitest/src/com/vaadin/tests/tb3/TB3TestFinder.java b/uitest/src/com/vaadin/tests/tb3/TB3TestFinder.java new file mode 100644 index 0000000000..9eba031a7d --- /dev/null +++ b/uitest/src/com/vaadin/tests/tb3/TB3TestFinder.java @@ -0,0 +1,200 @@ +/* + * 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.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.jar.JarEntry; + +import org.junit.runners.Suite; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.RunnerScheduler; + +public class TB3TestFinder extends Suite { + + public static class ParallelScheduler implements RunnerScheduler { + private final List> fResults = new ArrayList>(); + + private final ExecutorService fService = Executors + .newCachedThreadPool(); + + @Override + public void schedule(final Runnable childStatement) { + fResults.add(fService.submit(new Callable() { + @Override + public Object call() throws Exception { + childStatement.run(); + return null; + } + })); + } + + @Override + public void finished() { + for (Future each : fResults) { + try { + each.get(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + /** + * @param klass + * @param suiteClasses + * @throws InitializationError + */ + public TB3TestFinder(Class klass, Class baseClass, + String basePackage, String[] ignorePackages) + throws InitializationError { + // Could consider reading settings from annoations on klass instead + // creating sub classes + super(klass, getAllTB3Tests(baseClass, basePackage, ignorePackages)); + setScheduler(new ParallelScheduler()); + } + + /** + * @since + * @return + */ + private static Class[] getAllTB3Tests(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; + } + + 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(); + findPackages(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; + } + + 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); + } + } + + } + + private static void findPackages(JarURLConnection juc, + String javaPackage, Class baseClass, + Collection> result) throws IOException { + String prefix = "com/vaadin/ui"; + Enumeration ent = juc.getJarFile().entries(); + while (ent.hasMoreElements()) { + JarEntry e = ent.nextElement(); + if (e.getName().endsWith(".class") + && e.getName().startsWith(prefix)) { + String fullyQualifiedClassName = e.getName().replace('/', '.') + .replace(".class", ""); + addClassIfMatches(result, fullyQualifiedClassName, baseClass); + } + } + } + + @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 -- 2.39.5