diff options
5 files changed, 154 insertions, 41 deletions
diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java index c86866b591..38915fe3e2 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AbstractDirectoryScanningSassTests.java @@ -31,25 +31,17 @@ import junit.framework.TestCase; import org.apache.commons.io.IOUtils; import org.junit.Assert; -import org.junit.Test; import com.vaadin.sass.internal.ScssStylesheet; +import com.vaadin.sass.testcases.scss.SassTestRunner.FactoryTest; public abstract class AbstractDirectoryScanningSassTests extends TestCase { - private String scssResourceName; - - protected AbstractDirectoryScanningSassTests(String scssResourceName) { - this.scssResourceName = scssResourceName; - } - - public static Collection<Object[]> getScssResourceNames(URL directoryUrl) + public static Collection<String> getScssResourceNames(URL directoryUrl) throws URISyntaxException { - List<Object[]> resources = new ArrayList<Object[]>(); - // temporary instance to enable subclasses to define where to scan for - // files + List<String> resources = new ArrayList<String>(); for (File scssFile : getScssFiles(directoryUrl)) { - resources.add(new Object[] { scssFile.getName() }); + resources.add(scssFile.getName()); } return resources; } @@ -72,8 +64,8 @@ public abstract class AbstractDirectoryScanningSassTests extends TestCase { protected abstract URL getResourceURL(String path); - @Test - public void compareScssWithCss() throws Exception { + @FactoryTest + public void compareScssWithCss(String scssResourceName) throws Exception { String referenceCss; File scssFile = getSassLangResourceFile(scssResourceName); File cssFile = getCssFile(scssFile); diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AutomaticSassTests.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AutomaticSassTests.java index 1e5ec1ad37..fbccae349a 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AutomaticSassTests.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/AutomaticSassTests.java @@ -20,15 +20,11 @@ import java.net.URL; import java.util.Collection; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -@RunWith(Parameterized.class) -public class AutomaticSassTests extends AbstractDirectoryScanningSassTests { +import com.vaadin.sass.testcases.scss.SassTestRunner.TestFactory; - public AutomaticSassTests(String scssResourceName) { - super(scssResourceName); - } +@RunWith(SassTestRunner.class) +public class AutomaticSassTests extends AbstractDirectoryScanningSassTests { @Override protected URL getResourceURL(String path) { @@ -39,8 +35,8 @@ public class AutomaticSassTests extends AbstractDirectoryScanningSassTests { return AutomaticSassTests.class.getResource("/automatic" + path); } - @Parameters - public static Collection<Object[]> getScssResourceNames() + @TestFactory + public static Collection<String> getScssResourceNames() throws URISyntaxException { return getScssResourceNames(getResourceURLInternal("")); } diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTests.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTests.java index 7aabb9d23c..d0e53a8726 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTests.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTests.java @@ -20,15 +20,11 @@ import java.net.URL; import java.util.Collection; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -@RunWith(Parameterized.class) -public class SassLangTests extends AbstractDirectoryScanningSassTests { +import com.vaadin.sass.testcases.scss.SassTestRunner.TestFactory; - public SassLangTests(String scssResourceName) { - super(scssResourceName); - } +@RunWith(SassTestRunner.class) +public class SassLangTests extends AbstractDirectoryScanningSassTests { @Override protected URL getResourceURL(String path) { @@ -39,8 +35,8 @@ public class SassLangTests extends AbstractDirectoryScanningSassTests { return SassLangTests.class.getResource("/sasslang" + path); } - @Parameters - public static Collection<Object[]> getScssResourceNames() + @TestFactory + public static Collection<String> getScssResourceNames() throws URISyntaxException { return getScssResourceNames(getResourceURLInternal("")); } diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTestsBroken.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTestsBroken.java index 6e9ed007cd..a84a8ca814 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTestsBroken.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassLangTestsBroken.java @@ -20,15 +20,11 @@ import java.net.URL; import java.util.Collection; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -@RunWith(Parameterized.class) -public class SassLangTestsBroken extends AbstractDirectoryScanningSassTests { +import com.vaadin.sass.testcases.scss.SassTestRunner.TestFactory; - public SassLangTestsBroken(String scssResourceName) { - super(scssResourceName); - } +@RunWith(SassTestRunner.class) +public class SassLangTestsBroken extends AbstractDirectoryScanningSassTests { @Override protected URL getResourceURL(String path) { @@ -39,8 +35,8 @@ public class SassLangTestsBroken extends AbstractDirectoryScanningSassTests { return SassLangTestsBroken.class.getResource("/sasslangbroken" + path); } - @Parameters - public static Collection<Object[]> getScssResourceNames() + @TestFactory + public static Collection<String> getScssResourceNames() throws URISyntaxException { return getScssResourceNames(getResourceURLInternal("")); } diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassTestRunner.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassTestRunner.java new file mode 100644 index 0000000000..f871d43b6c --- /dev/null +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/SassTestRunner.java @@ -0,0 +1,133 @@ +/* + * Copyright 2012 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.sass.testcases.scss; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.Parameterized; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; + +/** + * Test runner that executes methods annotated with @{@link FactoryTest} with + * all the values returned by a method annotated with @{@link TestFactory} as + * their parameters parameter. + * + * This runner is loosely based on FactoryTestRunner by Ted Young + * (http://tedyoung.me/2011/01/23/junit-runtime-tests-custom-runners/). The + * generated test names give information about the parameters used (unlike + * {@link Parameterized}). + * + * @since 7.0 + */ +public class SassTestRunner extends BlockJUnit4ClassRunner { + + @Target(ElementType.METHOD) + @Retention(RetentionPolicy.RUNTIME) + public @interface TestFactory { + } + + @Target(ElementType.METHOD) + @Retention(RetentionPolicy.RUNTIME) + public @interface FactoryTest { + } + + public SassTestRunner(Class<?> klass) throws InitializationError { + super(klass); + } + + @Override + protected List<FrameworkMethod> computeTestMethods() { + List<FrameworkMethod> tests = new LinkedList<FrameworkMethod>(); + + // Final all methods in our test class marked with @TestFactory. + for (FrameworkMethod method : getTestClass().getAnnotatedMethods( + TestFactory.class)) { + // Make sure the TestFactory method is static + if (!Modifier.isStatic(method.getMethod().getModifiers())) { + throw new IllegalArgumentException("TestFactory " + method + + " must be static."); + } + + // Execute the method (statically) + Object params; + try { + params = method.getMethod().invoke( + getTestClass().getJavaClass()); + } catch (Throwable t) { + throw new RuntimeException("Could not run test factory method " + + method.getName()); + } + + // Did the factory return an array? If so, make it a list. + if (params.getClass().isArray()) { + params = Arrays.asList((Object[]) params); + } + + // Did the factory return a scalar object? If so, put it in a list. + if (!(params instanceof Iterable<?>)) { + params = Collections.singletonList(params); + } + + // For each object returned by the factory. + for (Object param : (Iterable<?>) params) { + // Find any methods marked with @SassTest. + for (FrameworkMethod m : getTestClass().getAnnotatedMethods( + FactoryTest.class)) { + tests.add(new ParameterizedFrameworkMethod(m.getMethod(), + new Object[] { param })); + } + } + } + + return tests; + } + + private static class ParameterizedFrameworkMethod extends FrameworkMethod { + private Object[] params; + + public ParameterizedFrameworkMethod(Method method, Object[] params) { + super(method); + this.params = params; + } + + @Override + public Object invokeExplosively(Object target, Object... params) + throws Throwable { + // Executes the test method with the supplied parameters (returned + // by the + // TestFactory) and not the instance generated by FrameworkMethod. + return super.invokeExplosively(target, this.params); + } + + @Override + public String getName() { + return String.format("%s[%s]", getMethod().getName(), + Arrays.toString(params)); + } + } +} |