Move tests for typeresolution and symboltable to java sub-module

This commit is contained in:
Andreas Dangel
2014-10-04 20:44:01 +02:00
parent 8165bc2bf5
commit e41b56a8e5
14 changed files with 2 additions and 1 deletions

View File

@ -0,0 +1,41 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.symboltable;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.pmd.lang.java.symboltable.Applier;
import net.sourceforge.pmd.util.UnaryFunction;
import org.junit.Test;
public class ApplierTest {
private static class MyFunction implements UnaryFunction<Object> {
private boolean gotCallback;
public void applyTo(Object o) {
this.gotCallback = true;
}
public boolean gotCallback() {
return this.gotCallback;
}
}
@Test
public void testSimple() {
MyFunction f = new MyFunction();
List<Object> l = new ArrayList<Object>();
l.add(new Object());
Applier.apply(f, l.iterator());
assertTrue(f.gotCallback());
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ApplierTest.class);
}
}

View File

@ -0,0 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution;
import java.util.Map;
/**
* This class is to verify that the inner class "Entry" of java.util.Map
* is imported correctly.
*/
public class ClassWithImportInnerOnDemand {
public void foo(Map<String, String> m) {
Map.Entry<String, String> e = m.entrySet().iterator().next();
assert e != null;
}
}

View File

@ -0,0 +1,11 @@
package net.sourceforge.pmd.typeresolution;
import java.util.ArrayList;
import java.util.List;
public class ClassWithImportOnDemand {
public List foo() {
return new ArrayList();
}
}

View File

@ -0,0 +1,122 @@
package net.sourceforge.pmd.typeresolution;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.Map;
import net.sourceforge.pmd.lang.java.typeresolution.PMDASMClassLoader;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class PMDASMClassLoaderTest {
private PMDASMClassLoader cl;
@Before
public void setUp() throws Exception {
cl = PMDASMClassLoader.getInstance(getClass().getClassLoader());
}
@Test
public void testLoadClassWithImportOnDemand() throws Exception {
String className = "net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand";
Class<?> clazz = cl.loadClass(className);
assertNotNull(clazz);
Map<String, String> imports = cl.getImportedClasses(className);
assertNotNull(imports);
assertEquals("java.util.List", imports.get("List"));
assertEquals("java.util.ArrayList", imports.get("ArrayList"));
assertEquals("java.lang.Object", imports.get("Object"));
assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportOnDemand", imports.get("ClassWithImportOnDemand"));
}
@Test
public void testClassWithImportInnerOnDemand() throws Exception {
String className = "net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand";
Class<?> clazz = cl.loadClass(className);
assertNotNull(clazz);
Map<String, String> imports = cl.getImportedClasses(className);
assertNotNull(imports);
assertEquals("java.util.Iterator", imports.get("Iterator"));
assertEquals("java.util.Map", imports.get("Map"));
assertEquals("java.util.Set", imports.get("Set"));
assertEquals("java.util.Map$Entry", imports.get("Entry"));
assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
assertEquals("java.util.Map$Entry", imports.get("Map$Entry"));
assertEquals("java.lang.Object", imports.get("Object"));
assertEquals("net.sourceforge.pmd.typeresolution.ClassWithImportInnerOnDemand", imports.get("ClassWithImportInnerOnDemand"));
}
/**
* Unit test for bug 3546093.
*
* @throws Exception any error
*/
@Test
public void testCachingOfNotFoundClasses() throws Exception {
MockedClassLoader mockedClassloader = new MockedClassLoader();
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassloader);
String notExistingClassname = "that.clazz.doesnot.Exist";
try {
cl.loadClass(notExistingClassname);
fail();
} catch (ClassNotFoundException e) {
// expected
}
try {
cl.loadClass(notExistingClassname);
fail();
} catch (ClassNotFoundException e) {
// expected
}
assertEquals(1, mockedClassloader.findClassCalls);
}
private static class MockedClassLoader extends ClassLoader {
int findClassCalls = 0;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
findClassCalls++;
return super.findClass(name);
}
}
/**
* With this test you can verify, how much memory could be consumed
* by the dontBother cache.
* @throws Exception any error
*/
@Ignore
@Test
public void testCachingMemoryConsumption() throws Exception {
MockedClassLoader mockedClassLoader = new MockedClassLoader();
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassLoader);
Runtime runtime = Runtime.getRuntime();
System.gc();
long usedBytesBefore = runtime.totalMemory() - runtime.freeMemory();
for (long i = 0; i < 3000; i++) {
try {
cl.loadClass("com.very.long.package.name.and.structure.MyClass" + i);
} catch (ClassNotFoundException e) {
// expected
}
}
long usedBytesAfter = runtime.totalMemory() - runtime.freeMemory();
System.out.println((usedBytesAfter - usedBytesBefore)/(1024.0*1024.0) + " mb needed");
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(PMDASMClassLoaderTest.class);
}
}

View File

@ -0,0 +1,18 @@
package net.sourceforge.pmd.typeresolution.testdata;
import java.util.AbstractList;
import java.util.List;
public class AnonymousInnerClass {
List<Object> list = new AbstractList<Object>() {
@Override
public Object get(int index) {
return null;
}
@Override
public int size() {
return 0;
}
};
}

View File

@ -0,0 +1,10 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
import java.util.ArrayList;
public class ArrayListFound {
ArrayList x;
}

View File

@ -0,0 +1,8 @@
package net.sourceforge.pmd.typeresolution.testdata;
public class DefaultJavaLangImport {
@Override
public String toString() {
return "foo";
}
}

View File

@ -0,0 +1,9 @@
package net.sourceforge.pmd.typeresolution.testdata;
public enum EnumWithAnonymousInnerClass {
A;
interface Inner { int get(); }
private static final Inner VAL = new Inner() {
@Override public int get() { return 1; }
};
}

View File

@ -0,0 +1,7 @@
package net.sourceforge.pmd.typeresolution.testdata;
public class ExtraTopLevelClass {
}
class TheExtraTopLevelClass {
}

View File

@ -0,0 +1,9 @@
package net.sourceforge.pmd.typeresolution.testdata;
public class InnerClass {
public class TheInnerClass {
}
public void foo(TheInnerClass arg) {
}
}

View File

@ -0,0 +1,42 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
public class Literals {
String s = "s";
boolean boolean1 = false;
boolean boolean2 = true;
Object obj = null;
byte byte1 = 0;
byte byte2 = 0x0F;
byte byte3 = -007;
short short1 = 0;
short short2 = 0x0F;
short short3 = -007;
char char1 = 0;
char char2 = 0x0F;
char char3 = 007;
char char4 = 'a';
int int1 = 0;
int int2 = 0x0F;
int int3 = -007;
int int4 = 'a';
long long1 = 0;
long long2 = 0x0F;
long long3 = -007;
long long4 = 0L;
long long5 = 0x0Fl;
long long6 = -007L;
long long7 = 'a';
float float1 = 0.0f;
float float2 = -10e+01f;
float float3 = 0x08.08p3f;
float float4 = 0xFF;
float float5 = 'a';
double double1 = 0.0;
double double2 = -10e+01;
double double3 = 0x08.08p3;
double double4 = 0xFF;
double double5 = 'a';
}

View File

@ -0,0 +1,65 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
import java.util.List;
public class Operators {
public void unaryLogicalOperators() {
boolean t;
t = !true;
t = !false;
}
public void binaryLogicalOperators() {
boolean t;
t = true | false;
t = true & false;
t = true ^ false;
t = true && false;
t = true || false;
t = 1 > 1;
t = 1 >= 1;
t = 1 == 1;
t = 1 != 1;
t = 1 <= 1;
t = 1 < 1;
t = this instanceof List;
t = this instanceof Operators;
}
public void unaryNumericOperators() {
double t;
t = +1;
t = -1;
t++;
t--;
++t;
--t;
}
public void binaryNumericOperators() {
long t;
t = 1 + 1;
t = 1 - 1;
t = 1 / 1;
t = 1 * 1;
t = 1 % 1;
t = 1 << 1;
t = 1 >> 1;
t = 1 >>> 1;
}
public void assignmentOperators() {
long t;
t = 1;
t *= 1;
t /= 1;
t %= 1;
t += 1;
t -= 1;
t <<= 1;
t >>= 1;
t >>>= 1;
t &= 1;
t ^= 1;
t |= 1;
}
}

View File

@ -0,0 +1,80 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
public class Promotion {
public void unaryNumericPromotion() {
double t;
t = +((byte)1);
t = +((short)1);
t = +((char)1);
t = +((int)1);
t = +((long)1);
t = +((float)1);
t = +((double)1);
}
public void binaryNumericPromotion() {
double t;
t = ((byte)1) + ((byte)2);
t = ((byte)1) + ((short)2);
t = ((byte)1) + ((char)2);
t = ((byte)1) + ((int)2);
t = ((byte)1) + ((long)2);
t = ((byte)1) + ((float)2);
t = ((byte)1) + ((double)2);
t = ((short)1) + ((byte)2);
t = ((short)1) + ((short)2);
t = ((short)1) + ((char)2);
t = ((short)1) + ((int)2);
t = ((short)1) + ((long)2);
t = ((short)1) + ((float)2);
t = ((short)1) + ((double)2);
t = ((char)1) + ((byte)2);
t = ((char)1) + ((short)2);
t = ((char)1) + ((char)2);
t = ((char)1) + ((int)2);
t = ((char)1) + ((long)2);
t = ((char)1) + ((float)2);
t = ((char)1) + ((double)2);
t = ((int)1) + ((byte)2);
t = ((int)1) + ((short)2);
t = ((int)1) + ((char)2);
t = ((int)1) + ((int)2);
t = ((int)1) + ((long)2);
t = ((int)1) + ((float)2);
t = ((int)1) + ((double)2);
t = ((long)1) + ((byte)2);
t = ((long)1) + ((short)2);
t = ((long)1) + ((char)2);
t = ((long)1) + ((int)2);
t = ((long)1) + ((long)2);
t = ((long)1) + ((float)2);
t = ((long)1) + ((double)2);
t = ((float)1) + ((byte)2);
t = ((float)1) + ((short)2);
t = ((float)1) + ((char)2);
t = ((float)1) + ((int)2);
t = ((float)1) + ((long)2);
t = ((float)1) + ((float)2);
t = ((float)1) + ((double)2);
t = ((double)1) + ((byte)2);
t = ((double)1) + ((short)2);
t = ((double)1) + ((char)2);
t = ((double)1) + ((int)2);
t = ((double)1) + ((long)2);
t = ((double)1) + ((float)2);
t = ((double)1) + ((double)2);
}
public void binaryStringPromotion() {
String t;
t = "" + 0;
t = 0 + "";
t = "" + "";
t = "" + null;
t = null + "";
}
}