Skip to content

Commit

Permalink
Refactor test init pattern and usage of pre-injected instances from o…
Browse files Browse the repository at this point in the history
…ther tests
  • Loading branch information
mgoellnitz committed Mar 30, 2016
1 parent 6b2b47e commit 489bbf4
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 154 deletions.
12 changes: 6 additions & 6 deletions coma/test/org/tangram/coma/test/IncludeTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.tangram.coma.test;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -32,11 +33,11 @@
import org.tangram.Constants;
import org.tangram.coma.tags.IncludeTag;
import org.tangram.coma.tags.LinkTag;
import org.tangram.content.BeanFactory;
import org.tangram.content.Content;
import org.tangram.mock.content.MockBeanFactory;
import org.tangram.view.ViewUtilities;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -45,13 +46,12 @@
*/
public class IncludeTagTest {

private final MockBeanFactory beanFactory = new MockBeanFactory();
private final BeanFactory beanFactory;


@BeforeClass
public void init() throws Exception {
beanFactory.init();
} // init()
public IncludeTagTest() throws FileNotFoundException {
beanFactory = MockBeanFactory.getInstance();
} // ()


@Test
Expand Down
10 changes: 5 additions & 5 deletions coma/test/org/tangram/coma/test/LinkTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.tangram.coma.test;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -31,12 +32,12 @@
import org.springframework.mock.web.MockServletContext;
import org.tangram.Constants;
import org.tangram.coma.tags.LinkTag;
import org.tangram.content.BeanFactory;
import org.tangram.content.Content;
import org.tangram.link.Link;
import org.tangram.link.LinkFactoryAggregator;
import org.tangram.mock.content.MockBeanFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -45,12 +46,11 @@
*/
public class LinkTagTest {

private final MockBeanFactory beanFactory = new MockBeanFactory();
private final BeanFactory beanFactory;


@BeforeClass
public void init() throws Exception {
beanFactory.init();
public LinkTagTest() throws FileNotFoundException {
beanFactory = MockBeanFactory.getInstance();
} // init()


Expand Down
14 changes: 6 additions & 8 deletions coma/test/org/tangram/components/coma/test/ComaHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.tangram.link.Link;
import org.tangram.link.TargetDescriptor;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -52,11 +51,11 @@ public class ComaHandlerTest {

private final static String BLOB_CONTENT_ID = "42";

private ComaContent comaContent;
private final ComaContent comaContent;

private ComaContent comaBlobContent;
private final ComaContent comaBlobContent;

private ComaBlob comaBlob;
private final ComaBlob comaBlob;

@Spy
private final Set<ComaBeanPopulator> populators = new HashSet<>(); // NOPMD - this field is not really unused
Expand All @@ -68,15 +67,14 @@ public class ComaHandlerTest {
private final ComaHandler comaHandler = new ComaHandler();


@BeforeClass
public void init() {
public ComaHandlerTest() {
comaContent = new ComaContent(CONTENT_ID, "Content", null);
comaBlob = new ComaBlob(BLOB_CONTENT_ID, "data", "image/png", 0, new byte[0]);
Map<String,Object> properties = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
properties.put("data", comaBlob);
comaBlobContent = new ComaContent(BLOB_CONTENT_ID, "Image", properties);
MockitoAnnotations.initMocks(this);
} // init()
} // ()


@Test
Expand Down
3 changes: 1 addition & 2 deletions core/test/logback.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2015 Martin Goellnitz
* Copyright 2015-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -29,5 +29,4 @@ appender('CONSOLE', ConsoleAppender) {
appenders.add('CONSOLE')

root WARN, appenders
logger "dinistiq", DEBUG, appenders, false
logger "org.tangram", DEBUG, appenders, false
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.tangram.content.CodeResourceCache;
import org.tangram.link.TargetDescriptor;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand Down Expand Up @@ -68,18 +67,15 @@ public class GenericAuthorizationServiceTest {
private final GenericAuthorizationService authorizationService = new GenericAuthorizationService();


@BeforeClass
public void init() throws Exception {
GenericCodeResourceCacheTest codeResourceCacheTest = new GenericCodeResourceCacheTest();
codeResourceCacheTest.init("/auth-content.xml");
codeResourceCache = codeResourceCacheTest.getInstance();
public GenericAuthorizationServiceTest() throws Exception {
codeResourceCache = new GenericCodeResourceCacheTest("/auth-content.xml").getInstance();
freeUrls.add("/free");
adminUsers.add("form:admin");
allowedUsers.add("form:testuser");
loginProviders.add("form");
MockitoAnnotations.initMocks(this);
authorizationService.afterPropertiesSet();
} // init()
} // ()


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
*/
package org.tangram.components.test;

import java.io.FileNotFoundException;
import java.util.Map;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tangram.PersistentRestartCache;
import org.tangram.components.GenericCodeResourceCache;
import org.tangram.content.BeanFactory;
import org.tangram.content.CodeResource;
import org.tangram.content.CodeResourceCache;
import org.tangram.content.TransientCode;
import org.tangram.mock.content.MockBeanFactory;
import org.tangram.util.DummyRestartCache;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -39,27 +42,28 @@
*/
public class GenericCodeResourceCacheTest {

private static final Logger LOG = LoggerFactory.getLogger(GenericCodeResourceCacheTest.class);

@Spy
private final PersistentRestartCache restartCache = new DummyRestartCache(); // NOPMD - this field is not really unused

@Spy
private final MockBeanFactory factory = new MockBeanFactory();
private BeanFactory factory;

@InjectMocks
private final GenericCodeResourceCache codeResourceCache = new GenericCodeResourceCache();


public void init(String contentResource) throws Exception {
public GenericCodeResourceCacheTest(String contentResource) throws FileNotFoundException {
factory = MockBeanFactory.getInstance(contentResource);
MockitoAnnotations.initMocks(this);
factory.init(contentResource);
codeResourceCache.afterPropertiesSet();
} // init()
} // ()


@BeforeClass
public void init() throws Exception {
init("/mock-content.xml");
} // init()
public GenericCodeResourceCacheTest() throws FileNotFoundException {
this(null);
} // ()


/**
Expand All @@ -74,7 +78,9 @@ public CodeResourceCache getInstance() {

@Test
public void testInit() {
boolean isInInterval = (System.currentTimeMillis()-codeResourceCache.getLastUpdate())<20;
long difference = System.currentTimeMillis()-codeResourceCache.getLastUpdate();
LOG.debug("testInit() difference={}", difference);
boolean isInInterval = difference<30000;
Assert.assertTrue(isInInterval, "Modification time of cache should be initialized.");
} // testInit()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
package org.tangram.components.test;

import groovy.lang.GroovyClassLoader;
import java.io.FileNotFoundException;
import javax.inject.Singleton;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.tangram.PersistentRestartCache;
import org.tangram.components.GroovyClassRepository;
import org.tangram.content.BeanFactory;
import org.tangram.content.CodeResourceCache;
import org.tangram.mock.content.MockBeanFactory;
import org.tangram.util.DummyRestartCache;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -43,7 +42,7 @@ public class GroovyClassRepositoryTest {
private final PersistentRestartCache restartCache = new DummyRestartCache(); // NOPMD - this field is not really unused

@Spy
private final MockBeanFactory factory = new MockBeanFactory();
private MockBeanFactory factory; // NOPMD - this field is not really unused

@Spy
private CodeResourceCache codeCache; // NOPMD - this field is not really unused
Expand All @@ -52,21 +51,17 @@ public class GroovyClassRepositoryTest {
private final GroovyClassRepository repository = new GroovyClassRepository();


public void init(String contentResource) throws Exception {
GenericCodeResourceCacheTest codeCacheTest = new GenericCodeResourceCacheTest();
codeCacheTest.init(contentResource);
public GroovyClassRepositoryTest(String contentResource) throws FileNotFoundException {
GenericCodeResourceCacheTest codeCacheTest = new GenericCodeResourceCacheTest(contentResource);
codeCache = codeCacheTest.getInstance();
factory = MockBeanFactory.getInstance(contentResource);
MockitoAnnotations.initMocks(this);
factory.init(contentResource);
repository.afterPropertiesSet();
} // init()


@BeforeClass
public void init() throws Exception {
init("/mock-content.xml");
} // init()
} // ()

public GroovyClassRepositoryTest() throws FileNotFoundException {
this(null);
} // ()

/**
* Return the instance in test with every mock needed for other tests to include a working instance.
Expand All @@ -78,11 +73,6 @@ public GroovyClassRepository getInstance() {
} // getInstance()


public BeanFactory getBeanFactory() {
return factory;
} // getBeanFactory


@Test
public void testListenerHandling() {
MockBeanListener beanListener = new MockBeanListener();
Expand Down
20 changes: 11 additions & 9 deletions core/test/org/tangram/components/test/MetaLinkHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
import org.tangram.link.Link;
import org.tangram.link.TargetDescriptor;
import org.tangram.logic.ClassRepository;
import org.tangram.mock.content.MockBeanFactory;
import org.tangram.monitor.Statistics;
import org.tangram.view.DynamicViewContextFactory;
import org.tangram.view.GenericPropertyConverter;
import org.tangram.view.PropertyConverter;
import org.tangram.view.ViewContext;
import org.tangram.view.ViewUtilities;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -66,7 +66,8 @@ public class MetaLinkHandlerTest {

private static final Link ACTION_LINK = new Link("/actionlinkresult");

private BeanFactory beanFactory;
@Spy
private MockBeanFactory beanFactory;

@Spy
private final Statistics statistics = new SimpleStatistics(); // NOPMD - this field is not really unused
Expand Down Expand Up @@ -95,6 +96,9 @@ public class MetaLinkHandlerTest {
private final MetaLinkHandler metaLinkHandler = new MetaLinkHandler();


/**
* Mock test class to bring bean factory aware and line handler together like in real world code.
*/
public class MockLinkHandler implements org.tangram.link.LinkHandler, BeanFactoryAware {

private BeanFactory beanFactory;
Expand Down Expand Up @@ -181,13 +185,11 @@ public Link createLink(HttpServletRequest request, HttpServletResponse response,
} // TestHandler


@BeforeClass
public void init() throws Exception {
public MetaLinkHandlerTest() throws Exception {
aggregator.setDispatcherPath("/testapp");
GroovyClassRepositoryTest groovyClassRepositoryTest = new GroovyClassRepositoryTest();
groovyClassRepositoryTest.init();
repository = groovyClassRepositoryTest.getInstance();
beanFactory = groovyClassRepositoryTest.getBeanFactory();

repository = new GroovyClassRepositoryTest().getInstance();
beanFactory = MockBeanFactory.getInstance();
MockitoAnnotations.initMocks(this);
viewContextFactory.afterPropertiesSet();
metaLinkHandler.afterPropertiesSet();
Expand All @@ -197,7 +199,7 @@ public void init() throws Exception {
metaLinkHandler.registerLinkHandler(atActionHandler);
TestHandler testHandler = new TestHandler();
metaLinkHandler.registerLinkHandler(testHandler);
} // init()
} // ()


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.tangram.link.LinkHandlerRegistry;
import org.tangram.link.TargetDescriptor;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


Expand All @@ -70,20 +69,17 @@ public class PacAuthenticationServiceTest {
private final Set<String> loginProviders = new HashSet<>();

@Spy
private final Map<String,String> userIdAttributes = new HashMap<>();
private final Map<String, String> userIdAttributes = new HashMap<>();

@InjectMocks
private final PacAuthenticationService pacAuthenticationService = new PacAuthenticationService();

private final FormClient formLogin = new FormClient();


@BeforeClass
public void init() throws Exception {
SimpleAuthenticatorTest authenticatorTest = new SimpleAuthenticatorTest();
authenticatorTest.init();
public PacAuthenticationServiceTest() {
formLogin.setName("form");
formLogin.setAuthenticator(authenticatorTest.getInstance());
formLogin.setAuthenticator(new SimpleAuthenticatorTest().getInstance());
formLogin.setProfileCreator(new UsernameProfileCreator());

clientSet.add(formLogin);
Expand Down
Loading

0 comments on commit 489bbf4

Please sign in to comment.