1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.JarPluginArtifact;
5   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
6   import com.atlassian.plugin.osgi.factory.transform.TransformStage;
7   import com.atlassian.plugin.osgi.factory.transform.model.SystemExports;
8   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
9   import com.atlassian.plugin.test.PluginJarBuilder;
10  
11  import org.dom4j.Document;
12  import org.dom4j.DocumentException;
13  import org.dom4j.DocumentHelper;
14  import org.dom4j.Element;
15  import org.dom4j.Node;
16  import org.dom4j.XPath;
17  import org.dom4j.io.OutputFormat;
18  import org.dom4j.io.XMLWriter;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.StringWriter;
23  import java.util.*;
24  
25  import junit.framework.Assert;
26  
27  /**
28   * Executes a transformation stage and tests xpath expressions against it
29   */
30  public class SpringTransformerTestHelper
31  {
32  
33      public static Element transform(final TransformStage transformer, final Element pluginRoot, final String... xpaths) throws IOException, DocumentException
34      {
35          return transform(transformer, null, pluginRoot, xpaths);
36      }
37  
38      public static Element transform(final TransformStage transformer, final List<HostComponentRegistration> regs, final Element pluginRoot, final String... xpaths) throws IOException, DocumentException
39      {
40          return transform(transformer, null, regs, pluginRoot, xpaths);
41      }
42  
43      public static Element transform(final TransformStage transformer, File pluginJar, final List<HostComponentRegistration> regs, final Element pluginRoot, final String... xpaths) throws IOException, DocumentException
44      {
45          if (pluginJar == null)
46          {
47              final String swriter = elementToString(pluginRoot);
48              pluginJar = new PluginJarBuilder().addResource(PluginAccessor.Descriptor.FILENAME, swriter).build();
49          }
50          Set<String> keys = new HashSet<String>(Arrays.asList("foo"));
51          final TransformContext context = new TransformContext(regs, SystemExports.NONE, new JarPluginArtifact(pluginJar), keys, PluginAccessor.Descriptor.FILENAME);
52  
53          transformer.execute(context);
54  
55          byte[] content = null;
56          for (byte[] entry : context.getFileOverrides().values())
57          {
58              if (entry.length > 0)
59              {
60                  content = entry;
61              }
62          }
63          if (content == null)
64          {
65              return null;
66          }
67          final Document springDoc = DocumentHelper.parseText(new String(content));
68          final Element springRoot = springDoc.getRootElement();
69  
70          for (final String xp : xpaths)
71          {
72              final XPath xpath = DocumentHelper.createXPath(xp);
73              final Object obj = xpath.selectObject(springRoot);
74              if (obj instanceof Node)
75              {
76                  // test passed
77              }
78              else if (obj instanceof Boolean)
79              {
80                  if (!((Boolean) obj).booleanValue())
81                  {
82                      printDocument(springDoc);
83                      Assert.fail("Unable to match xpath: " + xp);
84                  }
85              }
86              else if (obj == null)
87              {
88                  printDocument(springDoc);
89                  Assert.fail("Unable to match xpath: " + xp);
90              }
91              else
92              {
93                  printDocument(springDoc);
94                  Assert.fail("Unexpected result:" + obj);
95              }
96          }
97          return springRoot;
98      }
99  
100     static String elementToString(Element pluginRoot)
101             throws IOException
102     {
103         final StringWriter swriter = new StringWriter();
104         final OutputFormat outformat = OutputFormat.createPrettyPrint();
105         final XMLWriter writer = new XMLWriter(swriter, outformat);
106         writer.write(pluginRoot.getDocument());
107         writer.flush();
108         return swriter.toString();
109     }
110 
111     private static void printDocument(final Document springDoc) throws IOException
112     {
113         final OutputFormat outformat = OutputFormat.createPrettyPrint();
114         final XMLWriter writer = new XMLWriter(System.out, outformat);
115         writer.write(springDoc);
116         writer.flush();
117     }
118 
119 }