2011-03-30 118 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爲此)