Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Adding test cases for StringConverter. Closes x-stream#187.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshuai007 authored and joehni committed Mar 15, 2020
1 parent b06b964 commit fd15041
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 12. Mar 2020 by Zezeng Wang
*/

package com.thoughtworks.xstream.converters.basic;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import junit.framework.TestCase;


/**
* Tests {@link StringConverter}.
*/
public class StringConverterTest extends TestCase {

/**
* Tests use of own map implementation for cache.
*/
public void testOwnMapImplementationForCache() {
// Using the parameter map constructor
final Map<String, String> map = new ConcurrentHashMap<>();
final StringConverter converter = new StringConverter(map);
assertSame(converter.fromString("JUnit"), converter.fromString(new String("JUnit"))); // cached value
assertEquals(1, map.size());
}

/**
* Tests cache limitation for string length.
*/
public void testCacheLimitationBasedOnStringLength() {
// Using the int constructor
final StringConverter converter = new StringConverter(4);
assertSame(converter.fromString("Test"), converter.fromString(new String("Test"))); // cached value
assertNotSame(converter.fromString("JUnit"), converter.fromString(new String("JUnit"))); // non-cached value
}

/**
* Tests no cache.
*/
public void testNoCache() {
final StringConverter converter = new StringConverter(null);
assertNotSame(converter.fromString("JUnit"), converter.fromString(new String("JUnit"))); // non-cached value
}

/**
* Tests own map implementation and string length limit for cache.
*/
public void testOwnMapImplementationAndStringLegnthLimitForCache() {
// Using the map and int constructor
final Map<String, String> map = new ConcurrentHashMap<>();
final StringConverter converter = new StringConverter(map, 4);
assertSame(converter.fromString("Test"), converter.fromString(new String("Test"))); // cached value
assertNotSame(converter.fromString("JUnit"), converter.fromString(new String("JUnit"))); // non-cached value
assertEquals(1, map.size());
}
}

0 comments on commit fd15041

Please sign in to comment.