I want to write some tests for my compiler but can't get past an error.
I'm following an example from 'Implementing DSL with Xtext and Xtend' by L. Bettini (great book btw). I've downloaded the code for the 'entities' DSL from https://github.com/LorenzoBettini/packtpub-xtext-book-examples and the tests in EntitiesGenerator.xtend work great.
If I write a test for the default DSL (MyDsl) using the same code, I've got an error:
org.eclipse.xtext.xbase.compiler.CompilationTestHelper cannot be resolved to a type.
or, if I add org.eclipse.xtext.xbase.junit (2.4.1) to the list of required plug-ins, I get
Discouraged access: The type CompilationTestHelper is not accessible due to restriction on required project org.xtext.example.myDsl.tests
I can allow access to it, but then get a runtime error anyway. If I try to add org.eclipse.xtext.xbase.lib as well, only org.eclipse.xtext.xbase.lib.source appears in the list. I don't know it that matters. In any case, adding it doesn't change anything.
What do I need to do to make it work?
I'm using Juno with Xtext 2.4.1., Java 1.7.
The content of the test class:
package org.xtext.example.myDsl.tests
import com.google.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.xbase.compiler.CompilationTestHelper // error here
import org.xtext.example.myDsl.MyDslInjectorProvider
import org.junit.Test
import org.junit.runner.RunWith
#RunWith(typeof(XtextRunner))
#InjectWith(typeof(MyDslInjectorProvider))
class MyDslGeneratorTest {
#Inject extension CompilationTestHelper
#Test
def void testGeneratedCode() {
'''
Hello some1!
Hello some2!
'''.assertCompilesTo(
'''some content''')
}
}
Thank you in advance!
the xtext guys mark stuff that may be changed NOT as api. this is why you get this warning.
it should work anyway. (although it is meant to be used for xbase languages only)
P.S: you have to add a dependency to jdt.core too
Related
I am migrating a Java code that is running on Java 8 and I need to compile it to run on Java 17, so Just downloaded eclipse 2021-09 import the code from Git, change project facet selecting Java, add to Java Build Path the libraries.
libraries added
This is the code:
package itksoluciones.avl.lst.dboperation;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class PoolConexionUC {
public static DataSource dataSourceUC;
public PoolConexionUC() {
inicializaDataSourceUC();
}
private void inicializaDataSourceUC() {
BasicDataSource basicDataSourceUC = new BasicDataSource();
basicDataSourceUC.setDriverClassName(DataConexion.driverUC);
basicDataSourceUC.setUsername(DataConexion.userUC);
basicDataSourceUC.setPassword(DataConexion.passUC);
basicDataSourceUC.setUrl(DataConexion.urlUC);
basicDataSourceUC.setMaxActive(Integer.parseInt(DataConexion.maxActiveUC));
basicDataSourceUC.setMaxIdle(DataConexion.defaultMaxIdleUC);
basicDataSourceUC.setMaxWait(DataConexion.defaultMaxWaitUC);
basicDataSourceUC.setRemoveAbandoned(DataConexion.removeAbandonedUC);
basicDataSourceUC.setRemoveAbandonedTimeout(DataConexion.removeAbandonedTimeoutUC);
basicDataSourceUC.setValidationQuery(DataConexion.validationQueryUC);
basicDataSourceUC.setTestOnBorrow(DataConexion.testOnBorrowUC);
dataSourceUC = basicDataSourceUC;
}
}
This are the two errors:
Import org.apache could not be resolved
and
BasicDatasourceCore could not be resolved to a type
Errors
As you could see from the error the library seems available, also it autocompletes when typing.
Also one of the solutions suggested by eclipse is to import org.apache.commons.dbcp.BasicDataSource. (after doing it by double clicking on the suggestion first error is resolved but the second one persists).
What is also very curious is that if I copy paste the class PoolConexionCore to PoolConexionCore2. The problem is gone in PoolConexionCore2.
BUT I HAVE MANY CLASSES WITH THE SAME PROBLEM so I really appreciate any suggestion to avoid having to be dedicated to copy pasted for several days.
After doing copy paste
I'm currently going through the book GWT in Action 2nd Edition and its example code. In chapter 5 under the discussions on ClientBundle usage they have example code where there is an interface that extends com.google.gwt.rpc.client.RpcService. When I loaded this example project into my Eclipse IDE, the code shows red as the package com.google.gwt.rpc does not exist. This is most likely because I'm using GWT 2.7 and the book was written back in GWT 2.5. I attempted to look into the JavaDoc to see when it was removed, and what its replacement should be, but the only JavaDoc is for the latest, and downloads for 2.5 from the website returns no page found (404) errors. My IDE is suggesting that I change the requested interface to com.google.gwt.user.client.rpc.RemoteService but without knowing if this is the correct replacement, it seems a bit odd.
The code example they provide is as follows:
package com.manning.gwtia.ch05.client.cssresource;
import java.util.HashMap;
import java.util.List;
import com.google.gwt.rpc.client.RpcService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("CSSResourceService")
public interface ResourceService extends RpcService {
List<String> getThemes();
HashMap<String, String> getTheme(String name);
}
Does anyone know what the proper replacement interface for RpcService and maybe also tell me in which version it was removed?
com.google.gwt.rpc was an experiment aimed at replacing RPC from com.google.gwt.user. It didn't met expectations and was ultimately removed in 2.7. So yes, use RemoteService, like you should have actually always done.
I just found out about a pretty weird behaviour of Scala scoping when bytecode generated from Scala code is used from Java code. Consider the following snippet using Spark (Spark 1.4, Hadoop 2.6):
import java.util.Arrays;
import java.util.List;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.broadcast.Broadcast;
public class Test {
public static void main(String[] args) {
JavaSparkContext sc =
new JavaSparkContext(new SparkConf()
.setMaster("local[*]")
.setAppName("test"));
Broadcast<List<Integer>> broadcast = sc.broadcast(Arrays.asList(1, 2, 3));
broadcast.destroy(true);
// fails with java.io.IOException: org.apache.spark.SparkException:
// Attempted to use Broadcast(0) after it was destroyed
sc.parallelize(Arrays.asList("task1", "task2"), 2)
.foreach(x -> System.out.println(broadcast.getValue()));
}
}
This code fails, which is expected as I voluntarily destroy a Broadcast before using it, but the thing is that in my mental model it should not even compile, let alone running fine.
Indeed, Broadcast.destroy(Boolean) is declared as private[spark] so it should not be visible from my code. I'll try looking at the bytecode of Broadcast but it's not my specialty, that's why I prefer posting this question. Also, sorry I was too lazy to create an example that does not depend on Spark, but at least you get the idea. Note that I can use various package-private methods of Spark, it's not just about Broadcast.
Any idea of what's going on ?
If we reconstruct this issue with a simpler example:
package yuvie
class X {
private[yuvie] def destory(d: Boolean) = true
}
And decompile this in Java:
[yuvali#localhost yuvie]$ javap -p X.class
Compiled from "X.scala"
public class yuvie.X {
public boolean destory(boolean);
public yuvie.X();
}
We see that private[package] in Scala becomes public in Java. Why? This comes from the fact that Java private package isn't equivalent to Scala private package. There is a nice explanation in this post:
The important distinction is that 'private [mypackage]' in Scala is
not Java package-private, however much it looks like it. Scala
packages are truly hierarchical, and 'private [mypackage]' grants
access to classes and objects up to "mypackage" (including all the
hierarchical packages that may be between). (I don't have the Scala
spec reference for this and my understating here may be hazy, I'm
using [4] as a reference.) Java's packages are not hierarchical, and
package-private grants access only to classes in that package, as well
as subclasses of the original class, something that Scala's 'private
[mypackage]' does not allow.
So, 'package [mypackage]' is both more and less restrictive that Java
package-private. For both reasons, JVM package-private can't be used
to implement it, and the only option that allows the uses that Scala
exposes in the compiler is 'public.'
I am trying to put statement like
when(myDao.fetchTree(anyLong())).thenReturn(myTreeList);
and I have import
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.thenReturn; //not importing
import for thenReturn is not mapping. Is this a version issue? I am using Mockito 1.8.4.
Mockito's when returns an object of class OngoingStubbing. This class has a method thenReturn(), and that's what gets called in your example code. No additional import is needed.
when(myDao.fetchTree(anyLong())).thenReturn(myTreeList);
could be broken up as
OngoingStubbing thing = when(myDao.fetchTree(anyLong()));
thing.thenReturn(myTreeList);
You are just calling the thenReturn method of OngoingStubbing.
It should be enough if you use:
import static org.mockito.Mockito.*;
And remove the rest.
Your question: Is this a version issue?
I'd say NO, that is not a version issue.
As suggested previously, you should
create minimal test with this code in test
when(myDao.fetchTree(anyLong())).thenReturn(myTreeList);
run this code from command-line (not inside in STS or any IDE or something a like)
Q: Why run it from command-line and avoid using IDE-s etc?
A: Because sometimes code parsers and checkers and validators of your favorited IDE reports false positives about some corner-cases in code.
I know this is a common question asked but I've been searching and I've included the class into eclipse through the buildpath. I start to write the import statement and it autocompletes options for me so I know it's finding the class.
My problem is how come it's giving this error when I'm reading the docs and it says the constructor method is MimeUtil2() ?
http://www.jarvana.com/jarvana/view/eu/medsea/mimeutil/mime-util/2.1/mime-util-2.1-javadoc.jar!/eu/medsea/mimeutil/MimeUtil2.html#MimeUtil2()
package com.jab.app;
import java.io.File;
import eu.medsea.mimeutil.*;
public class CheckFileType {
private void GetMimeType(File filename){
MimeUtil2 test = new MimeUtil2(); //Produces the error saying java type cannot be resolved
}
I think you need to import
import eu.medsea.mimeutil.*;
According to the documentation, the type is eu.medsea.mimeutil.MimeUtil2
I ended up finding out that I was using the test-source.jar not the main jar file itself. The sourceforge page made the default as the source file instead of the main jar file.
It was buried inside of the files page.