2011-03-30 115 views
1

有没有办法改变文件的属性?我试图用Spring和Jetty并行运行硒测试,所以我试图配置数据库的url,jettyserver的端口和selenium服务器的端口。这样我就可以初始化可运行测试的两台或更多台服务器。在春天设置属性

我server.properties文件包含此:

jdbc.url=jdbc:hsqldb:hsql://localhost/bibliothouris_scenario 
jetty.port=8081 
seleniumServer.port=4444 

我可以提供一个PropertyPlaceholderConfigurer读取这些属性,我需要的数据库URL,jettyport和seleniumserver端口要灵活。

我已经宣布他们是这样的:

在我的applicationContext.xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:server.properties</value> 
    </property> 
</bean> 

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="sa" /> 
    <property name="password" value="" /> 
</bean> 

在serverContext.xml文件:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:server.properties</value> 
    </property> 
</bean> 

<bean class="com.~companyName~.bibliothouris.jetty.JettyServer" init-method="start" destroy-method="stop"> 
    <constructor-arg value="${jetty.port}" /> 
    <constructor-arg ref="dataSource" /> 
</bean> 

<bean class="org.openqa.selenium.server.SeleniumServer" init-method="start" destroy-method="stop"> 
    <constructor-arg> 
     <bean class="org.openqa.selenium.server.RemoteControlConfiguration"> 
      <property name="port" value="${seleniumServer.port}" /> 
      <property name="singleWindow" value="true" /> 
      <property name="timeoutInSeconds" value="10" /> 
     </bean> 
    </constructor-arg> 
</bean> 

<bean class="com.thoughtworks.selenium.DefaultSelenium" init-method="start" destroy-method="stop" lazy-init="true"> 
    <constructor-arg> 
     <bean class="com.thoughtworks.selenium.HttpCommandProcessor"> 
      <constructor-arg value="localhost" /> 
      <constructor-arg value="${seleniumServer.port}" /> 
      <constructor-arg value="*firefox c:/~companyname~/firefox/firefox.exe" /> 
      <constructor-arg value="http://localhost:${jetty.port}" /> 
     </bean> 
    </constructor-arg> 
</bean> 

当我改变服务器的数据。属性硒测试运行在正确的服务器与正确的端口,没有失败。

所以现在我正在寻找一种方法来更改server.properties文件中的属性。提前

回答

1

感谢您的帮助球员,没有您的信息,我找不到我自己的解决方案。那就是:

try { 
     Properties props = new Properties(); 
     FileInputStream fileInputStream = new FileInputStream(
      "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties"); 
     props.load(fileInputStream); 
     fileInputStream.close(); 
     props.setProperty("seleniumServer.port", "4445"); 

     FileOutputStream fileOutputStream = new FileOutputStream(
      "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties"); 
     props.store(fileOutputStream, ""); 
     fileOutputStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

我在TestClass中写了一段代码,现在我要创建的是一个方法,它需要几个参数(网址,jettyport和seleniumport)。我必须改变路径到相对的路径。

感谢您的帮助!

+0

@Walle,请提供帮助的答案,我们也在这里声望;) – 2011-03-30 12:06:04

+0

我需要15个声望来做到这一点:D – Walle 2011-03-30 13:52:23

+1

垫是正确的,你应该访问classpath资源通过getResourceAsStream,否则你的代码会在不同的环境中执行时破坏 – 2011-03-31 07:26:05

1

亲切的问候,并感谢我通过在编译过程中一个标志(我使用Maven),其选择在最后的战争中包含哪些属性文件解决了这个。通过这种方式,您可以包含不同属性的不同属性(不同属性文件),而不必混淆Spring的低级属性支持。

如果您确实需要这样做只是Spring,我会建议您使用基于Java的配置,您可以通过代码而不是XML来获取和设置属性。

+0

我也在使用Maven,但这是我第一次使用它。我会试一试;) – Walle 2011-03-30 08:43:57

1

有没有办法改变文件的 属性?

不,但您可以通过以下方式解决此问题。

  • 拆分经由一个src /测试/资源资源的属性成jdbc.properties(对于applicationContext.xml中)和test.properties(对于serverContext.xml)
  • 倍率server.properties
  • 使用系统属性除了server.properties(使用PropertyPlaceholderConfigurer.setSystemPropertiesMode为此)