Java tutorial
/******************************************************************************* * Copyright (C) 2011 Atlas of Living Australia * All Rights Reserved. * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. ******************************************************************************/ package au.org.ala.delta; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import au.org.ala.delta.editor.slotfile.SlotFile; import au.org.ala.delta.io.BinFileMode; import au.org.ala.delta.model.CharacterType; import au.org.ala.delta.model.MutableDeltaDataSet; import au.org.ala.delta.model.Item; import au.org.ala.delta.model.MultiStateCharacter; import au.org.ala.delta.rtf.RTFUtils; import junit.framework.TestCase; public abstract class DeltaTestCase extends TestCase { private List<File> _tempFiles = new ArrayList<File>(); /** * Makes a copy of the supplied DELTA file and returns a new SlotFile created from the copied file. * @param fileName the ClassLoader relative name of the DELTA file. * @return a new SlotFile. * @throws IOException if the file cannot be found. */ protected SlotFile copyAndOpen(String fileName) throws IOException { URL deltaFileUrl = getClass().getResource(fileName); File tempDeltaFile = File.createTempFile("test", ".dlt"); _tempFiles.add(tempDeltaFile); FileUtils.copyURLToFile(deltaFileUrl, tempDeltaFile); SlotFile slotFile = new SlotFile(tempDeltaFile.getAbsolutePath(), BinFileMode.FM_EXISTING); return slotFile; } /** * Copies the specified resource to a temp file, and returns that File * @param fileName the ClassLoader relative name of the file to open. * @return a new File. * @throws IOException if the file cannot be found. */ protected File copyURLToFile(String filename) throws IOException { URL deltaFileUrl = getClass().getResource(filename); File tempFile = File.createTempFile("test", ".dlt"); _tempFiles.add(tempFile); FileUtils.copyURLToFile(deltaFileUrl, tempFile); return tempFile; } protected File newTempFile() throws IOException { File tempFile = File.createTempFile("test", ".dlt"); _tempFiles.add(tempFile); return tempFile; } @Override protected void tearDown() throws Exception { super.tearDown(); for (File f : _tempFiles) { System.out.println("Deleting temp file: " + f.getAbsolutePath()); f.delete(); } } /** * Reads all of the Characters and Items from a data set. Makes a good double check * for tests that appear to work yet have the side effect of corrupting the slot file * in some way. * @param ds the data set to read. */ protected void deepRead(MutableDeltaDataSet ds) { // Chars... System.out.println("Processing " + ds.getNumberOfCharacters() + " characters"); for (int i = 1; i <= ds.getNumberOfCharacters(); ++i) { System.out.println("Processing character: " + i); au.org.ala.delta.model.Character ch = ds.getCharacter(i); ch.getDescription(); ch.getNotes(); ch.isMandatory(); switch (ch.getCharacterType()) { case UnorderedMultiState: case OrderedMultiState: MultiStateCharacter msc = (MultiStateCharacter) ch; msc.getStates(); msc.isExclusive(); break; default: } } System.out.println("Processing " + ds.getMaximumNumberOfItems() + " Items"); for (int i = 1; i <= ds.getMaximumNumberOfItems(); ++i) { Item item = ds.getItem(i); for (int j = 1; j <= ds.getNumberOfCharacters(); ++j) { au.org.ala.delta.model.Character ch = ds.getCharacter(j); au.org.ala.delta.model.Attribute a = item.getAttribute(ch); if (a != null) { String strValue = a.getValueAsString(); if (ch.getCharacterType() == CharacterType.Text) { RTFUtils.stripFormatting(strValue); } } } } } }