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