Skip to content

Commit

Permalink
Add extra tests regarding solving of symbols in JavassistClassDeclara…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
WimTibackx committed Oct 18, 2017
1 parent a09ceb1 commit 90cbbde
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 11 deletions.
Expand Up @@ -24,34 +24,119 @@
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import javassist.NotFoundException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class SymbolSolverWithJavassistClassTest extends AbstractTest {
private TypeSolver typeSolver;
private SymbolSolver symbolSolver;
private JavassistClassDeclaration classDeclaration;
private JavassistClassDeclaration classDeclarationConcreteClass;
private JavassistClassDeclaration classDeclarationSubClassOwnJar;
private JavassistClassDeclaration classDeclarationInterfaceUserOwnJar;
private JavassistClassDeclaration classDeclarationSubClassIncludedJar;
private JavassistClassDeclaration classDeclarationInterfaceUserIncludedJar;
private JavassistClassDeclaration classDeclarationSubClassExcludedJar;
private JavassistClassDeclaration classDeclarationInterfaceUserExcludedJar;

@Before
public void setup() throws IOException {
final String pathToJar = adaptPath("src/test/resources/javassist_symbols/main_jar/main_jar.jar");
typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver());
final String pathToMainJar = adaptPath("src/test/resources/javassist_symbols/main_jar/main_jar.jar");
final String pathToIncludedJar = adaptPath("src/test/resources/javassist_symbols/included_jar/included_jar.jar");
typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToIncludedJar), new JarTypeSolver(pathToMainJar), new ReflectionTypeSolver());

symbolSolver = new SymbolSolver(typeSolver);

classDeclaration = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.ConcreteClass");
classDeclarationConcreteClass = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.ConcreteClass");
classDeclarationSubClassOwnJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubClassOwnJar");
classDeclarationSubClassIncludedJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubClassIncludedJar");
classDeclarationSubClassExcludedJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubClassExcludedJar");
classDeclarationInterfaceUserOwnJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.InterfaceUserOwnJar");
classDeclarationInterfaceUserIncludedJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.InterfaceUserIncludedJar");
classDeclarationInterfaceUserExcludedJar = (JavassistClassDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.InterfaceUserExcludedJar");
}

@Test
public void testSolveSymbolCanSolveFirstOwnField() {
SymbolReference<? extends ResolvedValueDeclaration> solvedSymbol = symbolSolver.solveSymbolInType(classDeclaration, "STATIC_STRING");
public void testSolveSymbolInTypeCanSolveFirstOwnField() {
assertCanSolveSymbol("STATIC_STRING", classDeclarationConcreteClass);
}

@Test
public void testSolveSymbolInTypeCanSolveSecondOwnField() {
assertCanSolveSymbol("SECOND_STRING", classDeclarationConcreteClass);
}

@Test
public void testSolveSymbolInTypeCantResolveNonExistantField() {
SymbolReference<? extends ResolvedValueDeclaration> solvedSymbol = symbolSolver.solveSymbolInType(classDeclarationConcreteClass, "FIELD_THAT_DOES_NOT_EXIST");

assertFalse(solvedSymbol.isSolved());

try {
solvedSymbol.getCorrespondingDeclaration();
} catch (Exception e) {
assertTrue(e instanceof UnsupportedOperationException);
assertNull(e.getMessage());
return;
}
fail("Expected UnsupportedOperationException when requesting CorrespondingDeclaration on unsolved SymbolRefernce");
}

@Test
public void testSolveSymbolInTypeCanResolveFieldInSuper() {
assertCanSolveSymbol("SUPER_FIELD", classDeclarationSubClassOwnJar);
}

@Test
@Ignore // TODO This fails at the moment, I think it might be an issue -- discussion ongoing on Gitter
public void testSolveSymbolInTypeCanResolveFieldInSuperIncludedJar() {
assertCanSolveSymbol("SUPER_FIELD", classDeclarationSubClassIncludedJar);
}

@Test
public void testSolveSymbolInTypeThrowsExceptionOnResolveFieldInSuperExcludedJar() {
try {
symbolSolver.solveSymbolInType(classDeclarationSubClassExcludedJar, "SUPER_FIELD");
} catch (Exception e) {
assertTrue(e.getCause() instanceof NotFoundException);
assertEquals("com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar.SuperClassExcludedJar", e.getCause().getMessage());
return;
}
fail("Excepted NotFoundException wrapped in a RuntimeException, but got no exception.");
}

@Test
public void testSolveSymbolInTypeCanResolveFieldInInterface() {
assertCanSolveSymbol("INTERFACE_FIELD", classDeclarationInterfaceUserOwnJar);
}

@Test
@Ignore // TODO This fails at the moment, I think it might be an issue -- discussion ongoing on Gitter
public void testSolveSymbolInTypeCanResolveFieldInInterfaceIncludedJar() {
assertCanSolveSymbol("INTERFACE_FIELD", classDeclarationInterfaceUserIncludedJar);
}

@Test
public void testSolveSymbolInTypeThrowsExceptionOnResolveFieldInInterfaceExcludedJar() {
try {
symbolSolver.solveSymbolInType(classDeclarationInterfaceUserExcludedJar, "INTERFACE_FIELD");
} catch (Exception e) {
assertTrue(e.getCause() instanceof NotFoundException);
assertEquals("com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar.InterfaceExcludedJar", e.getCause().getMessage());
return;
}
fail("Excepted NotFoundException wrapped in a RuntimeException, but got no exception.");
}

private void assertCanSolveSymbol(String symbolName, JavassistClassDeclaration classDeclaration) {
SymbolReference<? extends ResolvedValueDeclaration> solvedSymbol = symbolSolver.solveSymbolInType(classDeclaration, symbolName);

assertTrue(solvedSymbol.isSolved());
assertEquals("STATIC_STRING", solvedSymbol.getCorrespondingDeclaration().asField().getName());
assertEquals(symbolName, solvedSymbol.getCorrespondingDeclaration().asField().getName());
}
}
@@ -1,15 +1,33 @@
For the tests regarding resolving symbols in Jar files, we need some jar files. Some other tests within JavaSymbolSolver use established external jars for that purpose. Given the very specific cases we need here, that would severly complicate writing and maintaining tests. Therefore, I've decided to write custom jars for the cases we need.

`main_jar` contains most of the necessary classes, while `extra_jar` (TODO) is used for main_jar to refer to with the goal that it would not be included in the SymbolSolver and thus trigger an error.
`main_jar` contains most of the necessary classes, `included_jar` and `excluded_jar` both contain an interface and a superclass. Included_jar is included in the combined type solver, while excluded_jar is not.

If you need to make changes to either jar, run the following commands to generate the new jars (assuming that a JDK bin directory is in your path).
When you need to rebuild the jar, it is important to make sure you actually update the jar in git. Jar-files are in the git-ignore, so you'll have to force-add them using `git -f main_jar.jar`.

## Excluded jar

```
In ./java-symbol-solver-testing/src/test/resources/javassist_symbols/excluded_jar
javac -d result/ src/com/github/javaparser/javasymbolsolver/javassist_symbols/excluded_jar/*.java
cd result/
jar cf ../excluded_jar.jar com/*
```

## Included jar

```
In ./java-symbol-solver-testing/src/test/resources/javassist_symbols/included_jar
javac -d result/ src/com/github/javaparser/javasymbolsolver/javassist_symbols/included_jar/*.java
cd result/
jar cf ../included_jar.jar com/*
```

## Main jar

```
In ./java-symbol-solver-testing/src/test/resources/javassist_symbols/main_jar
javac -d result/ src/com/github/javaparser/javasymbolsolver/javassist_symbols/main_jar/ConcreteClass.java
javac -cp ../included_jar/included_jar.jar;../excluded_jar/excluded_jar.jar -d result/ src/com/github/javaparser/javasymbolsolver/javassist_symbols/main_jar/*.java
cd result/
jar cf ../main_jar.jar com/*
```
Expand Down
Binary file not shown.
Empty file.
@@ -0,0 +1,5 @@
package com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar;

public interface InterfaceExcludedJar {
public static final String INTERFACE_FIELD = "ThisIsAString";
}
@@ -0,0 +1,5 @@
package com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar;

public class SuperClassExcludedJar {
public static final String SUPER_FIELD = "ThisIsAString";
}
Binary file not shown.
Empty file.
@@ -0,0 +1,5 @@
package com.github.javaparser.javasymbolsolver.javassist_symbols.included_jar;

public interface InterfaceIncludedJar {
public static final String INTERFACE_FIELD = "ThisIsAString";
}
@@ -0,0 +1,5 @@
package com.github.javaparser.javasymbolsolver.javassist_symbols.included_jar;

public class SuperClassIncludedJar {
public static final String SUPER_FIELD = "ThisIsAString";
}

0 comments on commit 90cbbde

Please sign in to comment.