Skip to content

Commit

Permalink
Add test for beans file
Browse files Browse the repository at this point in the history
  • Loading branch information
iay committed Sep 7, 2017
1 parent c406eaf commit 53aa345
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${spring.groupId}</groupId>
<artifactId>spring-beans</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${spring.groupId}</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${spring.groupId}</groupId>
<artifactId>spring-core</artifactId>
<scope>test</scope>
</dependency>

<!-- Managed Dependencies -->

Expand Down
60 changes: 60 additions & 0 deletions src/test/java/uk/org/iay/incommon/mda/dom/saml/BeansFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

package uk.org.iay.incommon.mda.dom.saml;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.testng.Assert;
import org.testng.annotations.Test;

public class BeansFileTest {

@Test
public void testBeans() throws Exception {
// Create an application context, which also acts as a bean definition registry
final GenericApplicationContext ctx = new GenericApplicationContext();

// Load bean definitions into the registry
final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new ClassPathResource("uk/org/iay/incommon//mda/beans.xml"));

final String[] defNames = ctx.getBeanDefinitionNames();
for (final String defName : defNames) {
final BeanDefinition def = ctx.getBeanDefinition(defName);

// All bean definitions should start with "inc."
Assert.assertTrue(defName.startsWith("inc."), "does not start with correct prefix: " + defName);

// All bean definitions should be abstract
Assert.assertTrue(def.isAbstract(), "not abstract: " + defName);

// Deal with the class name, if the definition has one
final String className = def.getBeanClassName();
if (className != null) {
try {
// Check that the class can be loaded
Class.forName(className);

// The name of the class within its package should be included in the bean
// name's prefix; there may be more after that
final String classLastPart = className.replaceFirst("^.*\\.", "");
Assert.assertTrue(defName.startsWith("inc." + classLastPart), "does not start with correct prefix: " + defName);

// If the class represents a stage, its parent should be the stage parent
if (className.endsWith("Stage")) {
Assert.assertEquals(def.getParentName(), "inc.stage_parent");
}
} catch (ClassNotFoundException e) {
// Could not load class
Assert.fail("could not load class " + className + " for bean " + defName);
}
}
}

// Refresh the context to process the bean definitions
ctx.refresh();

}

}

0 comments on commit 53aa345

Please sign in to comment.