Search in sources :

Example 1 with UrlAttributes

use of org.apache.wicket.request.resource.ResourceReference.UrlAttributes in project wicket by apache.

the class PackageResourceReferenceTest method userAttributesPreference.

/**
 * Assert preference for specified locale and style over session ones
 */
@Test
public void userAttributesPreference() {
    tester.getSession().setLocale(new Locale("en"));
    tester.getSession().setStyle("style");
    Locale[] userLocales = { null, new Locale("pt"), new Locale("pt", "BR") };
    String userStyle = "style2";
    for (Locale userLocale : userLocales) {
        for (String variation : variations) {
            ResourceReference reference = new PackageResourceReference(scope, "resource.txt", userLocale, userStyle, variation);
            UrlAttributes urlAttributes = reference.getUrlAttributes();
            assertEquals(userLocale, urlAttributes.getLocale());
            assertEquals(userStyle, urlAttributes.getStyle());
            assertEquals(variation, urlAttributes.getVariation());
        }
    }
}
Also used : Locale(java.util.Locale) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) Test(org.junit.Test)

Example 2 with UrlAttributes

use of org.apache.wicket.request.resource.ResourceReference.UrlAttributes in project wicket by apache.

the class ResourceUtilTest method decodeResourceReferenceAttributesWithUrl.

@Test
public void decodeResourceReferenceAttributesWithUrl() throws Exception {
    Url url = Url.parse("www.funny.url/?param1=value1");
    UrlAttributes attributes = ResourceUtil.decodeResourceReferenceAttributes(url);
    assertEquals(new UrlAttributes(null, null, null), attributes);
    url = Url.parse("www.funny.url/?de_DE");
    attributes = ResourceUtil.decodeResourceReferenceAttributes(url);
    assertEquals(Locale.GERMANY, attributes.getLocale());
    assertNull(attributes.getStyle());
    assertNull(attributes.getVariation());
    url = Url.parse("www.funny.url/?-style");
    attributes = ResourceUtil.decodeResourceReferenceAttributes(url);
    assertNull(attributes.getLocale());
    assertEquals("style", attributes.getStyle());
    assertNull(attributes.getVariation());
}
Also used : UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 3 with UrlAttributes

use of org.apache.wicket.request.resource.ResourceReference.UrlAttributes in project wicket by apache.

the class ResourceUtilTest method decodeResourceReferenceAttributesWithString.

@Test
public void decodeResourceReferenceAttributesWithString() throws Exception {
    String urlParameter = "en_GB-style-variation";
    UrlAttributes attributes = ResourceUtil.decodeResourceReferenceAttributes(urlParameter);
    assertEquals(Locale.UK, attributes.getLocale());
    assertEquals("style", attributes.getStyle());
    assertEquals("variation", attributes.getVariation());
    attributes = ResourceUtil.decodeResourceReferenceAttributes("it_IT");
    assertEquals(Locale.ITALY, attributes.getLocale());
    assertNull(attributes.getStyle());
    assertNull(attributes.getVariation());
    attributes = ResourceUtil.decodeResourceReferenceAttributes("-style-variation");
    assertNull(attributes.getLocale());
    assertEquals("style", attributes.getStyle());
    assertEquals("variation", attributes.getVariation());
    attributes = ResourceUtil.decodeResourceReferenceAttributes("--variation");
    assertNull(attributes.getLocale());
    assertNull(attributes.getStyle());
    assertEquals("variation", attributes.getVariation());
    attributes = ResourceUtil.decodeResourceReferenceAttributes("-style");
    assertNull(attributes.getLocale());
    assertEquals("style", attributes.getStyle());
    assertNull(attributes.getVariation());
}
Also used : UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) Test(org.junit.Test)

Example 4 with UrlAttributes

use of org.apache.wicket.request.resource.ResourceReference.UrlAttributes in project wicket by apache.

the class ResourceUtilTest method encodeResourceReferenceAttributesWithResource.

@Test
public void encodeResourceReferenceAttributesWithResource() throws Exception {
    ResourceReference resourceReference = Mockito.mock(ResourceReference.class);
    // test with all null attributes
    UrlAttributes attributes = new UrlAttributes(null, null, null);
    String urlString = "www.funny.url";
    Url url = Url.parse(urlString);
    Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
    ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
    assertEquals(urlString, url.toString());
    Mockito.reset(resourceReference);
    // test with locale, style and variation
    attributes = new UrlAttributes(Locale.CANADA_FRENCH, "style", "variation");
    Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
    ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
    assertEquals(urlString + "?fr_CA-style-variation", url.toString());
    Mockito.reset(resourceReference);
    // test with just variation
    attributes = new UrlAttributes(null, null, "variation");
    url = Url.parse(urlString);
    Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
    ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
    assertEquals(urlString + "?--variation", url.toString());
}
Also used : UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ResourceReference(org.apache.wicket.request.resource.ResourceReference) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 5 with UrlAttributes

use of org.apache.wicket.request.resource.ResourceReference.UrlAttributes in project wicket by apache.

the class ResourceUtilTest method encodeResourceReferenceAttributes.

@Test
public void encodeResourceReferenceAttributes() throws Exception {
    UrlAttributes attributes = new UrlAttributes(null, null, null);
    assertNull(ResourceUtil.encodeResourceReferenceAttributes(attributes));
    attributes = new UrlAttributes(Locale.CANADA_FRENCH, "style", "variation");
    assertEquals("fr_CA-style-variation", ResourceUtil.encodeResourceReferenceAttributes(attributes));
    attributes = new UrlAttributes(null, null, "variation");
    assertEquals("--variation", ResourceUtil.encodeResourceReferenceAttributes(attributes));
}
Also used : UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) Test(org.junit.Test)

Aggregations

UrlAttributes (org.apache.wicket.request.resource.ResourceReference.UrlAttributes)7 Test (org.junit.Test)7 Locale (java.util.Locale)3 Url (org.apache.wicket.request.Url)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)1 ResourceReference (org.apache.wicket.request.resource.ResourceReference)1 ByteArrayResponse (org.apache.wicket.response.ByteArrayResponse)1