[2.0] (Java) Bytecode enhancement in "test"

373 views
Skip to first unread message

Mikael Selander

unread,
Apr 17, 2012, 7:53:39 AM4/17/12
to play-fr...@googlegroups.com
Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

Guillaume Bort

unread,
Apr 17, 2012, 10:19:39 AM4/17/12
to play-fr...@googlegroups.com
Hi, yes you are right but it has been fixed in trunk. 
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Mikael Selander

unread,
Apr 18, 2012, 3:10:54 AM4/18/12
to play-fr...@googlegroups.com
Ah, ok. Cool. Would that be a part of 2.1 or a 2.0 service release?

Thanks,
Mikael


Den tisdagen den 17:e april 2012 kl. 16:19:39 UTC+2 skrev Guillaume Bort:
Hi, yes you are right but it has been fixed in trunk. 

On 17 avr. 2012, at 13:53, Mikael Selander <cid...@gmail.com> wrote:

Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Mikael Selander

unread,
Apr 18, 2012, 3:35:02 AM4/18/12
to play-fr...@googlegroups.com
And also, after looking through the commit that allows it to be enabled, I can't really say that I understand how to enable it. Do I need to add an extra parameter to play compile?


Den tisdagen den 17:e april 2012 kl. 16:19:39 UTC+2 skrev Guillaume Bort:
Hi, yes you are right but it has been fixed in trunk. 

On 17 avr. 2012, at 13:53, Mikael Selander <cid...@gmail.com> wrote:

Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
Den tisdagen den 17:e april 2012 kl. 16:19:39 UTC+2 skrev Guillaume Bort:
Hi, yes you are right but it has been fixed in trunk. 

On 17 avr. 2012, at 13:53, Mikael Selander <cid...@gmail.com> wrote:

Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Joel Söderström

unread,
Aug 27, 2012, 1:35:32 PM8/27/12
to play-fr...@googlegroups.com
Hi,

Would also be interested to know if it's possible to "enhance" the test directory, and in that case how? I am currently on 2.0.3-java. 

I am trying to create fixtures for integration tests (as in running a fake application with db), and my workaround is to create them in a separate class in the "app" folder. 
It works OK but it's a hack which I'd prefer not to do. 

Thanks,
Joel



On Wednesday, April 18, 2012 9:35:02 AM UTC+2, Mikael Selander wrote:
And also, after looking through the commit that allows it to be enabled, I can't really say that I understand how to enable it. Do I need to add an extra parameter to play compile?

Den tisdagen den 17:e april 2012 kl. 16:19:39 UTC+2 skrev Guillaume Bort:
Hi, yes you are right but it has been fixed in trunk. 

On 17 avr. 2012, at 13:53, Mikael Selander <cid...@gmail.com> wrote:

Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Den tisdagen den 17:e april 2012 kl. 16:19:39 UTC+2 skrev Guillaume Bort:
Hi, yes you are right but it has been fixed in trunk. 

On 17 avr. 2012, at 13:53, Mikael Selander <cid...@gmail.com> wrote:

Am I correct in assuming that code in the "test"-directory is not enhanced in the same way that code in the "app"-directory is enhanced? I've been having some issues with Ebean not being able to detect dirty entities in my unit-tests. For example:

Entity e = Entity.getById(1L);
e.name = "Foo";

e.save();

assertThat(Entity.getById(1L).name).isEqualTo("Foo"); // Fails

However, if I directly call the magically added e.setName("Foo"), which makes IntelliJ really confused btw :), everything works as expected.

Would it be possible to turn on enhancement in the "test" directory as well? Maybe by an argument in the console or a config value if having it on by default messes with other stuff.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/P1tgQVUBD8kJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Sean Brady

unread,
Aug 27, 2012, 1:55:50 PM8/27/12
to <play-framework@googlegroups.com>
We use the following:

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA, settings = s).settings(
    compile in Test <<= PostCompileTemp(Test)

  )

  def PostCompileTemp(scope: Configuration) = (sourceDirectory in scope, dependencyClasspath in scope, compile in scope, javaSource in scope, sourceManaged in scope, classDirectory in scope, ebeanEnabled) map {
    (src, deps, analysis, javaSrc, srcManaged, classes, ebean) =>

      val classpath = (deps.map(_.data.getAbsolutePath).toArray :+ classes.getAbsolutePath).mkString(java.io.File.pathSeparator)

      val javaClasses = (javaSrc ** "*.java").get.map {
        sourceFile =>
          analysis.relations.products(sourceFile)
      }.flatten.distinct

      javaClasses.foreach(play.core.enhancers.PropertiesEnhancer.generateAccessors(classpath, _))
      javaClasses.foreach(play.core.enhancers.PropertiesEnhancer.rewriteAccess(classpath, _))

      // EBean
      if (ebean) {

        val originalContextClassLoader = Thread.currentThread.getContextClassLoader

        try {

          val cp = deps.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL

          Thread.currentThread.setContextClassLoader(new java.net.URLClassLoader(cp, ClassLoader.getSystemClassLoader))

          import com.avaje.ebean.enhance.agent._
          import com.avaje.ebean.enhance.ant._
          import collection.JavaConverters._
          import com.typesafe.config._

          val cl = ClassLoader.getSystemClassLoader

          val t = new Transformer(cp, "debug=-1")

          val ft = new OfflineFileTransform(t, cl, classes.getAbsolutePath, classes.getAbsolutePath)

          val config = ConfigFactory.load(ConfigFactory.parseFileAnySyntax(new File("conf/application.conf")))

          val models = try {
            config.getConfig("ebean").entrySet.asScala.map(_.getValue.unwrapped).toSet.mkString(",")
          } catch {
            case e: ConfigException.Missing => "models.*"
          }

          try {
            ft.process(models)
          } catch {
            case _ =>
          }

        } catch {
          case e => throw e
        } finally {
          Thread.currentThread.setContextClassLoader(originalContextClassLoader)
        }
      }
      // Copy managed classes - only needed in Compile scope
      if (scope.name.toLowerCase == "compile") {
        val managedClassesDirectory = classes.getParentFile / (classes.getName + "_managed")

        val managedClasses = ((srcManaged ** "*.scala").get ++ (srcManaged ** "*.java").get).map {
          managedSourceFile =>
            analysis.relations.products(managedSourceFile)
        }.flatten x rebase(classes, managedClassesDirectory)

        // Copy modified class files
        val managedSet = IO.copy(managedClasses)

        // Remove deleted class files
        (managedClassesDirectory ** "*.class").get.filterNot(managedSet.contains(_)).foreach(_.delete())
      }
      analysis
  }
}

To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/A3YNH11lE_gJ.

Joel Söderström

unread,
Aug 28, 2012, 6:13:36 AM8/28/12
to play-fr...@googlegroups.com
Worked like a charm, thanks!

I documented this here for future reference. 
To post to this group, send email to play-f...@googlegroups.com.
To unsubscribe from this group, send email to play-framewo...@googlegroups.com.

rafar ms

unread,
Feb 29, 2016, 7:23:55 PM2/29/16
to play-framework, joel.so...@gmail.com
hello,
It seems that since v 2.0.x some options for the build configuration have changed. So the solution proposed here is not working for me, or I'm doing something wrong.

Does anyone tried the configuration mentioned above, but using Play 2.4.x?
I tried putting it into the "scala.sbt" and resulted in a problem with the imports. Maybe the package's names have changed, but i cannot find any references (except for the Play's API, which is only available for the 2.0 version).

Do I have to use "scala.build" instead? (I cannot make it work neither).

Or should I have to forget bytecode enhancement and write getters/setters myself?

thank you.
Reply all
Reply to author
Forward
0 new messages