View Javadoc
1   package org.apache.maven.shared.utils;
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 org.junit.Test;
23  import org.junit.Before;
24  import org.junit.After;
25  import org.junit.Assert;
26  
27  import java.lang.reflect.Field;
28  import java.lang.reflect.Modifier;
29  import java.util.Set;
30  
31  import static org.hamcrest.CoreMatchers.*;
32  
33  /**
34   * This class will test the 'Os' class which evaluates
35   * operation system specific settings.
36   *
37   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
38   */
39  public class OsTest
40      extends Assert
41  {
42      private String origOsName;
43      private String origOsFamily;
44      private String origOsArch;
45      private String origOsVersion;
46  
47  
48      @Before
49      public void setUp()
50          throws Exception
51      {
52          origOsName = System.getProperty( "os.name" );
53          origOsArch = System.getProperty( "os.arch" );
54          origOsVersion = System.getProperty( "os.version" );
55          origOsFamily = Os.OS_FAMILY;
56  
57          // and now set some special settings ;)
58          System.setProperty( "os.name"   , "os/2" );
59          System.setProperty( "os.arch"   , "i386" );
60          System.setProperty( "os.version", "2.1.32" );
61  
62          // blow away the originally loaded values
63          setStaticOsField( "OS_NAME", "os/2" );
64          setStaticOsField( "OS_FAMILY", "os/2" );
65          setStaticOsField( "OS_ARCH", "i386" );
66          setStaticOsField( "OS_VERSION", "2.1.32" );
67      }
68  
69      @After
70      public void tearDown()
71          throws Exception
72      {
73          // set the original OS settings again
74          System.setProperty( "os.name"   , origOsName );
75          System.setProperty( "os.arch"   , origOsArch );
76          System.setProperty( "os.version", origOsVersion );
77  
78          // restore the originally loaded values
79          setStaticOsField( "OS_NAME", origOsName );
80          setStaticOsField( "OS_ARCH", origOsArch );
81          setStaticOsField( "OS_VERSION", origOsVersion );
82          setStaticOsField( "OS_FAMILY", origOsFamily );
83      }
84  
85      private void setStaticOsField( String variableName, Object value )
86          throws NoSuchFieldException, IllegalAccessException
87      {
88          Field field = Os.class.getField( variableName );
89  
90          Field modifiersField = Field.class.getDeclaredField( "modifiers" );
91          modifiersField.setAccessible( true );
92          modifiersField.setInt( field, field.getModifiers() & ~Modifier.FINAL );
93  
94          field.setAccessible( true );
95          field.set( null, value );
96      }
97  
98  
99      @Test
100     public void testConstructor()
101         throws Exception
102     {
103         Os os  = new Os();
104         os.eval();
105 
106         assertTrue( Os.isName( Os.FAMILY_OS2 ) );
107 
108         assertFalse( Os.isName( Os.FAMILY_DOS ) );
109         assertFalse( Os.isName( Os.FAMILY_MAC ) );
110         assertFalse( Os.isName( Os.FAMILY_NETWARE ) );
111         assertFalse( Os.isName( Os.FAMILY_OPENVMS ) );
112         assertFalse( Os.isName( Os.FAMILY_OS400 ) );
113         assertFalse( Os.isName( Os.FAMILY_TANDEM ) );
114         assertFalse( Os.isName( Os.FAMILY_UNIX ) );
115         assertFalse( Os.isName( Os.FAMILY_WIN9X ) );
116         assertFalse( Os.isName( Os.FAMILY_WINDOWS ) );
117         assertFalse( Os.isName( Os.FAMILY_ZOS ) );
118     }
119 
120     @Test
121     public void testFamilyNames()
122     {
123         assertEquals( Os.FAMILY_DOS, "dos" );
124         assertEquals( Os.FAMILY_MAC, "mac" );
125         assertEquals( Os.FAMILY_NETWARE, "netware" );
126         assertEquals( Os.FAMILY_OPENVMS, "openvms" );
127         assertEquals( Os.FAMILY_OS2, "os/2" );
128         assertEquals( Os.FAMILY_OS400, "os/400" );
129         assertEquals( Os.FAMILY_TANDEM, "tandem" );
130         assertEquals( Os.FAMILY_UNIX, "unix" );
131         assertEquals( Os.FAMILY_WIN9X, "win9x" );
132         assertEquals( Os.FAMILY_WINDOWS, "windows" );
133         assertEquals( Os.FAMILY_ZOS, "z/os" );
134     }
135 
136     @Test
137     public void testGetValidFamilies()
138     {
139         Set<String> osFamilies = Os.getValidFamilies();
140 
141         assertTrue( "OsFamilies Set size"
142                   , osFamilies.size() >= 11 );
143         
144         assert( osFamilies.contains( Os.FAMILY_DOS ) );
145         assert( osFamilies.contains( Os.FAMILY_MAC ) );
146         assert( osFamilies.contains( Os.FAMILY_NETWARE ) );
147         assert( osFamilies.contains( Os.FAMILY_OPENVMS ) );
148         assert( osFamilies.contains( Os.FAMILY_OS2 ) );
149         assert( osFamilies.contains( Os.FAMILY_OS400 ) );
150         assert( osFamilies.contains( Os.FAMILY_TANDEM ) );
151         assert( osFamilies.contains( Os.FAMILY_UNIX ) );
152         assert( osFamilies.contains( Os.FAMILY_WIN9X ) );
153         assert( osFamilies.contains( Os.FAMILY_WINDOWS ) );
154         assert( osFamilies.contains( Os.FAMILY_ZOS ) );
155         
156     }
157 
158 
159     @Test
160     public void testIsArch()
161     {
162         assertThat( "Arch is i386"
163                   , Os.isArch( "i386" )
164                   , is( true ) );
165 
166         assertThat( "Os is not Mac"
167                   , Os.isArch( "x86_64" )
168                   , is( false ) );
169     }
170 
171     @Test
172     public void testIsFamily()
173     {
174         assertThat( "Family is os/2"
175                   , Os.isFamily( Os.FAMILY_OS2 )
176                   , is( true ) );
177 
178         assertThat( "Family is not mac"
179                   , Os.isFamily( Os.FAMILY_MAC )
180                   , is( false ) );
181     }
182 
183     @Test
184     public void testIsName()
185     {
186         assertThat( "Name is os/2"
187                   , Os.isName( "os/2" )
188                   , is( true ) );
189 
190         assertThat( "Name is not Mac OS X"
191                   , Os.isName( "Mac OS X" )
192                   , is( false ) );
193     }
194 
195     @Test
196     public void testIsValidFamily()
197     {
198         assertThat( "os/2 isValidFamily"
199                   , Os.isValidFamily( Os.FAMILY_OS2 )
200                   , is( true ) );
201 
202         assertThat( "iPone != isValidFamily"
203                   , Os.isValidFamily( "iPhone" )
204                   , is( false ) );
205     }
206 
207     @Test
208     public void testIsVersion()
209     {
210         assertThat( "isVersion"
211                   , Os.isVersion( "2.1.32" )
212                   , is( true ) );
213 
214         assertThat( "isVersion"
215                   , Os.isVersion( "2.1" )
216                   , is( false ) );
217 
218         assertThat( "isVersion"
219                   , Os.isVersion( "4.5" )
220                   , is( false ) );
221 
222     }
223 }