Skip to content

Commit

Permalink
[FLINK-924] Add JarFileCreator tests for anonymous classes and Java 8…
Browse files Browse the repository at this point in the history
… lambdas

This closes #35
  • Loading branch information
qmlmoon authored and aljoscha committed Apr 28, 2015
1 parent c527a2b commit 6460743
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 80 deletions.
@@ -0,0 +1,111 @@
/**
* 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.
*/

package org.apache.flink.runtime.util;


import org.apache.flink.runtime.util.jartestprogram.FilterLambda1;
import org.apache.flink.runtime.util.jartestprogram.FilterLambda2;
import org.apache.flink.runtime.util.jartestprogram.FilterLambda3;
import org.apache.flink.runtime.util.jartestprogram.FilterLambda4;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

public class JarFileCreatorLambdaTest {
@Test
public void TestFilterFunctionOnLambda1() throws Exception {
File out = new File(System.getProperty("java.io.tmpdir"), "jarcreatortest.jar");
JarFileCreator jfc = new JarFileCreator(out);
jfc.addClass(FilterLambda1.class)
.createJarFile();

Set<String> ans = new HashSet<String>();
ans.add("org/apache/flink/runtime/util/jartestprogram/FilterLambda1.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/WordFilter.class");

Assert.assertTrue("Jar file for java 8 lambda is not correct", validate(ans, out));
out.delete();
}

@Test
public void TestFilterFunctionOnLambda2() throws Exception{
File out = new File(System.getProperty("java.io.tmpdir"), "jarcreatortest.jar");
JarFileCreator jfc = new JarFileCreator(out);
jfc.addClass(FilterLambda2.class)
.createJarFile();

Set<String> ans = new HashSet<String>();
ans.add("org/apache/flink/runtime/util/jartestprogram/FilterLambda2.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/WordFilter.class");

Assert.assertTrue("Jar file for java 8 lambda is not correct", validate(ans, out));
out.delete();
}

@Test
public void TestFilterFunctionOnLambda3() throws Exception {
File out = new File(System.getProperty("java.io.tmpdir"), "jarcreatortest.jar");
JarFileCreator jfc = new JarFileCreator(out);
jfc.addClass(FilterLambda3.class)
.createJarFile();

Set<String> ans = new HashSet<String>();
ans.add("org/apache/flink/runtime/util/jartestprogram/FilterLambda3.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/WordFilter.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/UtilFunction.class");

Assert.assertTrue("Jar file for java 8 lambda is not correct", validate(ans, out));
out.delete();
}

@Test
public void TestFilterFunctionOnLambda4() throws Exception {
File out = new File(System.getProperty("java.io.tmpdir"), "jarcreatortest.jar");
JarFileCreator jfc = new JarFileCreator(out);
jfc.addClass(FilterLambda4.class)
.createJarFile();

Set<String> ans = new HashSet<String>();
ans.add("org/apache/flink/runtime/util/jartestprogram/FilterLambda4.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/WordFilter.class");
ans.add("org/apache/flink/runtime/util/jartestprogram/UtilFunctionWrapper$UtilFunction.class");

Assert.assertTrue("Jar file for java 8 lambda is not correct", validate(ans, out));
out.delete();
}

public boolean validate(Set<String> expected, File out) throws Exception {

JarInputStream jis = new JarInputStream(new FileInputStream(out));
ZipEntry ze;
int count = expected.size();
while ((ze = jis.getNextEntry()) != null) {
count--;
expected.remove(ze.getName());
}
return count == 0 && expected.size() == 0;
}
}
@@ -0,0 +1,39 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;


import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;

public class FilterLambda1 {

public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> input = env.fromElements("Please filter", "the words", "but not this");

FilterFunction<String> filter = (v) -> WordFilter.filter(v);

DataSet<String> output = input.filter(filter);
output.print();

env.execute();
}
}
@@ -0,0 +1,35 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;

public class FilterLambda2 {

public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> input = env.fromElements("Please filter", "the words", "but not this");

DataSet<String> output = input.filter((v) -> WordFilter.filter(v));
output.print();

env.execute();
}
}
@@ -0,0 +1,36 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;

public class FilterLambda3 {

public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> input = env.fromElements("Please filter", "the words", "but not this");

DataSet<String> output = input.filter(UtilFunction.getWordFilter());
output.print();

env.execute();
}

}
@@ -0,0 +1,35 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;

public class FilterLambda4 {

public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> input = env.fromElements("Please filter", "the words", "but not this");

DataSet<String> output = input.filter(UtilFunctionWrapper.UtilFunction.getWordFilter());
output.print();

env.execute();
}
}
@@ -0,0 +1,27 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

import org.apache.flink.api.common.functions.FilterFunction;

public class UtilFunction {
public static FilterFunction<String> getWordFilter() {
return (v) -> WordFilter.filter(v);
}
}
@@ -0,0 +1,29 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

import org.apache.flink.api.common.functions.FilterFunction;

public class UtilFunctionWrapper {
public static class UtilFunction {
public static FilterFunction<String> getWordFilter() {
return (v) -> WordFilter.filter(v);
}
}
}
@@ -0,0 +1,25 @@
/**
* 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.
*/

package org.apache.flink.runtime.util.jartestprogram;

public class WordFilter {
public static boolean filter(String value) {
return !value.contains("not");
}
}
2 changes: 1 addition & 1 deletion flink-runtime/pom.xml
Expand Up @@ -119,7 +119,7 @@ under the License.
<dependency> <dependency>
<groupId>org.ow2.asm</groupId> <groupId>org.ow2.asm</groupId>
<artifactId>asm-all</artifactId> <artifactId>asm-all</artifactId>
<version>5.0.3</version> <version>${asm.version}</version>
</dependency> </dependency>


<dependency> <dependency>
Expand Down

0 comments on commit 6460743

Please sign in to comment.