1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.thrift.maven;
20 
21 import org.codehaus.plexus.util.FileUtils;
22 import org.codehaus.plexus.util.cli.CommandLineException;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 
27 import java.io.File;
28 
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33 
34 public class TestThrift {
35 
36     private File testRootDir;
37     private File idlDir;
38     private File genJavaDir;
39     private Thrift.Builder builder;
40 
41     @Before
setup()42     public void setup() throws Exception {
43         final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
44         testRootDir = new File(tmpDir, "thrift-test");
45 
46         if (testRootDir.exists()) {
47             FileUtils.cleanDirectory(testRootDir);
48         } else {
49             assertTrue("Failed to create output directory for test: " + testRootDir.getPath(), testRootDir.mkdir());
50         }
51 
52         File testResourceDir = new File("src/test/resources");
53         assertTrue("Unable to find test resources", testRootDir.exists());
54 
55         String thriftExecutable = System.getProperty("thriftExecutable", "thrift");
56         if (!(new File(thriftExecutable).exists())) {
57             thriftExecutable = "thrift";
58         }
59         System.out.println("Thrift compiler: " + thriftExecutable);
60 
61         idlDir = new File(testResourceDir, "idl");
62         genJavaDir = new File(testRootDir, Thrift.GENERATED_JAVA);
63         builder = new Thrift.Builder(thriftExecutable, testRootDir);
64         builder
65             .setGenerator("java")
66             .addThriftPathElement(idlDir);
67     }
68 
69     @Test
testThriftCompile()70     public void testThriftCompile() throws Exception {
71         executeThriftCompile();
72     }
73 
74     @Test
testThriftCompileWithGeneratorOption()75     public void testThriftCompileWithGeneratorOption() throws Exception {
76         builder.setGenerator("java:private_members");
77         executeThriftCompile();
78     }
79 
executeThriftCompile()80     private void executeThriftCompile() throws CommandLineException {
81         final File thriftFile = new File(idlDir, "shared.thrift");
82 
83         builder.addThriftFile(thriftFile);
84 
85         final Thrift thrift = builder.build();
86 
87         assertTrue("File not found: shared.thrift", thriftFile.exists());
88         assertFalse("gen-java directory should not exist", genJavaDir.exists());
89 
90         // execute the compile
91         final int result = thrift.compile();
92         assertEquals(0, result);
93 
94         assertFalse("gen-java directory was not removed", genJavaDir.exists());
95         assertTrue("generated java code doesn't exist",
96             new File(testRootDir, "shared/SharedService.java").exists());
97     }
98 
99     @Test
testThriftMultipleFileCompile()100     public void testThriftMultipleFileCompile() throws Exception {
101         final File sharedThrift = new File(idlDir, "shared.thrift");
102         final File tutorialThrift = new File(idlDir, "tutorial.thrift");
103 
104         builder.addThriftFile(sharedThrift);
105         builder.addThriftFile(tutorialThrift);
106 
107         final Thrift thrift = builder.build();
108 
109         assertTrue("File not found: shared.thrift", sharedThrift.exists());
110         assertFalse("gen-java directory should not exist", genJavaDir.exists());
111 
112         // execute the compile
113         final int result = thrift.compile();
114         assertEquals(0, result);
115 
116         assertFalse("gen-java directory was not removed", genJavaDir.exists());
117         assertTrue("generated java code doesn't exist",
118             new File(testRootDir, "shared/SharedService.java").exists());
119         assertTrue("generated java code doesn't exist",
120             new File(testRootDir, "tutorial/InvalidOperation.java").exists());
121     }
122 
123     @Test
testBadCompile()124     public void testBadCompile() throws Exception {
125         final File thriftFile = new File(testRootDir, "missing.thrift");
126         builder.addThriftPathElement(testRootDir);
127 
128         // Hacking around checks in addThrift file.
129         assertTrue(thriftFile.createNewFile());
130         builder.addThriftFile(thriftFile);
131         assertTrue(thriftFile.delete());
132 
133         final Thrift thrift = builder.build();
134 
135         assertTrue(!thriftFile.exists());
136         assertFalse("gen-java directory should not exist", genJavaDir.exists());
137 
138         // execute the compile
139         final int result = thrift.compile();
140         assertEquals(1, result);
141     }
142 
143     @Test
testFileInPathPreCondition()144     public void testFileInPathPreCondition() throws Exception {
145         final File thriftFile = new File(testRootDir, "missing.thrift");
146 
147         // Hacking around checks in addThrift file.
148         assertTrue(thriftFile.createNewFile());
149         try {
150             builder.addThriftFile(thriftFile);
151             fail("Expected IllegalStateException");
152         } catch (IllegalStateException e) {
153         }
154     }
155 
156     @After
cleanup()157     public void cleanup() throws Exception {
158         if (testRootDir.exists()) {
159             FileUtils.cleanDirectory(testRootDir);
160             assertTrue("Failed to delete output directory for test: " + testRootDir.getPath(), testRootDir.delete());
161         }
162     }
163 }
164