1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.core.util;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.test.AbstractTest;
20
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.Map;
26
27/**
28 * Tests for the Assert class
29 *
30 * @author Chris Mair
31 * @version $Revision$ - $Date$
32 */
33public class AssertTest extends AbstractTest {
34
35    private static final Logger LOG = Logger.getLogger(AssertTest.class);
36
37    /**
38     * This interface defines a generic closure (a generic wrapper for a block of code).
39     */
40    private static interface ExceptionClosure {
41        /**
42         * Execute arbitrary logic that can throw any type of Exception
43         *
44         * @throws Exception
45         */
46        public void execute() throws Exception;
47    }
48
49
50    private static final String MESSAGE = "exception message";
51
52    /**
53     * Test the assertNull() method
54     */
55    public void testAssertNull() {
56
57        Assert.isNull(null, MESSAGE);
58
59        try {
60            Assert.isNull("OK", MESSAGE);
61            fail("Expected IllegalArumentException");
62        }
63        catch (AssertFailedException expected) {
64            LOG.info("Expected: " + expected);
65            assertExceptionMessageContains(expected, MESSAGE);
66        }
67    }
68
69
70    /**
71     * Test the assertNotNull() method
72     */
73    public void testAssertNotNull() {
74
75        Assert.notNull("OK", MESSAGE);
76
77        try {
78            Assert.notNull(null, MESSAGE);
79            fail("Expected IllegalArumentException");
80        }
81        catch (AssertFailedException expected) {
82            LOG.info("Expected: " + expected);
83            assertExceptionMessageContains(expected, MESSAGE);
84        }
85    }
86
87    /**
88     * Test the assertTrue() method
89     */
90    public void testAssertTrue() throws Exception {
91
92        Assert.isTrue(true, MESSAGE);
93
94        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
95            public void execute() throws Exception {
96                Assert.isTrue(false, MESSAGE);
97            }
98        });
99    }
100
101    /**
102     * Test the assertFalse() method
103     */
104    public void testAssertFalse() throws Exception {
105
106        Assert.isFalse(false, MESSAGE);
107
108        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
109            public void execute() throws Exception {
110                Assert.isFalse(true, MESSAGE);
111            }
112        });
113    }
114
115    /**
116     * Test the assertNotEmpty(Collection,String) method
117     */
118    public void testAssertNotNullOrEmpty_Collection() throws Exception {
119
120        final Collection COLLECTION = Collections.singletonList("item");
121        Assert.notNullOrEmpty(COLLECTION, MESSAGE);
122
123        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
124            public void execute() throws Exception {
125                Assert.notNullOrEmpty((Collection) null, MESSAGE);
126            }
127        });
128
129        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
130            public void execute() throws Exception {
131                Assert.notNullOrEmpty(new ArrayList(), MESSAGE);
132            }
133        });
134    }
135
136    /**
137     * Test the assertNotEmpty(Map,String) method
138     */
139    public void testAssertNotNullOrEmpty_Map() throws Exception {
140
141        final Map MAP = Collections.singletonMap("key", "value");
142        Assert.notNullOrEmpty(MAP, MESSAGE);
143
144        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
145            public void execute() throws Exception {
146                Assert.notNullOrEmpty((Map) null, MESSAGE);
147            }
148        });
149
150        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
151            public void execute() throws Exception {
152                Assert.notNullOrEmpty(new HashMap(), MESSAGE);
153            }
154        });
155    }
156
157    /**
158     * Test the assertNotEmpty(Objecct[],String) method
159     */
160    public void testAssertNotNullOrEmpty_array() throws Exception {
161
162        final Object[] ARRAY = {"1", "2"};
163        Assert.notNullOrEmpty(ARRAY, MESSAGE);
164
165        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
166            public void execute() throws Exception {
167                Assert.notNullOrEmpty((Object[]) null, MESSAGE);
168            }
169        });
170
171        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
172            public void execute() throws Exception {
173                Assert.notNullOrEmpty(new String[]{}, MESSAGE);
174            }
175        });
176    }
177
178    /**
179     * Test the assertNotEmpty(String,String) method
180     */
181    public void testAssertNotNullOrEmpty_String() throws Exception {
182
183        Assert.notNullOrEmpty("OK", MESSAGE);
184
185        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
186            public void execute() throws Exception {
187                Assert.notNullOrEmpty((String) null, MESSAGE);
188            }
189        });
190
191        verifyThrowsAssertFailedException(true, new ExceptionClosure() {
192            public void execute() throws Exception {
193                Assert.notNullOrEmpty("", MESSAGE);
194            }
195        });
196    }
197
198    //-------------------------------------------------------------------------
199    // Helper Methods
200    //-------------------------------------------------------------------------
201
202    private void assertExceptionMessageContains(Throwable exception, String text) {
203        String message = exception.getMessage();
204        assertTrue("Exception message [" + message + "] does not contain [" + text + "]", message.indexOf(text) != -1);
205    }
206
207    /**
208     * Verify that execution of the ExceptionClosure (code block) results in an
209     * AssertFailedException being thrown with the constant MESSAGE as its message.
210     *
211     * @param closure - the ExceptionClosure encapsulating the code to execute
212     */
213    private void verifyThrowsAssertFailedException(boolean checkMessage, ExceptionClosure closure)
214            throws Exception {
215
216        try {
217            closure.execute();
218            fail("Expected IllegalArumentException");
219        }
220        catch (AssertFailedException expected) {
221            LOG.info("Expected: " + expected);
222            if (checkMessage) {
223                assertExceptionMessageContains(expected, MESSAGE);
224            }
225		}
226	}
227
228}
229