Skip to content

Commit

Permalink
WICKET-7119 fixed non-idempotent unit tests (#929)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1a55391)
  • Loading branch information
kaiyaok2 authored and martin-g committed Jul 30, 2024
1 parent d20b702 commit dfa5713
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -41,6 +42,16 @@ class ComponentOnConfigureTest extends WicketTestCase
private static final AtomicInteger BEHAVIOR = new AtomicInteger(0);
private static final AtomicInteger APPLICATION_LISTENER = new AtomicInteger(0);

@BeforeEach
void resetCounters()
{
COUNTER.set(0);
PAGE.set(0);
COMPONENT.set(0);
BEHAVIOR.set(0);
APPLICATION_LISTENER.set(0);
}

/**
* testOrder()
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,104 @@
/*
* 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.wicket.markup.repeater;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.Test;

/**
* Test for {@link AbstractPageableView}.
*/
public class AbstractPageableViewTest extends WicketTestCase
{
private static int count = 5;

/**
*/
@Test
public void cachedItemCount()
{
View view = new View("f");

assertEquals(5, view.getItemCount());

count = 6;

assertEquals(5, view.getItemCount(), "still 5 cached");

view.beforeRender();

assertEquals(6, view.getItemCount(), "cached cleared before render");

byte[] bytes = tester.getApplication().getFrameworkSettings().getSerializer().serialize(view);

view = (View)tester.getApplication().getFrameworkSettings().getSerializer().deserialize(bytes);

count = 7;

assertEquals(7, view.getItemCount(), "cached cleared when deserialized");

view.detach();

count = 8;

assertEquals(8, view.getItemCount(), "cached cleared when detached");
}

static class View extends AbstractPageableView<Integer>
{
public View(String id)
{
super(id);
}

@Override
protected void populateItem(Item<Integer> item)
{
}

@Override
protected long internalGetItemCount()
{
return count;
}

@Override
protected Iterator<IModel<Integer>> getItemModels(long offset, long size)
{
List<IModel<Integer>> models = new ArrayList<>();
for (int m = 0; m < count; m++) {
models.add(Model.of(m));
}
return models.iterator();
}
}
/*
* 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.wicket.markup.repeater;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test for {@link AbstractPageableView}.
*/
public class AbstractPageableViewTest extends WicketTestCase
{
private static int count = 5;

@BeforeEach
public void resetCount()
{
count = 5;
}

/**
*/
@Test
public void cachedItemCount()
{
View view = new View("f");

assertEquals(5, view.getItemCount());

count = 6;

assertEquals(5, view.getItemCount(), "still 5 cached");

view.beforeRender();

assertEquals(6, view.getItemCount(), "cached cleared before render");

byte[] bytes = tester.getApplication().getFrameworkSettings().getSerializer().serialize(view);

view = (View)tester.getApplication().getFrameworkSettings().getSerializer().deserialize(bytes);

count = 7;

assertEquals(7, view.getItemCount(), "cached cleared when deserialized");

view.detach();

count = 8;

assertEquals(8, view.getItemCount(), "cached cleared when detached");
}

static class View extends AbstractPageableView<Integer>
{
public View(String id)
{
super(id);
}

@Override
protected void populateItem(Item<Integer> item)
{
}

@Override
protected long internalGetItemCount()
{
return count;
}

@Override
protected Iterator<IModel<Integer>> getItemModels(long offset, long size)
{
List<IModel<Integer>> models = new ArrayList<>();
for (int m = 0; m < count; m++) {
models.add(Model.of(m));
}
return models.iterator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public <T extends Serializable> T getSessionAttribute(String key,
};
assertTrue(store.canBeAsynchronous(context));

store.destroy();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.Path;
import java.util.Collections;

import org.apache.wicket.core.util.lang.WicketObjects;
import org.apache.wicket.util.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -55,8 +59,14 @@ void testFileSystemResourceReferenceWithZip() throws IOException, URISyntaxExcep
try
{
URL resource = FileSystemResourceReferenceTest.class.getResource("FileSystemResourceReferenceTest.zip");
Path path = FileSystemResourceReference.getPath(URI.create("jar:" + resource.toURI() +
"!/folderInZip/FileSystemResourceReference.txt"));
URI uri = URI.create("jar:" + resource.toURI() + "!/folderInZip/FileSystemResourceReference.txt");
FileSystem fs = null;
try {
fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
} catch (FileSystemAlreadyExistsException e) {
fs = FileSystems.getFileSystem(uri);
}
Path path = fs.getPath("/folderInZip/FileSystemResourceReference.txt");
final FileSystemResource fileSystemResource = new FileSystemResource(path);
FileSystemResourceReference fileSystemResourceReference = new FileSystemResourceReference(
"test", path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public final class Strings
* Constructs something like <em>;jsessionid=</em>. This is what {@linkplain Strings#stripJSessionId(String)}
* actually uses.
*/
private static final String SESSION_ID_PARAM = ';' + SESSION_ID_PARAM_NAME + '=';
// the field is not 'final' because we need to modify it in a unit test
// see https://github.com/openjdk/jdk/pull/5027#issuecomment-968177213
private static String SESSION_ID_PARAM = ';' + SESSION_ID_PARAM_NAME + '=';

/**
* Private constructor prevents construction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ void stripJSessionId() throws Exception
sessionIdParamField.setAccessible(true);
Field modifiersField = getModifiersField();
modifiersField.setAccessible(true);
String origSessionIdParam = (String) sessionIdParamField.get(null);
try {
final String customSessionIdParam = ";Custom seSsion - ид=";
modifiersField.setInt(sessionIdParamField, sessionIdParamField.getModifiers() & ~Modifier.FINAL );
sessionIdParamField.set(null, customSessionIdParam);
assertEquals(url + ";a=b;c=d?param=a;b",
Strings.stripJSessionId(url + ";a=b;c=d" + customSessionIdParam + "12345?param=a;b"));
} finally {
sessionIdParamField.set(null, "jsessionid");
sessionIdParamField.set(null, origSessionIdParam);
modifiersField.setInt(sessionIdParamField, sessionIdParamField.getModifiers() & Modifier.FINAL );
modifiersField.setAccessible(false);
sessionIdParamField.setAccessible(false);
Expand Down

0 comments on commit dfa5713

Please sign in to comment.