DATACOUCH-134 - allow changing the name of field storing type.

This commit is contained in:
Simon Baslé
2015-07-07 11:38:24 +02:00
parent c29727595c
commit 2bf2d3e544
8 changed files with 126 additions and 6 deletions

View File

@@ -64,4 +64,10 @@ public class IntegrationTestApplicationConfig extends AbstractCouchbaseConfigura
template.setWriteResultChecking(WriteResultChecking.LOG);
return template;
}
//change the name of the field that will hold type information
@Override
public String typeKey() {
return "javaClass";
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.data.couchbase.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
import org.junit.Before;
import org.junit.Test;
@@ -26,6 +28,9 @@ import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.repository.User;
/**
* @author Michael Nitschinger
@@ -72,4 +77,27 @@ public class CouchbaseTemplateParserIntegrationTests {
factory.getBean("cb-template-second");
}
/**
* Test case for DATACOUCH-134 in xml: field for storing type information can be renamed.
*/
@Test
public void testTypeFieldCanBeChosen() {
reader.loadBeanDefinitions(new ClassPathResource("configurations/couchbase-typekey.xml"));
CouchbaseTemplate template = factory.getBean("couchbaseTemplate", CouchbaseTemplate.class);
assertTrue(template.getConverter() instanceof MappingCouchbaseConverter);
MappingCouchbaseConverter converter = ((MappingCouchbaseConverter) template.getConverter());
assertEquals("javaXmlClass", converter.getTypeKey());
User u = new User("specialSaveUser", "John Locke");
template.save(u);
JsonDocument uJsonDoc = template.getCouchbaseBucket().get("specialSaveUser");
template.getCouchbaseBucket().remove("specialSaveUser");
assertNotNull(uJsonDoc);
JsonObject uJson = uJsonDoc.content();
assertNull(uJson.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT));
assertEquals("org.springframework.data.couchbase.repository.User", uJson.getString("javaXmlClass"));
assertEquals("John Locke", uJson.getString("username"));
}
}

View File

@@ -50,6 +50,7 @@ import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.IntegrationTestApplicationConfig;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Field;
import org.springframework.test.context.ContextConfiguration;
@@ -95,7 +96,9 @@ public class CouchbaseTemplateTests {
assertNotNull(result);
Map<String, Object> resultConv = MAPPER.readValue(result, new TypeReference<Map<String, Object>>() {});
assertEquals("org.springframework.data.couchbase.core.Beer", resultConv.get("_class"));
//DATACOUCH-134: the class holding field can be changed
assertNull(resultConv.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT));
assertEquals("org.springframework.data.couchbase.core.Beer", resultConv.get("javaClass"));
assertEquals(false, resultConv.get("is_active"));
assertEquals("The Awesome Stout", resultConv.get("name"));
}