在引用 Felix OSGI 捆绑 JUnit 代码的 Adob​​e CQ5 CRXDE 中编译单元测试

发布于 2024-12-27 05:09:53 字数 1504 浏览 4 评论 0原文

我想编写一些在 Adob​​e CQ 5.4 中运行的单元测试。我正在按照本文所述在 CQ 中进行测试:

http://jtoee.com/2011/ 09/799/

但是,在我在 Java 代码中创建单元测试类后,它不会在 CRXDE 中编译,因为它无法解析 org.junit 命名空间。我按照描述(Apache Sling JUnit Core)在 Felix 中安装并激活了 JUnit 捆绑包,但我猜测我还需要做一些其他事情才能在 CRXDE 中找到这个活动的 Felix 捆绑包。我连接的 CQ5 实例中的 Felix 捆绑包显示了这些导出的包:

junit.framework,version=4.8.2
org.apache.sling.junit,version=1.0.7.SNAPSHOT
org.apache.sling.junit.annotations,version=1.0.7.SNAPSHOT
org.junit,version=4.8.2
org.junit.matchers,version=4.8.2
org.junit.rules,version=4.8.2
org.junit.runner,version=4.8.2
org.junit.runner.manipulation,version=4.8.2
org.junit.runner.notification,version=4.8.2
org.junit.runners,version=4.8.2
org.junit.runners.model,version=4.8.2

在下面的示例单元测试代码中,最后三个导入语句“无法解析”。

import org.apache.sling.api.resource.*;
import org.junit.*;
import org.junit.runner.*;
import org.apache.sling.junit.annotations.*;

@RunWith(SlingAnnotationsTestRunner.class)
public class MyUnitTest {

    public ResourceResolver getResourceResolver() {
        try {
            return getResourceResolverFactory().
                    getAdministrativeResourceResolver(null);
        } catch (LoginException e) {
            fail(e.toString());
        }
        return null;
    }
}

据我所知,Felix 中安装的 OSGI 捆绑包应该可供我使用 CRXDE 在我的 Java 类中引用,但对于我安装的 JUnit 捆绑包却不会发生这种情况。为什么不呢?我需要做什么才能让 CRXDE 找到 OSGI 捆绑包参考并在 CRXDE 中进行编译?

I want to write some unit tests that run within Adobe CQ 5.4. I am doing what is described in this article for testing within CQ:

http://jtoee.com/2011/09/799/

However, after I create the unit test class in my Java code, it won't compile within CRXDE because it can't resolve the org.junit namespaces. I installed and activated the JUnit bundle in Felix as described (Apache Sling JUnit Core), but I am guessing there is something else I need to do in order for this active Felix bundle to be found in CRXDE. The Felix bundle in the CQ5 instance I am connected to shows these exported packages:

junit.framework,version=4.8.2
org.apache.sling.junit,version=1.0.7.SNAPSHOT
org.apache.sling.junit.annotations,version=1.0.7.SNAPSHOT
org.junit,version=4.8.2
org.junit.matchers,version=4.8.2
org.junit.rules,version=4.8.2
org.junit.runner,version=4.8.2
org.junit.runner.manipulation,version=4.8.2
org.junit.runner.notification,version=4.8.2
org.junit.runners,version=4.8.2
org.junit.runners.model,version=4.8.2

In this sample unit test code below, the last three import statements "cannot be resolved."

import org.apache.sling.api.resource.*;
import org.junit.*;
import org.junit.runner.*;
import org.apache.sling.junit.annotations.*;

@RunWith(SlingAnnotationsTestRunner.class)
public class MyUnitTest {

    public ResourceResolver getResourceResolver() {
        try {
            return getResourceResolverFactory().
                    getAdministrativeResourceResolver(null);
        } catch (LoginException e) {
            fail(e.toString());
        }
        return null;
    }
}

It is my novice understanding that the OSGI bundle installed in Felix should be accessible for me to reference in my Java classes using CRXDE, but it isn't happening for the JUnit bundle I installed. Why not? What do I need to do to get CRXDE to find the OSGI bundle reference and compile within CRXDE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2025-01-03 05:09:53

乍一看你所做的事情看起来是正确的。

您是否尝试在安装所需的捆绑包后重新启动 CQ?从理论上讲,这不应该是必需的,但我想知道捆绑编译器是否正确地选择了新可用的包。

我已经在 http 上传了一个包含类似简单示例的内容包://dl.dropbox.com/u/715349/cq5-examples/junit-tests-1.0.zip(md5 2915123ad581aa225bd531247ea02878),在新的 CQ 5.4 实例上安装此软件包后,示例测试通过 http:// 正确执行localhost:4502/system/sling/junit/

您可能想尝试我的示例并与您的进行比较。

What you're doing looks correct at first sight.

Did you try restarting CQ after installing the required bundles? In theory that should not be required but I'm wondering if the bundle compiler is picking up the newly available packages correctly.

I have uploaded a content package with a similar simple example at http://dl.dropbox.com/u/715349/cq5-examples/junit-tests-1.0.zip (md5 2915123ad581aa225bd531247ea02878), after installing this package on a fresh CQ 5.4 instance the example test is correctly executed via http://localhost:4502/system/sling/junit/

You might want to try my sample and compare with yours.

空心↖ 2025-01-03 05:09:53

简答

问题不在于 CQ,而在于 CRXDE。 CRXDE 会自动下载所需的 jar 文件并将其缓存在本地计算机上,因此不必不断从 CQ 检索它们。

如果您切换到“Package Explore”导航,然后展开项目“{SERVER}{PORT}{HASH}”,您应该会看到一个名为“Referenced Libraries”的文件夹。右键单击并选择构建路径>>配置构建路径。从那里您可以将任何您想要的依赖项添加到项目中。

长答案

CRXDE 不是创建捆绑包的好工具。最好通过成熟的 IDE(例如 Eclipse)创建捆绑包并利用 Apache Maven 作为构建工具。 Apache Maven 可以自动管理您的依赖项、对您的代码运行测试并将测试与运行时依赖项分开。

这样您就可以避免将您并不真正需要这样的 jUnit 的依赖项加载到 OSGi 控制台中,并且您可以更好地控制捆绑包的构建和部署方式。

Day 有一个非常好的指南,帮助您设置使用 Eclipse 构建 CQ 项目。
http://dev.day.com/docs/v5_2/html -resources/cq5_guide_developer/ch04s02.html

Short Answer

The problem is not with CQ, the problem is with CRXDE. CRXDE automatically downloads and caches required jar files on your local machine so they don't have to be retrieved constantly from CQ.

If you switch to the 'Package Explore' navigation and then expand the project '{SERVER}{PORT}{HASH}' you should see a folder called Referenced Libraries. Right click and select Build Path >> Configure Build Path. From there you can add any dependencies you want into the project.

Long Answer

CRXDE is not a good tool for creating bundles. It is much better to create bundles through a full fledged IDE such as Eclipse and utilize Apache Maven as a build tool. Apache Maven can automatically manage your dependencies, run tests on your code and separate test vs. runtime dependencies.

That way you can avoid having to load dependencies that you don't really need such a jUnit into your OSGi console and you have more control over how your bundle is built and deployed.

Day has a really nice guide to getting you set up with building CQ projects with Eclipse.
http://dev.day.com/docs/v5_2/html-resources/cq5_guide_developer/ch04s02.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文