#265 - Polishing.

Add license headers. Replace CouchbaseConfiguration with Spring Boot properties. Post-process example data to make it accessible for repository use. Fix Id type. Add examples for N1ql and view access.

Enable couchbase examples in parent pom.

Add test rule to skip tests if Couchbase is not available.

Original pull request: #275.
This commit is contained in:
Mark Paluch
2017-11-21 13:03:04 +01:00
parent 5ba65308a7
commit b61875f97c
13 changed files with 371 additions and 155 deletions

24
couchbase/util/pom.xml Normal file
View File

@@ -0,0 +1,24 @@
<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>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-couchbase-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-data-couchbase-example-utils</artifactId>
<name>Spring Data Couchbase - Example Utilities</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.couchbase.util;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.Duration;
import javax.net.SocketFactory;
import org.junit.AssumptionViolatedException;
import org.junit.rules.ExternalResource;
import com.couchbase.client.core.env.DefaultCoreEnvironment;
/**
* Rule to check Couchbase server availability. If Couchbase is not running, tests are skipped.
*
* @author Mark Paluch
*/
public class CouchbaseAvailableRule extends ExternalResource {
private final String host;
private final int port;
private final Duration timeout = Duration.ofSeconds(1);
private CouchbaseAvailableRule(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Create a new rule requiring Couchbase running on {@code localhost} on
* {@link DefaultCoreEnvironment#BOOTSTRAP_HTTP_DIRECT_PORT}.
*
* @return the test rule.
*/
public static CouchbaseAvailableRule onLocalhost() {
return new CouchbaseAvailableRule("localhost", DefaultCoreEnvironment.BOOTSTRAP_HTTP_DIRECT_PORT);
}
@Override
protected void before() throws Throwable {
Socket socket = SocketFactory.getDefault().createSocket();
try {
socket.connect(new InetSocketAddress(host, port), Math.toIntExact(timeout.toMillis()));
} catch (IOException e) {
throw new AssumptionViolatedException(
String.format("Couchbase not available on on %s:%d. Skipping tests.", host, port), e);
} finally {
socket.close();
}
}
}