Commit 143834a7 authored by David Trott's avatar David Trott

Added code to remove the "gen-java" directory.

This makes the output consistent with other maven code generators.
Additionally added a unit test.
parent 37947ff2
......@@ -29,6 +29,9 @@ import java.util.Set;
* @author gak@google.com (Gregory Kick)
*/
final class Thrift {
final static String GENERATED_JAVA = "gen-java";
private final String executable;
private final ImmutableSet<File> thriftPathElements;
private final ImmutableSet<File> thriftFiles;
......@@ -64,7 +67,13 @@ final class Thrift {
cl.addArguments(buildThriftCommand().toArray(new String[]{}));
StreamConsumer output = new DefaultConsumer();
StreamConsumer error = new DefaultConsumer();
return CommandLineUtils.executeCommandLine(cl, null, output, error);
final int result = CommandLineUtils.executeCommandLine(cl, null, output, error);
if (result == 0) {
moveGeneratedFiles();
}
return result;
}
/**
......@@ -91,6 +100,27 @@ final class Thrift {
return ImmutableList.copyOf(command);
}
private void moveGeneratedFiles() {
File genDir = new File(javaOutputDirectory, GENERATED_JAVA);
final File[] generatedFiles = genDir.listFiles();
for (File generatedFile : generatedFiles) {
final String filename = generatedFile.getName();
final File targetLocation = new File(javaOutputDirectory, filename);
if (targetLocation.exists()) {
if (!targetLocation.delete()) {
throw new RuntimeException("File Overwrite Failed: " + targetLocation.getPath());
}
}
if (!generatedFile.renameTo(targetLocation)) {
throw new RuntimeException("Rename Failed: " + targetLocation.getPath());
}
}
if (!genDir.delete()) {
throw new RuntimeException("Failed to delete directory: " + genDir.getPath());
}
}
/**
* This class builds {@link Thrift} instances.
*
......
package org.apache.thrift.maven;
import org.codehaus.plexus.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.File;
public class TestThrift {
private File testRootDir;
private File testResourceDir;
@Before
public void setup() throws Exception {
final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
testRootDir = new File(tmpDir, "thrift-test");
if (testRootDir.exists()) {
FileUtils.cleanDirectory(testRootDir);
} else {
assertTrue("Failed to create output directory for test: "+ testRootDir.getPath(), testRootDir.mkdir());
}
testResourceDir = new File("src/test/resources");
assertTrue("Unable to find test resources", testRootDir.exists());
}
@Test
public void testThriftCompile() throws Exception {
final File idlDir = new File(testResourceDir, "idl");
final File genJavaDir = new File(testRootDir, Thrift.GENERATED_JAVA);
final File thriftFile = new File(idlDir, "shared.thrift");
Thrift.Builder builder = new Thrift.Builder("thrift", testRootDir);
builder.addThriftPathElement(idlDir);
builder.addThriftFile(thriftFile);
final Thrift thrift = builder.build();
assertTrue("File not found: shared.thrift", thriftFile.exists());
assertFalse("gen-java directory should not exist", genJavaDir.exists());
// execute the compile
final int result = thrift.compile();
assertEquals(0, result);
assertFalse("gen-java directory was not removed", genJavaDir.exists());
assertTrue("generated java code doesn't exist",
new File(testRootDir, "shared/SharedService.java").exists());
}
@After
public void cleanup() throws Exception {
if (testRootDir.exists()) {
FileUtils.cleanDirectory(testRootDir);
assertTrue("Failed to delete output directory for test: "+ testRootDir.getPath(), testRootDir.delete());
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/**
* This Thrift file can be included by other Thrift files that want to share
* these definitions.
*/
namespace cpp shared
namespace java shared
namespace perl shared
struct SharedStruct {
1: i32 key
2: string value
}
service SharedService {
SharedStruct getStruct(1: i32 key)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment