View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugin.logging.SystemStreamLog;
29  import org.codehaus.plexus.util.Os;
30  
31  /**
32   * Exhaustively check the OS mojo.
33   *
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   */
36  public class TestRequireOS
37      extends TestCase
38  {
39  
40      /**
41       * Test os.
42       */
43      public void testOS()
44      {
45          Log log = new SystemStreamLog();
46  
47          RequireOS rule = new RequireOS();
48          rule.displayOSInfo( log, true );
49  
50          Iterator<String> iter = Os.getValidFamilies().iterator();
51          String validFamily = null;
52          String invalidFamily = null;
53          while ( iter.hasNext() )
54          {
55              String fam = (String) iter.next();
56              if ( !Os.isFamily( fam ) )
57              {
58                  invalidFamily = fam;
59                  break;
60              }
61          }
62  
63          validFamily = Os.OS_FAMILY;
64  
65          log.info( "Testing Mojo Using Valid Family: " + validFamily + " Invalid Family: " + invalidFamily );
66  
67          rule.setFamily( validFamily );
68          assertTrue( rule.isAllowed() );
69  
70          rule.setFamily( invalidFamily );
71          assertFalse( rule.isAllowed() );
72  
73          rule.setFamily( "!" + invalidFamily );
74          assertTrue( rule.isAllowed() );
75  
76          rule.setFamily( "junk" );
77          try
78          {
79              rule.execute( EnforcerTestUtils.getHelper() );
80              fail( "Expected MojoExecution Exception becuase of invalid family type" );
81          }
82          catch ( EnforcerRuleException e )
83          {
84              log.info( "Caught Expected Exception:" + e.getLocalizedMessage() );
85          }
86  
87          rule.setFamily( null );
88          rule.setArch( Os.OS_ARCH );
89          assertTrue( rule.isAllowed() );
90  
91          rule.setArch( "somecrazyarch" );
92          assertFalse( rule.isAllowed() );
93  
94          rule.setArch( "!somecrazyarch" );
95          assertTrue( rule.isAllowed() );
96  
97          rule.setArch( null );
98  
99          rule.setName( Os.OS_NAME );
100         assertTrue( rule.isAllowed() );
101 
102         rule.setName( "somecrazyname" );
103         assertFalse( rule.isAllowed() );
104 
105         rule.setName( "!somecrazyname" );
106         assertTrue( rule.isAllowed() );
107 
108         rule.setName( null );
109 
110         rule.setVersion( Os.OS_VERSION );
111         assertTrue( rule.isAllowed() );
112 
113         rule.setVersion( "somecrazyversion" );
114         assertFalse( rule.isAllowed() );
115 
116         rule.setVersion( "!somecrazyversion" );
117         assertTrue( rule.isAllowed() );
118     }
119 
120     /**
121      * Test id.
122      */
123     public void testId()
124     {
125         RequireOS rule = new RequireOS();
126         rule.getCacheId();
127     }
128 
129 }