DATACOUCH-235 - Fixed multiple Couchbase environments.

Defining <couchbase:env/> in XML configuration was causing a double
instantiation of DefaultCoreEnvironment, with subsequent warning in
logs:

[main] WARN c.c.client.core.env.CoreEnvironment - More than 1 Couchbase
Environments found (2), this can have severe impact on performance and
stability. Reuse environments!
This commit is contained in:
Simon Bland
2016-06-15 15:37:56 +02:00
committed by sbland
parent c5a1bf0515
commit a40919ef8e
3 changed files with 113 additions and 106 deletions

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2012-2016 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 org.springframework.data.couchbase.config;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.util.ReflectionTestUtils;
import com.couchbase.client.core.env.DefaultCoreEnvironment;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
/**
* @author Simon Bland
*/
public class CouchbaseSingleEnvironmentParserTest {
/**
* @see DATACOUCH-235
*/
@Test
public void testSingleCouchbaseEnvironment() throws Exception {
Integer instanceCounterBefore = (Integer) ReflectionTestUtils.getField(DefaultCoreEnvironment.class, "instanceCounter");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("configurations/couchbaseSingleEnv-bean.xml"));
GenericApplicationContext context = new GenericApplicationContext(factory);
context.refresh();
CouchbaseEnvironment env = context.getBean("singleEnv", CouchbaseEnvironment.class);
context.close();
Integer instanceCounterAfter = (Integer) ReflectionTestUtils.getField(DefaultCoreEnvironment.class, "instanceCounter");
assertThat(env, is(instanceOf(DefaultCouchbaseEnvironment.class)));
assertThat(instanceCounterAfter, is(instanceCounterBefore + 1));
}
}