If controller has a field called messages, messages field in AbstractFrame is not injected. #PL-2030

This commit is contained in:
Konstantin Krivopustov 2013-06-17 12:48:30 +00:00
parent bdee1b07aa
commit 16485a970c
2 changed files with 68 additions and 17 deletions

View File

@ -73,27 +73,11 @@ public class ControllerDependencyInjector {
for (Class c : classes) {
if (c != Object.class) {
for (Field field : c.getDeclaredFields()) {
int idx = indexOfFieldWithSameName(list, field);
if (idx > -1)
list.set(idx, field);
else
list.add(field);
}
Collections.addAll(list, c.getDeclaredFields());
}
}
return list;
}
private int indexOfFieldWithSameName(List<Field> fields, Field field) {
for (int i = 0; i < fields.size(); i++) {
Field f = fields.get(i);
if (f.getName().equals(field.getName()))
return i;
}
return -1;
}
private Class injectionAnnotation(AnnotatedElement element) {
if (element.isAnnotationPresent(Named.class))
return Named.class;

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2013 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
*/
package com.haulmont.cuba.gui;
import com.haulmont.cuba.client.sys.MessagesClientImpl;
import com.haulmont.cuba.client.testsupport.CubaClientTestCase;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.gui.components.AbstractFrame;
import com.haulmont.cuba.gui.components.AbstractWindow;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import javax.inject.Inject;
import java.lang.reflect.Field;
import java.util.Collections;
import static junit.framework.Assert.assertTrue;
/**
* @author krivopustov
* @version $Id$
*/
public class ControllerDependencyInjectorTest extends CubaClientTestCase {
private Messages messages = new MessagesClientImpl();
@Before
public void setUp() throws Exception {
setupInfrastructure();
new NonStrictExpectations() {
@Mocked ApplicationContext applicationContext;
{
AppContext.getApplicationContext();
result = applicationContext;
applicationContext.getBeansOfType(Messages.class, true, true);
result = Collections.singletonMap(Messages.NAME, messages);
}
};
}
@Test
public void testInjectMessagesIntoAbstractFrame() throws Exception {
TestController controller = new TestController();
ControllerDependencyInjector injector = new ControllerDependencyInjector(controller);
injector.inject();
assertTrue(controller.messages == messages);
Field field = AbstractFrame.class.getDeclaredField("messages");
field.setAccessible(true);
assertTrue(field.get(controller) == messages);
}
private class TestController extends AbstractWindow {
@Inject
protected Messages messages;
}
}