1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Properties;
26
27 import static java.nio.charset.StandardCharsets.ISO_8859_1;
28 import static org.apache.maven.surefire.util.ReflectionUtils.loadClass;
29
30
31
32
33 public class TypeEncodedValue
34 {
35 private final String type;
36 private final String value;
37
38 public TypeEncodedValue( String type, String value )
39 {
40 this.type = type;
41 this.value = value;
42 }
43
44 public boolean isTypeClass()
45 {
46 return Class.class.getName().equals( type );
47 }
48
49 public Object getDecodedValue()
50 {
51 return getDecodedValue( Thread.currentThread().getContextClassLoader() );
52 }
53
54 public Object getDecodedValue( ClassLoader classLoader )
55 {
56 if ( type.trim().isEmpty() )
57 {
58 return null;
59 }
60 else if ( type.equals( String.class.getName() ) )
61 {
62 return value;
63 }
64 else if ( isTypeClass() )
65 {
66 return loadClass( classLoader, value );
67 }
68 else if ( type.equals( File.class.getName() ) )
69 {
70 return new File( value );
71 }
72 else if ( type.equals( Boolean.class.getName() ) )
73 {
74 return Boolean.valueOf( value );
75 }
76 else if ( type.equals( Integer.class.getName() ) )
77 {
78 return Integer.valueOf( value );
79 }
80 else if ( type.equals( Properties.class.getName() ) )
81 {
82 Properties result = new Properties();
83 try
84 {
85 result.load( new ByteArrayInputStream( value.getBytes( ISO_8859_1 ) ) );
86 return result;
87 }
88 catch ( IOException e )
89 {
90 throw new IllegalStateException( "bug in property conversion", e );
91 }
92 }
93 else
94 {
95 throw new IllegalArgumentException( "Unknown parameter type: " + type );
96 }
97 }
98
99 @Override
100 public boolean equals( Object o )
101 {
102 if ( this == o )
103 {
104 return true;
105 }
106 if ( o == null || getClass() != o.getClass() )
107 {
108 return false;
109 }
110
111 TypeEncodedValue that = (TypeEncodedValue) o;
112
113 return equalsType( that ) && equalsValue( that );
114
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 private boolean equalsType( TypeEncodedValue that )
126 {
127 return type == null ? that.type == null : type.equals( that.type );
128 }
129
130 private boolean equalsValue( TypeEncodedValue that )
131 {
132 return value == null ? that.value == null : value.equals( that.value );
133 }
134 }