1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project.harness;
20
21 import java.io.File;
22 import java.util.Iterator;
23 import java.util.Objects;
24
25 import org.apache.commons.jxpath.JXPathContext;
26 import org.apache.commons.jxpath.JXPathNotFoundException;
27 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
28 import org.apache.maven.project.MavenProject;
29
30 public class PomTestWrapper {
31
32 private File pomFile;
33
34 private JXPathContext context;
35
36 private MavenProject mavenProject;
37
38 static {
39 JXPathContextReferenceImpl.addNodePointerFactory(new Xpp3DomPointerFactory());
40 }
41
42 public PomTestWrapper(File pomFile, MavenProject mavenProject) {
43 this.mavenProject = Objects.requireNonNull(mavenProject, "mavenProject cannot be null");
44 this.pomFile = pomFile;
45 context = JXPathContext.newContext(mavenProject.getModel());
46 }
47
48 public PomTestWrapper(MavenProject mavenProject) {
49 this.mavenProject = Objects.requireNonNull(mavenProject, "mavenProject cannot be null");
50 context = JXPathContext.newContext(mavenProject.getModel());
51 }
52
53 public MavenProject getMavenProject() {
54 return mavenProject;
55 }
56
57 public File getBasedir() {
58 return (pomFile != null) ? pomFile.getParentFile() : null;
59 }
60
61 public void setValueOnModel(String expression, Object value) {
62 context.setValue(expression, value);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 public Iterator<?> getIteratorForXPathExpression(String expression) {
77 return context.iterate(expression);
78 }
79
80 public boolean containsXPathExpression(String expression) {
81 return context.getValue(expression) != null;
82 }
83
84 public Object getValue(String expression) {
85 try {
86 return context.getValue(expression);
87 } catch (JXPathNotFoundException e) {
88 return null;
89 }
90 }
91
92 public boolean xPathExpressionEqualsValue(String expression, String value) {
93 return context.getValue(expression) != null
94 && context.getValue(expression).equals(value);
95 }
96 }