/*
 * 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.jmeter.testelement;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.config.LoginConfig;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.sampler.DebugSampler;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.NullProperty;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.junit.Ignore;
import org.junit.Test;

public class PackageTest {

    // Test needs to run in this package in order to give access to AbstractTestElement.addProperty()
    @Test
    @Ignore // should be OK now the bug is fixed, but it fails
    public void testBug50799() throws Exception {
        HeaderManager headerManager = new HeaderManager();
        headerManager.add(new Header("1stLevelTestHeader", "testValue1"));
        HeaderManager headerManager2 = new HeaderManager();
        headerManager2.add(new Header("2ndLevelTestHeader", "testValue2"));

        DebugSampler debugSampler = new DebugSampler();
        debugSampler.addProperty(new StringProperty("name", "DebugSampler_50799"));
        debugSampler.setRunningVersion(true);
        assertTrue(debugSampler.getProperty("HeaderManager.headers") instanceof NullProperty);
        debugSampler.addTestElement(headerManager);
        assertFalse(debugSampler.getProperty("HeaderManager.headers") instanceof NullProperty);
        assertEquals(debugSampler.getProperty("HeaderManager.headers").getStringValue() ,"[1stLevelTestHeader\ttestValue1]");

        debugSampler.addTestElement(headerManager2);
        assertEquals(debugSampler.getProperty("HeaderManager.headers").getStringValue() ,"[1stLevelTestHeader\ttestValue1, 2ndLevelTestHeader\ttestValue2]");
        assertEquals(2, ((CollectionProperty)debugSampler.getProperty("HeaderManager.headers")).size());
        
        headerManager.recoverRunningVersion();
        headerManager2.recoverRunningVersion();
        debugSampler.recoverRunningVersion();

        assertEquals(1, headerManager.size()); // Test fails here - size is 0 - but why?
        assertEquals(1, headerManager2.size());
        assertEquals(0, ((CollectionProperty)debugSampler.getProperty("HeaderManager.headers")).size());
        assertEquals(new Header("1stLevelTestHeader", "testValue1"), headerManager.get(0)); // This also fails because the size is zero
        assertEquals(new Header("2ndLevelTestHeader", "testValue2"), headerManager2.get(0));
    }

    @Test
    public void testRecovery() throws Exception {
        ConfigTestElement config = new ConfigTestElement();
        config.addProperty(new StringProperty("name", "config"));
        config.setRunningVersion(true);
        LoginConfig loginConfig = new LoginConfig();
        loginConfig.setUsername("user1");
        loginConfig.setPassword("pass1");
        assertTrue(config.getProperty("login") instanceof NullProperty);
        // This test should work whether or not all Nulls are equal
        assertEquals(new NullProperty("login"), config.getProperty("login"));
        config.addProperty(new TestElementProperty("login", loginConfig));
        assertEquals(loginConfig.toString(), config.getPropertyAsString("login"));
        config.recoverRunningVersion();
        assertTrue(config.getProperty("login") instanceof NullProperty);
        assertEquals(new NullProperty("login"), config.getProperty("login"));
    }

    @Test
    public void testArguments() throws Exception {
        Arguments args = new Arguments();
        args.addArgument("arg1", "val1", "=");
        TestElementProperty prop = new TestElementProperty("args", args);
        ConfigTestElement te = new ConfigTestElement();
        te.addProperty(prop);
        te.setRunningVersion(true);
        Arguments config = new Arguments();
        config.addArgument("config1", "configValue", "=");
        TestElementProperty configProp = new TestElementProperty("args", config);
        ConfigTestElement te2 = new ConfigTestElement();
        te2.addProperty(configProp);
        te.addTestElement(te2);
        assertEquals(2, args.getArgumentCount());
        assertEquals("config1=configValue", args.getArgument(1).toString());
        te.recoverRunningVersion();
        te.addTestElement(te2);
        assertEquals(2, args.getArgumentCount());
        assertEquals("config1=configValue", args.getArgument(1).toString());

    }
}

@@ -887,52 +887,8 @@
 ls;%0A
-import static org.junit.Assert.assertFalse;%0A
 impo
@@ -1070,238 +1070,8 @@
 ig;%0A
-import org.apache.jmeter.protocol.http.control.Header;%0Aimport org.apache.jmeter.protocol.http.control.HeaderManager;%0Aimport org.apache.jmeter.sampler.DebugSampler;%0Aimport org.apache.jmeter.testelement.property.CollectionProperty;%0A
 impo
@@ -1259,33 +1259,8 @@
 ty;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1311,2005 +1311,8 @@
  %7B%0A%0A
-    // Test needs to run in this package in order to give access to AbstractTestElement.addProperty()%0A    @Test%0A    @Ignore // should be OK now the bug is fixed, but it fails%0A    public void testBug50799() throws Exception %7B%0A        HeaderManager headerManager = new HeaderManager();%0A        headerManager.add(new Header(%221stLevelTestHeader%22, %22testValue1%22));%0A        HeaderManager headerManager2 = new HeaderManager();%0A        headerManager2.add(new Header(%222ndLevelTestHeader%22, %22testValue2%22));%0A%0A        DebugSampler debugSampler = new DebugSampler();%0A        debugSampler.addProperty(new StringProperty(%22name%22, %22DebugSampler_50799%22));%0A        debugSampler.setRunningVersion(true);%0A        assertTrue(debugSampler.getProperty(%22HeaderManager.headers%22) instanceof NullProperty);%0A        debugSampler.addTestElement(headerManager);%0A        assertFalse(debugSampler.getProperty(%22HeaderManager.headers%22) instanceof NullProperty);%0A        assertEquals(debugSampler.getProperty(%22HeaderManager.headers%22).getStringValue() ,%22%5B1stLevelTestHeader%5CttestValue1%5D%22);%0A%0A        debugSampler.addTestElement(headerManager2);%0A        assertEquals(debugSampler.getProperty(%22HeaderManager.headers%22).getStringValue() ,%22%5B1stLevelTestHeader%5CttestValue1, 2ndLevelTestHeader%5CttestValue2%5D%22);%0A        assertEquals(2, ((CollectionProperty)debugSampler.getProperty(%22HeaderManager.headers%22)).size());%0A        %0A        headerManager.recoverRunningVersion();%0A        headerManager2.recoverRunningVersion();%0A        debugSampler.recoverRunningVersion();%0A%0A        assertEquals(1, headerManager.size()); // Test fails here - size is 0 - but why?%0A        assertEquals(1, headerManager2.size());%0A        assertEquals(0, ((CollectionProperty)debugSampler.getProperty(%22HeaderManager.headers%22)).size());%0A        assertEquals(new Header(%221stLevelTestHeader%22, %22testValue1%22), headerManager.get(0)); // This also fails because the size is zero%0A        assertEquals(new Header(%222ndLevelTestHeader%22, %22testValue2%22), headerManager2.get(0));%0A    %7D%0A%0A
     

Remove wrong Test case