Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-45130] Add tests to demonstrate issue
  • Loading branch information
stephenc committed Jun 26, 2017
1 parent 50278b7 commit d89586e
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 0 deletions.
@@ -0,0 +1,30 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.structs.describable;

import hudson.model.AbstractDescribableImpl;

public abstract class AbstractSharedName extends AbstractDescribableImpl<AbstractSharedName> {
}
@@ -0,0 +1,52 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.structs.describable;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Describable;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;

public class AmbiguousArrayContainer extends AbstractDescribableImpl<AmbiguousArrayContainer> {
private final Describable<?>[] array;

@DataBoundConstructor
public AmbiguousArrayContainer(Describable<?>... array) {
this.array = array.clone();
}

public Describable<?>[] getArray() {
return array.clone();
}

@Extension
public static class DescriptorImpl extends Descriptor<AmbiguousArrayContainer> {
@Override
public String getDisplayName() {
return "ambiguous array container";
}
}
}
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.structs.describable;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Describable;
import hudson.model.Descriptor;
import java.util.ArrayList;
import java.util.List;
import org.kohsuke.stapler.DataBoundConstructor;

public class AmbiguousListContainer extends AbstractDescribableImpl<AmbiguousListContainer> {
public final List<Describable<?>> list;

@DataBoundConstructor
public AmbiguousListContainer(List<Describable<?>> list) {
this.list = new ArrayList<Describable<?>>(list);
}

@Extension
public static class DescriptorImpl extends Descriptor<AmbiguousListContainer> {
@Override
public String getDisplayName() {
return "ambiguous list container";
}
}
}
Expand Up @@ -27,6 +27,7 @@
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.BooleanParameterValue;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
Expand All @@ -41,6 +42,7 @@
import org.jenkinsci.plugins.structs.FishingNet;
import org.jenkinsci.plugins.structs.Internet;
import org.jenkinsci.plugins.structs.Tech;
import org.jenkinsci.plugins.structs.describable.first.SharedName;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -59,6 +61,7 @@
import java.util.logging.Level;

import static org.apache.commons.lang3.SerializationUtils.roundtrip;
import static org.hamcrest.Matchers.instanceOf;
import static org.jenkinsci.plugins.structs.describable.DescribableModel.*;
import static org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable.ANONYMOUS_KEY;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -764,6 +767,91 @@ public void ambiguousSimpleName() throws Exception {
assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class"));
}

@Issue("JENKINS-45130") //
@Test
public void ambiguousTopLevelSimpleName() throws Exception {
AmbiguousContainer container = new AmbiguousContainer(new SharedName("first"),
new UnambiguousClassName("second"));

UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container);

Object o = ud.toMap().get("ambiguous");
assertTrue(o instanceof Map);
Map<String,Object> m = (Map<String,Object>)o;

// Make sure the ambiguous class is fully qualified.
assertEquals(SharedName.class.getName(), m.get("$class"));

Object o2 = ud.toMap().get("unambiguous");
assertTrue(o2 instanceof Map);
Map<String,Object> m2 = (Map<String,Object>)o2;

// Make sure the unambiguous class just uses the simple name.
assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class"));
}

@Issue("JENKINS-45130")
@Test
public void ambiguousTopLevelSimpleNameInList() throws Exception {
SharedName first = new SharedName("first");
first.setTwo("something");
AmbiguousListContainer container = new AmbiguousListContainer(Arrays.<Describable<?>>asList(first,
new UnambiguousClassName("second")));

UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container);

Object o = ud.toMap().get("list");
assertTrue(o instanceof List);
List<Map<String, Object>> l = (List<Map<String, Object>>) o;

Map<String,Object> m = l.get(0);

// Make sure the ambiguous class is fully qualified.
assertEquals(SharedName.class.getName(), m.get("$class"));

Map<String,Object> m2 = l.get(1);

// Make sure the unambiguous class just uses the simple name.
assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class"));

System.out.println(ud.toString());

AmbiguousListContainer roundtrip = (AmbiguousListContainer) ud.instantiate();
assertThat(roundtrip.list.get(0), instanceOf(SharedName.class));
assertThat(roundtrip.list.get(1), instanceOf(UnambiguousClassName.class));
}

@Issue("JENKINS-45130")
@Test
public void ambiguousTopLevelSimpleNameInArray() throws Exception {
SharedName first = new SharedName("first");
first.setTwo("something");
AmbiguousArrayContainer container = new AmbiguousArrayContainer(first,
new UnambiguousClassName("second"));

UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container);

Object o = ud.toMap().get("array");
assertTrue(o instanceof List);
List<Map<String, Object>> l = (List<Map<String, Object>>) o;

Map<String,Object> m = l.get(0);

// Make sure the ambiguous class is fully qualified.
assertEquals(SharedName.class.getName(), m.get("$class"));

Map<String,Object> m2 = l.get(1);

// Make sure the unambiguous class just uses the simple name.
assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class"));

System.out.println(ud.toString());

AmbiguousArrayContainer roundtrip = (AmbiguousArrayContainer) ud.instantiate();
assertThat(roundtrip.getArray()[0], instanceOf(SharedName.class));
assertThat(roundtrip.getArray()[1], instanceOf(UnambiguousClassName.class));
}

private static Map<String,Object> map(Object... keysAndValues) {
if (keysAndValues.length % 2 != 0) {
throw new IllegalArgumentException();
Expand Down
@@ -0,0 +1,73 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.structs.describable.first;

import hudson.Extension;
import hudson.model.Descriptor;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.describable.AbstractSharedName;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class SharedName extends AbstractSharedName {
private final String one;
private String two;

@DataBoundConstructor
public SharedName(String one) {
this.one = one;
}

public String getOne() {
return one;
}

public String getTwo() {
return two;
}

@DataBoundSetter
public void setTwo(String two) {
this.two = two;
}

public String getLegacyTwo() {
return two;
}

@Deprecated
@DataBoundSetter
public void setLegacyTwo(String two) {
this.two = two;
}

@Extension
public static class DescriptorImpl extends Descriptor<AbstractSharedName> {
@Override
public String getDisplayName() {
return "first.SharedName";
}
}
}
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.structs.describable.second;

import hudson.Extension;
import hudson.model.Descriptor;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.describable.AbstractSharedName;
import org.kohsuke.stapler.DataBoundConstructor;

public class SharedName extends AbstractSharedName {
public final String two;

@DataBoundConstructor
public SharedName(String two) {
this.two = two;
}

@Extension
public static class DescriptorImpl extends Descriptor<AbstractSharedName> {
@Override
public String getDisplayName() {
return "second.SharedName";
}
}
}

0 comments on commit d89586e

Please sign in to comment.