View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      public int containerCountForUri( String uri )
67          throws IOException
68      {
69          Validate.notEmpty( uri, "uri can neither be null nor empty " );
70          ModelDataSource source = new DefaultModelDataSource();
71          source.init( domainModel.getModelProperties(), null );
72          return source.queryFor( uri ).size();
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  }