`
raydian
  • 浏览: 4985 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

在eclipse中利用spring osgi test框架对osgi 的 boundle进行测试

阅读更多

很久前写的一篇文章,已经不记得放在那里了,翻了半天总算找到了,决定放在javaeye当中保存下来,以便以后查阅。

 

http://static.springframework.org/osgi/docs/current/reference/html/testing.html 【1】 中对spring-osgi的测试介绍的应该说是比较到位,但如果对于第一次接触spring-osgi,而且还是刚接触maven的人来说,文章上面说的就有些含糊不清,甚至很多地方容易让人产生误解。让我们先了解一下 spring-osgi的测试集成体系:

      extended by junit.framework.TestCase
          extended by org.springframework.test.ConditionalTestCase
              extended by org.springframework.test.AbstractSpringContextTests
                  extended by org.springframework.test.AbstractSingleSpringContextTests
                      extended by org.springframework.test.AbstractDependencyInjectionSpringContextTests
                          extended by org.springframework.osgi.test.AbstractOptionalDependencyInjectionTests
                              extended by org.springframework.osgi.test.AbstractOsgiTests
                                  extended by org.springframework.osgi.test.AbstractConfigurableOsgiTests
                                      extended by org.springframework.osgi.test.AbstractSynchronizedOsgiTests
                                          extended by org.springframework.osgi.test.AbstractDependencyManagerTests
                                              extended by org.springframework.osgi.test.AbstractOnTheFlyBundleCreatorTests
                                                  extended by org.springframework.osgi.test.AbstractConfigurableBundleCreatorTests

如果我们要写一个spring-osgi的测试类的话集成AbstractConfigurableBundleCreatorTests就可以了。

在这个集成体系当中有四个类比较重要AbstractConfigurableBundleCreatorTests,AbstractDependencyManagerTests,AbstractOsgiTests,AbstractDependencyInjectionSpringContextTests。 其中,

AbstractConfigurableBundleCreatorTests 是我们写测试类的直接集成类;通过复写getTestBundlesNames()方法,来设定测试类所依赖的特定boundle包。boundle的名称在这里说明一下,要符合csv文件的格式规则利用','进行分割,一般分为3部分‘groupID,artifactid,versionNumber’,三者缺一不可,更不要出错,否则将会出现相应的jar包找不到的情况
在 getTestBundlesNames()方法中可能会依赖到一些我们自己些的一些boundle,那么此时,一定要将这个被依赖的boundle部署 到我们的maven repository当中去,否则在运行测试时,将会报告找不到对应的boundle jar包。如何部署,参看相关maven教程中的deploy部分。部署完成后,在本机的maven repository 目录下面将会有相应boundle的jar文件放置其中。

另外在,该类中有三个成员变量,很重要:

 private static final String ROOT_DIR = "root.dir";设定我们测试环境下所使用的class,xml,properties所在目录,默认为“file:./target/test-classes”,这与我们通常eclipse工程当中的build, bin作为class的输出目录不一样,这里一定注意,否则在运行时,会报告该目录不存在 。如何解决我们下面会有说明。

 static final String INCLUDE_PATTERNS = "include.patterns";//设定文件的过滤规则默认为/**/*.class, /**/*.xml,/**/*.properties

 static final String MANIFEST = "manifest";设定manifest.mf所在目录,默认为空。由程序自动创建。

如果需要改变这三个变量的值,只需要在该类的目录下创建一个“类名-bundle.properties ”的配置文件设定相应变量的值即可。如,我们的工程buildpath路径为build,那么在root.dir=‘build’更改所制定的资源目录。

AbstractDependencyManagerTests 负责对测试环境所依赖的boundle负责加载管理,
private static final String TEST_FRRAMEWORK_BUNDLES_CONF_FILE = "/org/springframework/osgi/test/internal/boot-bundles.properties";负责设置测试 框架运行中所依赖的boundle jar包,该文件在spring-osgi-test-*.*.jar包中的对应的目录下面。该配置文件中指定了测试环境下所以来的bounle包。
private ArtifactLocator locator = new LocalFileSystemMavenRepository();该成员是maven的本地boundle jar的加载工具。通过该类负责加载maven repository相应目录下面的boundle jar资源。具体实现可以参看 org.springframework.osgi.test.provisioning.internal.LocalFileSystemMavenRepository 的localMavenBundle()方法。
    在这里提一下LocalFileSystemMavenRepository,该类实现 org.springframework.osgi.test.provisioning.ArtifactLocator接口,在spring2.5中 现在只有一个实现类。LocalFileSystemMavenRepository的作用是根据artifactid和groupid加载maven中 repository的boundle。该类作为一个 org.springframework.osgi.test.AbstractDependencyManagerTests的成员些死在类当中,因 此,这也就是为什么在spring-osgi2.5当中我们必须使用maven的原因了。

 

AbstractOsgiTests 为我们提供测试过程中的osgi运行环境。

AbstractDependencyInjectionSpringContextTests 为我们提供了spring 中application context的支持。我们可以通过复写getConfigLocations()方法设定junit测试类所依存的application context。

在使用spring-osgi的测试框架之前,必须要清楚明白maven的使用,否则就会产生一系列的boundle jar文件找不到的问题。在开始例子之前声明几个注意的地方:

1.spring-osgi是不依赖于具体的osgi平台的,也就是说,不需要先启动一个osgi环境在进行测试运行,因为在spring- osgi中,所有的junit 测试类均为org.springframework.osgi.test.AbstractOsgiTests该类的子类(但具体的junit测试类不直 接集成该类),该类为spring-osgi在测试声明周期内提供osgi工作环境。因此,在eclipse当中使用spring-osgi的测试类,同 使用一般的junit测试类完全相同。在文档【1】中所提及的the scenario supported by Spring-DM testing framework 指的是AbstractOsgiTests类中运行osgi环境的步骤,而这一切对我们osgi测试类来说都是透明的。

2.spring-osgi的测试类对maven的依赖性十分强,如果你打算使用spring-osgi提供给我们的测试类,那么就必须要使用到maven,因此,在开始之前请先去http://maven.apache.org/http://m2eclipse.codehaus.org/ 下载maven和maven在ecplise当中的工具。具体如何配置使用,http://m2eclipse.codehaus.org/Maven_2.0_Plugin_for_Eclipse.html  这里提供了maven的视频教程。但有一点注意,视频上的内容和我们下载的插件是不一样的。插件的安装就不用多说了,不会的去网上搜索如何安装ecplise插件就行了。安装完成之后。点击eplise->windows->preferences

查看maven选项,其中offline为脱机运行,在第一次使用时不要勾掉,因为maven需要到远端的 repository中下载相应需要的jar包,之后的使用的时候把它个勾选上就可以了,否则速度会很慢。绿色的框框是用来对现在已经下载好的jar文件 创建索引用的。另外就是红框中就是设置maven的settings.xml和local repository的地方了。因为我用的是windows系统。这里面一定要注意了。maven在安装的时候默认的就将.m2目录安装到了用户的 document and settings 目录,但安装之后目录下面没有settings.xml文件,需要从maven包中拷贝一份到.m2目录下面。如果需要改变local repository的路径的话,则直接在settings.xml中直接修改。但本人强烈建议不要修改 ,否则当我们在运行junit测试类的时候将会出现找不到repository 目录的情况,因为spring-osgi测试类会去.m2目录下面去找repository的目录。当然也可以进行更改,只需要在每次运行程序的时候设置系统变量'localRepository' 的值为你的repository的目录路径 。 

例子:

1.创建一个simple.service工程

 

2.设定工程的名字,源码目录,输出目录,该输出目录在测试时需要进行更改。选择目标平台为osgi framework standard

3.制定MANIFEST.mf文件中的属性,注意,这里的plug-in options 下面的选择框不要勾选,因为我们是利用spring dynamic module进行开发,不需要创建相应的Activator的实现




4.在src目录下创建test目录作为测试代码目录,作为源码文件夹,工程目录结构图:




5.将工程部署在maven管理之下,选择如图:




6.输入maven的pom文件的属性描述,我们使用默认值。


7. 选择工程所依赖的jar包,如何在你的机器上已经安装了maven 并且repository中有相应的jar包,而且你清楚要导入的jar包时,可以通过add方式进行加入所依赖的jar包,这里我们通过编辑 pom.xml文件来直接编辑工程的jar包依赖,点击finish。



8.在工程的根目录下生成了一个pom.xml文件,该文件即为maven的工程管理文件。





9. 打开pom.xml 文件,在modelVersion标签前加入<parent>标签,表名该pom.xml的父pom.xml,这里我们制定的是 spring-osgi作为工程的父pom.xml,这样做的好处是,可以不用在导入所有的依赖支撑jar包。

< parent >
  
< artifactId > spring-osgi </ artifactId >
  
< groupId > org.springframework.osgi </ groupId >
  
< version > 1.0-rc1 </ version >
 
</ parent >


10.另外还要加上对spring的依赖,osgi test的依赖,spring-test的依赖

 

< dependencies >

  
< dependency >
   
< groupId > org.springframework.osgi </ groupId >
   
< artifactId > spring-osgi-test </ artifactId >
   
< version > ${project.parent.version} </ version >
   
< scope > provided </ scope >
  
</ dependency >

  
< dependency >
   
< groupId > org.springframework </ groupId >
   
< artifactId > spring-test </ artifactId >
   
< version > ${spring.maven.artifact.version} </ version >
   
< type > jar </ type >
   
< scope > test </ scope >
  
</ dependency >

  
< dependency >
   
< groupId > org.springframework.osgi </ groupId >
   
< artifactId > spring-osgi-core </ artifactId >
   
< version > ${project.parent.version} </ version >
   
< type > jar </ type >
   
< scope > provided </ scope >
  
</ dependency >

  
< dependency >
   
< groupId > org.springframework </ groupId >
   
< artifactId > spring-core </ artifactId >
   
< version > ${spring.maven.artifact.version} </ version >
   
< scope > test </ scope >
  
</ dependency >
  
  
< dependency >
   
< groupId > org.springframework </ groupId >
   
< artifactId > spring-context </ artifactId >
   
< version > ${spring.maven.artifact.version} </ version >
   
< scope > test </ scope >
  
</ dependency >
  
  
< dependency >
   
< groupId > org.springframework </ groupId >
   
< artifactId > spring-beans </ artifactId >
   
< version > ${spring.maven.artifact.version} </ version >
   
< scope > test </ scope >
  
</ dependency >
  
 
</ dependencies >

10,编辑完成后保存,此时如若在本机的 local repository 中没有对应的jar的话,那么maven将会连接远端的maven repository 对所需要的jar下载到本地的repository 当中来。

11.完成之后,的classpath路径上将会出现maven的依赖包



12.打开工程的属性面板,修改outputfolder为target/test-classes。如果这里不做修改,那么就需要在测试类目录下面配置一个‘类名-boundle.properties’的配置文件来制定'root.dir'的值,作为输出folder





13.对工程加入log4j.properties配置文件

log4j.rootCategory = warn ,  stdout
log4j.appender.stdout
= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout.ConversionPattern
= %p  [ %c ]  - %m%n
log4j.appender.stdout.layout
= org.apache.log4j.PatternLayout

log4j.logger.org.springframework.osgi
= warn

14.在test目录下创建测试类,就是spring-dm-reference文档中的simple test例子

package  org.spring;

import  org.osgi.framework.Constants;
import  org.springframework.osgi.test.AbstractConfigurableBundleCreatorTests;

public   class  SimpleOsgiTest  extends  AbstractConfigurableBundleCreatorTests  ... {
    
    
public   void  testOsgiPlatformStarts()  throws  Exception  ... {
        System.out.println(bundleContext.getProperty(Constants.FRAMEWORK_VENDOR));
        System.out.println(bundleContext.getProperty(Constants.FRAMEWORK_VERSION));
        System.out.println(bundleContext.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT));
    }


}

15.在eclipse当中运行junit test


正确控制台输出为:

Eclipse
1.3.0
OSGi/Minimum-1.0,OSGi/Minimum-1.1,JRE-1.1,J2SE-1.2,J2SE-1.3,J2SE-1.4,J2SE-1.5,JavaSE-1.6

 

如果包括其中某个jar包的路径找不到那么按照下图操作,





如 果仍然有包找不到,点击maven install,此时,请确保maven插件的配置项offline不要被勾选,稍等一段时间,第一次耗时可能会长一些,耗时在330s左右,如果 maven不能成功的被运行,那么查看错误,一般为某个jar不能被正常安装,那么解决方法就是手动创建相应的repository目录路径,并将相应的 jar包放入制定目录当中去。创建时一定要注意路径名称和包名称。




最后可以通过修改log4j.properties配置文件来详细的查看spring-osgi的测试环境的全部生命周期的轨迹。级别设置为debug即可。

5
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics