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