【问题标题】:AEM JUnit Test with a DAM Asset使用 DAM 资产进行 AEM JUnit 测试
【发布时间】:2017-06-28 16:08:32
【问题描述】:

我尝试在 AEM 中使用 JUnit 测试来测试一些 DAM 资产。

起初,我将现有资产作为 JSON 复制到资源文件中。 接下来,我使用 ContentLoader 将 JSON 文件写入模拟存储库。

final ContentLoader contentLoader = new ContentLoader(this.getResourceResolver());
contentLoader.json("content/assets/testAsset.json", "/content/assets/testAsset");

然后,我使用 ResourceResolver 来解析destinationPath 中的Resource。

Resource assetRecource = resourceResolver.resolve("/content/assets/testAsset");

到目前为止一切顺利。 当我尝试将assetRecource 调整为Asset.class 时,结果为null。

Asset asset = assetRecource.adaptTo(Asset.class); // is null

在我看来,这是因为缺少演绎版。

有人知道如何解决这个问题吗? 也许使用 ContentLoader#binaryResource()?

【问题讨论】:

  • 我想testAsset json 的主要类型为dam:Asset。您也可以尝试检查被解析的资源是否具有相同的资源类型。

标签: java junit aem dam


【解决方案1】:

检查资源的resourceType是否为dam:Asset

Resource assetRecource = resourceResolver.resolve("/content/assets/testAsset");
DamUtil.isAsset(assetRecource);   // This would be true if the asset is dam:Asset
assetRecource.getResourceType();  // This would return the resourceType of the resource.

adapto 为 null 的可能原因是资源不能适应 Asset Class。

参考:

  1. https://docs.adobe.com/content/docs/en/cq/5-6-1/javadoc/com/day/cq/dam/api/Asset.html
  2. http://sling.apache.org/apidocs/sling5/org/apache/sling/api/adapter/Adaptable.html#adaptTo(java.lang.Class)

【讨论】:

  • 可悲的是,这不是错。 resourceType 是 'dam:Asset'。
  • 但是 DamUtil 抛出 NoClassDefFoundError 并导致 ImageReadException。
  • @Unkall - DamUtil 的 isAsset 方法只是检查资源类型。它不应该抛出 NoClassDefFoundError。如果您可以提供一些您实际使用的代码和错误日志,那就太好了。 NoClassDefFoundError Reference
【解决方案2】:

您需要添加以下依赖项来测试 DAM 资产:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-imaging -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-imaging</artifactId>
            <version>1.0-R1534292</version>
        </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2017-05-12
    • 2023-04-11
    • 2014-05-28
    相关资源
    最近更新 更多