DATACOUCH-3 - Renamed base package to org.springframework.data.couchbase.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
|
||||
@Configuration
|
||||
public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CouchbaseClient couchbaseClient() throws IOException {
|
||||
String defaultHost = "http://127.0.0.1:8091/pools";
|
||||
String host = env.getProperty("couchbase.host", defaultHost);
|
||||
|
||||
String bucket = env.getProperty("couchbase.bucket", "default");
|
||||
String pass = env.getProperty("couchbase.password", "");
|
||||
return new CouchbaseClient(Arrays.asList(URI.create(host)), bucket, pass);
|
||||
}
|
||||
|
||||
}
|
||||
63
src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheManagerTest.java
vendored
Normal file
63
src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheManagerTest.java
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import java.util.HashMap;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.cache.CouchbaseCacheManager;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Verifies the correct functionality of the CouchbaseCacheManager.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class CouchbaseCacheManagerTest {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
|
||||
/**
|
||||
* Tests the main functionality of the manager: loading the caches.
|
||||
*/
|
||||
@Test
|
||||
public void testCacheInit() {
|
||||
HashMap<String, CouchbaseClient> instances =
|
||||
new HashMap<String, CouchbaseClient>();
|
||||
instances.put("test", client);
|
||||
|
||||
CouchbaseCacheManager manager = new CouchbaseCacheManager(instances);
|
||||
assertEquals(instances, manager.getClients());
|
||||
}
|
||||
|
||||
}
|
||||
104
src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheTest.java
vendored
Normal file
104
src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheTest.java
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache.ValueWrapper;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.cache.CouchbaseCache;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Tests the CouchbaseCache class and verifies its functionality.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class CouchbaseCacheTest {
|
||||
|
||||
/**
|
||||
* Contains a reference to the actual CouchbaseClient.
|
||||
*/
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
/**
|
||||
* Simple name of the cache bucket to create.
|
||||
*/
|
||||
private String cacheName = "test";
|
||||
|
||||
/**
|
||||
* Tests the basic Cache construction functionality.
|
||||
*/
|
||||
@Test
|
||||
public void testConstruction() {
|
||||
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
|
||||
|
||||
assertEquals(cacheName, cache.getName());
|
||||
assertEquals(client, cache.getNativeCache());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies set() and get() of cache objects.
|
||||
*/
|
||||
@Test
|
||||
public void testGetSet() {
|
||||
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
|
||||
|
||||
String key = "couchbase-cache-test";
|
||||
String value = "Hello World!";
|
||||
cache.put(key, value);
|
||||
|
||||
String stored = (String) client.get(key);
|
||||
assertNotNull(stored);
|
||||
assertEquals(value, stored);
|
||||
|
||||
ValueWrapper loaded = cache.get(key);
|
||||
assertEquals(value, loaded.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the deletion of cache objects.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testEvict() throws Exception {
|
||||
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
|
||||
|
||||
String key = "couchbase-cache-test";
|
||||
String value = "Hello World!";
|
||||
|
||||
Boolean success = client.set(key, 0, value).get();
|
||||
assertTrue(success);
|
||||
|
||||
cache.evict(key);
|
||||
Object result = client.get(key);
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.config;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Unit test for {@link AbstractCouchbaseConfiguration}
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class AbstractCouchbaseConfigurationTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Test
|
||||
public void usesConfigClassPackageAsBaseMappingPackage() throws Exception {
|
||||
AbstractCouchbaseConfiguration config = new SampleCouchbaseConfiguration();
|
||||
|
||||
assertEquals(config.getMappingBasePackage(),
|
||||
SampleCouchbaseConfiguration.class.getPackage().getName());
|
||||
assertEquals(config.getInitialEntitySet().size(), 1);
|
||||
assertTrue(config.getInitialEntitySet().contains(Entity.class));
|
||||
}
|
||||
|
||||
class SampleCouchbaseConfiguration extends AbstractCouchbaseConfiguration {
|
||||
@Bean
|
||||
@Override
|
||||
public CouchbaseClient couchbaseClient() throws Exception {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@Document
|
||||
static class Entity {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.mapping.Field;
|
||||
|
||||
|
||||
/**
|
||||
* Test class for persisting and loading from {@link CouchbaseTemplate}.
|
||||
*/
|
||||
public class Beer {
|
||||
|
||||
@Id
|
||||
private final String id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Field("is_active")
|
||||
private boolean active = true;
|
||||
|
||||
public Beer(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Beer [id=" + id + ", name=" + name + ", active=" + active + "]";
|
||||
}
|
||||
|
||||
public Beer setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Beer setActive(boolean active) {
|
||||
this.active = active;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.core.mapping.Field;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.*;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class CouchbaseTemplateTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Test
|
||||
public void saveSimpleEntityCorrectly() throws Exception {
|
||||
String id = "beers:awesome-stout";
|
||||
String name = "The Awesome Stout";
|
||||
boolean active = false;
|
||||
Beer beer = new Beer(id).setName(name).setActive(active);
|
||||
|
||||
template.save(beer);
|
||||
String result = (String) client.get(id);
|
||||
|
||||
String expected = "{\"is_active\":" + active + ",\"name\":\"" + name + "\"}";
|
||||
assertNotNull(result);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveDocumentWithExpiry() throws Exception {
|
||||
String id = "simple-doc-with-expiry";
|
||||
DocumentWithExpiry doc = new DocumentWithExpiry(id);
|
||||
template.save(doc);
|
||||
assertNotNull(client.get(id));
|
||||
Thread.sleep(3000);
|
||||
assertNull(client.get(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertDoesNotOverride() {
|
||||
String id ="double-insert-test";
|
||||
String expected = "{\"name\":\"Mr. A\"}";
|
||||
|
||||
SimplePerson doc = new SimplePerson(id, "Mr. A");
|
||||
template.insert(doc);
|
||||
String result = (String) client.get(id);
|
||||
assertEquals(expected, result);
|
||||
|
||||
doc = new SimplePerson(id, "Mr. B");
|
||||
template.insert(doc);
|
||||
result = (String) client.get(id);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateDoesNotInsert() {
|
||||
String id ="update-does-not-insert";
|
||||
SimplePerson doc = new SimplePerson(id, "Nice Guy");
|
||||
template.update(doc);
|
||||
assertNull(client.get(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validFindById() {
|
||||
String id = "beers:findme-stout";
|
||||
String name = "The Findme Stout";
|
||||
boolean active = true;
|
||||
Beer beer = new Beer(id).setName(name).setActive(active);
|
||||
template.save(beer);
|
||||
|
||||
Beer found = template.findById(id, Beer.class);
|
||||
assertNotNull(found);
|
||||
assertEquals(id, found.getId());
|
||||
assertEquals(name, found.getName());
|
||||
assertEquals(active, found.getActive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDocument() {
|
||||
String id = "beers:findme-stout";
|
||||
Object result = client.get(id);
|
||||
assertNotNull(result);
|
||||
|
||||
Beer beer = new Beer(id);
|
||||
template.remove(beer);
|
||||
|
||||
result = client.get(id);
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeListsAndMaps() {
|
||||
String id ="persons:lots-of-names";
|
||||
List<String> names = new ArrayList<String>();
|
||||
names.add("Michael");
|
||||
names.add("Thomas");
|
||||
List<Integer> votes = new LinkedList<Integer>();
|
||||
Map<String, Boolean> info1 = new HashMap<String, Boolean>();
|
||||
info1.put("foo", true);
|
||||
info1.put("bar", false);
|
||||
Map<String, Integer> info2 = new HashMap<String, Integer>();
|
||||
|
||||
ComplexPerson complex = new ComplexPerson(id, names, votes, info1, info2);
|
||||
|
||||
template.save(complex);
|
||||
|
||||
String expected = "{\"info1\":{\"foo\":true,\"bar\":false},\"votes\":[],"
|
||||
+ "\"firstnames\":[\"Michael\",\"Thomas\"],\"info2\":{}}";
|
||||
assertEquals(expected, client.get(id));
|
||||
|
||||
ComplexPerson response = template.findById(id, ComplexPerson.class);
|
||||
assertEquals(names, response.getFirstnames());
|
||||
assertEquals(votes, response.getVotes());
|
||||
assertEquals(id, response.getId());
|
||||
assertEquals(info1, response.getInfo1());
|
||||
assertEquals(info2, response.getInfo2());
|
||||
}
|
||||
|
||||
/**
|
||||
* A sample document with just an id and property.
|
||||
*/
|
||||
@Document
|
||||
static class SimplePerson {
|
||||
@Id
|
||||
private final String id;
|
||||
@Field
|
||||
private final String name;
|
||||
|
||||
public SimplePerson(String id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sample document that expires in 2 seconds.
|
||||
*/
|
||||
@Document(expiry=2)
|
||||
static class DocumentWithExpiry {
|
||||
@Id
|
||||
private final String id;
|
||||
|
||||
public DocumentWithExpiry(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
@Document
|
||||
static class ComplexPerson {
|
||||
@Id
|
||||
private final String id;
|
||||
@Field
|
||||
private final List<String> firstnames;
|
||||
@Field
|
||||
private final List<Integer> votes;
|
||||
|
||||
@Field
|
||||
private final Map<String, Boolean> info1;
|
||||
@Field
|
||||
private final Map<String, Integer> info2;
|
||||
|
||||
public ComplexPerson(String id, List<String> firstnames,
|
||||
List<Integer> votes, Map<String, Boolean> info1,
|
||||
Map<String, Integer> info2) {
|
||||
this.id = id;
|
||||
this.firstnames = firstnames;
|
||||
this.votes = votes;
|
||||
this.info1 = info1;
|
||||
this.info2 = info2;
|
||||
}
|
||||
|
||||
List<String> getFirstnames() {
|
||||
return firstnames;
|
||||
}
|
||||
|
||||
List<Integer> getVotes() {
|
||||
return votes;
|
||||
}
|
||||
|
||||
Map<String, Boolean> getInfo1() {
|
||||
return info1;
|
||||
}
|
||||
|
||||
Map<String, Integer> getInfo2() {
|
||||
return info2;
|
||||
}
|
||||
|
||||
String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.springframework.data.couchbase.core.mapping;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.couchbase.core.mapping.BasicCouchbasePersistentEntity;
|
||||
import org.springframework.data.couchbase.core.mapping.BasicCouchbasePersistentProperty;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Verifies the correct behavior of properties on persistable objects.
|
||||
*/
|
||||
public class BasicCouchbasePersistentPropertyTest {
|
||||
|
||||
/**
|
||||
* Holds the entity to test against (contains the properties).
|
||||
*/
|
||||
CouchbasePersistentEntity<Beer> entity;
|
||||
|
||||
/**
|
||||
* Create an instance of the demo entity.
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
entity = new BasicCouchbasePersistentEntity<Beer>(
|
||||
ClassTypeInformation.from(Beer.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the name of the property without annotations.
|
||||
*/
|
||||
@Test
|
||||
public void usesPropertyFieldName() {
|
||||
Field field = ReflectionUtils.findField(Beer.class, "description");
|
||||
assertEquals("description", getPropertyFor(field).getFieldName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the name of the property with custom name annotation.
|
||||
*/
|
||||
@Test
|
||||
public void usesAnnotatedFieldName() {
|
||||
Field field = ReflectionUtils.findField(Beer.class, "name");
|
||||
assertEquals("foobar", getPropertyFor(field).getFieldName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a property out of the field.
|
||||
*
|
||||
* @param field the field to retrieve the properties from.
|
||||
* @return the actual BasicCouchbasePersistentProperty instance.
|
||||
*/
|
||||
private CouchbasePersistentProperty getPropertyFor(Field field) {
|
||||
return new BasicCouchbasePersistentProperty(field, null, entity,
|
||||
new SimpleTypeHolder());
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple POJO to test attribute properties and annotations.
|
||||
*/
|
||||
public class Beer {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@org.springframework.data.couchbase.core.mapping.Field("foobar")
|
||||
String name;
|
||||
|
||||
String description;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.data.couchbase.monitor;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.monitor.ClientInfo;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class ClientInfoTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
private ClientInfo ci;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ci = new ClientInfo(client);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hostNames() {
|
||||
String hostnames = ci.getHostNames();
|
||||
assertNotNull(hostnames);
|
||||
assertFalse(hostnames.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.springframework.data.couchbase.monitor;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.monitor.ClusterInfo;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class ClusterInfoTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
private ClusterInfo ci;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ci = new ClusterInfo(client);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void totalRAMAssigned() {
|
||||
assertTrue(ci.getTotalRAMAssigned() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void totalRAMUsed() {
|
||||
assertTrue(ci.getTotalRAMUsed() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.couchbase.TestApplicationConfig;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class SimpleCouchbaseRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Test
|
||||
public void simpleCrud() {
|
||||
String key = "my_unique_user_key";
|
||||
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
||||
UserRepository repository = factory.getRepository(UserRepository.class);
|
||||
User instance = new User(key, "foobar");
|
||||
repository.save(instance);
|
||||
|
||||
User found = repository.findOne(key);
|
||||
assertEquals(instance.getKey(), found.getKey());
|
||||
assertEquals(instance.getUsername(), found.getUsername());
|
||||
|
||||
assertTrue(repository.exists(key));
|
||||
repository.delete(found);
|
||||
|
||||
assertNull(repository.findOne(key));
|
||||
assertFalse(repository.exists(key));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
* User: michael
|
||||
* Date: 5/29/13
|
||||
* Time: 10:27 AM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
private final String key;
|
||||
|
||||
private final String username;
|
||||
|
||||
public User(String key, String username) {
|
||||
this.key = key;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import org.springframework.data.couchbase.repository.CouchbaseRepository;
|
||||
|
||||
|
||||
public interface UserRepository extends CouchbaseRepository<User, String>{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user