How to load many classes in the TestFramework ?

In the test framework,

configureFromFileText("Logger.java", LOGGER_CLASS);


would let you load 1 class, that you could later retrieve with

GlobalSearchScope resolveScope = element.getResolveScope();
PsiClass loggerObject = manager.findClass("spike.Logger",
resolveScope),


I tried calling configureFromFileText(..) many times, but only the last
call has an effect.


Question:
-


How to "load" additional (non-inner) classes in the system, that you
could later retrieve with findClass.


My goal :
-


I want to load fake
org.apache.log4j.Logger
org.apache.log4j.Level
org.apache.log4j.Category


, to further write code that will recognize this pattern.

> if (aLogger.isDebugEnabled()) { > aLogger.log(Level.DEBUG, "This is a log message" ); >]]> }


Alain

5 comments
Comment actions Permalink

We have such a possibility internally but this is not open unfortunately.

--

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


"Alain Ravet" <alain.ravet.list@wanadoo.be> wrote in message
news:bm3rrn$11m$1@is.intellij.net...

In the test framework,

>

configureFromFileText("Logger.java", LOGGER_CLASS);

>
>

would let you load 1 class, that you could later retrieve with

>

GlobalSearchScope resolveScope = element.getResolveScope();
PsiClass loggerObject = manager.findClass("spike.Logger",
resolveScope),

>
>

I tried calling configureFromFileText(..) many times, but only the last
call has an effect.

>
>

Question:
----------
How to "load" additional (non-inner) classes in the system, that you
could later retrieve with findClass.

>
>

My goal :
---------
I want to load fake
org.apache.log4j.Logger
org.apache.log4j.Level
org.apache.log4j.Category

>
>

, to further write code that will recognize this pattern.

>

> if (aLogger.isDebug<caret>Enabled()) {
> aLogger.log(Level.DEBUG, "This is a log message" );
> }

>
>

Alain

>


0
Comment actions Permalink

Maxim Shafirov (JetBrains) wrote:

>We have such a possibility internally but this is not open unfortunately.

>

Any hope you would open it soon?

The only trick I've found is to use inner classes, but it's butt-ugly,
and not really what I seek.

Alain

0
Comment actions Permalink

Alain Ravet wrote:

The only trick I've found is to use inner classes, but it's butt-ugly,
and not really what I seek.




Well, I spoke too fast : inner classes don't seem to work in the -
public version of the - test framework :

InheritanceUtil.isInheritorOrSelf(innerClass, outerClass, true);

generates an odd stacktrace :

isInheritor 1 = true
isInheritor 2 = true

java.lang.NoClassDefFoundError:
org/apache/tools/ant/filters/StringInputStream
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
...

(see the code below)


Too bad, back to the old, pre-testframework darkage days.

Gosh.

Alain





package spike;

import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.LightCodeInsightTestCase;
import ravet.intellij.plugins.commons.U;

public class DetectLog4JInIfStatement extends LightCodeInsightTestCase
{
final static String
LOGGER_CLASS =
"package spike;\n\n" +
"public class Logger {\n" +
" public void test() {\n\n" +
" if (LOG.isDebugE]]>nabled()) {\n" +
" LOG.log(spike.Logger.Level.DEBUG, \"This is
a log message\" );\n" +
" }\n" +
" }\n" +
" private void log(spike.Logger.Level i_debug, String
s) { }\n" +
" private static spike.Logger LOG = new
spike.Logger();\n" +
" public boolean isDebugEnabled() { return
false; }\n" +
"\n" +
" public static class Level extends spike.Logger {\n" +
" public static Level DEBUG = new Level();\n" +
" }\n" +
"}\n" ;

public void testGetClassChild() throws Exception
{
configureFromFileText("Logger.java", LOGGER_CLASS);
PsiManager manager = getPsiManager();

PsiElement element = U.elementAtCaret(getEditor(), getFile());
PsiIfStatement ifStmt = U.enclosingIfStatement(element);

GlobalSearchScope resolveScope = element.getResolveScope();
PsiClass loggerObject = manager.findClass("spike.Logger",
resolveScope),
levelObject =
manager.findClass("spike.Logger.Level", resolveScope);

assertNotNull( levelObject);
assertNotNull( loggerObject);

boolean isInheritor;
// WORKS FINE
isInheritor = InheritanceUtil.isInheritorOrSelf(levelObject,
levelObject, true);
System.out.println("isInheritor 1 = " + isInheritor);

isInheritor = InheritanceUtil.isInheritorOrSelf(loggerObject,
loggerObject, true);
System.out.println("isInheritor 2 = " + isInheritor);
// FAILS and generates stacktrace/
isInheritor = InheritanceUtil.isInheritorOrSelf(levelObject,
loggerObject, true);
}


0
Comment actions Permalink

Hi Alain!

Please send us a full stacktrace. It seems that ant.jar in your tests
classpath could help. But to be sure I need detailed stacktrace or your
utility classes.

Thanks,

IK

public version of the - test framework :

InheritanceUtil.isInheritorOrSelf(innerClass, outerClass, true);

generates an odd stacktrace :

isInheritor 1 = true
isInheritor 2 = true

java.lang.NoClassDefFoundError:
org/apache/tools/ant/filters/StringInputStream
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
...

(see the code below)


Too bad, back to the old, pre-testframework darkage days.

Gosh.

Alain





package spike;

import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.LightCodeInsightTestCase;
import ravet.intellij.plugins.commons.U;

public class DetectLog4JInIfStatement extends LightCodeInsightTestCase
{
final static String
LOGGER_CLASS =
"package spike;\n\n" +
"public class Logger {\n" +
" public void test() {\n\n" +
" if (LOG.isDebugE<caret>nabled()) {\n" +
" LOG.log(spike.Logger.Level.DEBUG, \"This is
a log message\" );\n" +
" }\n" +
" }\n" +
" private void log(spike.Logger.Level i_debug, String
s) { }\n" +
" private static spike.Logger LOG = new
spike.Logger();\n" +
" public boolean isDebugEnabled() { return
false; }\n" +
"\n" +
" public static class Level extends spike.Logger {\n" +
" public static Level DEBUG = new Level();\n" +
" }\n" +
"}\n" ;

public void testGetClassChild() throws Exception
{
configureFromFileText("Logger.java", LOGGER_CLASS);
PsiManager manager = getPsiManager();

PsiElement element = U.elementAtCaret(getEditor(), getFile());
PsiIfStatement ifStmt = U.enclosingIfStatement(element);

GlobalSearchScope resolveScope = element.getResolveScope();
PsiClass loggerObject = manager.findClass("spike.Logger",
resolveScope),
levelObject =
manager.findClass("spike.Logger.Level", resolveScope);

assertNotNull( levelObject);
assertNotNull( loggerObject);

boolean isInheritor;
// WORKS FINE
isInheritor = InheritanceUtil.isInheritorOrSelf(levelObject,
levelObject, true);
System.out.println("isInheritor 1 = " + isInheritor);

isInheritor = InheritanceUtil.isInheritorOrSelf(loggerObject,
loggerObject, true);
System.out.println("isInheritor 2 = " + isInheritor);
// FAILS and generates stacktrace/
isInheritor = InheritanceUtil.isInheritorOrSelf(levelObject,
loggerObject, true);
}



--
Igor Kuralenok
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Igor Kuralenok wrote:

>Please send us a full stacktrace. It seems that ant.jar in your tests
>classpath could help.
>

Duh. Shame on me. Of course it helped.

Anyway, here is the full stacktrace, in case it's still useful to you.

Alain

java.lang.NoClassDefFoundError:
org/apache/tools/ant/filters/StringInputStream
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at com.intellij.psi.f.a.a(a.java:23)
at com.intellij.psi.f.a.]]>(a.java:1)
at com.intellij.psi.impl.source.bj.j(bj.java:54)
at com.intellij.psi.scope.h.a(h.java:61)
at com.intellij.psi.scope.h.a(h.java:128)
at com.intellij.psi.scope.h.a(h.java:1)
at com.intellij.psi.scope.h.a(h.java:175)
at com.intellij.psi.impl.source.bc.e(bc.java:333)
at com.intellij.psi.impl.source.bc.e(bc.java:4)
at com.intellij.psi.impl.source.bc.a(bc.java:173)
at com.intellij.psi.impl.source.y.b(y.java:0)
at com.intellij.psi.impl.source.y.a(y.java:10)
at com.intellij.psi.impl.source.e.c.a(c.java:28)
at com.intellij.psi.impl.source.bc.multiResolve(bc.java:313)
at com.intellij.psi.impl.source.bc.advancedResolve(bc.java:302)
at com.intellij.psi.scope.h.a(h.java:93)
at com.intellij.psi.scope.h.a(h.java:175)
at com.intellij.psi.impl.source.bc.e(bc.java:333)
at com.intellij.psi.impl.source.bc.a(bc.java:173)
at com.intellij.psi.impl.source.y.b(y.java:0)
at com.intellij.psi.impl.source.y.a(y.java:10)
at com.intellij.psi.impl.source.e.c.a(c.java:28)
at com.intellij.psi.impl.source.bc.multiResolve(bc.java:313)
at com.intellij.psi.impl.source.bc.advancedResolve(bc.java:302)
at com.intellij.psi.impl.source.cc.resolveGenerics(cc.java)
at com.intellij.psi.impl.source.cc.resolve(cc.java:14)
at com.intellij.psi.impl.ba.f(ba.java:257)
at com.intellij.psi.impl.ba.h(ba.java:98)
at com.intellij.psi.impl.ba.g(ba.java:116)
at com.intellij.psi.impl.source.bj.getSupers(bj.java:218)
at com.intellij.psi.impl.e.a.h.a(h.java:21)
at com.intellij.psi.impl.e.a.h.a(h.java:56)
at com.intellij.psi.util.InheritanceUtil.a(InheritanceUtil.java:49)
at
com.intellij.psi.util.InheritanceUtil.isInheritorOrSelf(InheritanceUtil.java:44)
at spike.Foo.testGetClassChild(Foo.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
com.intellij.testFramework.LightCodeInsightTestCase.a(LightCodeInsightTestCase.java:107)
at
com.intellij.testFramework.LightCodeInsightTestCase$2.run(LightCodeInsightTestCase.java)
at com.intellij.openapi.command.b.b.executeCommand(b.java:31)
at
com.intellij.testFramework.LightCodeInsightTestCase$1.run(LightCodeInsightTestCase.java)
at com.intellij.openapi.application.a.d.runWriteAction(d.java:170)
at
com.intellij.testFramework.LightCodeInsightTestCase.runTest(LightCodeInsightTestCase.java:77)
at
com.intellij.testFramework.LightIdeaTestCase.d(LightIdeaTestCase.java:67)
at
com.intellij.testFramework.LightIdeaTestCase.a(LightIdeaTestCase.java:9)
at
com.intellij.testFramework.LightIdeaTestCase$4.run(LightIdeaTestCase.java)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:171)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Process terminated with exit code -1

0

Please sign in to leave a comment.