在单元测试中自动生成数据存储区索引

时间:2013-06-14 16:55:12

标签: java google-app-engine maven objectify

  

配置: Maven 3,Objectify,Google-Appengine,Java 7

我想找到一种方法,只使用单元测试自动生成索引(在datastore-indexes-auto.xml文件中)。但是,尽管测试会调用数据存储区查询,但不会生成该文件。

在我的项目中,我使用标准的Maven布局:

src/
    main/
    test/
target/

使用Maven运行我的单元测试时,没有生成datastore-indexes-auto.xml文件(至少我在项目目录中找不到它)。当然,所有测试都通过了。

有没有办法只使用单元测试自动生成索引?

以下是pom.xml文件(根据正确答案更新):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.something.example</groupId>
  <artifactId>example</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Example</name>
  <description>Example POM for my appengine project.</description>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gae.version>1.8.1</gae.version>
    <objectify.version>4.0rc1</objectify.version>
    <java.version>1.7</java.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${gae.version}</version>
        <configuration>
          <oauth2>false</oauth2>
          <address>0.0.0.0</address>
        </configuration>
      </plugin>
      <!-- UPDATE: Based on @Amir's answer, I have added this plugin in order to
        -- copy the generated index file to the webapp directory. -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>test</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/${project.artifactId}-${project.version}/WEB-INF/appengine-generated</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/WEB-INF/appengine-generated</directory>
                  <filtering>false</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- UPDATE: This will remove the genreated WEB-INF directory with the mvn clean 
        -- command. -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <filesets>
            <fileset>
              <directory>WEB-INF</directory>
            </fileset>
          </filesets>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>0.11.8</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.googlecode.objectify</groupId>
      <artifactId>objectify</artifactId>
      <version>${objectify.version}</version>
    </dependency>
    <!-- GAE -->
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-1.0-sdk</artifactId>
      <version>${gae.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-labs</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-stubs</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-testing</artifactId>
      <version>${gae.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>httpunit</groupId>
      <artifactId>httpunit</artifactId>
      <version>1.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mozilla</groupId>
      <artifactId>rhino</artifactId>
      <version>1.7R4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

2 个答案:

答案 0 :(得分:2)

我让它以单向工作。它有点hacky但似乎确实有用。

请注意,使这项工作与maven没什么关系。关键是配置本地数据存储区服务以生成datastore-indexes-auto.xml。

步骤1:将本地数据存储区服务测试配置修改为setNoIndexAutoGen(false)。另外,如果您希望“datastore-indexes-auto.xml”包含测试所需的所有索引,那么您不得不注释掉LocalServiceTestHelper tearDown()调用

请注意,我的所有数据存储区测试都继承自PersistenceTestHelper。所以这就是我改变它的方式:

public class PersistenceTestHelper {

  private LocalServiceTestHelper appEngineHelper;

  @Before
  public void setUp() throws Exception {
    LocalDatastoreServiceTestConfig dsConfig = new LocalDatastoreServiceTestConfig();
    dsConfig.setNoIndexAutoGen(false);
    appEngineHelper = new LocalServiceTestHelper(dsConfig);
    appEngineHelper.setUp();
    ...
  }

  @After
  public void tearDown() throws Exception {
    // appEngineHelper.tearDown();
  }
}

第2步:执行“mvn test”

第3步:在您执行“mvn test”的目录中查找WEB-INF子目录。在那里你会找到“WEB-INF / appengine-generated / datastore-indexes-auto.xml”。这是项目目录/ pom.xml independent。

$ pwd
/home/avaliani/src/karma-exchange
$ mvn test
...
$ cat WEB-INF/appengine-generated/datastore-indexes-auto.xml 
<!-- Indices written at Tue, 2 Jul 2013 03:52:26 UTC -->

<datastore-indexes>

    <!-- Used 1 time in query history -->
    <datastore-index kind="Review" ancestor="true" source="auto">
        <property name="+commentCreationDate" direction="asc"/>
    </datastore-index>

    <!-- Used 1 time in query history -->
    <datastore-index kind="Review" ancestor="true" source="auto">
        <property name="commentCreationDate" direction="desc"/>
    </datastore-index>

</datastore-indexes>

<强>详细信息: 我正在使用“Apache Maven 3.0.4”和App Engine 1.7.7 SDK。我的项目与App Engine的maven插件集成:https://developers.google.com/appengine/docs/java/tools/maven

我的pom.xml: https://github.com/karma-exchange-org/karma-exchange/blob/master/pom.xml

答案 1 :(得分:0)

您是否尝试将datastore-indexes.xml文件设置为以下格式:

<?xml version="1.0" encoding="UTF-8"?>
<datastore-indexes autoGenerate="true">
</datastore-indexes>