akka with play framework - java

still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks

Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.

Related

How to create a simple android test case for a utiltiy class

I've got a utility class that I've created:
package com.g2.quizification.utils;
import com.g2.quizification.domain.Question;
public class ParsingUtils {
public static Question parse(String raw) {
Question question = new Question();
//TODO: parse some stuff
return question;
}
}
...that lives here:
I've also followed the tutorials and created a testing app, that looks like this:
And here's my test code, just waiting for some good 'ole TDD:
package com.g2.quizification.utils.test;
import com.g2.quizification.domain.Question;
import com.g2.quizification.utils.ParsingUtils;
public class ParsingUtilsTest {
public void testParse() {
String raw = "Q:Question? A:Answer.";
Question question = ParsingUtils.parse(raw);
//assertEquals("Question?", question.getQuestion());
//assertEquals("Answer.", question.getAnswer());
}
}
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
The best approach for test project is to add the test project so that its root directory tests/ is at the same level as the src/ directory of the main application's project. If you are using junit4 and eclipse, you can just right-click on the util class you want to test and choose New -> JUnit Test Case.
Basically I would expect a new test class named ParsingUtilTest under the source folder tests/ and within the package com.g2.quizification.utils.test. The test class should extend TestCase and each method you want to test in that util class should have a new method in the test class with the name preceded with "test". I mean to say, suppose you have a method name in ParsingUtils called parseXml. The test method name in ParsingUtilsTest (which Extend 'TestCase') should be named testParseXml
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
Yes, as long as the class your are testing has nothing to do with android apis. And if you do need to test code with android api dependencies, for example, testing a view or an activity, you might want to have a try with robolectric. It's faster than the ones that extend ActivityUnitTestCase.
I have been playing with robolectric a lot (to do TDD on android), and so far, I prefer version 1.1 or 1.2 to 2.x, more stable and run fast.
Besides the tools mentioned above, there are many practices for writing good test cases, naming conventions, code refactoring and such.
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
Its good to begin with small steps, xUnit Test Patterns: Refactoring Test Code and Extreme Programming Explained are some good books for your reference.

Play Framework custom java extensions for template

I am using Play Framework 1.2.5, and trying to use DateTime from Joda Time instead of the usual java.util.Date. I am trying to implement a format method for use in my views.
The Play documentation says I can create my own custom java extensions for use in templates, but it doesn't seem to be working for me. I have followed the example in the docs to no avail.
My custom extension:
package ext;
import org.joda.time.DateTime;
import play.templates.JavaExtensions;
public class DateTimeExtensions extends JavaExtensions {
public static String format(DateTime datetime, String format) {
return datetime==null ? "" : datetime.toString(format);
}
}
My template code:
${subProject?.startDate?.format('yyyy-MM-dd')}
And the error I am receiving:
Exception raised was MissingMethodException : No signature of method: org.joda.time.DateTime.format() is applicable for argument types: (java.lang.String) values: [yyyy-MM-dd]
It looks like Play isn't detecting my custom extension as the documentation says it should. Does anyone have any suggestions on how to make this work?
Your extension class looks good to me. The documentation states that you have to restart your application for the extension to become active. If that doesn't work, try running play clean. Doing so deletes temporary files, including cached bytecode, which will hopefully resolve your issue.

Eclipse: Scala cannot access Java Class members - just after some cleanup

Im trying to use Scala and Java in one project. Im working with the Scla IDE for Eclipse. I have two packages in my Scala Project: one for my scala code and one for my java code.
Now lets say I create new JavaClass with one static member.
package javastuff;
public class MyJavaClass {
public static String MESSAGE = "Im Java";
}
After that Im trying to get access to this variale and somehow I cannot. Funny thing, because scala is able to see the Java class "MyJavaClass" just not able to see MESSAGE.
import javastuff.MyJavaClass
object Main {
def main(args: Array[String]) {
println(MyJavaClass.MESSAGE)
}
}
Value MESSAGE is not a member of object javastuff.MyJavaClass
If I use Project/Clean... 1-2x times eclipse is maybe starting realizing that the member MESSAGE is really there and everything is fine. Is this normal? Maybe Im doing something wrong, I know eclipse is really a bad IDE and I should maybe try IntelliJ, but somehow I like eclipse and I would like to use later some of my favorite plugins, thats why I would not change the IDE just because of this problem. Any ideas how to handle this problem better?
Scala doesn't have any static fields. Here is a blogpost about it
btw. public static without final is pretty bad design (no encapsulation => possible memory leaks)

Why can't I render an object to my view in Play 2?

I think I have misunderstood something about the Play 2 framework.
In my Application Controller I fetch a Company object from the DB
and I would like to make some operations on it in my view.
companyView.scala.html:
#(company: Company)
#main("Welcome to Play 2.0") {
<h1>#{company.name}</h1>
}
Application Controller:
package controllers;
import models.Company;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result company(String rest) {
Company company =
Company.find.where().ilike("restfulIdentifier.identifier", rest).findUnique();
return ok(companyView.render(company));
}
}
But return ok(companyView.render(company)); results in compilation error since companyView.render wants a string.
If I look at the forms sample application:
/**
* Handle the form submission.
*/
public static Result submit() {
Form<Contact> filledForm = contactForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(form.render(filledForm));
} else {
Contact created = filledForm.get();
return ok(summary.render(created));
}
}
There is no problem with rendering an object. I guess that the solution is fairly simple and
that I have missed some crucial part of the documentation. Please explain this to me!
My steps in this case would be as follows:
Change the scala template, we hve to tell the scala templates the our Company belongs to the model class: (but also change to #company.name as suggested by Jordan.
#(company: models.Company)
#main("Welcome to Play 2.0") {
<h1>#company.name</h1>
}
run command play clean
Then run play debug ~run
By executing play debug ~run you will trigger to compile the the play application on each SAVE of one of your project files.
NOTE: The Play templates are basically functions. These functions needs to be compiled and everything used in these functions needs to be declared before use. Just as in regular Java development.
The fact that the your render object wants a string could be the result of:
#(company: Company) could not be resolved to the model Company.
The last compilation had a #(company: String)
Good luck!
I don't know if this will fix your problem or not but it's worth a try. Try removing changing:
#{company.name}
to:
#company.name

Scala trouble accessing Java methods

So, I have something written in Java, and I want to extend it in Scala... The issue I'm running into is that Scala isn't seeing methods I need.
Here is how it's set up:
Player extends Mob, and Mob extends Entity.
I need to access a method in Player that isn't defined in Mob or Entity, but Scala doesn't think it exists even though Java does.
It can see methods defined by Mob and Entity just fine. Also, all the methods I'm talking about are non-static.
So, am I doing something wrong, or is this a limitation imposed by Scala?
Edit --
Here is the relevant code:
package test
import rsca.gs.model.Player
object Test {
def handle(p:Player): Unit = {
p.getActionSender().sendTeleBubble(0, 0, false);
}
}
Player class:
package rsca.gs.model;
// imports
public final class Player extends Mob {
// Implemented methods (not going to post them, as there are quite a few)
// Relevant code
private MiscPacketBuilder actionSender;
public MiscPacketBuilder getActionSender() {
return actionSender;
}
}
Error:
value getActionSender is not a member of rsca.gs.model.Player
I never encountered such problems, and you probably checked your configuration and everything else twice, so I would guess this is some Eclipse related build issue. You should try to build from the command line in order to see whether Scala or Eclipse is the problem.
Is it possible for you to run a test against the class just to see if you got the right one?
p.getClass.getMethods
... and if possible (may run into NPE) in order to find the source:
p.getClass.getProtectionDomain.getCodeSource.getLocation.getPath
When compiling the Scala class, do something like this:
scalac *.scala *.java
This way, Scala will look a the Java code to see what is available. If, however, the Java code is already compiled and provided as a jar file, just add it to the classpath used when compiling the Scala code.

Categories

Resources