سؤال

لنفترض أنني أريد إنشاء قاعدة بيانات H2 واستخدامها لإجراء اختبارات التكامل الخاصة بي.

لدى Maven أمرا لتشغيل الاختبارات: mvn test.

هل هناك طريقة لإخبار Maven ببدء تشغيل خادم قاعدة بيانات H2 للاختبارات وإيقافها عند الانتهاء؟

أتصور هذا العمل مماثلة لكيفية تشغيل Tomcat عبر قيادة من Maven (mvn tomcat:run).

آسف إذا كان هذا السؤال لا معنى له، ما زلت ألف رأسي حول مفاهيم جديدة.

هل كانت مفيدة؟

المحلول

كنت قادرا على الحصول عليها للعمل دون استخدام خادم خارجي فقط عن طريق إضافة التبعية إلى H2 عبر Maven ثم استخدام هذه الفول:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:file:h2\db"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>        
</bean>

ثم مرة أخرى، هذا مطلوب أن أستخدم DB استنادا عن الملفات بدلا من الذاكرة. لكنها تفعل الخدعة.

نصائح أخرى

يمكنك إنشاء فصلين صغيرين مع الأساليب الرئيسية التي تبدأ وإيقاف قاعدة البيانات. تتمثل الفكرة في تشغيل فئة StartServer قبل تشغيل اختبارات التكامل ثم توقف الفئة بعد تشغيل الاختبارات.

يجب أن تفعل الشيء نفسه لخادم DB كما هو موضح في مكان ما في هذا المستند (الوصف هو بداية وإيقاف رصاصي في اختبارات التكامل)

في Pom.xml الخاص بك يجب عليك تحديد البرنامج المساعد Maven-Exec-Run exec: جاوة الهدف وإنشاء 2 عمليات إعدام (1 لاستدعاء StartServer و 1 ل StopServer):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1.1</version>
  <executions>
    <execution>
      <!-- start server before integration tests -->
      <id>start</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <mainClass>com.foo.StartServer</mainClass>
      </configuration>
     </execution>
     <execution>
      <!-- stop server after integration tests -->
      <id>stop</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <mainClass>com.foo.StopServer</mainClass>
      </configuration>
     </execution>
   </executions>
 </plugin>

آمل أن يكون هذا ما تريد

يعمل هذا البرنامج المساعد جيدا على تفرخ H2 DB H2 جديد مع وضع TCP قبل اختبارات التكامل (مرحلة المساعد الافتراضية): H2-Maven-Plugin على جيثب

لم يتم توثيقها جيدا ولكن يمكنك التحقق من مصادر Mojo لمعرفة خيارات التكوين. تم نشره على Maven Central.


في الأساس، للحصول على اختبارات التكامل، قد ترغب في مافن إلى:

  • حجز منافذ الشبكة المتوفرة بشكل عشوائي، لخادم Tomcat الخاص بك، و H2 الخاص بك (لتجنب تعارضات المنفذ)
  • بدء تشغيل خادم H2
  • بدء خادم Tomcat
  • تشغيل اختبارات التكامل
  • إيقاف خادم Tomcat
  • إيقاف خادم H2

يمكن تحقيق ذلك بتكوين Maven يبدو وكأنه هذا. فيما يتعلق بتحقيق اختبارات التكامل الخاصة بك مع واجهة مخصصة Junit الفئة:

@Category(IntegrationTest.class)

هذا التكوين Maven يعمل بشكل جيد بالنسبة لي:

<profile>
   <id>it</id>
   <build>
     <plugins>

       <!-- Reserve randomly available network ports -->
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>build-helper-maven-plugin</artifactId>
         <executions>
           <execution>
             <id>reserve-network-port</id>
             <goals>
               <goal>reserve-network-port</goal>
             </goals>
             <phase>process-resources</phase>
             <configuration>
               <portNames>
                 <portName>tomcat.test.http.port</portName>
                 <portName>h2.test.tcp.port</portName>
               </portNames>
             </configuration>
           </execution>
         </executions>
       </plugin>


       <!-- Start H2 before integration tests, accepting tcp connections on the randomly selected port -->
       <plugin>
         <groupId>com.edugility</groupId>
         <artifactId>h2-maven-plugin</artifactId>
         <version>1.0</version>
         <configuration>
           <port>${h2.test.tcp.port}</port>
         </configuration>
         <executions>
             <execution>
               <id>Spawn a new H2 TCP server</id>
               <goals>
                 <goal>spawn</goal>
               </goals>
             </execution>
             <execution>
               <id>Stop a spawned H2 TCP server</id>
               <goals>
                 <goal>stop</goal>
               </goals>
             </execution>
           </executions>
       </plugin>


       <!-- Start Tomcat before integration tests on the -->
       <plugin>
         <groupId>org.apache.tomcat.maven</groupId>
         <artifactId>tomcat7-maven-plugin</artifactId>
         <configuration>
           <systemProperties>
             <spring.profiles.active>integration_tests</spring.profiles.active>
             <httpPort>${http.test.http.port}</httpPort>
             <h2Port>${h2.test.tcp.port}</h2Port>
           </systemProperties>
           <port>${http.test.http.port}</port>
           <contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>
           <fork>true</fork>
         </configuration>
         <executions>
           <execution>
             <id>run-tomcat</id>
             <phase>pre-integration-test</phase>
             <goals>
               <goal>run</goal>
             </goals>
           </execution>
           <execution>
             <id>stop-tomcat</id>
             <phase>post-integration-test</phase>
             <goals>
               <goal>shutdown</goal>
             </goals>
           </execution>
         </executions>
         <dependencies>
           <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql.version}</version>
           </dependency>
           <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
             <version>${h2.version}</version>
           </dependency>
         </dependencies>
       </plugin>


       <!-- Run the integration tests annotated with @Category(IntegrationTest.class) -->
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-failsafe-plugin</artifactId>
         <!-- Bug in 2.12.x -->
         <version>2.11</version>
         <dependencies>
           <dependency>
             <groupId>org.apache.maven.surefire</groupId>
             <artifactId>surefire-junit47</artifactId>
             <version>2.12.4</version>
           </dependency>
         </dependencies>
         <configuration>
           <groups>com.mycompany.junit.IntegrationTest</groups>
           <failIfNoTests>false</failIfNoTests>
           <junitArtifactName>junit:junit-dep</junitArtifactName>
           <systemPropertyVariables>
             <httpPort>${tomcat.test.http.port}</httpPort>
             <h2Port>${h2.test.tcp.port}</h2Port>
           </systemPropertyVariables>
         </configuration>
         <executions>
           <execution>
             <goals>
               <goal>integration-test</goal>
             </goals>
           </execution>
         </executions>
       </plugin>

     </plugins>
   </build>
 </profile>

قد ترغب في استخدام مرشحات Maven على ملف سياق Tomcat بحيث يتم استبدال المنفذ:

   <contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>

مع كون محتوى الملفات:

  <Resource name="jdbc/dataSource"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maxWait="10000"
            username=""
            password=""
            driverClassName="org.h2.Driver"
            url="jdbc:h2:tcp://localhost:${h2.test.tcp.port}/mem:db;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL"/>

أو إذا كنت لا تريد DataSource JNDI، فيمكنك استخدام DataSource المعلنة المعلنة، باستخدام نفس الممتلكات ...


رحلة إضافية واحدة إذا كنت تريد أن تكون قادرا على إعداد اختبارات التكامل الخاصة بك Tomcat، وتشغيل اختبارات التكامل من IDE:

يمكنك استخدام استخدام خاصية للشوكة أو لا خادم Tomcat:

<fork>${integrationTestsForkTomcatJvm}</fork>

عند تعيين شوكة = FALSE، فلن يستمر الخادم ولم يستمر Maven، لذلك لن يتم تشغيل اختبارات التكامل، لكنك ستتمكن من تشغيلها من IDE الخاص بك.

لقد بدأت للتو مشروع البرنامج المساعد H2 ل Maven @ Bitbucket. سوف أقدر أي مساعدة في ذلك.

https://bitbucket.org/dohque/maven-h2-plugin.

نأمل أن تكون مفيدة.

في مشروعي، للاختبار الوحدة، سألت الربيع للتعامل مع إنشاء قاعدة البيانات هذه التهيئة. كما هو مذكور في الوثائق H2, ، يمكنك إنشاء فول لذلك:

<bean id = "org.h2.tools.Server"
    class="org.h2.tools.Server"
    factory-method="createTcpServer"
    init-method="start"
    destroy-method="stop">
    <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
</bean>

تحتاج ببساطة إلى بدء سياق الربيع مع هذا التكوين عند بدء تشغيل اختبارات الوحدة الخاصة بك.

أقوم بإنشاء قاعدة بيانات H2 المستندة إلى الملفات قبل تشغيل اختبارات الوحدات. الملف يعيش في target دليل ويمكن إزالتها في أي وقت باستخدام mvn clean.

يمكنني استخدام البرنامج المساعد Maven-SQL كما يلي:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>sql-maven-plugin</artifactId>
  <version>1.5</version>
  <dependencies>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId> 
      <version>1.3.166</version>
    </dependency>
  </dependencies>
  <configuration>
    <driver>org.h2.Driver</driver>
    <url>jdbc:h2:file:target/db/testdb</url>
    <username>sa</username>
    <password></password>
    <autocommit>true</autocommit>
    <skip>${maven.test.skip}</skip>
  </configuration>
  <executions>
    <execution>
      <id>create-db</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <srcFiles>
          <srcFile>${sql.dir}/drop_db.sql</srcFile>
          <srcFile>${sql.dir}/tables.sql</srcFile>
          <srcFile>${sql.dir}/constraints.sql</srcFile>
          ... etc ...
        </srcFiles>
      </configuration>
    </execution>
  </executions>
</plugin>

يمكن إنشاء قاعدة البيانات عن طريق تشغيل mvn process-test-resources. وبعد عند تشغيل الاختبارات، تأكد من الاتصال بقاعدة البيانات في target/db/testdb عبر خصائص السبات.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"
      p:driverClassName="org.h2.Driver"
      p:url="jdbc:h2:file:target/db/testdb"
      p:username="sa"
      p:password="" />

ستحتاج أيضا إلى الاعتماد على com.h2database.h2 في تبعيات مافن.

إذا كنت ترغب في تحقيق ذلك في الذاكرة، فما عليك سوى استخدام عنوان URL مختلف:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:mem:db"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>        
</bean>

يمكنك تقديم خيارات إضافية، مثل:؛ db_close_delay = -1

يرى: http://www.h2database.com/html/features.html#in_memory_databases.

نظرا لأن H2 لا يوفر البرنامج المساعد Maven، يجب عليك بدء تشغيله باستخدام Maven-Antrun-Plugin. اكتب الرمز للبدء وإيقاف محرك H2 في مهمة النمل واتصل به عند بدء تشغيل اختبار التكامل الخاص بك والتوقف.

انظر التفاصيل http://docs.codehaus.org/display/mavenuser/maven+and+intingting

التالية يفعل المهمة بالنسبة لي (فقط باستخدام h2 الاعتماد و exec-maven-plugin):

    <build>
        <plugins>
            <!-- start/stop H2 DB as a server -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>start-h2</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>org.h2.tools.Server</mainClass>
                            <arguments>
                                <argument>-tcp</argument>
                                <argument>-tcpDaemon</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-h2</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>org.h2.tools.Server</mainClass>
                            <arguments>
                                <argument>-tcpShutdown</argument>
                                <argument>tcp://localhost:9092</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <includeProjectDependencies>true</includeProjectDependencies>
                    <includePluginDependencies>true</includePluginDependencies>
                    <executableDependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                    </executableDependency>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>1.3.173</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
</build>

يرجى ملاحظة، في بلدي pom.xml ال com.h2database:h2 لم يكن الاعتماد على المشروع. في حالة وجودك قد لا تحتاج إلى تسمية ذلك صراحة كإعالة مساعدية.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top