package org.hybridlabs.source.formatter;
/**
* Copyright 2008 hybrid labs
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.eclipse.xpand2.output.FileHandle;
import org.eclipse.xpand2.output.FileHandleImpl;
import org.eclipse.xpand2.output.PostProcessor;
import org.hybridlabs.file.FileHelper;
import org.hybridlabs.source.beautifier.CharacterSequence;
public class JavaImportBeautifierTest extends TestCase {
private JavaImportBeautifier beautifier = new JavaImportBeautifier();
@Override
protected void setUp() throws Exception {
super.setUp();
beautifier.setOrganizeImports(true);
beautifier.setStrict(true);
}
public void testSample0() throws Exception {
PostProcessor postProcessor = new JavaImportBeautifier();
Properties p = System.getProperties();
File file = new File("src/test/resources/Sample0.java");
FileHandle fh = new FileHandleImpl(null, file);
fh.setBuffer(new CharacterSequence(loadTestFile("Sample01.java_input")));
postProcessor.beforeWriteAndClose(fh);
assertNotNull(fh.getBuffer());
}
public void testSample1() throws Exception {
testIndexedSample("01", false, false);
}
public void testSample1Formatted() throws Exception {
CharacterSequence sequence = new CharacterSequence(loadTestFile("Sample01.java_input"));
beautifier.beautify(sequence);
CharacterSequence expectedOutput = new CharacterSequence(loadTestFile("Sample01.java_formatted_output"));
compare(expectedOutput, sequence);
}
public void testSample2() throws Exception {
testIndexedSample("02", false, false);
}
public void testSample3() throws Exception {
testIndexedSample("03", false, false);
}
public void testSample03Formatted() throws Exception {
beautifier.setFormat(true);
CharacterSequence sequence = new CharacterSequence(loadTestFile("Sample03.java_input"));
beautifier.beautify(sequence);
CharacterSequence expectedOutput = new CharacterSequence(loadTestFile("Sample03.java_output_formatted"));
compare(expectedOutput, sequence);
}
public void testSample4() throws Exception {
testIndexedSample("04", false);
}
public void testSample5() throws Exception {
testIndexedSample("05", false);
}
public void testSample6() throws Exception {
testIndexedSample("06", false);
}
public void testSample7() throws Exception {
testIndexedSample("07", true);
}
public void testSample8() throws Exception {
testIndexedSample("08", true);
}
public void testSample9() throws Exception {
testIndexedSample("09", true);
}
public void testSample10() throws Exception {
testIndexedSample("10", true);
}
public void testSample11() throws Exception {
testIndexedSample("11", true);
}
public void testSample12() throws Exception {
testIndexedSample("12", true);
}
public void testSample13() throws Exception {
testIndexedSample("13", true);
}
public void testSample14() throws Exception {
testIndexedSample("14", false);
}
public void testSample15() throws Exception {
testIndexedSample("15", false, false);
}
public void testSample16() throws Exception {
testIndexedSample("16", false, false);
}
public void testSample17() throws Exception {
beautifier.setStrict(false);
testIndexedSample("17", false, false);
}
public void testSample18() throws Exception {
beautifier.setStrict(false);
testIndexedSample("18", false, false);
}
public void testSample20() throws Exception {
testIndexedSample("20", false);
}
public void testSample21() throws Exception {
testIndexedSample("21", false);
}
public void testSample22() throws Exception {
testIndexedSample("22", false);
}
public void testSample23() throws Exception {
testIndexedSample("23", false);
}
public void testSample24() throws Exception {
testIndexedSample("24", false);
}
public void testSample25() throws Exception {
testIndexedSample("25");
}
public void testSample26() throws Exception {
testIndexedSample("26");
}
public void testSample27() throws Exception {
testIndexedSample("27");
}
public void testSample28() throws Exception {
testIndexedSample("28");
}
public void testSample29() throws Exception {
testIndexedSample("29");
}
public void testSample30() throws Exception {
testIndexedSample("30");
}
public void testSample31() throws Exception {
testIndexedSample("31");
}
public void testSample32() throws Exception {
testIndexedSample("32");
}
public void testSample33() throws Exception {
testIndexedSample("33");
}
public void testSample34() throws Exception {
testIndexedSample("34");
}
public void testSample35() throws Exception {
testIndexedSample("35");
}
/*
FIXME
public void testSample36() throws Exception {
testIndexedSample("36");
}
*/
private void testIndexedSample(String sampleIndex) throws Exception {
testIndexedSample(sampleIndex, true, true);
}
private void testIndexedSample(String sampleIndex, boolean useConventionFile) throws Exception {
testIndexedSample(sampleIndex, useConventionFile, true);
}
private void testIndexedSample(String sampleIndex, boolean useConventionFile, boolean format) throws Exception {
beautifier.setFormat(format);
if (useConventionFile) {
beautifier.setConventionFilePath("src/test/resources/test-convention.xml");
}
CharacterSequence sequence = new CharacterSequence(loadTestFile("Sample" + sampleIndex + ".java_input"));
beautifier.beautify(sequence);
CharacterSequence expectedOutput = new CharacterSequence(loadTestFile("Sample" + sampleIndex + ".java_output"));
compare(expectedOutput, sequence);
}
private StringBuffer loadTestFile(String name) throws Exception {
InputStream is = ClassLoader.getSystemResourceAsStream(name);
if (is == null) {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}
Assert.assertNotNull("Could not open test file "+name, is);
return FileHelper.loadStringBuffer(new InputStreamReader(is));
}
private void compare(CharacterSequence expected, CharacterSequence actual) {
assertEquals(normalized(expected), normalized(actual));
}
private String normalized (CharSequence seq) {
return seq.toString().trim().replaceAll("\r\n", System.getProperty("line.separator"));
}
}