View Javadoc
1   package org.apache.maven.surefire.booter;
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.io.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.Objects;
26  import java.util.Properties;
27  
28  import static java.nio.charset.StandardCharsets.ISO_8859_1;
29  import static org.apache.maven.surefire.api.util.ReflectionUtils.loadClass;
30  
31  /**
32   * @author Kristian Rosenvold
33   */
34  public final class TypeEncodedValue
35  {
36      private final String type;
37      private final String value;
38  
39      public TypeEncodedValue( String type, String value )
40      {
41          this.type = type;
42          this.value = value;
43      }
44  
45      private boolean isTypeClass()
46      {
47          return Class.class.getName().equals( type );
48      }
49  
50      public Object getDecodedValue()
51      {
52          return getDecodedValue( Thread.currentThread().getContextClassLoader() );
53      }
54  
55      public Object getDecodedValue( ClassLoader classLoader )
56      {
57          if ( type.trim().isEmpty() )
58          {
59              return null;
60          }
61          else if ( type.equals( String.class.getName() ) )
62          {
63              return value;
64          }
65          else if ( isTypeClass() )
66          {
67              return loadClass( classLoader, value );
68          }
69          else if ( type.equals( File.class.getName() ) )
70          {
71              return new File( value );
72          }
73          else if ( type.equals( Boolean.class.getName() ) )
74          {
75              return Boolean.valueOf( value );
76          }
77          else if ( type.equals( Integer.class.getName() ) )
78          {
79              return Integer.valueOf( value );
80          }
81          else if ( type.equals( Properties.class.getName() ) )
82          {
83              Properties result = new Properties();
84              try
85              {
86                  result.load( new ByteArrayInputStream( value.getBytes( ISO_8859_1 ) ) );
87                  return result;
88              }
89              catch ( IOException e )
90              {
91                  throw new IllegalStateException( "bug in property conversion", e );
92              }
93          }
94          else
95          {
96              throw new IllegalArgumentException( "Unknown parameter type: " + type );
97          }
98      }
99  
100     @Override
101     public boolean equals( Object o )
102     {
103         if ( this == o )
104         {
105             return true;
106         }
107         if ( o == null || getClass() != o.getClass() )
108         {
109             return false;
110         }
111 
112         TypeEncodedValue that = (TypeEncodedValue) o;
113 
114         return equalsType( that ) && equalsValue( that );
115     }
116 
117     @Override
118     public int hashCode()
119     {
120         int result = type != null ? type.hashCode() : 0;
121         result = 31 * result + ( value != null ? value.hashCode() : 0 );
122         return result;
123     }
124 
125     @Override
126     public String toString()
127     {
128         return "TypeEncodedValue{" + "type='" + type + '\'' + ", value='" + value + '\'' + '}';
129     }
130 
131     private boolean equalsType( TypeEncodedValue that )
132     {
133         return Objects.equals( type, that.type );
134     }
135 
136     private boolean equalsValue( TypeEncodedValue that )
137     {
138         return Objects.equals( value, that.value );
139     }
140 }