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  // TODO: Auto-generated Javadoc
32  /**
33   * Exhaustively check the OS mojo.
34   *
35   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
36   */
37  public class TestRequireOS
38      extends TestCase
39  {
40  
41      /**
42       * Test os.
43       */
44      public void testOS()
45      {
46          Log log = new SystemStreamLog();
47  
48          RequireOS rule = new RequireOS();
49          rule.displayOSInfo( log, true );
50  
51          Iterator iter = Os.getValidFamilies().iterator();
52          String validFamily = null;
53          String invalidFamily = null;
54          while ( iter.hasNext() )
55          {
56              String fam = (String) iter.next();
57              if ( !Os.isFamily( fam ) )
58              {
59                  invalidFamily = fam;
60                  break;
61              }
62          }
63  
64          validFamily = Os.OS_FAMILY;
65  
66          log.info( "Testing Mojo Using Valid Family: " + validFamily + " Invalid Family: " + invalidFamily );
67  
68          rule.setFamily( validFamily );
69          assertTrue( rule.isAllowed() );
70  
71          rule.setFamily( invalidFamily );
72          assertFalse( rule.isAllowed() );
73  
74          rule.setFamily( "!" + invalidFamily );
75          assertTrue( rule.isAllowed() );
76  
77          rule.setFamily( "junk" );
78          try
79          {
80              rule.execute( EnforcerTestUtils.getHelper() );
81              fail( "Expected MojoExecution Exception becuase of invalid family type" );
82          }
83          catch ( EnforcerRuleException e )
84          {
85              log.info( "Caught Expected Exception:" + e.getLocalizedMessage() );
86          }
87  
88          rule.setFamily( null );
89          rule.setArch( Os.OS_ARCH );
90          assertTrue( rule.isAllowed() );
91  
92          rule.setArch( "somecrazyarch" );
93          assertFalse( rule.isAllowed() );
94  
95          rule.setArch( "!somecrazyarch" );
96          assertTrue( rule.isAllowed() );
97  
98          rule.setArch( null );
99  
100         rule.setName( Os.OS_NAME );
101         assertTrue( rule.isAllowed() );
102 
103         rule.setName( "somecrazyname" );
104         assertFalse( rule.isAllowed() );
105 
106         rule.setName( "!somecrazyname" );
107         assertTrue( rule.isAllowed() );
108 
109         rule.setName( null );
110 
111         rule.setVersion( Os.OS_VERSION );
112         assertTrue( rule.isAllowed() );
113 
114         rule.setVersion( "somecrazyversion" );
115         assertFalse( rule.isAllowed() );
116 
117         rule.setVersion( "!somecrazyversion" );
118         assertTrue( rule.isAllowed() );
119     }
120 
121     /**
122      * Test id.
123      */
124     public void testId()
125     {
126         RequireOS rule = new RequireOS();
127         rule.getCacheId();
128     }
129 
130 }