1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.css;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.fail;
  30 
  31 import java.net.MalformedURLException;
  32 import java.net.URL;
  33 
  34 import javafx.css.ParsedValue;
  35 import javafx.scene.text.Font;
  36 
  37 import org.junit.Before;
  38 import org.junit.Test;
  39 
  40 import com.sun.javafx.css.converters.StringConverter;
  41 import com.sun.javafx.css.converters.URLConverter;
  42 
  43 
  44 public class URLTypeTest {
  45 
  46     public URLTypeTest() {
  47     }
  48 
  49     static final String absClassName = "/com/sun/javafx/css/URLTypeTest.class";
  50     static final String classURL = URLTypeTest.class.getResource("URLTypeTest.class").toExternalForm();
  51 
  52     final String baseURL = "http://a/b/c/d;p?q";
  53 
  54     // from rfc3986, section 5
  55     final String[][] testPairs = new String[][] {
  56         {"file:h"           ,  "file:h"},
  57         {"g"             ,  "http://a/b/c/g"},
  58         {"./g"           ,  "http://a/b/c/g"},
  59         {"g/"            ,  "http://a/b/c/g/"},
  60         // The following is relative to classloader root, and since it won't be found, will return null
  61         {"/g"            ,  null},
  62         // The following is relative to classloader root and will be resolved as such
  63         {absClassName    ,  classURL},
  64         {"//g"           ,  "http://g"},
  65         // actual is http://a/b/c/?y - bug in java.net.URI?       {"?y"            ,  "http://a/b/c/d;p?y"},
  66         {"g?y"           ,  "http://a/b/c/g?y"},
  67         {"#s"            ,  "http://a/b/c/d;p?q#s"},
  68         {"g#s"           ,  "http://a/b/c/g#s"},
  69         {"g?y#s"         ,  "http://a/b/c/g?y#s"},
  70         {";x"            ,  "http://a/b/c/;x"},
  71         {"g;x"           ,  "http://a/b/c/g;x"},
  72         {"g;x?y#s"       ,  "http://a/b/c/g;x?y#s"},
  73         // empty string causes URISyntaxException, so converter returns null {""              ,  "http://a/b/c/d;p?q"},
  74         {"", null}, // not part of the rfc test suite - converter returns null if resolving base against empty string
  75         {"."             ,  "http://a/b/c/"},
  76         {"./"            ,  "http://a/b/c/"},
  77         {".."            ,  "http://a/b/"},
  78         {"../"           ,  "http://a/b/"},
  79         {"../g"          ,  "http://a/b/g"},
  80         {"../.."         ,  "http://a/"},
  81         {"../../"        ,  "http://a/"},
  82         {"../../g"       ,  "http://a/g"}
  83       };
  84 
  85     /**
  86      * Test of convert method, of class URLType.
  87      */
  88     @Test
  89     public void testConvert() {
  90         //System.out.println("convert");
  91         ParsedValue<ParsedValue[],String>[] urls = new ParsedValue[testPairs.length];
  92 
  93         for(int n=0; n<testPairs.length; n++) {
  94             ParsedValue[] values = new ParsedValue[] {
  95                 new ParsedValueImpl<String,String>(testPairs[n][0], StringConverter.getInstance()),
  96                 new ParsedValueImpl<String, String>(baseURL, null)
  97             };
  98             urls[n] = new ParsedValueImpl<ParsedValue[],String>(values, URLConverter.getInstance());
  99         };
 100 
 101         ParsedValue<ParsedValue<ParsedValue[],String>[],String[]> value =
 102                 new ParsedValueImpl<ParsedValue<ParsedValue[],String>[],String[]>(urls, URLConverter.SequenceConverter.getInstance());
 103 
 104         Font font = null;
 105         String[] result = value.convert(font);
 106         assertEquals(testPairs.length, result.length);
 107         for(int n=0; n<result.length; n++) {
 108             String msg = "[" + n + "]" + "resolve \'" + testPairs[n][0] + "\'";
 109             assertEquals(msg, testPairs[n][1], result[n]);
 110         }
 111     }
 112 
 113 }