0

我有以下课程:

package backend.link

import org.bson.types.ObjectId
import com.novus.salat.annotations.raw.Salat
import com.novus.salat.dao.ModelCompanion
import com.novus.salat.dao.SalatDAO
import model.hybrid.HybridEntity
import play.api.Play.current
import se.radley.plugin.salat.mongoCollection
import com.novus.salat.annotations.raw.Key
import backend.data.MongoDBLayer
import com.mongodb.casbah.commons.MongoDBObject
import backend.data.SpaceDao
import backend.data.PageDao
import backend.data.UserDao
import se.radley.plugin.salat._
import org.bson.types.ObjectId
import com.novus.salat.{ TypeHintFrequency, StringTypeHintStrategy, Context }
import play.api.Play
import play.api.Play.current

/*
  Adding a custom Salat context to work with Play's Classloader
  Using example from:
  https://github.com/leon/play-salat/blob/master/sample/app/models/mongoContext.scala
*/
package object mongoContext {
  implicit val context = {
    val context = new Context {
      val name = "global"
      override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
    }
    context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id")
    context.registerClassLoader(Play.classloader)
    context
  }
}
import mongoContext._

/**
 * class for storing hybridLInks
 */
case class HybridLink(
  @Key("_id") id: ObjectId = new ObjectId,
  val name: String,
  val origin_id: String,
  val origin_type: String,
  val target_id: String,
  val target_type: String)

/**
 * the companion object for HybridLink that acts as a Dao
 */
object HybridLink extends ModelCompanion[HybridLink, ObjectId] {

  def collection = MongoDBLayer.mongoDB("hybridlink")
  val dao = new SalatDAO[HybridLink, ObjectId](collection) {}

  def apply(name: String, origin: HybridEntity, target: HybridEntity): HybridLink =
    {
      HybridLink(null, name, origin.id, origin.getClass().getName(), target.id, target.getClass().getName())
    }

  /**
   * finds all Objects that are pointing to a certain HybridEntity
   */
  def findByReferenced(entity: HybridEntity): List[HybridEntity] = {
    val it = find(MongoDBObject("target_id" -> entity.id, "target_type" -> entity.getClass().getName()))
    val linkList = it.toList
    for (link <- linkList)
      yield this.getHybridEntitybyClassandId(link.origin_type, link.origin_id)

  }

  /**
   * finds all Objects that are pointed at from a certain hybridEntity
   */
  def findReferencing(entity: HybridEntity): List[HybridEntity] = {
    val it = find(MongoDBObject("origin_id" -> entity.id, "origin_type" -> entity.getClass().getName()))
    val linkList = it.toList
    for (link <- linkList)
      yield this.getHybridEntitybyClassandId(link.target_type, link.target_id)
  }

  /**
   * finds a list of all outgoing Links
   */
  def findReferencinglinks(entity: HybridEntity): List[HybridLink] = {
    val it = find(MongoDBObject("origin_id" -> entity.id, "origin_type" -> entity.getClass().getName()))
    val linkList = it.toList
    return linkList
  }

  /**
   * returns a list of all links that reference the specified HbyridEntity
   */
   def findIncominglinks(entity: HybridEntity): List[HybridLink] = {
    val it = find(MongoDBObject("target_id" -> entity.id, "target_type" -> entity.getClass().getName()))
    val linkList = it.toList
    return linkList
  }

  /**
   * retrieves the corresponding HbyridEntity by id and type
   */
  def getHybridEntitybyClassandId(cls: String, id: String): HybridEntity =
    cls match {
      case "model.hybrid.Space" => SpaceDao.findById(id)
      case "model.hybrid.Page" => PageDao.findById(id)
      case _ => throw new IllegalArgumentException("couldnt find corresponding dao for class " + cls)
    }

  /**
   * helper method for removing all references that are pointing to a specified HybridEntity
   */
  def removeAllReferences(entity: HybridEntity): Unit =
    {
      remove(MongoDBObject("target.id" -> entity.id, "target._typeHint" -> entity.getClass().getName()))
    }

}

如您所见,它使用自定义上下文来处理 play 的类加载器。

但是我无法在 Junit 测试中访问它。

我收到错误消息:“java.lang.NoClassDefFoundError:无法在 backend.core.search.LinkService$.linkObjects(LinkService.scala:31) 初始化类 backend.link.HybridLink$”

任何关于如何解决这个问题的想法将不胜感激 -

4

1 回答 1

0

这对我来说很愚蠢,因为我什至没有包括测试方法:/(对此感到抱歉)

我只是忘了补充:

@Test def testImport() {
    running(FakeApplication()) {
      //test code goes here
    }

现在一切都像魅力一样。

于 2013-08-24T00:42:26.000 回答