简体   繁体   English

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

[英]Auto-generating datastore indexes in unit tests

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

I'd like to find a way to auto-generate indexes (in the datastore-indexes-auto.xml file) using just unit tests. 我想找到一种方法来自动生成索引(在datastore-indexes-auto.xml文件中),只使用单元测试。 However, although the tests call the datastore query, the file is not generated. 但是,尽管测试调用数据存储区查询,但不会生成该文件。

In my project, I use the standard Maven layout: 在我的项目中,我使用标准的Maven布局:

src/
    main/
    test/
target/

When running my unit tests with Maven, no datastore-indexes-auto.xml file is generated (at least I can't find it anywhere in the project directory). 使用Maven运行单元测试时,不会生成任何datastore-indexes-auto.xml文件(至少我在项目目录中的任何位置都找不到它)。 All tests pass, of course. 当然,所有测试都通过了。

Is there a way to generate the indexes automatically using just unit tests? 有没有办法只使用单元测试自动生成索引?

Here is the pom.xml file (updated based on the correct answer): 这是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>

I got it to work one way. 我让它以一种方式工作。 It's a bit hacky but it does seem to work. 它有点hacky但它​​似乎确实有效。

Note that making this work has little to do with maven. 请注意,使这项工作与maven没什么关系。 The key is to configure the local datastore service to generate datastore-indexes-auto.xml. 关键是配置本地数据存储区服务以生成datastore-indexes-auto.xml。

Step 1: Modify your local datastore service test config to setNoIndexAutoGen(false). 步骤1:将本地数据存储区服务测试配置修改为setNoIndexAutoGen(false)。 Also, you unfortunately have to comment out the LocalServiceTestHelper tearDown() call if you want "datastore-indexes-auto.xml" to contain all the indexes required by your tests. 此外,如果您希望“datastore-indexes-auto.xml”包含测试所需的所有索引, 那么您不得不注释掉LocalServiceTestHelper tearDown()调用

Note that all my datastore tests inherit from PersistenceTestHelper. 请注意,我的所有数据存储区测试都继承自PersistenceTestHelper。 So this is how I changed it: 所以这就是我改变它的方式:

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();
  }
}

Step 2: Execute "mvn test" 第2步:执行“mvn test”

Step 3: In the directory you executed "mvn test" look for a WEB-INF sub-directory. 步骤3:在您执行“mvn test”的目录中查找WEB-INF子目录。 There you will find "WEB-INF/appengine-generated/datastore-indexes-auto.xml". 在那里你会找到“WEB-INF / appengine-generated / datastore-indexes-auto.xml”。 This is project directory / pom.xml independent. 这是项目目录/ pom.xml独立的。

$ 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>

Details: I'm using "Apache Maven 3.0.4" and App Engine 1.7.7 SDK. 详细信息:我正在使用“Apache Maven 3.0.4”和App Engine 1.7.7 SDK。 My project is integrated with App Engine's maven plugin: https://developers.google.com/appengine/docs/java/tools/maven 我的项目与App Engine的maven插件集成: https//developers.google.com/appengine/docs/java/tools/maven

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

Have you tried having your datastore-indexes.xml file as something of the form: 您是否尝试将datastore-indexes.xml文件设置为以下形式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM