I'm writing an annotation processor and want to write some unit tests for it by using google-compile-testing and truth:
So I want to write a very simple unit test.
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
#Test
public void componentOnConcreteClass() {
JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.NotAClass",
"package test;",
"",
"import my.annotation.MyAnnotation;",
"",
"#MyAnnotation",
"interface NotAComponent {}");
assertAbout(javaSource()).that(componentFile)
.processedWith(new MyProcessor())
.failsToCompile()
.withErrorContaining("interface");
}
So basically I have copy an pasted a simple test from google's dagger2 repo and replaced the relevant data with my annotation processor.
I'm using maven, and I'm using the same dependencies as dagger2:
<dependency>
<groupId>com.google.testing.compile</groupId>
<artifactId>compile-testing</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
But I can't compile the code. I guess there is an generics param problem, but can't figure out what the problem is.
Compilation failure:
[ERROR] ProcessorTest.java:[46,5] method assertAbout in class com.google.common.truth.Truth cannot be applied to given types;
[ERROR] required: com.google.common.truth.SubjectFactory<S,T>
[ERROR] found: com.google.testing.compile.JavaSourceSubjectFactory
[ERROR] reason: no instance(s) of type variable(s) S,T exist so that argument type com.google.testing.compile.JavaSourceSubjectFactory conforms to formal parameter type com.google.common.truth.SubjectFactory<S,T>
Any hint what I'm doing wrong? I can't find any difference to google dagger2 tests (which by the way compiles on my machine)
The artifact com.google.testing.compile:compile-testing:0.5 depends on org.truth0:truth:0.15 which is the old location of Truth. Try using version 0.6 of compile-testing instead.
Related
I have project in java and i am using Allure for generating a test report.
I found this question and I need to rename my test using the below code:
AllureLifecycle lifecycle = Allure.getLifecycle();
//change test name to "CHANGED_NAME"
lifecycle.updateTestCase(testResult -> testResult.setName("CHANGED_NAME"));
I have imported the AllureLifecycle from below:
import io.qameta.allure.AllureLifecycle;
and using the below dependency:
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-java-commons</artifactId>
<version>2.13.3</version>
</dependency>
But, in the line Allure.getLifecycle() it is complaining with Cannot resolve method 'getLifecycle' in 'Allure'.
How can fix the error?
You need to add import of io.qameta.allure.Allure class
I'm trying to implement custom OSGI service predicate for pathbrowser. If somebody have any idea what is wrong with this code :) There is exception below. Maybe it is something with the #Component or dependency
<path jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldDescription="List item link"
fieldLabel="List Item link"
name="./path"
predicate="predicate"
rootPath="/content">
</path>
Predicate implementation:
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import com.day.cq.commons.predicate.AbstractResourcePredicate;
import com.day.cq.wcm.api.Page;
#Component(label = "Content-page Predicate", description = "This predicate is used to restricted to allow selection of pages that have template content-page")
#Service(value = Predicate.class)
#Properties({
#Property(label = "Predicate Name", name = "predicate.name", value = "predicate", propertyPrivate = true) })
public class ContentPagePredicate extends AbstractResourcePredicate {
private static final String CQ_TEMPLATE_CONTENT = "/conf/xxx-lab/settings/wcm/templates/content-page";
#Override
public boolean evaluate(Resource resource) {
if (null != resource) {
if (!resource.getResourceType().equals("cq:Page")) {
return false;
}
Page page = resource.adaptTo(Page.class);
return page.getTemplate().getName().equals(CQ_TEMPLATE_CONTENT);
}
return false;
}
}
Maven build output:
[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.20.0:scr (generate-scr-scrdescriptor) on project SomethingDemo.core: Execution generate-scr-scrdescriptor of goal org.apache.felix:maven-scr-plugin:1.20.0:scr failed: An API incompatibility was encountered while executing org.apache.felix:maven-scr-plugin:1.20.0:scr: java.lang.VerifyError: Constructor must call super() or this() before return
[ERROR] Exception Details:
[ERROR] Location:
[ERROR] com/day/cq/commons/predicate/AbstractNodePredicate.<init>()V #1: return
[ERROR] Reason:
[ERROR] Error exists in the bytecode
[ERROR] Bytecode:
[ERROR] 0x0000000: 2ab1
The error you see can happen when you extend a class from the AEM API that's annotated with SCR annotations (used to generate OSGi bundle descriptors) and, at the same time, obfuscated in the Uber Jar you're using.
You can find an unobfuscated Uber Jar for the AEM version you're using in Adobe's public Maven repository.
If you represent a customer or a partner, you should also be able to download one from the help site https://daycare.day.com/home/products/uberjar.html
If your project is using a repository that already has the unobfuscated Jar, it should be as simple as changing the dependency.
For example, in a project using the AEM 6.2 Uber Jar with obfuscated classes
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<version>6.2.0</version>
<scope>provided</scope>
<classifier>obfuscated-apis</classifier>
</dependency>
Just change the classifier to get an unobfuscated version:
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<version>6.2.0</version>
<scope>provided</scope>
<classifier>apis</classifier>
</dependency>
Check out this Github issue for a wider discussion on a very similar problem.
You may also find this Adobe Help Forum thread interesting,
although it pertains to a beta version.
Just try to implement org.apache.commons.collections.Predicate.
Also: resource.getResourceType().equals("cq:Page") will never be true, as cq:Page is the resource's jcr:pimaryType. page.getTemplate() does not work on publish:
public booean evaluate(Resource resource) {
if (null == resource) return false;
final ValueMap map = resource.getValueMap();
return "cq:Page".equals(map.get("jcr:primaryType", "")
&& CQ_TEMPLATE_CONTENT.equals(map.get("cq:template", "")
}
In Java if I have in my source code a method call that does not provide the correct parameter types, or if I try to assign to a variable of one type a value of another incompatible type, then this will provoke a compilation error, as it ought to. But, when using Maven (mvn) the compilation error will be something like (taken from real code)
error: method put in interface Map<K,V> cannot be applied to given types;
full stop. Whereas, when using javac (javac) the compilation error will be something like
error: method put in interface Map<K,V> cannot be applied to given types;
elements.put(coords, val);}
^
required: Integer[],T
found: int[],T
reason: actual argument int[] cannot be converted to Integer[] by method invocation conversion
where T,K,V are type-variables:
T extends Object declared in class Grid
K extends Object declared in interface Map
V extends Object declared in interface Map
I consider this to be more helpful.
How do I get Maven to emit compile errors like javac does? In particular, how do I get it to provide more contextual information?
The -e and -X switches to mvn do not solve this problem, by the way.
You are probably using version 2.5.1 or lesser of the Maven Compiler Plugin.
If you use version 3.0, you actually get more detailed error messages:
[ERROR] required: java.lang.String,java.lang.String
[ERROR] found: int,int
[ERROR] reason: actual argument int cannot be converted to java.lang.String by method invocation conversion
I am new to Play Framework, and attempting to build a Todo-list from this manual.
When I try to run the application I get the error:
Compilation Error
error: method render in class index cannot be applied to given types;
My code is (the relevant parts):
MainController.java:
final static Form<Task> taskForm = Form.form(Task.class);
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm)
);
}
index.sacala.html:
#(tasks: List[Models.Task], taskForm: Form[Models.Task])
I looked around, the closest thread I found was this one, but I was not able to resolve the issue using it (might be due to lack of understanding of the environment / framework...).
One last thing that is worth mentioning:
If I change 'index.sacala.html' to be parameter-less (and change the 'MainController' accordingly, everything works perfectly.
Would appreciate any thoughts about resolving this compilation error.
EDIT:
The Task.all() code is:
public static List<Task> all() {
return new ArrayList<Task>();
}
Most probably your package is models NOT Models isn't it?
BTW this package is autoimported so you can just use:
#(tasks: List[Task], taskForm: Form[Task])
Hm, changes... Actually log in the console says everything
[error] /www/play20apps/testing/Todo-List/app/controllers/MainController.java:24: error: method render in class index cannot be applied to given types;
[error] return ok(views.html.index.render(Task.all(), taskForm));
[error] ^
[error] required: List<Task>,play.api.data.Form<Task>
[error] found: List<Task>,play.data.Form<Task>
[error] reason: actual argument play.data.Form<Task> cannot be converted to play.api.data.Form<Task> by method invocation conversion
[error] 1 error
especially these lines:
[error] required: List<Task>,play.api.data.Form<Task>
[error] found: List<Task>,play.data.Form<Task>
TBH I didn't ever test the Activator but it looks that imports play.api.data.Form into views, which is incorrect for Java controllers. solution is full qualified path for Form:
#(tasks: java.util.List[Task], taskForm: play.data.Form[Task])
As mentioned in comment *.api.* imports are for Scala and normal are for Java, that's the rule of the thumb in Play 2.+
PostScriptum: Just realized that in your build.sbt you have play.Project.playScalaSettings and actually it should be play.Project.playJavaSettings, this change fixes your problems with Activator.
It looks like Task.all() returns a Java list, while the scala template is probably expecting a Scala list.
I woul suggest changing the return type of Task.all() if possible, or fully qualified the definition in the template:
#(tasks: java.util.List[Models.Task], taskForm: Form[Models.Task])
Although biesior's answer has some nice insights and tips, it did not solve the issue.
At the end I have abndoned type TypeSafe Activator and created the site from scratch using play comamnd line and it worked perfectly.
I never found the origin of the issue in the question, I am leaving this answer for a future reference for the googlers.
If any one has a better solution, please leave your answer and if it works I will mark it as accepted.
EDIT:
#biesior was kind enough to go through my code and he did find the issue. If you have the same issue, take a look at his answer.
I'm trying to use Guava 12.0's FluentIterable with GWT 2.0.3, like so:
import com.google.common.collect.FluentIterable;
class FooPresenter {
// snip
private List<NullSafeCheckBox> asCheckboxes() {
return FluentIterable.from(getDisplay().getMetricInputs())
.transform(new Function<HasValueChangeHandlers<Boolean>, NullSafeCheckBox>() {
#Override
public NullSafeCheckBox apply(#Nullable HasValueChangeHandlers<Boolean> checkbox) {
return (NullSafeCheckBox) checkbox;
}
})
.toImmutableList();
}
}
However, when I run GWT in dev mode, I get the following error when trying to load the first module:
DEBUG: Validating newly compiled units
ERROR: Errors in 'jar:file:/home/josh/.m2/repository/com/google/guava/guava-gwt/12.0/guava-gwt-12.0.jar!/com/google/common/math/super/com/google/common/math/LongMath.java'
ERROR: Line 23: The import java.math cannot be resolved
My pom.xml looks like this:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-gwt</artifactId>
<version>12.0</version>
</dependency>
And my Application.gwt.xml sucks in Guava like this:
<!-- Guava -->
<inherits name="com.google.common.collect.Collect"/>
Update
Following the advice in Arcadian's answer, I added gwt-math to my pom.xml and my Application.gwt.xml:
<!-- Guava -->
<inherits name="com.google.common.collect.Collect"/>
<inherits name="com.googlecode.gwt.math.Math" />
Now I get this error when running hosted mode:
DEBUG: Validating newly compiled units
WARN: Warnings in 'jar:file:/home/josh/.m2/repository/videoplaza-third-party/gwt-incubator/20100204-r1747/gwt-incubator-20100204-r1747.jar!/com/google/gwt/widgetideas/client/impl/GlassPanelImpl.java'
WARN: Line 30: Referencing deprecated class 'com.google.gwt.user.client.impl.DocumentRootImpl'
WARN: Line 38: Referencing deprecated class 'com.google.gwt.user.client.impl.DocumentRootImpl'
ERROR: Errors in 'jar:file:/home/josh/.m2/repository/com/google/guava/guava-gwt/12.0/guava-gwt-12.0.jar!/com/google/common/primitives/UnsignedLong.java'
ERROR: Line 77: The method bitLength() is undefined for the type BigInteger
ERROR: Line 79: The method longValue() is undefined for the type BigInteger
ERROR: Line 200: The method valueOf(long) is undefined for the type BigInteger
ERROR: Line 202: The method setBit(int) is undefined for the type BigInteger
You can try to add gwt-java-math project as dependency. As said on their wiki,
This library is in the process of getting merged into GWT itself!
Currently it is on GWT trunk.
It may be available in the upcoming v2.5.
FYI, it looks to me like java.math should be available in current versions of GWT, though probably not in 2.0.3. See, e.g., the JRE emulation reference for GWT 2.2, probably the first version where it was available.
I don't know what to expect in combination with gwt-java-math; possibly Guava itself would need to declare a dependency on its module for it to work?
We do need to do a much better of of determining and advertising which version of GWT is required for Guava, including running our GWT tests against Guava (and not just our internal version of it) to verify that.