Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
add tests for DocumentType enum
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Aug 5, 2013
1 parent 7c104e6 commit 0fd56c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
@@ -1,7 +1,8 @@
package org.zanata.common;

import static java.util.Collections.unmodifiableList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public enum DocumentType
Expand All @@ -24,7 +25,7 @@ public enum DocumentType

IDML("idml");

private static List<String> allExtensions = buildExtensionsList();
private static final List<String> allExtensions = buildExtensionsList();

private static List<String> buildExtensionsList()
{
Expand All @@ -33,7 +34,7 @@ private static List<String> buildExtensionsList()
{
allExtensions.add(type.getExtension());
}
return allExtensions;
return unmodifiableList(allExtensions);
}

/**
Expand All @@ -42,10 +43,10 @@ private static List<String> buildExtensionsList()
*/
public static List<String> getAllExtensions()
{
return Collections.unmodifiableList(allExtensions);
return allExtensions;
}


// FIXME damason: rename typeFor to fromString
public static DocumentType typeFor(String extension)
{
for (DocumentType type : DocumentType.values())
Expand All @@ -55,16 +56,18 @@ public static DocumentType typeFor(String extension)
return type;
}
}
// FIXME damason: throw new IllegalArgumentException
return null;
}

private final String extension;

private String extension;
DocumentType(String extension)
{
this.extension = extension;
}

// FIXME damason: rename getExtension to toString
public String getExtension()
{
return extension;
Expand Down
@@ -0,0 +1,73 @@
package org.zanata.common;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zanata.common.DocumentType.*;

import java.util.List;

import org.testng.SkipException;
import org.testng.annotations.Test;

@Test(groups = { "unit-tests" })
public class DocumentTypeTest
{

public void typeForExtantType()
{
assertThat(typeFor("txt"), is(PLAIN_TEXT));
}

@Test(enabled = true)
public void typeForNonExistentTypeCurrentBehaviour()
{
assertThat(typeFor("unknown"), is(nullValue()));
}

@Test(enabled = false, expectedExceptions = IllegalArgumentException.class)
public void typeForNonExistentTypeBetterBehaviour()
{
typeFor("unknown");
}

public void typeForKnownTypeAfterDot()
{
assertThat(typeFor(".txt"), is(nullValue()));
}

public void typeForKnownTypeWithPrefix()
{
assertThat(typeFor("foo.txt"), is(nullValue()));
}

public void getAllExtensionsNotEmpty()
{
List<String> allExtensions = getAllExtensions();
assertThat(allExtensions, not(empty()));
assertThat(allExtensions, containsInAnyOrder("po", "pot", "txt", "dtd", "idml",
// "properties",
"odt", "fodt", "odp", "fodp", "ods", "fods", "odg", "fodg", "odb", "odf"));
}

@Test(expectedExceptions = UnsupportedOperationException.class)
public void getAllExtensionsReadOnlyCannotAdd()
{
List<String> allExtensions = getAllExtensions();
allExtensions.add("newExtension");
}

@Test(expectedExceptions = UnsupportedOperationException.class)
public void getAllExtensionsReadOnlyCannotClear()
{
List<String> allExtensions = getAllExtensions();
allExtensions.clear();
}

@Test(expectedExceptions = UnsupportedOperationException.class)
public void getAllExtensionsReadOnlyCannotRemove()
{
List<String> allExtensions = getAllExtensions();
allExtensions.remove(0);
}

}

0 comments on commit 0fd56c7

Please sign in to comment.