This question it to some extend inspired by Retrieving Data From Returned Oracle Timestamp Column
I query my Oracle database like
<cfquery name="getstuff" ...>
SELECT timestampfld
FROM myTable
</cfquery>
The column is of type timestamp and is returned as oracle.sql.TIMESTAMP.
A call to
<cfdump var="#timestampfld#">
displays that the class is supposed to have a function timestampValue() that takes no arguments. However, when I call
<cfdump var="#timestampfld.timestampValue()#">
I'm presented with an exception
Either there are no methods with the specified method name and
argument types or the timestampValue method is overloaded with
argument types that ColdFusion cannot decipher reliably. ColdFusion
found 0 methods that match the provided arguments. If this is a Java
object and you verified that the method exists, use the javacast
function to reduce ambiguity.
How can I call the method timestampValue() on my column?
EDIT
When I CFDUMP the oracle.sql.TIMESTAMP I see the followig picture which says there is a timestampValue() method.
Also, when I look into the Oracle Thin driver JAR file using a Java Decompiler I see the signature of the method
As so many times in the ColdFusion world: Ben Nadel to the rescue.
I already tried to call timestampValue() using the CFINVOKE tag like
<cfinvoke component="#timestampfld#" method="timestampValue" returnvariable="ts"></cfinvoke>
but was presented with an exception
The component attribute can either be a string (component name) or a component object
The component attribute in cfinvoke tag has invalid value.
Said blog describes how to use the function instead of the tag:
fortunately, invoke() works with any non-String Java object where the invoke() signature is disambiguated
This made me write the following code, which actually outputs a nicely formatted date
#dateTimeFormat( invoke( timestampfld, "timestampValue" ), "dd.mm.yyyy HH:nn:ss.L" )#
Related
I have to dynamically load tables for which I was passing through controller and when I have to write hibernate peristance logic of suppose createCriteria, I gave:
getSession().createCriteria( Class.forName(tableName)).list()
where tableName is a string which would contain the name of the class. Even then I was unscessful and getting ClassNotFound Exception. It works for
getSession().createCriteria(Book.class).list() which I feel is hardcoding in my case. Please help regarding with which I could dynamically call tables.
Are you passing the exact classname? Like com.myproject.Myclass or you are just writing the table name????
You should use the complete classname to use with Class.forName(..) method.
I have a Controller Unit test using Mockito and MockMvc.
After a POST request, the POSTed object is resolved correctly, but my repository mock is not triggered.
Here is the mock code:
Date mydate = new Date();
Notification not = new Notification();
not.setId(-1L);
not.setUserid("BaBlubb");
not.setTimestamp(mydate);
not.setContent("MyContent");
Notification not2 = new Notification();
not2.setId(1L);
not2.setUserid("BaBlubb");
not2.setTimestamp(mydate);
not2.setContent("MyContent");
when(notificationRepository.save(not)).thenReturn(not2);
So this really just should simulate the saving of an object (ID is set and a route is generated out of it).
Unfortunately, the repository always returns null, so my code later fails when trying to return a new created Route out of null.
The mocks are correctly injected and do work for e.g. String comparison or if I only check for the function to have been called, I just can't get it to trigger on Objects.
The same issue occurs on
verify(notificationRepository, times(1)).save(not);
it does not trigger.
The questions would be:
1.) Why does the mock not trigger? I suppose it does not check for value equality in the object, but for object identifiers, which are not the same since the object is serialized and de-serialized in between.
2.) How can I get a generic mock? e.g. whenever repository.save() is called, no matter the parameter, it always should perform a specific way, e.g. instead of
when(notificationRepository.save(not)).thenReturn(not2);
I'd like to have
when(notificationRepository.save()).thenReturn(not2);
P.S. If for whatever reason you need the rest of the code, here is the submitted part, object is the json representation of the notification (with jackson)
mockMvc.perform(post("/api/notification").content(object)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON));
and here is the Controller header, the Object is de-serialized perfectly, the values are 1:1 the same
#RequestMapping(method=RequestMethod.POST)
public ResponseEntity<?> postNotification(#RequestBody Notification n) {
logger.debug("Saving userid "+n.getId());
Thanks for helping.
1.) Why does the mock not trigger? I suppose it does not check for value equality in the object, but for object identifiers, which are not the same...
By default, Mockito delegates to your object's equals method. If you haven't overridden that, then it checks references by default. The following two lines are equivalent:
when(notificationRepository.save(not)).thenReturn(not2);
when(notificationRepository.save(not)).thenReturn(eq(not2)); // uses eq explicitly
If all Notification objects with the same fields are the same, then overriding equals and hashCode will get you where you need to go. Beware, though, that this may have unintended side-effects with regard to Set and Map behavior, especially if your Notification objects don't have an ID until they are saved.
2.) How can I get a generic mock? e.g. whenever repository.save() is called, no matter the parameter, it always should perform a specific way
With Matchers, this is very simple:
when(notificationRepository.save(not)).thenReturn(any(Notification.class));
Though Matchers are very powerful, beware: they have some tricky rules associated with their use.
For (1), as mentioned by Jeff, You might need to use eq() instead of direct reference to not1
For (2) You can use Mockito.any()
For e.g. when(notificationRepository.save(any(Notification.class))).thenReturn(not2);
This would create stubbing on mocked object notificationRepository which would always return not2 for any argument which is of type Notification. if save() method accepts Object then you can writewhen(notificationRepository.save(any(Object.class))).thenReturn(not2); which would return not2 for any argument of type Object
I have been trying to use BLToolkit to activate an Oracle stored procedure which takes a User Defined Type as an argument as an output parameter and changes it.
I have managed to do this on a primitive type, and and also by manually calling SetSpCommamd however I would like to use the abstract class generation method but can't seem to get it to work.
I'm pretty sure the code I wrote is correct (works for the primitive). When debugging I found the SetSpCommamd called by the generated code gets wierd parameters instead of the ones I provided as opposed to when I call the method manually (the it gets the exact parameters I'd like). I wish I could see the code generated by the reflection emit to see what's wrong there.
Can anyone please help me figure out why this is not working?
Found the problem (Potentially a bug in BLToolkit).
BLToolkit does not pass the UDT Class as is to the procedure (instead it tries to flatten it or something and pass the insides of the object). I Changed the object to a Struct instead of a Class and that fixed it.
Later on I also changed it back to class and made a patch in the 'IsScaler()' method in BLToolkits code.
I will report this as a Bug I hope they fix it.
I am trying to understand what BeanPropertyBindingResult does in the following code. Unfortunately, the javadoc is quite useless.
Please take a look at the following code:
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item");
validator.validate(item, errors);
My questions are:
As far as I can see, BeanPropertyBindingResult is basically some kind of a Map that can contain key/value pairs of (field name, error text). Is this correct, or is the truth more complicated?
When I create a new BeanPropertyBindingResult, why do I need to provide it (as the constructor's first parameter) with the object I am going to validate? As far as I can see, in the second line above, validator.validate(item, errors); the validator gets the object anyway.. so why doing it twice?
The javadoc says about the constructor's second parameter:
objectName - the name of the target object
yes, but why do I need that name? What am I supposed/able to do with it...?
1) Yes, that is my understanding too, even if it is technically a list. -- The most importent part is List<ObjectError> errors defined in the superclass AbstractBindingResult.
2) Because it is demanded by the BindingResult Interface. -- I know this is not a good answer, but If this interfaces requires that method, then ther is no otherway to implement it BTW: I think I have seen some example before where the Autor used null for that field, but I am not 100% if it works correct, but most of the code seams to be able to handle the null value.
3) If you use that binding result for example in a jsp to show the error messages for different input fields, then this must match the model attribute name.
Assume you have a command object with a field name. And a JSP page where the input filed is associated to myCommand.name. Then you need the name myCommand as some kind of prefix for the binding errors. -- It is hard to explain, I hope you understand what I mean
Important The question is not "What does Queryable.OfType do, it's "how does the code I see there accomplish that?"
Reflecting on Queryable.OfType, I see (after some cleanup):
public static IQueryable<TResult> OfType<TResult>(this IQueryable source)
{
return (IQueryable<TResult>)source.Provider.CreateQuery(
Expression.Call(
null,
((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(
new Type[] { typeof(TResult) }) ,
new Expression[] { source.Expression }));
}
So let me see if I've got this straight:
Use reflection to grab a reference to the current method (OfType).
Make a new method, which is exactly the same, by using MakeGenericMethod to change the type parameter of the current method to, er, exactly the same thing.
The argument to that new method will be not source but source.Expression. Which isn't an IQueryable, but we'll be passing the whole thing to Expression.Call, so that's OK.
Call Expression.Call, passing null as method (weird?) instance and the cloned method as its arguments.
Pass that result to CreateQuery and cast the result, which seems like the sanest part of the whole thing.
Now the effect of this method is to return an expression which tells the provider to omit returning any values where the type is not equal to TResult or one of its subtypes. But I can't see how the steps above actually accomplish this. It seems to be creating an expression representing a method which returns IQueryable<TResult>, and making the body of that method simply the entire source expression, without ever looking at the type. Is it simply expected that an IQueryable provider will just silently not return any records not of the selected type?
So are the steps above incorrect in some way, or am I just not seeing how they result in the behavior observed at runtime?
It's not passing in null as the method - it's passing it in as the "target expression", i.e. what it's calling the method on. This is null because OfType is a static method, so it doesn't need a target.
The point of calling MakeGenericMethod is that GetCurrentMethod() returns the open version, i.e. OfType<> instead of OfType<YourType>.
Queryable.OfType itself isn't meant to contain any of the logic for omitting returning any values. That's up to the LINQ provider. The point of Queryable.OfType is to build up the expression tree to include the call to OfType, so that when the LINQ provider eventually has to convert it into its native format (e.g. SQL) it knows that OfType was called.
This is how Queryable works in general - basically it lets the provider see the whole query expression as an expression tree. That's all it's meant to do - when the provider is asked to translate this into real code, that's where the magic happens.
Queryable couldn't possibly do the work itself - it has no idea what sort of data store the provider represents. How could it come up with the semantics of OfType without knowing whether the data store was SQL, LDAP or something else? I agree it takes a while to get your head round though :)