Using onMissingMethod cannot access object variables - coldfusion

In CF10, I want to access a variable Test object using onMissingMethod function in TestHelper object, but I am getting an error.
Test.cfc
component {
public Any function init(){
instance = { x = 1 };
return this;
}
public numeric function getX(){
return instance.x;
}
}
TestHelper.cfc
component {
public Any function init( ){
variables.testObj = new Test();
return this;
}
public any function onMissingMethod( required string missingMethodName, required struct missingMethodArguments ){
var func = variables.testObj[ arguments.missingMethodName ];
return func( argumentCollection = arguments.missingMethodArguments );
}
}
Calling the object
obj = new TestHelper();
writeOutput( obj.getX() ); //Element INSTANCE.X is undefined in VARIABLES
In CF10, this gives me an error that element X is undefined in instance. It doesn't seem to recognize the variable instance. I could explicitly define getX function in TestHelper, but I was hoping I could use the onMissingMethod function.
Am I misunderstanding how onMissingMethod supposed to work here? FWIW, the code works in Railo.

If I understand your issue, I'm surprised this code runs on Railo. I don't think it should.
The issue is with this code:
var func = variables.testObj[ arguments.missingMethodName ];
return func( argumentCollection = arguments.missingMethodArguments );
Here you are pulling the function getX() out of variables.testObj, and running it in the context of your TestHelper instance. And that object doesn't have a `variables.x. Hence the error.
You need to put your func reference into variables.testObj, not pull getX out of it. So like this:
var variables.testObj.func = variables.testObj[ arguments.missingMethodName ];
return variables.testObj.func( argumentCollection = arguments.missingMethodArguments );
That way you're running func() (your proxy to getX()) in the correct context, so it will see variabales.x.
Given this situation, there's no way this code should work on Railo (based on the info you've given us being all the relevant info, anyhow).

Related

how can i store the value returned by my init to be used in my functions

I have the init code and it returns me a structure
public any function init() {
httpService = new http();
httpService.setUrl("#Application.baseURL#security/oauth2/token");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
httpService.addParam(type="body", value="client_id=#application.clientID#&client_secret=#application.clientsecretID#&grant_type=#application.grant_type#");
result = httpService.send().getPrefix();
return this;
}
problem how can i use the token returned by the method in other methods, if i dump the init, i am just getting the functions, how can i use the data returned by the http
just not getting in my head, because the token is alive for 3600
Thanks
As James says, you should store the result of the http call as an instance variable. Here's one way of doing it using a property and specifying accessors=true for the component so that you can call setHttpResult() and getHttpResult() without having to write those methods. using the variables scope which will make it available to other methods within the component, but not outside.
/* Test.cfc */
component name="test"{
property name="httpResult" type="struct";
public any function init(){
//use "var" to ensure the variable is local to the function only
var httpService = new http();
httpService.setUrl("#Application.baseURL#security/oauth2/token");
httpService.setMethod("POST");
httpService.addParam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
httpService.addParam(type="body", value="client_id=#application.clientID#&client_secret=#application.clientsecretID#&grant_type=#application.grant_type#");
//store the result privately in the instance
variables.httpResult = httpService.send().getPrefix();
return this;
}
public void function someOtherMethod(){
// this method can access the result struct
var returnedContent = variables.httpResult.fileContent;
}
}
You can then use getHttpResult() inside or outside your component. For example from an external script:
test = New test(); // calls the init() method
WriteDump( test.getHttpResult() ); //auto-generated "getter"

Unit test of a singleton accessed as an interface

I have a singleton in my application, but it's published not as a struct directly, but as an interface (because I want to be able to dynamically select the particular implementation upon singleton initialization). Here's the code:
var once sync.Once
var instance defaultConfiguration
type Configuration interface {
GetFoo() string
}
type defaultConfiguration struct {
}
func (dc defaultConfiguration) GetFoo() string {
return "foo"
}
func NewConfiguration() Configuration {
once.Do(func() {
instance = defaultConfiguration{}
})
return instance
}
Then I decided to write a unit-test that would check that NewConfiguration() will actually return the same instance each time:
func TestNewConfigurationSameInstance(t *testing.T) {
configuration1 := NewConfiguration()
configuration2 := NewConfiguration()
if &configuration1 != &configuration2 {
t.Error()
}
}
I thought it would make sense to compare the addresses of the returned instances, however, this test fails.
Then I thought, well, maybe I have to return a pointer to an instance, so I've changed the code to look like this:
func NewConfiguration() *Configuration {
once.Do(func() {
instance = defaultConfiguration{}
})
return &instance
}
But this doesn't even compile: it fails with the error message
cannot use &instance (type *defaultConfiguration) as type *Configuration in return argument:
*Configuration is pointer to interface, not interface
And I've got very confused. Why can't I return a pointer to an interface? Or, why returning defaultConfiguration as Configuration is valid, but returning *defaultConfiguration as *Configuration is not?
And, after all, what is the proper unit-test for my use-case?
Your code should be:
var once sync.Once
var instance *defaultConfiguration
type Configuration interface {
GetFoo() string
}
type defaultConfiguration struct {
}
func (dc *defaultConfiguration) GetFoo() string {
return "foo"
}
func NewConfiguration() Configuration {
once.Do(func() {
instance = &defaultConfiguration{}
})
return instance
}
Since Configuration is an interface and you want a pointer to defaultConfiguration to implement it.
Pointers to interfaces (e.g. *Configuration) are rarely needed. An interface is already a reference value, and it's perfectly fine for a pointer to some type to implement an interface.
For more background on the root issue read this answer or similar resources.

How to get and set value for a object using Shim classes in C# Microsoft fakes unit testing?

Here is my main class which I want to test. It contains one private method.
Public class MyClass
{
private bool IsWorkDone(MyItem item, string name)
{
using (MyThing thingObj = new MyThing(item.ID))
{
using (MyWork workObj = thingObj.Open())
{
try
{
return false;
}
}
}
return true;
}
}
In my test class I have written below method
public void CheckIsWorkDoneTest()
{
using (ShimsContext.Create())
{
MyItem myitem = new ShimMyItem () {
itemGet = () => new ShimMyItem () {
IDGet = () => new Guid();
}
};
ShimMyClass.AllInstances.isWorkDoneItemString = (MyClass, MyItem, MyName) => "Here I'm stuck. I need help from stackoverflow users"
PrivateObject objMyClass = new PrivateObject(typeof(MyClass));
object[] parameters = new object[2] { myItem, workName };
bool result = Convert.ToBoolean(objMyClass.Invoke("IsWorkDone", parameters));
Assert.AreEqual(result,true);
}
}
I want to set the value for oSite object from statement => "using (MyThing thingObj = new MyThing(item.ID)) " from my main MyClass Class.
while debugging this line throws Object reference not set to an instance error.
So using ShimMyClass.Allinstance how can I get or set the value for it?
You have quite a few inconsistencies, so your problem is probably just straitening them out. If you're actual code is more consistent, then update your post. The main things
You show private method IsComplete but call isWorkflowCompleted from your Invoke method
You probably end up calling your shimmed method that will pass "Here I'm stuck" and try to convert that string to a Boolean.
I fudged your skeleton and got mine to work after adjusting some of the names.

Derived Class Method of Generic Class Template not being called

I have a generic class for making and processing JSON API requests. I pass in the TParam and TResult template parameters but when I use a derived type it's implementation is not being called.
Here is some code you can throw in a playground to illustrate:
import Cocoa
// Base class for parameters to POST to service
class APIParams {
func getData() -> Dictionary<String, AnyObject> {
return Dictionary<String, AnyObject>()
}
}
// Base class for parsing a JSON Response
class APIResult {
func parseData(data: AnyObject?) {
}
}
// Derived example for a login service
class DerivedAPIParams: APIParams {
var user = "some#one.com"
var pass = "secret"
// THIS METHOD IS CALLED CORRECTLY
override func getData() -> Dictionary<String, AnyObject> {
return [ "user": user, "pass": pass ]
}
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
var success = false
var token:String? = ""
// THIS METHOD IS NEVER CALLED
override func parseData(data: AnyObject?) {
/*
self.success = data!.valueForKey("success") as Bool
self.token = data!.valueForKey("token") as? String
*/
self.success = true
self.token = "1234"
}
}
class APIOperation<TParams: APIParams, TResult: APIResult> {
var url = "http://localhost:3000"
func request(params: TParams, done: (NSError?, TResult?) -> ()) {
let paramData = params.getData()
// ... snip making a request to website ...
let result = self.parseResult(nil)
done(nil, result)
}
func parseResult(data: AnyObject?) -> TResult {
var result = TResult.self()
// This should call the derived implementation if passed, right?
result.parseData(data)
return result
}
}
let derivedOp = APIOperation<DerivedAPIParams, DerivedAPIResult>()
let params = DerivedAPIParams()
derivedOp.request(params) {(error, result) in
if result? {
result!.success
}
}
The really weird thing is that only the DerivedAPIResult.parseData() is not called, whereas the DerivedAPIParams.getData() method is called. Any ideas why?
UPDATE: This defect is fixed with XCode 6.3 beta1 (Apple Swift version 1.2 (swiftlang-602.0.37.3 clang-602.0.37))
Added info for a workaround when using XCode 6.1 (Swift 1.1)
See these dev forum threads for details:
https://devforums.apple.com/thread/251920?tstart=30
https://devforums.apple.com/message/1058033#1058033
In a very similar code sample I was having the exact same issue. After waiting through beta after beta for a "fix", I did more digging and discovered that I can get the expect results by making the base class init() required.
By way of example, here is Matt Gibson's reduced example "fixed" by adding the proper init() to ApiResult
// Base class for parsing a JSON Response
class APIResult {
// adding required init() to base class yields the expected behavior
required init() {}
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<TResult: APIResult> {
init() {
// EDIT: workaround for xcode 6.1, tricking the compiler to do what we want here
let tResultClass : TResult.Type = TResult.self
var test = tResultClass()
// should be able to just do, but it is broken and acknowledged as such by Apple
// var test = TResult()
println(test.self) // now shows that we get DerivedAPIResult
}
}
// Templated creation creates APIResult
let derivedOp = APIOperation<DerivedAPIResult>()
I do not know why this works. If I get time I will dig deeper, but my best guess is that for some reason having required init is causing different object allocation/construction code to be generated that forces proper set up of the vtable we are hoping for.
Looks possibly surprising, certainly. I've reduced your case to something rather simpler, which might help to figure out what's going on:
// Base class for parsing a JSON Response
class APIResult {
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<TResult: APIResult> {
init() {
var test = TResult()
println(test.self) // Shows that we get APIResult, not DerivedAPIResult
}
}
// Templated creation creates APIResult
let derivedOp = APIOperation<DerivedAPIResult>()
...so it seems that creating a new instance of a templated class with a type constraint gives you an instance of the constraint class, rather than the derived class you use to instantiate the specific template instance.
Now, I'd say that the generics in Swift, looking through the Swift book, would probably prefer you not to create your own instances of derived template constraint classes within the template code, but instead just define places to hold instances that are then passed in. By which I mean that this works:
// Base class for parsing a JSON Response
class APIResult {
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<T: APIResult> {
var instance: T
init(instance: T) {
self.instance = instance
println(instance.self) // As you'd expect, this is a DerivedAPIResult
}
}
let derivedOpWithPassedInstance = APIOperation<DerivedAPIResult>(instance: DerivedAPIResult())
...but I'm not clear whether what you're trying should technically be allowed or not.
My guess is that the way generics are implemented means that there's not enough type information when creating the template to create objects of the derived type from "nothing" within the template—so you'd have to create them in your code, which knows about the derived type it wants to use, and pass them in, to be held by templated constrained types.
parseData needs to be defined as a class func which creates an instance of itself, assigns whatever instance properties, and then returns that instance. Basically, it needs to be a factory method. Calling .self() on the type is just accessing the type as a value, not an instance. I'm surprised you don't get some kind of error calling an instance method on a type.

NULL POINTER error when using ObjectLoad(ObjectSave())

Update this has been filed as a bug in ColdFusion, https://bugbase.adobe.com/index.cfm?event=bug&id=3546237
I've been having a problem with CF9 and NULL POINTER errors that do not appear to be an issue within Railo. I created a simple CFC and associated mxunit unit test to confirm this with.
On Railo (4.0.4) both unit tests pass. On Coldfusion (9.0.1), the unit test getMetaDataBeforeMethodInvocation fails with NULL POINTER error on the GetMetaData call.
At present I can only surmise that CF9 does not have access to the full metadata following ObjectLoad until a method within that component is called. Is anyone able to shed more light on this problem, and/or offer a better solution than to ensure that a method within the object is called prior to doing getMetaData?
Here is the CFC
// NullError.cfc
component {
public NullError function init() {
variables.uuid = CreateUUID();
return this;
}
public string function getUUID() {
return uuid;
}
}
and associated unit test
// NullErrorTest.cfc
component extends='mxunit.framework.TestCase' {
private any function setupTheTests() {
var o = new NullError();
debug(o.getUUID());
// Dump meta data
debug(GetMetaData(o));
// Save and load it, and return
return ObjectLoad(ObjectSave(o));
}
public void function getMetaDataBeforeMethodInvocation() {
var o = setupTheTests();
// Get meta data, and then get uuid, expecting this to ERROR (NULL POINTER)
debug(GetMetaData(o)); // CF FAILS HERE, RAILO DOES NOT
debug(o.getUUID());
}
public void function getMetaDataAfterMethodInvocation() {
var o = setupTheTests();
// Get uuid, and then get meta data, expecting this to be ok
debug(o.getUUID());
debug(GetMetaData(o));
}
}
I can confirm this buggy behaviour in both CF 9.0.2 and 10.0.9.
I'd raise a bug if I was you.
The repro case can be simplified a lot from what you have:
// C.cfc
component {}
<!--- test.cfm --->
<cfscript>
o1 = new C();
writeDump(getMetaData(o1)); // OK
o2 = objectLoad(objectSave(o1));
writeDump(getMetadata(o2)); // breaks
</cfscript>
I don't know what to suggest by way of work-around, given it's so clearly and fundamentally broken.