Issues with Mirah - codenameone

I am using the Mirah library in Codename One mainly as an object mapper between Json and Java classes.
I am running into the issue where Mirah is trying to map classes that were not declared in the .mirah class. For example my .mirah class has a data mapper for Product
data_mapper Product:ProductMapper
But the error is complaining about IUser interface and not the Product class! For a starter, I don't understand why Mirah would complain about a class that is not declared as a datamapper. Am I missing something ? Please keep in mind in my current code, Product has only primitive data type variables and absolutely no reference to the IUser interface or the User implementing class.
nbproject\mirah-build-cn1.xml:152:
java.lang.RuntimeException: Could not find stub for interface IUser
at ca.weblite.asm.JavaExtendedStubCompiler$2.visitClass(JavaExtendedStubCompiler.java:694)
at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:720)
at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:68)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:99)
at com.sun.source.util.TreeScanner.visitCompilationUnit(TreeScanner.java:120)
at ca.weblite.asm.JavaExtendedStubCompiler$2.visitCompilationUnit(JavaExtendedStubCompiler.java:275)
at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:550)
at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:68)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at ca.weblite.asm.JavaExtendedStubCompiler.compile(JavaExtendedStubCompiler.java:797)
at ca.weblite.asm.JavaExtendedStubCompiler.compileFile(JavaExtendedStubCompiler.java:174)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:211)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:193)
at ca.weblite.asm.WLMirahCompiler.compile(WLMirahCompiler.java:208)
at ca.weblite.mirah.ant.MirahcTask.execute(MirahcTask.java:158)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
BUILD FAILED (total time: 10 seconds)
I would also like to mention that I highly suspect that cleaning the project may not be cleaning all Mirah generated classes. Since I was unable to build my application at all due to the error above, I tried deleting the ".mirah" class from my project, then tried cleaning and rebuilding but I still received the same error from that point on.
The only way I was able to get the project to compile again is to start a brand new project and move my source file there leaving the .mirah class out.
I appreciate any insights!
More details Updates below
Hi Steve. Thank you very much for your feedback! I spent some time trying to track down the issue. I think the issue has to do somehow with Generics. As far as I know, generic types are substituted at compile time so I am guessing Codename One would support generics just fine. Also my code compiled well before attempting to go the Mirah route. I really think Mirah is a brilliant idea.
Here's few steps to recreate the issue. I started a CN1 Hello world project and created the following classes
1)IHouse
import java.util.List;
public interface IHouse<W extends IWindow> {
public int getHouseColor();
public void setHouseColor(int color);
public List<W> getWindows();
public void setWindows(List<W> windows);
}
2)IWindow
public interface IWindow {
public int getWindowColor();
public void setWindowColor(int newColor);
public String getShape();
}
3) RoundWindow
public class RoundWindow implements IWindow {
private int windowColor;
private String shape;
public RoundWindow() {
String shape = "ROUND";
}
#Override
public int getWindowColor() {
return windowColor;
}
#Override
public void setWindowColor(int newColor) {
windowColor = newColor;
}
#Override
public String getShape() {
return shape;
}
}
4) SquareWindow
public class SquareWindow implements IWindow {
private int windowColor;
private String shape;
public SquareWindow() {
String shape = "SQUARE";
}
#Override
public int getWindowColor() {
return windowColor;
}
#Override
public void setWindowColor(int newColor) {
windowColor = newColor;
}
#Override
public String getShape() {
return shape;
}
}
5) House
public class House implements IHouse<RoundWindow> {
private int houseColor;
List<RoundWindow> windows;
#Override
public int getHouseColor() {
return houseColor;
}
#Override
public void setHouseColor(int color) {
houseColor = color;
}
#Override
public List<RoundWindow> getWindows() {
return windows;
}
#Override
public void setWindows(List<RoundWindow> windows) {
this.windows = windows;
}
}
To Recap , I have a IHouse interface supporting any type of window that implements IWindow. Then the House class that implements the IHouse interface can strictly specify which Window it supports / works with.
This code compiles well in CN1 before adding the Mirah plugin. Though I receive an identical error to what I had explained before when I add Mirah.
the following error occurred while executing this line:
C:\Users\location\NetBeansProjects\Test\nbproject\mirah-build.xml:51:
java.lang.RuntimeException: Could not find stub for interface IHouse
at ca.weblite.asm.JavaExtendedStubCompiler$2.visitClass(JavaExtendedStubCompiler.java:694)
at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:720)
at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:68)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:99)
at com.sun.source.util.TreeScanner.visitCompilationUnit(TreeScanner.java:120)
at ca.weblite.asm.JavaExtendedStubCompiler$2.visitCompilationUnit(JavaExtendedStubCompiler.java:275)
at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:550)
at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:68)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at ca.weblite.asm.JavaExtendedStubCompiler.compile(JavaExtendedStubCompiler.java:797)
at ca.weblite.asm.JavaExtendedStubCompiler.compileFile(JavaExtendedStubCompiler.java:174)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:211)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:214)
at ca.weblite.asm.JavaExtendedStubCompiler.compileDirectory(JavaExtendedStubCompiler.java:193)
at ca.weblite.asm.WLMirahCompiler.compile(WLMirahCompiler.java:208)
at ca.weblite.mirah.ant.MirahcTask.execute(MirahcTask.java:158)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor323.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor323.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor323.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
BUILD FAILED (total time: 9 seconds)
Few Observation, if I remove the House class, the code builds well (showing "build successful" at the end) but I see the following warnings. I believe these are due to an attempt to pre-compile a generic interface where we don't know yet the exact type for w (until runtime). This could be the root cause for the issue.
Failed to get signature for method
public List<W> getWindows();
Failed to get signature for method
public void setWindows(List<W> windows);
Finally, changing my model design and getting rid of the Generics altogether gets my code to compile again without any Mirah complaint. In my real project, I would like to keep my current design (with generics) for many reasons.
Would it be possible to also use the Mirah Json to Java conversion tool along with generics?
Thank you in advance for your time!

The Mirah netbeans plugin allows for two-way mirah-java dependencies in project. To accomplish this, it first compiles "stubs" of the java sources in the project so that they can be referenced from Mirah during the mirah compile step. After mirah is finished compiling to .classes, it then does the "real" java compilation step which references the compiled mirah files. This is why it is referencing the IUser class.. that is the "pre-compile" step that is choking on it.
The error indicates that it can't find the IUser class while pre-compiling the java stubs. I'd have to see the project to know why.

Related

Flink BlockElement Exception when updating to version 1.14.2

Before, everything works well with flink 1.13.1, lately we update it to flink 1.14.2, the following code is run: and it throws this exception:
<T> DataStream<Tuple3<String, String, T>> returnsInternal(SiddhiOperatorContext siddhiContext, String[] executionPlanIds) {
if (createdDataStream == null) {
DataStream<Tuple2<StreamRoute, Object>> mapped = this.dataStream.map(new MapFunction<Tuple2<StreamRoute, Object>, Tuple2<StreamRoute, Object>>() {
#Override
public Tuple2<StreamRoute, Object> map(Tuple2<StreamRoute, Object> value) throws Exception {
if (executionPlanIds != null && executionPlanIds.length != 0) {
for (String executionPlanId : executionPlanIds) {
if (!executionPlanId.isEmpty()
&& siddhiContext.getExecutionPlan(executionPlanId).IsUsedStream(value.f0.getInputStreamId())) {
value.f0.addExecutionPlanId(executionPlanId);
}
}
}
return value;
}
});
createdDataStream = SiddhiStreamFactory.createDataStream(siddhiContext, mapped);
}
return createdDataStream;
}
The exception and callstack are as follows:
org.apache.flink.api.common.InvalidProgramException: The
implementation of the BlockElement is not serializable. The object
probably contains or references non serializable fields.
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:164)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:69)
at
org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:2139)
at
org.apache.flink.streaming.api.datastream.DataStream.clean(DataStream.java:203)
at
org.apache.flink.streaming.api.datastream.DataStream.map(DataStream.java:577)
at
org.apache.flink.streaming.siddhi.ExecutionSiddhiStream.ExecutionSiddhiStreamBase.returnsInternal(ExecutionSiddhiStreamBase.java:135)
at
org.apache.flink.streaming.siddhi.ExecutionSiddhiStream.ExecutionSiddhiStreamBase.returnsInternal(ExecutionSiddhiStreamBase.java:123)
at
org.apache.flink.streaming.siddhi.ExecutionSiddhiStream.ExecutionSiddhiStream.returnAsRow(ExecutionSiddhiStream.java:180)
at
org.apache.flink.streaming.siddhi.ExecutionSiddhiStream.ExecutionSiddhiStream.returnAsRowWithQueryId(ExecutionSiddhiStream.java:165)
at
org.apache.flink.streaming.siddhi.SiddhiCEPITCase.testSimplePojoStreamAndReturnRowWithQueryId(SiddhiCEPITCase.java:245)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at
org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
at
org.apache.flink.util.TestNameProvider$1.evaluate(TestNameProvider.java:45)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at
org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at
org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at
org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at
org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
at
org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
at org.junit.rules.RunRules.evaluate(RunRules.java:20) at
org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at
org.junit.runners.ParentRunner.run(ParentRunner.java:413) at
org.junit.runner.JUnitCore.run(JUnitCore.java:137) at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at
com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at
com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.io.NotSerializableException:
org.apache.flink.configuration.description.TextElement at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at
org.apache.flink.util.InstantiationUtil.serializeObject(InstantiationUtil.java:632)
at
org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:143)
... 45 more
So, why is there a problem and what's the difference between 1.13.1&1.14.0,how can we fix this problem?
Thank you,David Anderson. This should be a bug introduced by the latest flink commit of file flink-core/src/main/java/org/apache/flink/api/common/ExecutionConfig.java
Diff the file, we can find TextElement is used here where ClosureCleanerLevel is is used as a memeber of Serializable ExecutionConfig.
TextElement in ClosureCleanerLevel
In Flink Siddhi, ExecutionConfig is serialized which is used to serilize flink data to siddhi type on every taskmanager, so that should be the cause.
The simplest way to verify the problem is running code as followings in flink 1.13.5 and 1.14.0, the exception is reproduced in 1.14.0 . And the diff between 1.13.5 and 1.14.0 is only lates commit.
#Test
public void testExecutionConfigSerializable() throws Exception {
ExecutionConfig config = new ExecutionConfig();
ClosureCleaner.clean(config, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true);
}
Note that plain Java serialization still works for the ExecutionConfig, it is just the ClosureCleaner that rejects it because it does very strict checks w.r.t. serializability.
As such, the underlying problem could be that the closure of your map function is unnecessarily large.
The SiddhiOperatorContext that you pass into the method will become part of the map functiosn closure, so you could check whether you can minimize the size of that context such that it no longer relies on an ExecutionConfig.

Error using Windows Handle Selelnium

Below is the code that I wrote where i am using windows handle to validate the url new window being opened after clicking on a link into new page.
package pages;
import java.io.IOException;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import appSetup_Maven.test.BaseClass;
public class HelpCenter extends BaseClass {
#Test
public void Kb() throws IOException {
Login();
driver.findElement(By.xpath(".//*[#id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/a")).click();
Object[] handle = driver.getWindowHandles().toArray();
driver.findElement(By.xpath(".//*[#id='wrapper']/div[1]/div[2]/div/div/ul/li[3]/ul/li[4]/a")).click();
driver.switchTo().window((String) handle[0]);
String URL = driver.getCurrentUrl();
System.out.println(URL);
Assert.assertEquals(URL, "https://preprod.xyz.com/home");
driver.switchTo().window((String) handle[1]);
driver.getTitle();
Assert.assertEquals(URL, "https://kb.xyz.com/");
}
}
However the above code is not working at it fails and gives the below error -
java.lang.ArrayIndexOutOfBoundsException: 1
at pages.HelpCenter.Kb(HelpCenter.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
As a Test Engineer, my first point is getting to know the meaning of error and caused it. It can be easily debugged using the traceback that the IDE provides. You should refer to the documentation, and then try to apply logic why it failed.
Now, if I see your code, the exception that comes it ArrayIndexOutOfBoundsException. As per the documentation from Oracle, this is what they say about ArrayIndexOutOfBoundsException
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
And, then your traceback, suggests, this is at line 25
at pages.HelpCenter.Kb(HelpCenter.java:25)
which indicates to this piece of code
driver.switchTo().window((String) handle[1]);
The array that you created doesn't has element at index 1, and so it throws this error.
The reasons may include -
You're not giving enough time for WebDriver to recognise the window. I would suggest adding a Wait after you click on the web element, before switching to the window.
Using an Object array to get the windows. You are getting an array of Objects, and then converting this back to String? Why is that? Unless there is an need to be converted to Objects, I don't think you need an array of Objects. You can simply use String for the window handles itself. Try to simplify your code.
You can use something like
String parentWindowHandler = driver.getWindowHandle();
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext())
{
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);
}
driver.switchTo().window(parentWindowHandler);
Here is an excellent post on IndexOutOfBoundsException.

How to fix Cloudant-client connection error

I used cloudant-client-1.0.0.jar to make a test to check the connection to cloudant server as:
import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
public class test {
private CloudantClient client=null;
public CloudantClient connect(){
client=new CloudantClient("xxx", "xxx", "xxx");
System.out.println("Connection successful! "+client.getBaseUri());
return client;
}
public Database getDb(String dbName){
Database db=connect().database(dbName, true);
System.out.println("Datase avaible - "+db.getDBUri());
return db;
}
public static void main(String[] args){
new test().getDb("_user");
}
}
However, the system shows errors:
Exception in thread "main"
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpEntityEnclosingRequestBase
at connection.test.connect(test.java:13)
at connection.test.getDb(test.java:21)
at connection.test.main(test.java:30)
Caused by:
java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpEntityEnclosingRequestBase
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
How to fix it?
You're missing a dependency for the library, Apache http client. However you should use Maven, gradle or a similar build tool which understands Maven repositories. You should also look at upgrading to 2.4.1, it contains numerous improvements and bug fixes.

Google App Engine Entity Relationship Annotations end up HTTP 500

I am creating a simple REST API via Google app engine. I have Task and Project objects. A Project can have one or more Tasks. Here is some details about these data objects:
#Entity
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
private String title;
private String description;
private Date createdAt;
// Section 1
// #OneToMany(mappedBy = "project")
// private List<Task> tasks;
// ...
}
#Entity
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Key id;
private String shortDescription;
private String longDescription;
private Date createdAt;
private Date dueDate;
private boolean completed;
// Section 2
// #ManyToOne
// #JoinColumn(name = "id_project")
// private Project project;
// ...
}
The way I implemented the class above works fine (Section 1 and Section 2 are commented out). However, what I want to do is to relate Task objects to Project. Whenever I remove the comments above and activate Section 1 and Section 2 the errors below occur.
The error appears for Project operations
HTTP ERROR 500
Problem accessing /api/project. Reason:
Could not initialize class com.aspect.todo.dao.EMFService
Caused by:
java.lang.NoClassDefFoundError: Could not initialize class com.aspect.todo.dao.EMFService
at com.aspect.todo.dao.Dao.getProjects(Dao.java:144)
at com.aspect.todo.server.ProjectService.get(ProjectService.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
...
The error appears for Task operations
HTTP ERROR 500
Problem accessing /api/task. Reason:
INTERNAL_SERVER_ERROR
Caused by:
java.lang.ExceptionInInitializerError
at com.aspect.todo.dao.Dao.getTasks(Dao.java:98)
at com.aspect.todo.server.TaskService.get(TaskService.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...
Caused by:
javax.persistence.PersistenceException: Provider error. Provider: org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider
at javax.persistence.Persistence.createFactory(Persistence.java:176)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:112)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:66
...
Caused by:
Errors were encountered when initialising the specified MetaData. See the nested exceptions for details
org.datanucleus.exceptions.NucleusUserException: Errors were encountered when initialising the specified MetaData. See the nested exceptions for details
at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:892)
at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:794)
at org.datanucleus.jpa.EntityManagerFactoryImpl.initialisePMF(EntityManagerFactoryImpl.java:488)
...
Caused by:
Found Meta-Data for class com.aspect.todo.model.Task but this class is not enhanced!! Please enhance the class before running DataNucleus.
org.datanucleus.exceptions.NucleusUserException: Found Meta-Data for class com.aspect.todo.model.Task but this class is not enhanced!! Please enhance the class before running DataNucleus.
at org.datanucleus.metadata.MetaDataManager.initialiseClassMetaData(MetaDataManager.java:2225)
at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaData(MetaDataManager.java:2176)
at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:881)
...
The weird thing is when I start with these sections commented out, compile and run and then activate only Section 2 and rerun it works fine. If close and reopen Eclipse try again the error occurs again.
NOTE: Datanucleus JDO/JPA version: v1
Found Meta-Data for class com.aspect.todo.model.Task but this class is not enhanced!!
Please enhance the class before running DataNucleus.
The fact is that the classes are not enhanced at runtime and you have to find a way to enhance them prior to runtime that works for your environment ... Maven, Ant, command-line, DataNucleus Eclipse plugin, or GAE Eclipse plugin.
If you are using ANT then add the below lines in your build.xml before </project> tag close
<target name="datanucleusenhance" depends="compile"
description="Performs JDO enhancement on compiled data classes.">
<enhance_war war="war" />
And give the following command when build the ant "ant datanucleusenhance runserver"
Hope this is useful, it took me a while to find the solution.
Just upgrade your GAE SDK, it solved the problem for me.
Found Meta-Data for class com.aspect.todo.model.Task but this class is not enhanced!!
Please enhance the class before running DataNucleus.
I'll show here a way to enhance classes in Gradle environment. Use the following configuration.
Dependencies:
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:<version>'
compile 'org.datanucleus:datanucleus-enhancer:<version>'
compile 'org.datanucleus:datanucleus-api-jpa:<version>' // or datanucleus-api-jdo
/* other dependencies */
}
Plugin:
(for gradle-appengine-plugin version 1.9.5 or higher)
appengine {
enhancer {
version = "v2"
api = "jpa" // or "jdo"
enhanceOnBuild = true
}
/*...*/
}
(for older versions of gradle-appengine-plugin)
appengine {
enhancerVersion = "v2"
}
To make enhance run automatically before creating the war in your build.gradle file:
war.dependsOn appengineEnhance
If you have problems executing task :appengineEnhance, try to run it with --stacktrace (to view the stacktrace) or --info (to find the location of the error log file) options.

duplicate jdoconfig.xml on classpath during unit tests in Intellij IDEA (9.x pre-release)

As described here, IDEA is adding the jdoconfig.xml to the class path twice during unit test runs. Can anyone suggest a work-around for this?
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java -Dgwt.args=-gen /Users/jgc/Library/Caches/IntelliJIDEA9M1/gwt/recipes.recipesea699bb7/Recipes.4dd34334/test/gen -out /Users/jgc/Library/Caches/IntelliJIDEA9M1/gwt/recipes.recipesea699bb7/Recipes.4dd34334/test/www -Dfile.encoding=MacRoman -classpath /Users/jgc/gwt-versions/gwt-mac-1.7.0/gwt-dev-mac.jar:/Users/jgc/Documents/beanstalk/recipes/test:/Users/jgc/Documents/beanstalk/recipes/src:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/deploy.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/dt.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/javaws.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/jce.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/management-agent.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/plugin.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/sa-jdi.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/alt-rt.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/charsets.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/classes.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/dt.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/jce.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/jconsole.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/jsse.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/laf.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/management-agent.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/../Classes/ui.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/apple_provider.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/dnsns.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/localedata.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/sunjce_provider.jar:/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext/sunpkcs11.jar:/Users/jgc/Documents/beanstalk/recipes/out/test/Recipes:/Users/jgc/Documents/beanstalk/recipes/out/production/Recipes:/Users/jgc/gwt-versions/gwt-mac-1.7.0/gwt-user.jar:/Applications/IntelliJ IDEA 9.0M1.app/lib/javaee_6.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/appengine-api-1.0-sdk-1.2.5.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/geronimo-jpa_3.0_spec-1.1.1.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/datanucleus-jpa-1.1.5.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/datanucleus-core-1.1.5.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/appengine-api-labs-1.2.5.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/gwttheme.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/datanucleus-appengine-1.0.3.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/geronimo-jta_1.1_spec-1.1.1.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/jdo2-api-2.3-eb.jar:/Users/jgc/Documents/beanstalk/recipes/war/WEB-INF/lib/gwt-servlet.jar:/Users/jgc/appengine-java-sdk-1.2.5/lib:/Users/jgc/appengine-java-sdk-1.2.5/lib/appengine-tools-api.jar:/Applications/IntelliJ IDEA 9.0M1.app/lib/junit-4.6.jar:/Users/jgc/appengine-java-sdk-1.2.5/lib/impl/appengine-api-labs.jar:/Users/jgc/appengine-java-sdk-1.2.5/lib/impl/appengine-api.jar:/Users/jgc/appengine-java-sdk-1.2.5/lib/impl/appengine-api-stubs.jar:/Users/jgc/appengine-java-sdk-1.2.5/lib/impl:/Users/jgc/appengine-java-sdk-1.2.5/lib/impl/appengine-local-runtime.jar:/Applications/IntelliJ IDEA 9.0M1.app/lib/idea_rt.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.jgc.recipes.server.RecipeDAOTest
java.lang.ExceptionInInitializerError
at com.jgc.recipes.server.RecipeDAO.persistRecipe(RecipeDAO.java:27)
at com.jgc.recipes.server.RecipeDAOTest.testFoo(RecipeDAOTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:91)
at org.junit.runner.JUnitCore.run(JUnitCore.java:159)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:43)
Caused by: javax.jdo.JDOFatalUserException: Duplicate PMF name "transactions-optional" found in file:/Users/jgc/Documents/beanstalk/recipes/src/META-INF/jdoconfig.xml and file:/Users/jgc/Documents/beanstalk/recipes/out/production/Recipes/META-INF/jdoconfig.xml.
at javax.jdo.JDOHelper.getNamedPMFProperties(JDOHelper.java:1300)
at javax.jdo.JDOHelper.getPropertiesFromJdoconfig(JDOHelper.java:1232)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:1079)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:914)
at com.jgc.recipes.server.PMF.<clinit>(PMF.java:7)
... 20 more
Process finished with exit code 255
For the time being you could pass the PersistenceManagerFactory's properties programmatically, so without using a jdoconfig.xml file, e.g.
private static PersistenceManagerFactory pmfInstance;
static {
Map props = new HashMap();
props.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory");
props.put("javax.jdo.option.ConnectionURL", "appengine");
props.put("javax.jdo.option.NontransactionalRead", "true");
props.put("javax.jdo.option.NontransactionalWrite", "true");
props.put("javax.jdo.option.RetainValues", "true");
props.put("datanucleus.appengine.autoCreateDatastoreTxns", "true");
pmfInstance = JDOHelper.getPersistenceManagerFactory(props);
}
private PMF() {
}
public static PersistenceManagerFactory get() {
return pmfInstance;
}

Resources