load project version from property (#57)
This commit is contained in:
Родитель
a13e64f54b
Коммит
7f9a5e44b3
22
pom.xml
22
pom.xml
|
@ -48,6 +48,7 @@
|
|||
<spring.data.version>2.0.1.RELEASE</spring.data.version>
|
||||
|
||||
<mockito.core.version>2.8.9</mockito.core.version>
|
||||
<powermock.version>1.7.1</powermock.version>
|
||||
|
||||
<azure.documentdb.version>1.15.2</azure.documentdb.version>
|
||||
<azure.test.resourcegroup>documentdbtest</azure.test.resourcegroup>
|
||||
|
@ -119,6 +120,18 @@
|
|||
<version>${mockito.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -134,6 +147,15 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
<includes>
|
||||
<include>META-INF/project.properties</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
|
|
@ -10,11 +10,12 @@ import com.microsoft.azure.documentdb.ConnectionPolicy;
|
|||
import com.microsoft.azure.documentdb.ConsistencyLevel;
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.common.GetHashMac;
|
||||
import com.microsoft.azure.spring.data.documentdb.common.PropertyLoader;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class DocumentDbFactory {
|
||||
|
||||
private static final String USER_AGENT_SUFFIX = "spring-data/2.0.2-SNAPSHOT";
|
||||
private static final String USER_AGENT_SUFFIX = "spring-data/" + PropertyLoader.getProjectVersion();
|
||||
|
||||
private DocumentClient documentClient;
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
package com.microsoft.azure.spring.data.documentdb.common;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class PropertyLoader {
|
||||
private static final String PROJECT_PROPERTY_FILE = "/META-INF/project.properties";
|
||||
|
||||
public static String getProjectVersion() {
|
||||
String version = "unknown";
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = PropertyLoader.class.getResourceAsStream(PROJECT_PROPERTY_FILE);
|
||||
if (inputStream != null) {
|
||||
final Properties properties = new Properties();
|
||||
properties.load(inputStream);
|
||||
|
||||
version = properties.getProperty("project.version");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Omitted
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
// Omitted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
project.version=@project.version@
|
|
@ -13,7 +13,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class DocumentDbFactoryUnitTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNullKey() throws Exception {
|
||||
new DocumentDbFactory("https://fakeuri", null);
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
package com.microsoft.azure.spring.data.documentdb;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.common.PropertyLoader;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(PropertyLoader.class)
|
||||
@PowerMockIgnore({"javax.net.ssl.*", "javax.crypto.*"})
|
||||
public class UserAgentTest {
|
||||
private static final String TEST_VERSION = "1.0.0-FOR-TEST";
|
||||
|
||||
@Test
|
||||
public void testUserAgentSuffixAppended() {
|
||||
PowerMockito.mockStatic(PropertyLoader.class);
|
||||
BDDMockito.given(PropertyLoader.getProjectVersion()).willReturn(TEST_VERSION);
|
||||
|
||||
assertThat(PropertyLoader.getProjectVersion()).isEqualTo(TEST_VERSION);
|
||||
|
||||
final DocumentDbFactory factory = new DocumentDbFactory("https://fakeuri", "fakekey");
|
||||
assertThat(factory.getDocumentClient().getConnectionPolicy().getUserAgentSuffix()).contains(TEST_VERSION);
|
||||
}
|
||||
|
||||
}
|
|
@ -17,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
Загрузка…
Ссылка в новой задаче