Skip to content

Commit

Permalink
Rework tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthepsy committed Jul 2, 2015
1 parent 6b43ae5 commit 6e443d0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,19 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package eu.arthepsy.sonar.plugins.scapegoat.rule;
package eu.arthepsy.sonar.plugins.scapegoat;

import eu.arthepsy.sonar.plugins.scapegoat.ScapegoatConfiguration;
import org.junit.Test;
import eu.arthepsy.sonar.plugins.scapegoat.util.ClassDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;

public class ScapegoatConfigurationTest {

@Test
public void testClassDefinition() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = ScapegoatConfiguration.class;
assertThat(Modifier.isFinal(clazz.getModifiers())).isTrue();
final Constructor<?> constructor = clazz.getDeclaredConstructor();
assertThat(constructor.isAccessible()).isFalse();
for (final Method method: clazz.getMethods()) {
if (method.getDeclaringClass().equals(clazz)) {
assertThat(Modifier.isStatic(method.getModifiers())).isTrue();
}
}
public void testClassDefinition() {
ClassDefinition.testFinalClassDefinition(ScapegoatConfiguration.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package eu.arthepsy.sonar.plugins.scapegoat.rule;
package eu.arthepsy.sonar.plugins.scapegoat;

import eu.arthepsy.sonar.plugins.scapegoat.ScapegoatPlugin;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package eu.arthepsy.sonar.plugins.scapegoat.rule;
package eu.arthepsy.sonar.plugins.scapegoat.language;

import eu.arthepsy.sonar.plugins.scapegoat.language.Scala;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Scapegoat
* Copyright (C) 2015 Andris Raugulis
* moo@arthepsy.eu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package eu.arthepsy.sonar.plugins.scapegoat.util;

import static org.junit.Assert.fail;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static org.fest.assertions.Assertions.assertThat;

public final class ClassDefinition {

public static void testFinalClassDefinition(Class<?> clazz) {
testFinalClassDefinition(clazz, false);
}

public static void testFinalClassDefinition(Class<?> clazz, Boolean privateConstructor) {
assertThat(Modifier.isFinal(clazz.getModifiers())).isTrue();
final Constructor<?> constructor;
try {
constructor = clazz.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
fail(e.getMessage());
return;
}
if (privateConstructor) {
assertThat(Modifier.isPrivate(constructor.getModifiers())).isTrue();
}
constructor.setAccessible(true);
try {
constructor.newInstance();
} catch (InstantiationException e) {
fail(e.getMessage());
return;
} catch (IllegalAccessException e) {
fail(e.getMessage());
return;
} catch (InvocationTargetException e) {
fail(e.getMessage());
return;
}
constructor.setAccessible(false);
for (final Method method: clazz.getMethods()) {
if (method.getDeclaringClass().equals(clazz)) {
assertThat(Modifier.isStatic(method.getModifiers())).isTrue();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Scapegoat
* Copyright (C) 2015 Andris Raugulis
* moo@arthepsy.eu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package eu.arthepsy.sonar.plugins.scapegoat.util;

import org.junit.Test;

public class XmlUtilsTest {

@Test
public void testClassDefinition() {
ClassDefinition.testFinalClassDefinition(XmlUtils.class);
}

}

0 comments on commit 6e443d0

Please sign in to comment.