/* * Copyright (c) 2013, Inversoft Inc., All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific * language governing permissions and limitations under the License. */ package org.primeframework.mvc.parameter.convert.converters; import org.apache.commons.lang3.ArrayUtils; import org.primeframework.mvc.parameter.convert.GlobalConverter; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.fail; /** * This tests the URL converter. * * @author Brian Pontarelli */ public class URLConverterTest { @Test public void fromStrings() throws MalformedURLException { GlobalConverter converter = new URLConverter(); URL url = (URL) converter.convertFromStrings(URL.class, null, "testExpr", ArrayUtils.toArray((String) null)); assertNull(url); url = (URL) converter.convertFromStrings(URL.class, null, "testExpr", ArrayUtils.toArray("http://www.google.com")); assertEquals(url, new URL("http://www.google.com")); try { converter.convertFromStrings(URL.class, null, "testExpr", ArrayUtils.toArray("http://www.google.com", "http://www.inversoft.com")); fail("Should have failed"); } catch (UnsupportedOperationException e) { // Expected } } @Test public void toStrings() throws MalformedURLException { GlobalConverter converter = new URLConverter(); String str = converter.convertToString(URL.class, null, "testExpr", null); assertNull(str); str = converter.convertToString(URL.class, null, "testExpr", new URL("http://www.google.com")); assertEquals(str, "http://www.google.com"); } }