Titan: NullPointerException is thrown instead of IllegalArgumentException - titan

In this code
Vertex page = graph.getVertex(pageId);
If pageId doesn't exist it should throw IllegalArgumentException but instead it throws NullPointerException. Here page is null.
I am catching IllegalArgumentException exception but its never catched. Why?

If pageId is null then standard Blueprints behavior is to throw an IllegalArgumentException which is enforced by this test:
https://github.com/tinkerpop/blueprints/blob/2.5.0/blueprints-test/src/main/java/com/tinkerpop/blueprints/VertexTestSuite.java#L82
That said, I think you are talking about vertex existence (as you wrote "if pageId doesn't exist). In the case of existence, standard Blueprints behavior is to return null if the identifier does not find a vertex with it. This behavior is enforced by this test:
https://github.com/tinkerpop/blueprints/blob/2.5.0/blueprints-test/src/main/java/com/tinkerpop/blueprints/VertexTestSuite.java#L157
As you can see in my REPL session, Titan works as expected given the tests:
gremlin> g = TitanFactory.open('conf/titan-cassandra.properties')
==>titangraph[cassandrathrift:127.0.0.1]
gremlin> g.getVertex(4)
==>v[4]
gremlin> g.getVertex(8)
==>null
gremlin> g.getVertex(null)
Vertex id can not be null
Display stack trace? [yN] y
java.lang.IllegalArgumentException: Vertex id can not be null
This is all expected behavior given the Blueprints API. As far as "why" it works this way....I think that IllegalArgumentException wouldn't be appropriate for a "not found" situation. At the end of the day, this was largely just a design decision and we thought that returning null was better than tossing an exception to provide feedback on "existence".

Related

Is there a way to assert an expected exception with Apache beam PAssert?

I am building unit tests for a class which runs in an Apache beam pipeline.
One of the methods I'm testing should throw an exception after attempting some operation on a PCollection. Is there a way to expect this exception with PAssert?
for example
//Expect SomeClass to throw an exception when instantiated with this argument
val invalidParDo = ParDo.of(new SomeClass("This is an invalid parameter"))
val actualOutput = inputPCollection.apply("",invalidParDo)
//Here I'd like a way to assert that I expected a particular exception
PAssert.that(someWayToExpectExceptionHere)
testPipeline.run()
Or any similar alternative
Note that PAssert is a Beam transform that validates that a given PCollection is equal to one or more given values. Exceptions raised in invalidParDo that are not handled will result in a pipeline failure (after retries) hence it's not possible to validate such failures in a subsequent step. But if you update invalidParDo to generate an invalid value instead of raising an exception, you should be able to validate the generation of such invalid values using a PAssert.

Expect an exception in Scala tests (no JUnit)

I had tests that should give null:
Object.someFunction(argUsedToGiveNull) should === (null)
But I've modified the function, and now I want the test to expect/assert the function given that value throws an IllegalArgumentException.
I'd like to do something like:
Object.someFunction(argNowThrowsException) should throw IllegalArgumentException
and note I am not using #Test JUnit structure.
Thanks!
an[IllegalArgumentException] should be thrown by Object.someFunction(argNowThrowsException)
To test for an expected exception:
an [ExceptionType] should be thrownBy {
Object.someFunction(argThrowsException) }
Note that the evaluatingkeyword used to be used for this but is now deprecated.

Fragment Evaluation Error

Can someone tell me what "Fragment evaluation error" means, or where I might look for solutions? I sometimes (but not always) get lots of these errors (without changing my code):
[error] ! Fragment evaluation error
[error] ThrowableException: Could not initialize class code.model.Post$ (FutureTask.java:138)
[error] code.model.PostSpec$$anonfun$1$$anonfun$apply$1.apply$mcZ$sp(PostSpec.scala:68)
[error] code.model.PostSpec$$anonfun$1$$anonfun$apply$1.apply(PostSpec.scala:51)
[error] code.model.PostSpec$$anonfun$1$$anonfun$apply$1.apply(PostSpec.scala:51)
Line 68 of PostSpec is the first line in the (specs2) test that references the Post model companion object:
val test4 = Post.fixJValue(toextract4).extract[Selection]
I'm using Scala 2.9.0-1.
Also: I have no idea whether it matters, but Post is a net.liftweb.mongodb.record.MongoRecord class companion object:
object Post extends Post with MongoMetaRecord[Post] { ... }
In a specs2 specification, Fragments are pieces of the specification. A Fragment can be a Text, an Example, a Step.
Some fragments, like Example and Step are meant to be executed and are supposed to catch Exceptions so that they can be marked as failures. But they won't catch Errors (except AssertionErrors). So if an Example throws an OutOfMemoryError, this will be reported as a Fragment evaluation error.
Other fragments, like Text fragments are not supposed to throw exceptions when being evaluated. If they do, you will get the same Fragment evaluation error message.
Without seeing the full specification it's hard for me to say what's happening there but I suspect that you had a non-Exception type thrown in the body of an Example. But I have more questions than answers for now:
where is test4 declared? Inside the specification body? Inside a Context case class?
since errors happen intermittently, are you sure you always have a proper mongodb context? Maybe your specification examples are being executed concurrently on the same mongo db instance?

ILGenerator, make decision on return value of null

il.Emit(OpCodes.Callvirt, _compactBinaryReader_ReadObject);
this function is called and at a special condition a return value of 'null' is provided.
if that value is null i have to take a decision whether to jump on to a label or not
using after the method call
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse_S, DECISION);
gives me an exception "JIT Compiler encountered an internal limitation." when i call that function, the code builds correctly though.
tried OpCodes.Brfalse too.
what am i doing wrong ?
Found reasonS to the above problem,
one thing which should be understood that when an exception of
'CLR: Verification for Runtime Code Generation'
is thrown it means the code written is not in the correct format and when it is evaluated by the assembler it does not accept the written code, problem is usually because of stacks having extra values or less.
"JIT Compiler encountered an internal limitation." is thrown when at runtime it was expecting something else we provide something else in value or when stack has something else when something else was required.
In short, the later exception is thrown at runtime and the other is thrown when pre Run conditions are not met.
anyways i found the reason, i had some values still present on stack that i did not pop if Condition was met, so the POP OpCode did the trick, and by the way for me the Dup OpCode never worked out, it always pushes a null value on stack rather than duplicating the top most value.

Why does reader.GetOrdinal("FieldName") throw an exception?

this throws an exception, when the field does not exist:
reader.IsDbNull(reader.GetOrdinal("FieldName")) => bang
Why not return -1 ?
I'll try to guess here.
The common pattern for this method is to call GetOrdinal for column name and then call GetXXX() methods with given ordinal which is faster than do a search by column's name every time.
Therefore in case of exception here we fail fast and we can't ignore it. Wihtout exception we will try to find a column that doesn't exist and then try to find a field by given ordinal (without checking for -1 which is very easy to omit in this case) and only here we will realise that something went wrong few steps before (may be even too many steps before).