View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jpa;
5   
6   import java.io.IOException;
7   import java.util.HashMap;
8   import java.util.Map;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  
12  import javax.persistence.EntityManagerFactory;
13  import javax.persistence.spi.PersistenceProvider;
14  import javax.persistence.spi.PersistenceUnitInfo;
15  import javax.xml.parsers.DocumentBuilder;
16  import javax.xml.parsers.DocumentBuilderFactory;
17  import javax.xml.parsers.ParserConfigurationException;
18  import javax.xml.xpath.XPath;
19  import javax.xml.xpath.XPathConstants;
20  import javax.xml.xpath.XPathExpression;
21  import javax.xml.xpath.XPathExpressionException;
22  import javax.xml.xpath.XPathFactory;
23  
24  import com.github.smokestack.exception.NotYetImplementedException;
25  
26  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
27  import org.apache.commons.lang.builder.ToStringStyle;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.NodeList;
30  import org.xml.sax.SAXException;
31  
32  /**
33   * @author gliptak
34   *
35   */
36  public class MockPersistenceProviderImpl implements PersistenceProvider {
37  
38  	private static Logger log=Logger.getLogger(MockPersistenceProviderImpl.class.getName());
39  	private Map<String, MockEntityManagerFactory> factories=new HashMap<String, MockEntityManagerFactory>();
40  	
41  	/* (non-Javadoc)
42  	 * @see javax.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)
43  	 */
44  	public EntityManagerFactory createContainerEntityManagerFactory(
45  			PersistenceUnitInfo info, Map map) {
46  		throw new NotYetImplementedException();
47  	}
48  
49  	/* (non-Javadoc)
50  	 * @see javax.persistence.spi.PersistenceProvider#createEntityManagerFactory(java.lang.String, java.util.Map)
51  	 */
52  	public EntityManagerFactory createEntityManagerFactory(String emName, Map map) {
53  		
54  		NodeList nodes=null;
55  		
56  		try {
57  			nodes=getEntityManagerConfig(emName);
58  		} catch (Exception e) {
59  			log.log(Level.INFO, "createEntityManagerFactory failed", e);
60  		}
61  		
62  		if (nodes==null || nodes.getLength()==0){
63  			return null;
64  		}
65  		
66  		MockEntityManagerFactory f=new MockEntityManagerFactory();
67  		factories.put(emName, f);
68  		return f;
69  	}
70  	
71  	protected NodeList getEntityManagerConfig(String emName) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException{
72  		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73  		factory.setNamespaceAware(true); // never forget this!
74  		DocumentBuilder builder = factory.newDocumentBuilder();
75  		Document doc = builder.parse(getClass().getResourceAsStream("/META-INF/persistence.xml"));
76  
77  		XPathFactory xpfactory = XPathFactory.newInstance();
78  		XPath xpath = xpfactory.newXPath();
79  		xpath.setNamespaceContext(new PersistenceNamespaceContext());
80  		  
81  		XPathExpression expr = xpath.compile("/p:persistence/p:persistence-unit[@name='"+emName+"']");
82  
83  		Object result = expr.evaluate(doc, XPathConstants.NODESET);
84  
85  		return (NodeList) result;
86  	}
87  	
88  	/* (non-Javadoc)
89  	 * @see java.lang.Object#toString()
90  	 */
91  	public String toString(){
92  		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
93  	}
94  
95  	/**
96  	 * Used for validation
97  	 */
98  	public Map<String, MockEntityManagerFactory> getMockEntityManagerFactories() {
99  		return factories;		
100 	}
101 }