Skip to content

Commit

Permalink
Issue #40: Add JUnit tests for glob functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed Apr 10, 2015
1 parent 62ff5f9 commit bb28186
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
37 changes: 31 additions & 6 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<property name="groovy-tools.dir" location="src/tools/groovy"/>
<property name="maven-build-dir" location="build/maven"/>
<property name="documentation-dir" location="build/docs"/>
<property name="test-results-dir" location="build/test-results"/>

<property name="cli-classname" value="de.thetaphi.forbiddenapis.CliMain"/>

Expand Down Expand Up @@ -171,6 +172,14 @@
<path refid="path.run"/>
<pathelement path="build/tools"/>
</path>
<path id="path.junit-build">
<path refid="path.test"/>
<path refid="path.run"/>
</path>
<path id="path.junit-run">
<path refid="path.junit-build"/>
<pathelement path="build/test"/>
</path>
</target>

<target name="-install-tasks" depends="-init" unless="installed.tasks">
Expand Down Expand Up @@ -206,6 +215,10 @@
<compile module="tools" classpathref="path.run"/>
</target>

<target name="compile-test" depends="compile" description="Compile tests">
<compile module="test" classpathref="path.junit-build"/>
</target>

<target name="clean" description="Clean">
<delete dir="build"/>
<delete dir="dist"/>
Expand Down Expand Up @@ -396,7 +409,7 @@
</artifact:deploy>
</target>

<target name="test" depends="-check-myself,-check-bundled-signatures,-antunit,-runtime-unsupported" description="Run tests"/>
<target name="test" depends="-check-myself,-check-bundled-signatures,-junit,-antunit,-runtime-unsupported" description="Run tests"/>

<target name="check-bundled-signatures" depends="-check-bundled-signatures,-runtime-unsupported"
description="Checks bundled signatures file corresponding to the current JVM. Run after every update!"/>
Expand Down Expand Up @@ -435,27 +448,27 @@
</target>

<target name="-antunit-fake-pom">
<mkdir dir="build/test"/>
<mkdir dir="build/test-maven"/>
<local name="path.build.xslstr"/>
<pathconvert property="path.build.xslstr" pathsep="|" refid="path.build"/>
<xslt in="src/test/antunit/dummy.xml" out="build/test/fake-pom.xml"
<xslt in="src/test/antunit/dummy.xml" out="build/test-maven/fake-pom.xml"
style="src/test/antunit/pom-generator.xsl" force="true"
>
<outputproperty name="indent" value="yes"/>
<param name="jarfiles" expression="${path.build.xslstr}"/>
</xslt>
<property name="antunit.fake-pom" location="build/test/fake-pom.xml"/>
<property name="antunit.fake-pom" location="build/test-maven/fake-pom.xml"/>
</target>

<target name="-antunit" depends="install-maven-artifacts,-install-forbiddenapi-task,-install-antunit,-antunit-fake-pom" if="tests.supported">
<mkdir dir="build/test-results"/>
<mkdir dir="${test-results-dir}"/>
<property name="antunit.main.classes" location="build/main"/>
<property name="antunit.tools.classes" location="build/tools"/>
<property name="antunit.maven3.version" value="3.0.4"/>
<au:antunit>
<fileset dir="src/test/antunit" includes="Test*.xml"/>
<au:plainlistener/>
<au:xmllistener toDir="build/test-results" logLevel="info"/>
<au:xmllistener toDir="${test-results-dir}" logLevel="info"/>
<propertyset>
<propertyref prefix="antunit."/>
<propertyref name="jar-file"/>
Expand All @@ -470,6 +483,18 @@
</au:antunit>
</target>

<target name="-junit" depends="compile-test">
<mkdir dir="${test-results-dir}"/>
<junit haltonfailure="yes" includeantruntime="no">
<classpath refid="path.junit-run"/>
<formatter type="brief" usefile="false" />
<formatter type="xml"/>
<batchtest fork="no" todir="${test-results-dir}">
<fileset dir="src/test/java" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>

<target name="-runtime-unsupported" unless="tests.supported">
<echo level="warn" message="Test suite / bundled signatures checker was not executed, because the used JVM version is unsupported: ${build.java.info}"/>
</target>
Expand Down
2 changes: 2 additions & 0 deletions ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.2.2" conf="buildtools"/>
<dependency org="org.apache.ant" name="ant-antunit" rev="1.3" conf="test"/>
<dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" conf="test"/>
<dependency org="junit" name="junit" rev="4.12" conf="test"/>
<dependency org="org.hamcrest" name="hamcrest-core" rev="1.3" conf="test"/>
<exclude org="*" ext="*" matcher="regexp" type="source|javadoc"/>
</dependencies>
</ivy-module>
73 changes: 73 additions & 0 deletions src/test/java/de/thetaphi/forbiddenapis/AsmUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package de.thetaphi.forbiddenapis;

/*
* (C) Copyright Uwe Schindler (Generics Policeman) and others.
*
* 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.
*/

import static de.thetaphi.forbiddenapis.AsmUtils.*;
import static org.junit.Assert.*;

import java.util.regex.Pattern;

import org.junit.Test;

public final class AsmUtilsTest {

@Test
public void testIsGlob() {
assertTrue(isGlob("a.b.c.*"));
assertTrue(isGlob("sun.**"));
assertTrue(isGlob("a?bc.x"));
assertFalse(isGlob(Object.class.getName()));
assertFalse(isGlob(getClass().getName()));
assertFalse(isGlob("sun.misc.Unsafe$1"));
}

@Test
public void testGlob() {
Pattern pat = glob2Pattern("a.b.c.*");
assertTrue(pat.matcher("a.b.c.d").matches());
assertTrue(pat.matcher("a.b.c.def").matches());
assertFalse(pat.matcher("a.b.c").matches());
assertFalse(pat.matcher("a.b.c.d.e").matches());

pat = glob2Pattern("a.b.c.**");
assertTrue(pat.matcher("a.b.c.d").matches());
assertTrue(pat.matcher("a.b.c.def").matches());
assertTrue(pat.matcher("a.b.c.d.e").matches());
assertTrue(pat.matcher("a.b.c.d.e.f").matches());

pat = glob2Pattern("sun.*.*");
assertTrue(pat.matcher("sun.misc.Unsafe").matches());
assertTrue(pat.matcher("sun.misc.Unsafe$1").matches());
assertFalse(pat.matcher("sun.misc.Unsafe.xy").matches());

pat = glob2Pattern("java.**.Array?");
assertTrue(pat.matcher("java.util.Arrays").matches());
assertFalse(pat.matcher("java.util.ArrayList").matches());
assertFalse(pat.matcher("java.util.Array").matches());
assertTrue(pat.matcher("java.lang.reflect.Arrays").matches());
}

@Test
public void testInternalRuntime() {
assertTrue(isInternalClass("sun.misc.Unsafe"));
assertTrue(isInternalClass("jdk.internal.Asm"));
assertTrue(isInternalClass("sun.misc.Unsafe$1"));
assertFalse(isInternalClass(Object.class.getName()));
assertFalse(isInternalClass(getClass().getName()));
}

}

0 comments on commit bb28186

Please sign in to comment.