Add packaging abstraction

Closes gh-815

Co-authored-by: Stephane Nicoll <snicoll@pivotal.io>
This commit is contained in:
Andy Wilkinson 2019-02-07 15:33:40 +01:00 committed by Stephane Nicoll
parent 3f585337da
commit 2cb1f3e647
11 changed files with 354 additions and 0 deletions

View File

@ -0,0 +1,41 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging;
import java.util.Objects;
import org.springframework.core.io.support.SpringFactoriesLoader;
/**
* Application packaging, such as a jar file or a war file.
*
* @author Andy Wilkinson
*/
public interface Packaging {
String id();
static Packaging forId(String id) {
return SpringFactoriesLoader
.loadFactories(PackagingFactory.class, Packaging.class.getClassLoader())
.stream().map((factory) -> factory.createPackaging(id))
.filter(Objects::nonNull).findFirst()
.orElseThrow(() -> new IllegalStateException(
"Unrecognized packaging id '" + id + "'"));
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging;
/**
* A factory for creating a {@link Packaging}.
*
* @author Andy Wilkinson
*/
public interface PackagingFactory {
/**
* Creates and returns a {@link Packaging} for the given id. If the factory does not
* recognise the given {@code id}, {@code null} should be returned.
* @param id the id of the packaging
* @return the packaging or {@code null}
*/
Packaging createPackaging(String id);
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging.jar;
import io.spring.initializr.generator.packaging.Packaging;
/**
* Jar {@link Packaging}.
*
* @author Andy Wilkinson
*/
public final class JarPackaging implements Packaging {
/**
* Jar {@link Packaging} identifier.
*/
public static final String ID = "jar";
@Override
public String id() {
return ID;
}
@Override
public String toString() {
return id();
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging.jar;
import io.spring.initializr.generator.packaging.Packaging;
import io.spring.initializr.generator.packaging.PackagingFactory;
/**
* {@link PackagingFactory Factory} for {@link JarPackaging}.
*
* @author Andy Wilkinson
*/
class JarPackagingFactory implements PackagingFactory {
@Override
public Packaging createPackaging(String id) {
if (JarPackaging.ID.equals(id)) {
return new JarPackaging();
}
return null;
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
/**
* Java archive packaging.
*/
package io.spring.initializr.generator.packaging.jar;

View File

@ -0,0 +1,20 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
/**
* Packaging abstraction.
*/
package io.spring.initializr.generator.packaging;

View File

@ -0,0 +1,43 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging.war;
import io.spring.initializr.generator.packaging.Packaging;
/**
* War {@link Packaging}.
*
* @author Andy Wilkinson
*/
public final class WarPackaging implements Packaging {
/**
* War {@link Packaging} identifier.
*/
public static final String ID = "war";
@Override
public String id() {
return ID;
}
@Override
public String toString() {
return id();
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging.war;
import io.spring.initializr.generator.packaging.Packaging;
import io.spring.initializr.generator.packaging.PackagingFactory;
/**
* {@link PackagingFactory Factory} for {@link WarPackaging}.
*
* @author Andy Wilkinson
*/
class WarPackagingFactory implements PackagingFactory {
@Override
public Packaging createPackaging(String id) {
if (WarPackaging.ID.equals(id)) {
return new WarPackaging();
}
return null;
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
/**
* Web Application archive packaging.
*/
package io.spring.initializr.generator.packaging.war;

View File

@ -6,3 +6,7 @@ io.spring.initializr.generator.language.LanguageFactory=\
io.spring.initializr.generator.language.groovy.GroovyLanguageFactory,\
io.spring.initializr.generator.language.java.JavaLanguageFactory,\
io.spring.initializr.generator.language.kotlin.KotlinLanguageFactory
io.spring.initializr.generator.packaging.PackagingFactory=\
io.spring.initializr.generator.packaging.jar.JarPackagingFactory,\
io.spring.initializr.generator.packaging.war.WarPackagingFactory

View File

@ -0,0 +1,55 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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.
*/
package io.spring.initializr.generator.packaging;
import io.spring.initializr.generator.packaging.jar.JarPackaging;
import io.spring.initializr.generator.packaging.war.WarPackaging;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link Packaging}.
*
* @author Stephane Nicoll
*/
class PackagingTests {
@Test
void jarPackaging() {
Packaging jar = Packaging.forId("jar");
assertThat(jar).isInstanceOf(JarPackaging.class);
assertThat(jar.id()).isEqualTo("jar");
assertThat(jar.toString()).isEqualTo("jar");
}
@Test
void warPackaging() {
Packaging war = Packaging.forId("war");
assertThat(war).isInstanceOf(WarPackaging.class);
assertThat(war.id()).isEqualTo("war");
assertThat(war.toString()).isEqualTo("war");
}
@Test
void unknownPackaging() {
assertThatIllegalStateException().isThrownBy(() -> Packaging.forId("unknown"))
.withMessageContaining("Unrecognized packaging id 'unknown'");
}
}