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