File: PropertyUtilsTest.java

package info (click to toggle)
libbtm-java 2.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,568 kB
  • ctags: 7,437
  • sloc: java: 17,848; xml: 158; makefile: 19; sh: 10
file content (260 lines) | stat: -rw-r--r-- 9,076 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Bitronix Transaction Manager
 *
 * Copyright (c) 2010, Bitronix Software.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1301 USA
 */
package bitronix.tm.utils;

import junit.framework.TestCase;

import java.util.Properties;
import java.util.Map;

/**
 *
 * @author lorban
 */
public class PropertyUtilsTest extends TestCase {

    public void testSetProperties() throws Exception {
        Destination destination = new Destination();

        PropertyUtils.setProperty(destination, "props.key", "value");
        assertEquals("value", destination.getProps().getProperty("key"));
        PropertyUtils.setProperty(destination, "subDestination.props.key", "value");
        assertEquals("value", destination.getSubDestination().getProps().getProperty("key"));
        PropertyUtils.setProperty(destination, "anInteger", "10");
        assertEquals(10, destination.getAnInteger());
        PropertyUtils.setProperty(destination, "subDestination.anInteger", "20");
        assertEquals(20, destination.getSubDestination().getAnInteger());
        PropertyUtils.setProperty(destination, "aBoolean", "true");
        assertEquals(true, destination.isABoolean());
        PropertyUtils.setProperty(destination, "aWriteOnlyInt", "20");

        PrivateDestination privateDestination = new PrivateDestination();
        try {
            PropertyUtils.setProperty(privateDestination, "subDestination.props.key", "value");
            fail("it is not possible to set the 'subDestination' property, PropertyException should have been thrown");
        } catch (PropertyException ex) {
            assertEquals("cannot set property 'subDestination.props.key' - 'subDestination' is null and cannot be auto-filled", ex.getMessage());
        }
    }

    public void testSetPropertiesObjectLongKey() throws Exception {
        PrivateDestination destination = new PrivateDestination();
        
        PropertyUtils.setProperty(destination, "props.key", "value1");
        PropertyUtils.setProperty(destination, "props.a.dotted.key", "value2");

        assertEquals("value1", destination.getProps().get("key"));
        assertEquals("value2", destination.getProps().get("a.dotted.key"));
    }

    public void testSmartGetProperties() throws Exception {
        Destination destination = new Destination();
        destination.setAnInteger(10);
        destination.setABoolean(true);

        Properties props = new Properties();
        props.setProperty("number1", "one");
        props.setProperty("number2", "two");
        destination.setProps(props);

        Map map = PropertyUtils.getProperties(destination);

        assertEquals(12, map.size());
        assertEquals("one", map.get("props.number1"));
        assertEquals("two", map.get("props.number2"));
        assertEquals(new Integer(10), map.get("anInteger"));
        assertEquals(new Boolean(true), map.get("aBoolean"));
        assertEquals(new Boolean(false), map.get("anotherBoolean"));
        assertNull(map.get("subDestination"));
    }

    public void testSetPrimitiveTypes() throws Exception {
        Destination destination = new Destination();

        PropertyUtils.setProperty(destination, "aString", "this is my string");
        PropertyUtils.setProperty(destination, "aBoolean", "true");
        PropertyUtils.setProperty(destination, "aByte", "100");
        PropertyUtils.setProperty(destination, "aShort", "20000");
        PropertyUtils.setProperty(destination, "anInteger", "300000");
        PropertyUtils.setProperty(destination, "aLong", "4000000");
        PropertyUtils.setProperty(destination, "aFloat", "3.14");
        PropertyUtils.setProperty(destination, "aDouble", "0.654987");

        assertEquals("this is my string", destination.getAString());
        assertEquals(true, destination.isABoolean());
        assertEquals(100, destination.getAByte());
        assertEquals(20000, destination.getAShort());
        assertEquals(300000, destination.getAnInteger());
        assertEquals(4000000, destination.getALong());
        assertEquals(3.14f, destination.getAFloat(), 0.01f);
        assertEquals(0.654987, destination.getADouble(), 0.000001);
    }

    public void testGetPrimitiveTypes() throws Exception {
        Destination destination = new Destination();
        destination.setAString("this is my string");
        destination.setABoolean(true);
        destination.setAByte((byte) 100);
        destination.setAShort((short) 20000);
        destination.setAnInteger(300000);
        destination.setALong(4000000L);
        destination.setAFloat(3.14f);
        destination.setADouble(0.654987);

        assertEquals("this is my string", PropertyUtils.getProperty(destination, "aString"));
        assertEquals(Boolean.TRUE, PropertyUtils.getProperty(destination, "aBoolean"));
        assertEquals(new Byte((byte) 100), PropertyUtils.getProperty(destination, "aByte"));
        assertEquals(new Short((short) 20000), PropertyUtils.getProperty(destination, "aShort"));
        assertEquals(new Integer(300000), PropertyUtils.getProperty(destination, "anInteger"));
        assertEquals(new Long(4000000L), PropertyUtils.getProperty(destination, "aLong"));
        assertEquals(new Float(3.14f), PropertyUtils.getProperty(destination, "aFloat"));
        assertEquals(new Double(0.654987), PropertyUtils.getProperty(destination, "aDouble"));
    }

    public static class Destination {
        private Properties props;
        private Destination subDestination;
        private int anInteger;
        private int aWriteOnlyInt;
        private boolean aBoolean;
        private boolean anotherBoolean;
        private String aString;
        private byte aByte;
        private short aShort;
        private long aLong;
        private float aFloat;
        private double aDouble;

        public Properties getProps() {
            return props;
        }

        public void setProps(Properties props) {
            this.props = props;
        }

        public Destination getSubDestination() {
            return subDestination;
        }

        public void setSubDestination(Destination subDestination) {
            this.subDestination = subDestination;
        }

        public int getAnInteger() {
            return anInteger;
        }

        public void setAnInteger(int anInteger) {
            this.anInteger = anInteger;
        }

        public void setAWriteOnlyInt(int aWriteOnlyInt) {
            this.aWriteOnlyInt = aWriteOnlyInt;
        }

        public boolean isABoolean() {
            return aBoolean;
        }

        public void setABoolean(boolean aBoolean) {
            this.aBoolean = aBoolean;
        }

        public boolean isAnotherBoolean() {
            return anotherBoolean;
        }

        public void setAnotherBoolean(boolean anotherBoolean) {
            this.anotherBoolean = anotherBoolean;
        }

        public String getAString() {
            return aString;
        }

        public void setAString(String aString) {
            this.aString = aString;
        }

        public byte getAByte() {
            return aByte;
        }

        public void setAByte(byte aByte) {
            this.aByte = aByte;
        }

        public short getAShort() {
            return aShort;
        }

        public void setAShort(short aShort) {
            this.aShort = aShort;
        }

        public long getALong() {
            return aLong;
        }

        public void setALong(long aLong) {
            this.aLong = aLong;
        }

        public float getAFloat() {
            return aFloat;
        }

        public void setAFloat(float aFloat) {
            this.aFloat = aFloat;
        }

        public double getADouble() {
            return aDouble;
        }

        public void setADouble(double aDouble) {
            this.aDouble = aDouble;
        }
    }

    private class PrivateDestination {
        private Properties props;
        private PrivateDestination subDestination;

        public Properties getProps() {
            return props;
        }

        public void setProps(Properties props) {
            this.props = props;
        }

        public PrivateDestination getSubDestination() {
            return subDestination;
        }

        public void setSubDestination(PrivateDestination subDestination) {
            this.subDestination = subDestination;
        }
    }

}