Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TypeEncodedValue |
|
| 5.666666666666667;5,667 |
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.util.Properties; | |
25 | import org.apache.maven.surefire.util.NestedRuntimeException; | |
26 | import org.apache.maven.surefire.util.ReflectionUtils; | |
27 | ||
28 | /** | |
29 | * @author Kristian Rosenvold | |
30 | */ | |
31 | public class TypeEncodedValue | |
32 | { | |
33 | String type; | |
34 | ||
35 | String value; | |
36 | ||
37 | public TypeEncodedValue( String type, String value ) | |
38 | 0 | { |
39 | 0 | this.type = type; |
40 | 0 | this.value = value; |
41 | 0 | } |
42 | ||
43 | public boolean isTypeClass() | |
44 | { | |
45 | 0 | return Class.class.getName().equals( type ); |
46 | } | |
47 | ||
48 | public Object getDecodedValue() | |
49 | { | |
50 | 0 | return getDecodedValue( Thread.currentThread().getContextClassLoader() ); |
51 | } | |
52 | ||
53 | public Object getDecodedValue( ClassLoader classLoader ) | |
54 | { | |
55 | 0 | if ( type.trim().length() == 0 ) |
56 | { | |
57 | 0 | return null; |
58 | } | |
59 | 0 | else if ( type.equals( String.class.getName() ) ) |
60 | { | |
61 | 0 | return value; |
62 | } | |
63 | 0 | else if ( isTypeClass() ) |
64 | { | |
65 | 0 | return ReflectionUtils.loadClass( classLoader, value ); |
66 | } | |
67 | 0 | else if ( type.equals( File.class.getName() ) ) |
68 | { | |
69 | 0 | return new File( value ); |
70 | } | |
71 | 0 | else if ( type.equals( Boolean.class.getName() ) ) |
72 | { | |
73 | 0 | return Boolean.valueOf( value ); |
74 | } | |
75 | 0 | else if ( type.equals( Integer.class.getName() ) ) |
76 | { | |
77 | 0 | return Integer.valueOf( value ); |
78 | } | |
79 | 0 | else if ( type.equals( Properties.class.getName() ) ) |
80 | { | |
81 | 0 | final Properties result = new Properties(); |
82 | try | |
83 | { | |
84 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream( value.getBytes( "8859_1" ) ); |
85 | 0 | result.load( bais ); |
86 | } | |
87 | 0 | catch ( Exception e ) |
88 | { | |
89 | 0 | throw new NestedRuntimeException( "bug in property conversion", e ); |
90 | 0 | } |
91 | 0 | return result; |
92 | } | |
93 | else | |
94 | { | |
95 | 0 | throw new IllegalArgumentException( "Unknown parameter type: " + type ); |
96 | } | |
97 | } | |
98 | ||
99 | @SuppressWarnings( "SimplifiableIfStatement" ) | |
100 | public boolean equals( Object o ) | |
101 | { | |
102 | 0 | if ( this == o ) |
103 | { | |
104 | 0 | return true; |
105 | } | |
106 | 0 | if ( o == null || getClass() != o.getClass() ) |
107 | { | |
108 | 0 | return false; |
109 | } | |
110 | ||
111 | 0 | TypeEncodedValue that = (TypeEncodedValue) o; |
112 | ||
113 | 0 | if ( type != null ? !type.equals( that.type ) : that.type != null ) |
114 | { | |
115 | 0 | return false; |
116 | } | |
117 | 0 | return !( value != null ? !value.equals( that.value ) : that.value != null ); |
118 | ||
119 | } | |
120 | ||
121 | public int hashCode() | |
122 | { | |
123 | 0 | int result = type != null ? type.hashCode() : 0; |
124 | 0 | result = 31 * result + ( value != null ? value.hashCode() : 0 ); |
125 | 0 | return result; |
126 | } | |
127 | } |