aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-09-30 00:07:03 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-09-30 00:07:03 +0200
commitc6dab978b4e06918c7b96684a06c50c8aca74315 (patch)
treed16b803b094b8984ad722d87ae23d709da37f9df /sonar-testing-harness/src
parent73e2ceadeb3d0f44d3a96a07947e26531c409053 (diff)
downloadsonarqube-c6dab978b4e06918c7b96684a06c50c8aca74315.tar.gz
sonarqube-c6dab978b4e06918c7b96684a06c50c8aca74315.zip
Maven profiles for each technical stack
and remove sonar-channel helpers from sonar-testing-harness
Diffstat (limited to 'sonar-testing-harness/src')
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/api/server/ws/package-info.java22
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java29
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java57
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java40
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java46
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/package-info.java24
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java15
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java44
8 files changed, 76 insertions, 201 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/api/server/ws/package-info.java b/sonar-testing-harness/src/main/java/org/sonar/api/server/ws/package-info.java
deleted file mode 100644
index a216dca3631..00000000000
--- a/sonar-testing-harness/src/main/java/org/sonar/api/server/ws/package-info.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.api.server.ws;
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
index 0a5d8fcaef0..c8522ec3ed8 100644
--- a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
@@ -27,9 +27,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.URL;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.Fail.fail;
-
/**
* Utilities for unit tests
*
@@ -73,14 +70,24 @@ public final class TestUtils {
return getResource(resourcePath);
}
- public static void assertPrivateConstructor(Class clazz) {
- try {
- Constructor constructor = clazz.getDeclaredConstructor();
- assertThat(Modifier.isPrivate(constructor.getModifiers())).isTrue();
- constructor.setAccessible(true);
- constructor.newInstance();
- } catch (Exception e) {
- fail("Fail to instantiate " + clazz, e);
+ /**
+ * Asserts that all constructors are private, usually for helper classes with
+ * only static methods. If a constructor does not have any parameters, then
+ * it's instantiated.
+ */
+ public static boolean hasOnlyPrivateConstructors(Class clazz) {
+ boolean ok = true;
+ for (Constructor constructor : clazz.getDeclaredConstructors()) {
+ ok &= Modifier.isPrivate(constructor.getModifiers());
+ if (constructor.getParameterTypes().length == 0) {
+ constructor.setAccessible(true);
+ try {
+ constructor.newInstance();
+ } catch (Exception e) {
+ throw new IllegalStateException(String.format("Fail to instantiate %s", clazz), e);
+ }
+ }
}
+ return ok;
}
}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java
deleted file mode 100644
index c262cbca65e..00000000000
--- a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.test.channel;
-
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.sonar.channel.Channel;
-import org.sonar.channel.CodeReader;
-
-public class ChannelMatcher<O> extends BaseMatcher<Channel<O>> {
-
- private final String sourceCode;
- private final O output;
- private final CodeReader reader;
-
- public ChannelMatcher(String sourceCode, O output) {
- this.sourceCode = sourceCode;
- this.output = output;
- this.reader = new CodeReader(sourceCode);
- }
-
- public ChannelMatcher(CodeReader reader, O output) {
- this.output = output;
- this.sourceCode = new String(reader.peek(30));
- this.reader = reader;
- }
-
- public boolean matches(Object arg0) {
- if ( !(arg0 instanceof Channel)) {
- return false;
- }
- Channel<O> channel = (Channel<O>) arg0;
- return channel.consume(reader, output);
- }
-
- public void describeTo(Description description) {
- description.appendText("Channel consumes '" + sourceCode + "'");
- }
-
-}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java
deleted file mode 100644
index b3bce24c591..00000000000
--- a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.test.channel;
-
-import org.sonar.channel.CodeReader;
-
-public final class ChannelMatchers {
-
- private ChannelMatchers() {
- }
-
- public static <O> ChannelMatcher<O> consume(String sourceCode, O output) {
- return new ChannelMatcher<O>(sourceCode, output);
- }
-
- public static <O> ChannelMatcher<O> consume(CodeReader codeReader, O output) {
- return new ChannelMatcher<O>(codeReader, output);
- }
-
- public static ReaderHasNextCharMatcher hasNextChar(char nextChar) {
- return new ReaderHasNextCharMatcher(nextChar);
- }
-}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java
deleted file mode 100644
index 477dc89a29b..00000000000
--- a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.test.channel;
-
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.sonar.channel.CodeReader;
-
-public class ReaderHasNextCharMatcher extends BaseMatcher<CodeReader> {
-
- private final char nextChar;
-
- public ReaderHasNextCharMatcher(char nextChar) {
- this.nextChar = nextChar;
- }
-
- public boolean matches(Object arg0) {
- if ( !(arg0 instanceof CodeReader)) {
- return false;
- }
- CodeReader reader = (CodeReader) arg0;
- return reader.peek() == nextChar;
- }
-
- public void describeTo(Description description) {
- description.appendText("next char is '" + nextChar + "'");
- }
-
-}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/package-info.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/package-info.java
deleted file mode 100644
index 482c6191040..00000000000
--- a/sonar-testing-harness/src/main/java/org/sonar/test/channel/package-info.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-@ParametersAreNonnullByDefault
-package org.sonar.test.channel;
-
-import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java
index 3bc97ec8e44..360685bec9b 100644
--- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/I18nMatchers.java
@@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils;
import org.sonar.test.TestUtils;
import java.io.File;
+import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -49,7 +50,7 @@ public final class I18nMatchers {
* Checks that all the translation bundles found on the classpath are up to date with the corresponding default ones found in the classpath.
*/
public static void assertBundlesUpToDate() {
- File bundleFolder = TestUtils.getResource(BundleSynchronizedMatcher.L10N_PATH);
+ File bundleFolder = getResource(BundleSynchronizedMatcher.L10N_PATH);
if (bundleFolder == null || !bundleFolder.isDirectory()) {
fail("No bundle found in: " + BundleSynchronizedMatcher.L10N_PATH);
}
@@ -77,4 +78,16 @@ public final class I18nMatchers {
fail(message.toString());
}
}
+
+ private static File getResource(String path) {
+ String resourcePath = path;
+ if (!resourcePath.startsWith("/")) {
+ resourcePath = "/" + resourcePath;
+ }
+ URL url = TestUtils.class.getResource(resourcePath);
+ if (url != null) {
+ return FileUtils.toFile(url);
+ }
+ return null;
+ }
}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
index 54a0d903357..f06d434441c 100644
--- a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
@@ -24,6 +24,7 @@ import org.junit.Test;
import java.io.File;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.sonar.test.TestUtils.getResource;
public class TestUtilsTest {
@@ -45,4 +46,47 @@ public class TestUtilsTest {
File file = getResource("org/sonar/test/TestUtilsTest/unknown.txt");
assertThat(file).isNull();
}
+
+ @Test
+ public void hasOnlyPrivateConstructors() {
+ assertThat(TestUtils.hasOnlyPrivateConstructors(TestUtils.class)).isTrue();
+ assertThat(TestUtils.hasOnlyPrivateConstructors(OnlyPrivateConstructors.class)).isTrue();
+ assertThat(TestUtils.hasOnlyPrivateConstructors(MixOfPublicAndPrivateConstructors.class)).isFalse();
+ try {
+ TestUtils.hasOnlyPrivateConstructors(FailToInstantiate.class);
+ fail();
+ } catch (IllegalStateException e) {
+ // ok
+ }
+ }
+
+ public static class OnlyPrivateConstructors {
+ private OnlyPrivateConstructors() {
+ }
+
+ private OnlyPrivateConstructors(int i) {
+ }
+
+ public static void foo() {
+
+ }
+ }
+
+ public static class MixOfPublicAndPrivateConstructors {
+ private MixOfPublicAndPrivateConstructors() {
+ }
+
+ public MixOfPublicAndPrivateConstructors(int i) {
+ }
+
+ public static void foo() {
+
+ }
+ }
+
+ public static class FailToInstantiate {
+ private FailToInstantiate() {
+ throw new IllegalArgumentException();
+ }
+ }
}